Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract::PageIterator Class Reference

#include <pageiterator.h>

Inheritance diagram for tesseract::PageIterator:
tesseract::LTRResultIterator tesseract::ResultIterator tesseract::MutableIterator

Public Member Functions

 PageIterator (PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
 
virtual ~PageIterator ()
 
 PageIterator (const PageIterator &src)
 
const PageIteratoroperator= (const PageIterator &src)
 
bool PositionedAtSameWord (const PAGE_RES_IT *other) const
 
virtual void Begin ()
 
virtual void RestartParagraph ()
 
bool IsWithinFirstTextlineOfParagraph () const
 
virtual void RestartRow ()
 
virtual bool Next (PageIteratorLevel level)
 
virtual bool IsAtBeginningOf (PageIteratorLevel level) const
 
virtual bool IsAtFinalElement (PageIteratorLevel level, PageIteratorLevel element) const
 
int Cmp (const PageIterator &other) const
 
bool BoundingBox (PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
 
bool BoundingBoxInternal (PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
 
bool Empty (PageIteratorLevel level) const
 
PolyBlockType BlockType () const
 
Pix * GetBinaryImage (PageIteratorLevel level) const
 
Pix * GetImage (PageIteratorLevel level, int padding, int *left, int *top) const
 
bool Baseline (PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
 
void Orientation (tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
 
void ParagraphInfo (tesseract::ParagraphJustification *justification, bool *is_list_item, bool *is_crown, int *first_line_indent) const
 

Protected Member Functions

TESS_LOCAL void BeginWord (int offset)
 

Protected Attributes

PAGE_RESpage_res_
 
Tesseracttesseract_
 
PAGE_RES_ITit_
 
WERDword_
 
int word_length_
 
int blob_index_
 
C_BLOB_IT * cblob_it_
 
int scale_
 
int scaled_yres_
 
int rect_left_
 
int rect_top_
 
int rect_width_
 
int rect_height_
 

Detailed Description

Class to iterate over tesseract page structure, providing access to all levels of the page hierarchy, without including any tesseract headers or having to handle any tesseract structures. WARNING! This class points to data held within the TessBaseAPI class, and therefore can only be used while the TessBaseAPI class still exists and has not been subjected to a call of Init, SetImage, Recognize, Clear, End DetectOS, or anything else that changes the internal PAGE_RES. See apitypes.h for the definition of PageIteratorLevel. See also ResultIterator, derived from PageIterator, which adds in the ability to access OCR output with text-specific methods.

Definition at line 51 of file pageiterator.h.

Constructor & Destructor Documentation

tesseract::PageIterator::PageIterator ( PAGE_RES page_res,
Tesseract tesseract,
int  scale,
int  scaled_yres,
int  rect_left,
int  rect_top,
int  rect_width,
int  rect_height 
)

page_res and tesseract come directly from the BaseAPI. The rectangle parameters are copied indirectly from the Thresholder, via the BaseAPI. They represent the coordinates of some rectangle in an original image (in top-left-origin coordinates) and therefore the top-left needs to be added to any output boxes in order to specify coordinates in the original image. See TessBaseAPI::SetRectangle. The scale and scaled_yres are in case the Thresholder scaled the image rectangle prior to thresholding. Any coordinates in tesseract's image must be divided by scale before adding (rect_left, rect_top). The scaled_yres indicates the effective resolution of the binary image that tesseract has been given by the Thresholder. After the constructor, Begin has already been called.

Definition at line 29 of file pageiterator.cpp.

33  : page_res_(page_res), tesseract_(tesseract),
35  scale_(scale), scaled_yres_(scaled_yres),
36  rect_left_(rect_left), rect_top_(rect_top),
37  rect_width_(rect_width), rect_height_(rect_height) {
38  it_ = new PAGE_RES_IT(page_res);
40 }
#define NULL
Definition: host.h:144
tesseract::PageIterator::~PageIterator ( )
virtual

Definition at line 42 of file pageiterator.cpp.

42  {
43  delete it_;
44  delete cblob_it_;
45 }
tesseract::PageIterator::PageIterator ( const PageIterator src)

Page/ResultIterators may be copied! This makes it possible to iterate over all the objects at a lower level, while maintaining an iterator to objects at a higher level. These constructors DO NOT CALL Begin, so iterations will continue from the location of src.

PageIterators may be copied! This makes it possible to iterate over all the objects at a lower level, while maintaining an iterator to objects at a higher level.

Definition at line 52 of file pageiterator.cpp.

53  : page_res_(src.page_res_), tesseract_(src.tesseract_),
54  word_(NULL), word_length_(src.word_length_),
55  blob_index_(src.blob_index_), cblob_it_(NULL),
56  scale_(src.scale_), scaled_yres_(src.scaled_yres_),
57  rect_left_(src.rect_left_), rect_top_(src.rect_top_),
58  rect_width_(src.rect_width_), rect_height_(src.rect_height_) {
59  it_ = new PAGE_RES_IT(*src.it_);
60  BeginWord(src.blob_index_);
61 }
#define NULL
Definition: host.h:144
TESS_LOCAL void BeginWord(int offset)

Member Function Documentation

bool tesseract::PageIterator::Baseline ( PageIteratorLevel  level,
int *  x1,
int *  y1,
int *  x2,
int *  y2 
) const

Returns the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). WARNING: with vertical text, baselines may be vertical! Returns false if there is no baseline at the current position.

Returns the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). WARNING: with vertical text, baselines may be vertical!

Definition at line 448 of file pageiterator.cpp.

449  {
450  if (it_->word() == NULL) return false; // Already at the end!
451  ROW* row = it_->row()->row;
452  WERD* word = it_->word()->word;
453  TBOX box = (level == RIL_WORD || level == RIL_SYMBOL)
454  ? word->bounding_box()
455  : row->bounding_box();
456  int left = box.left();
457  ICOORD startpt(left, static_cast<inT16>(row->base_line(left) + 0.5));
458  int right = box.right();
459  ICOORD endpt(right, static_cast<inT16>(row->base_line(right) + 0.5));
460  // Rotate to image coordinates and convert to global image coords.
461  startpt.rotate(it_->block()->block->re_rotation());
462  endpt.rotate(it_->block()->block->re_rotation());
463  *x1 = startpt.x() / scale_ + rect_left_;
464  *y1 = (rect_height_ - startpt.y()) / scale_ + rect_top_;
465  *x2 = endpt.x() / scale_ + rect_left_;
466  *y2 = (rect_height_ - endpt.y()) / scale_ + rect_top_;
467  return true;
468 }
TBOX bounding_box()
Definition: werd.cpp:164
void rotate(const FCOORD &vec)
Definition: ipoints.h:241
BLOCK * block
Definition: pageres.h:258
ROW_RES * row() const
Definition: pageres.h:760
#define NULL
Definition: host.h:144
inT16 left() const
Definition: rect.h:67
Definition: rect.h:29
inT16 right() const
Definition: rect.h:74
WERD_RES * word() const
Definition: pageres.h:757
BLOCK_RES * block() const
Definition: pageres.h:763
WERD * word
Definition: pageres.h:334
float base_line(float xpos) const
Definition: ocrrow.h:56
Definition: ocrrow.h:32
Definition: werd.h:60
integer coordinate
Definition: points.h:30
TBOX bounding_box() const
Definition: ocrrow.h:85
FCOORD re_rotation() const
Definition: ocrblock.h:139
ROW * row
Definition: pageres.h:286
void tesseract::PageIterator::Begin ( )
virtual

Moves the iterator to point to the start of the page to begin an iteration.

Resets the iterator to point to the start of the page.

Reimplemented in tesseract::ResultIterator.

Definition at line 86 of file pageiterator.cpp.

86  {
88  BeginWord(0);
89 }
TESS_LOCAL void BeginWord(int offset)
WERD_RES * restart_page_with_empties()
Definition: pageres.h:716
void tesseract::PageIterator::BeginWord ( int  offset)
protected

Sets up the internal data for iterating the blobs of a new word, then moves the iterator to the given offset.

Definition at line 536 of file pageiterator.cpp.

536  {
537  WERD_RES* word_res = it_->word();
538  if (word_res == NULL) {
539  // This is a non-text block, so there is no word.
540  word_length_ = 0;
541  blob_index_ = 0;
542  word_ = NULL;
543  return;
544  }
545  if (word_res->best_choice != NULL) {
546  // Recognition has been done, so we are using the box_word, which
547  // is already baseline denormalized.
548  word_length_ = word_res->best_choice->length();
549  ASSERT_HOST(word_res->box_word != NULL);
550  if (word_res->box_word->length() != word_length_) {
551  tprintf("Corrupted word! best_choice[len=%d] = %s, box_word[len=%d]: ",
553  word_res->box_word->length());
554  word_res->box_word->bounding_box().print();
555  }
556  ASSERT_HOST(word_res->box_word->length() == word_length_);
557  word_ = NULL;
558  // We will be iterating the box_word.
559  if (cblob_it_ != NULL) {
560  delete cblob_it_;
561  cblob_it_ = NULL;
562  }
563  } else {
564  // No recognition yet, so a "symbol" is a cblob.
565  word_ = word_res->word;
566  ASSERT_HOST(word_->cblob_list() != NULL);
567  word_length_ = word_->cblob_list()->length();
568  if (cblob_it_ == NULL) cblob_it_ = new C_BLOB_IT;
569  cblob_it_->set_to_list(word_->cblob_list());
570  }
571  for (blob_index_ = 0; blob_index_ < offset; ++blob_index_) {
572  if (cblob_it_ != NULL)
573  cblob_it_->forward();
574  }
575 }
int length() const
Definition: ratngs.h:214
const STRING & unichar_string() const
Definition: ratngs.h:395
const int length() const
Definition: boxword.h:99
#define NULL
Definition: host.h:144
WERD_RES * word() const
Definition: pageres.h:757
WERD * word
Definition: pageres.h:334
const char * string() const
Definition: strngs.cpp:156
const TBOX & bounding_box() const
Definition: boxword.h:96
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
tesseract::BoxWord * box_word
Definition: pageres.h:387
#define ASSERT_HOST(x)
Definition: errcode.h:84
void print() const
Definition: rect.h:263
WERD_CHOICE * best_choice
Definition: pageres.h:359
PolyBlockType tesseract::PageIterator::BlockType ( ) const

Returns the type of the current block. See apitypes.h for PolyBlockType.

Definition at line 330 of file pageiterator.cpp.

330  {
331  if (it_->block() == NULL || it_->block()->block == NULL)
332  return PT_UNKNOWN; // Already at the end!
333  if (it_->block()->block->poly_block() == NULL)
334  return PT_FLOWING_TEXT; // No layout analysis used - assume text.
335  return it_->block()->block->poly_block()->isA();
336 }
BLOCK * block
Definition: pageres.h:258
PolyBlockType isA() const
Definition: polyblk.h:50
#define NULL
Definition: host.h:144
BLOCK_RES * block() const
Definition: pageres.h:763
POLY_BLOCK * poly_block() const
Definition: pdblock.h:62
bool tesseract::PageIterator::BoundingBox ( PageIteratorLevel  level,
int *  left,
int *  top,
int *  right,
int *  bottom 
) const

Returns the bounding rectangle of the current object at the given level. See comment on coordinate system above. Returns false if there is no such object at the current position. The returned bounding box is guaranteed to match the size and position of the image returned by GetBinaryImage, but may clip foreground pixels from a grey image. The padding argument to GetImage can be used to expand the image to include more foreground pixels. See GetImage below.

Returns the bounding rectangle of the current object at the given level in coordinates of the original image. See comment on coordinate system above. Returns false if there is no such object at the current position.

Definition at line 303 of file pageiterator.cpp.

305  {
306  if (!BoundingBoxInternal(level, left, top, right, bottom))
307  return false;
308  // Convert to the coordinate system of the original image.
309  *left = ClipToRange(*left / scale_ + rect_left_,
311  *top = ClipToRange(*top / scale_ + rect_top_,
313  *right = ClipToRange((*right + scale_ - 1) / scale_ + rect_left_,
314  *left, rect_left_ + rect_width_);
315  *bottom = ClipToRange((*bottom + scale_ - 1) / scale_ + rect_top_,
316  *top, rect_top_ + rect_height_);
317  return true;
318 }
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:64
bool tesseract::PageIterator::BoundingBoxInternal ( PageIteratorLevel  level,
int *  left,
int *  top,
int *  right,
int *  bottom 
) const

Returns the bounding rectangle of the object in a coordinate system of the working image rectangle having its origin at (rect_left_, rect_top_) with respect to the original image and is scaled by a factor scale_.

Returns the bounding rectangle of the current object at the given level in the coordinates of the working image that is pix_binary(). See comment on coordinate system above. Returns false if there is no such object at the current position.

Definition at line 246 of file pageiterator.cpp.

248  {
249  if (Empty(level))
250  return false;
251  TBOX box;
252  PARA *para = NULL;
253  switch (level) {
254  case RIL_BLOCK:
255  box = it_->block()->block->bounding_box();
256  break;
257  case RIL_PARA:
258  para = it_->row()->row->para();
259  // explicit fall-through.
260  case RIL_TEXTLINE:
261  box = it_->row()->row->bounding_box();
262  break;
263  case RIL_WORD:
264  box = it_->word()->word->bounding_box();
265  break;
266  case RIL_SYMBOL:
267  if (cblob_it_ == NULL)
268  box = it_->word()->box_word->BlobBox(blob_index_);
269  else
270  box = cblob_it_->data()->bounding_box();
271  }
272  if (level == RIL_PARA) {
273  PageIterator other = *this;
274  other.Begin();
275  do {
276  if (other.it_->block() &&
277  other.it_->block()->block == it_->block()->block &&
278  other.it_->row() && other.it_->row()->row &&
279  other.it_->row()->row->para() == para) {
280  box = box.bounding_union(other.it_->row()->row->bounding_box());
281  }
282  } while (other.Next(RIL_TEXTLINE));
283  }
284  if (level != RIL_SYMBOL || cblob_it_ != NULL)
285  box.rotate(it_->block()->block->re_rotation());
286  // Now we have a box in tesseract coordinates relative to the image rectangle,
287  // we have to convert the coords to a top-down system.
288  const int pix_height = pixGetHeight(tesseract_->pix_binary());
289  const int pix_width = pixGetWidth(tesseract_->pix_binary());
290  *left = ClipToRange(static_cast<int>(box.left()), 0, pix_width);
291  *top = ClipToRange(pix_height - box.top(), 0, pix_height);
292  *right = ClipToRange(static_cast<int>(box.right()), *left, pix_width);
293  *bottom = ClipToRange(pix_height - box.bottom(), *top, pix_height);
294  return true;
295 }
TBOX bounding_union(const TBOX &box) const
Definition: rect.cpp:130
TBOX bounding_box()
Definition: werd.cpp:164
BLOCK * block
Definition: pageres.h:258
ROW_RES * row() const
Definition: pageres.h:760
#define NULL
Definition: host.h:144
inT16 left() const
Definition: rect.h:67
Definition: rect.h:29
Definition: ocrpara.h:29
inT16 right() const
Definition: rect.h:74
WERD_RES * word() const
Definition: pageres.h:757
BLOCK_RES * block() const
Definition: pageres.h:763
WERD * word
Definition: pageres.h:334
PARA * para() const
Definition: ocrrow.h:112
inT16 top() const
Definition: rect.h:53
bool Empty(PageIteratorLevel level) const
TBOX bounding_box() const
Definition: ocrrow.h:85
tesseract::BoxWord * box_word
Definition: pageres.h:387
const TBOX & BlobBox(int index) const
Definition: boxword.h:102
void rotate(const FCOORD &vec)
Definition: rect.h:182
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:70
FCOORD re_rotation() const
Definition: ocrblock.h:139
Pix * pix_binary() const
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:64
ROW * row
Definition: pageres.h:286
inT16 bottom() const
Definition: rect.h:60
int tesseract::PageIterator::Cmp ( const PageIterator other) const

Returns whether this iterator is positioned before other: -1 equal to other: 0 after other: 1

Definition at line 216 of file pageiterator.cpp.

216  {
217  int word_cmp = it_->cmp(*other.it_);
218  if (word_cmp != 0)
219  return word_cmp;
220  if (blob_index_ < other.blob_index_)
221  return -1;
222  if (blob_index_ == other.blob_index_)
223  return 0;
224  return 1;
225 }
int cmp(const PAGE_RES_IT &other) const
Definition: pageres.cpp:851
bool tesseract::PageIterator::Empty ( PageIteratorLevel  level) const

Returns whether there is no object of a given level.

Return that there is no such object at a given level.

Definition at line 321 of file pageiterator.cpp.

321  {
322  if (it_->block() == NULL) return true; // Already at the end!
323  if (it_->word() == NULL && level != RIL_BLOCK) return true; // image block
324  if (level == RIL_SYMBOL && blob_index_ >= word_length_)
325  return true; // Zero length word, or already at the end of it.
326  return false;
327 }
#define NULL
Definition: host.h:144
WERD_RES * word() const
Definition: pageres.h:757
BLOCK_RES * block() const
Definition: pageres.h:763
Pix * tesseract::PageIterator::GetBinaryImage ( PageIteratorLevel  level) const

Returns a binary image of the current object at the given level. The position and size match the return from BoundingBoxInternal, and so this could be upscaled with respect to the original input image. Use pixDestroy to delete the image after use.

Returns a binary image of the current object at the given level. The position and size match the return from BoundingBoxInternal, and so this could be upscaled with respect to the original input image. Use pixDestroy to delete the image after use. The following methods are used to generate the images: RIL_BLOCK: mask the page image with the block polygon. RIL_TEXTLINE: Clip the rectangle of the line box from the page image. TODO(rays) fix this to generate and use a line polygon. RIL_WORD: Clip the rectangle of the word box from the page image. RIL_SYMBOL: Render the symbol outline to an image for cblobs (prior to recognition) or the bounding box otherwise. A reconstruction of the original image (using xor to check for double representation) should be reasonably accurate, apart from removed noise, at the block level. Below the block level, the reconstruction will be missing images and line separators. At the symbol level, kerned characters will be invade the bounding box if rendered after recognition, making an xor reconstruction inaccurate, but an or construction better. Before recognition, symbol-level reconstruction should be good, even with xor, since the images come from the connected components.

Definition at line 360 of file pageiterator.cpp.

360  {
361  int left, top, right, bottom;
362  if (!BoundingBoxInternal(level, &left, &top, &right, &bottom))
363  return NULL;
364  Pix* pix = NULL;
365  switch (level) {
366  case RIL_BLOCK:
367  case RIL_PARA:
368  int bleft, btop, bright, bbottom;
369  BoundingBoxInternal(RIL_BLOCK, &bleft, &btop, &bright, &bbottom);
370  pix = it_->block()->block->render_mask();
371  // AND the mask and the image.
372  pixRasterop(pix, 0, 0, pixGetWidth(pix), pixGetHeight(pix),
373  PIX_SRC & PIX_DST, tesseract_->pix_binary(),
374  bleft, btop);
375  if (level == RIL_PARA) {
376  // RIL_PARA needs further attention:
377  // clip the paragraph from the block mask.
378  Box* box = boxCreate(left - bleft, top - btop,
379  right - left, bottom - top);
380  Pix* pix2 = pixClipRectangle(pix, box, NULL);
381  boxDestroy(&box);
382  pixDestroy(&pix);
383  pix = pix2;
384  }
385  break;
386  case RIL_TEXTLINE:
387  case RIL_WORD:
388  case RIL_SYMBOL:
389  if (level == RIL_SYMBOL && cblob_it_ != NULL &&
390  cblob_it_->data()->area() != 0)
391  return cblob_it_->data()->render();
392  // Just clip from the bounding box.
393  Box* box = boxCreate(left, top, right - left, bottom - top);
394  pix = pixClipRectangle(tesseract_->pix_binary(), box, NULL);
395  boxDestroy(&box);
396  break;
397  }
398  return pix;
399 }
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
BLOCK * block
Definition: pageres.h:258
#define NULL
Definition: host.h:144
BLOCK_RES * block() const
Definition: pageres.h:763
Pix * render_mask()
Definition: ocrblock.h:165
Pix * pix_binary() const
Pix * tesseract::PageIterator::GetImage ( PageIteratorLevel  level,
int  padding,
int *  left,
int *  top 
) const

Returns an image of the current object at the given level in greyscale if available in the input. To guarantee a binary image use BinaryImage. NOTE that in order to give the best possible image, the bounds are expanded slightly over the binary connected component, by the supplied padding, so the top-left position of the returned image is returned in (left,top). These will most likely not match the coordinates returned by BoundingBox. Use pixDestroy to delete the image after use.

Definition at line 411 of file pageiterator.cpp.

412  {
413  int right, bottom;
414  if (!BoundingBox(level, left, top, &right, &bottom))
415  return NULL;
416  Pix* pix = tesseract_->pix_grey();
417  if (pix == NULL)
418  return GetBinaryImage(level);
419 
420  // Expand the box.
421  *left = MAX(*left - padding, 0);
422  *top = MAX(*top - padding, 0);
423  right = MIN(right + padding, rect_width_);
424  bottom = MIN(bottom + padding, rect_height_);
425  Box* box = boxCreate(*left, *top, right - *left, bottom - *top);
426  Pix* grey_pix = pixClipRectangle(pix, box, NULL);
427  boxDestroy(&box);
428  if (level == RIL_BLOCK) {
429  Pix* mask = it_->block()->block->render_mask();
430  Pix* expanded_mask = pixCreate(right - *left, bottom - *top, 1);
431  pixRasterop(expanded_mask, padding, padding,
432  pixGetWidth(mask), pixGetHeight(mask),
433  PIX_SRC, mask, 0, 0);
434  pixDestroy(&mask);
435  pixDilateBrick(expanded_mask, expanded_mask, 2*padding + 1, 2*padding + 1);
436  pixInvert(expanded_mask, expanded_mask);
437  pixSetMasked(grey_pix, expanded_mask, 255);
438  pixDestroy(&expanded_mask);
439  }
440  return grey_pix;
441 }
BLOCK * block
Definition: pageres.h:258
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
#define NULL
Definition: host.h:144
BLOCK_RES * block() const
Definition: pageres.h:763
Pix * pix_grey() const
Pix * render_mask()
Definition: ocrblock.h:165
#define MIN(x, y)
Definition: ndminx.h:28
#define MAX(x, y)
Definition: ndminx.h:24
Pix * GetBinaryImage(PageIteratorLevel level) const
bool tesseract::PageIterator::IsAtBeginningOf ( PageIteratorLevel  level) const
virtual

Returns true if the iterator is at the start of an object at the given level.

For instance, suppose an iterator it is pointed to the first symbol of the first word of the third line of the second paragraph of the first block in a page, then: it.IsAtBeginningOf(RIL_BLOCK) = false it.IsAtBeginningOf(RIL_PARA) = false it.IsAtBeginningOf(RIL_TEXTLINE) = true it.IsAtBeginningOf(RIL_WORD) = true it.IsAtBeginningOf(RIL_SYMBOL) = true

Returns true if the iterator is at the start of an object at the given level. Possible uses include determining if a call to Next(RIL_WORD) moved to the start of a RIL_PARA.

Reimplemented in tesseract::ResultIterator.

Definition at line 166 of file pageiterator.cpp.

166  {
167  if (it_->block() == NULL) return false; // Already at the end!
168  if (it_->word() == NULL) return true; // In an image block.
169  switch (level) {
170  case RIL_BLOCK:
171  return blob_index_ == 0 && it_->block() != it_->prev_block();
172  case RIL_PARA:
173  return blob_index_ == 0 &&
174  (it_->block() != it_->prev_block() ||
175  it_->row()->row->para() != it_->prev_row()->row->para());
176  case RIL_TEXTLINE:
177  return blob_index_ == 0 && it_->row() != it_->prev_row();
178  case RIL_WORD:
179  return blob_index_ == 0;
180  case RIL_SYMBOL:
181  return true;
182  }
183  return false;
184 }
BLOCK_RES * prev_block() const
Definition: pageres.h:754
ROW_RES * row() const
Definition: pageres.h:760
#define NULL
Definition: host.h:144
WERD_RES * word() const
Definition: pageres.h:757
BLOCK_RES * block() const
Definition: pageres.h:763
PARA * para() const
Definition: ocrrow.h:112
ROW_RES * prev_row() const
Definition: pageres.h:751
ROW * row
Definition: pageres.h:286
bool tesseract::PageIterator::IsAtFinalElement ( PageIteratorLevel  level,
PageIteratorLevel  element 
) const
virtual

Returns whether the iterator is positioned at the last element in a given level. (e.g. the last word in a line, the last line in a block)

Here's some two-paragraph example

text. It starts off innocuously enough but quickly turns bizarre. The author inserts a cornucopia of words to guard against confused references.

Now take an iterator it pointed to the start of "bizarre." it.IsAtFinalElement(RIL_PARA, RIL_SYMBOL) = false it.IsAtFinalElement(RIL_PARA, RIL_WORD) = true it.IsAtFinalElement(RIL_BLOCK, RIL_WORD) = false

Returns whether the iterator is positioned at the last element in a given level. (e.g. the last word in a line, the last line in a block)

Reimplemented in tesseract::ResultIterator.

Definition at line 190 of file pageiterator.cpp.

191  {
192  if (Empty(element)) return true; // Already at the end!
193  // The result is true if we step forward by element and find we are
194  // at the the end of the page or at beginning of *all* levels in:
195  // [level, element).
196  // When there is more than one level difference between element and level,
197  // we could for instance move forward one symbol and still be at the first
198  // word on a line, so we also have to be at the first symbol in a word.
199  PageIterator next(*this);
200  next.Next(element);
201  if (next.Empty(element)) return true; // Reached the end of the page.
202  while (element > level) {
203  element = static_cast<PageIteratorLevel>(element - 1);
204  if (!next.IsAtBeginningOf(element))
205  return false;
206  }
207  return true;
208 }
bool Empty(PageIteratorLevel level) const
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
bool tesseract::PageIterator::IsWithinFirstTextlineOfParagraph ( ) const

Return whether this iterator points anywhere in the first textline of a paragraph.

Definition at line 104 of file pageiterator.cpp.

104  {
105  PageIterator p_start(*this);
106  p_start.RestartParagraph();
107  return p_start.it_->row() == it_->row();
108 }
ROW_RES * row() const
Definition: pageres.h:760
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
bool tesseract::PageIterator::Next ( PageIteratorLevel  level)
virtual

Moves to the start of the next object at the given level in the page hierarchy, and returns false if the end of the page was reached. NOTE that RIL_SYMBOL will skip non-text blocks, but all other PageIteratorLevel level values will visit each non-text block once. Think of non text blocks as containing a single para, with a single line, with a single imaginary word. Calls to Next with different levels may be freely intermixed. This function iterates words in right-to-left scripts correctly, if the appropriate language has been loaded into Tesseract.

Moves to the start of the next object at the given level in the page hierarchy, and returns false if the end of the page was reached. NOTE (CHANGED!) that ALL PageIteratorLevel level values will visit each non-text block at least once. Think of non text blocks as containing a single para, with at least one line, with a single imaginary word, containing a single symbol. The bounding boxes mark out any polygonal nature of the block, and PTIsTextType(BLockType()) is false for non-text blocks. Calls to Next with different levels may be freely intermixed. This function iterates words in right-to-left scripts correctly, if the appropriate language has been loaded into Tesseract.

Reimplemented in tesseract::ResultIterator.

Definition at line 128 of file pageiterator.cpp.

128  {
129  if (it_->block() == NULL) return false; // Already at the end!
130  if (it_->word() == NULL)
131  level = RIL_BLOCK;
132 
133  switch (level) {
134  case RIL_BLOCK:
135  it_->forward_block();
136  break;
137  case RIL_PARA:
139  break;
140  case RIL_TEXTLINE:
141  for (it_->forward_with_empties(); it_->row() == it_->prev_row();
143  break;
144  case RIL_WORD:
146  break;
147  case RIL_SYMBOL:
148  if (cblob_it_ != NULL)
149  cblob_it_->forward();
150  ++blob_index_;
151  if (blob_index_ >= word_length_)
153  else
154  return true;
155  break;
156  }
157  BeginWord(0);
158  return it_->block() != NULL;
159 }
WERD_RES * forward_block()
Definition: pageres.cpp:1130
ROW_RES * row() const
Definition: pageres.h:760
WERD_RES * forward_paragraph()
Definition: pageres.cpp:1115
#define NULL
Definition: host.h:144
WERD_RES * word() const
Definition: pageres.h:757
BLOCK_RES * block() const
Definition: pageres.h:763
WERD_RES * forward_with_empties()
Definition: pageres.h:741
TESS_LOCAL void BeginWord(int offset)
ROW_RES * prev_row() const
Definition: pageres.h:751
const PageIterator & tesseract::PageIterator::operator= ( const PageIterator src)

Definition at line 63 of file pageiterator.cpp.

63  {
64  page_res_ = src.page_res_;
65  tesseract_ = src.tesseract_;
66  scale_ = src.scale_;
67  scaled_yres_ = src.scaled_yres_;
68  rect_left_ = src.rect_left_;
69  rect_top_ = src.rect_top_;
70  rect_width_ = src.rect_width_;
71  rect_height_ = src.rect_height_;
72  if (it_ != NULL) delete it_;
73  it_ = new PAGE_RES_IT(*src.it_);
74  BeginWord(src.blob_index_);
75  return *this;
76 }
#define NULL
Definition: host.h:144
TESS_LOCAL void BeginWord(int offset)
void tesseract::PageIterator::Orientation ( tesseract::Orientation orientation,
tesseract::WritingDirection writing_direction,
tesseract::TextlineOrder textline_order,
float *  deskew_angle 
) const

Returns orientation for the block the iterator points to. orientation, writing_direction, textline_order: see publictypes.h deskew_angle: after rotating the block so the text orientation is upright, how many radians does one have to rotate the block anti-clockwise for it to be level? -Pi/4 <= deskew_angle <= Pi/4

Definition at line 470 of file pageiterator.cpp.

473  {
474  BLOCK* block = it_->block()->block;
475 
476  // Orientation
477  FCOORD up_in_image(0.0, 1.0);
478  up_in_image.unrotate(block->classify_rotation());
479  up_in_image.rotate(block->re_rotation());
480 
481  if (up_in_image.x() == 0.0F) {
482  if (up_in_image.y() > 0.0F) {
483  *orientation = ORIENTATION_PAGE_UP;
484  } else {
485  *orientation = ORIENTATION_PAGE_DOWN;
486  }
487  } else if (up_in_image.x() > 0.0F) {
488  *orientation = ORIENTATION_PAGE_RIGHT;
489  } else {
490  *orientation = ORIENTATION_PAGE_LEFT;
491  }
492 
493  // Writing direction
494  bool is_vertical_text = (block->classify_rotation().x() == 0.0);
495  bool right_to_left = block->right_to_left();
496  *writing_direction =
497  is_vertical_text
499  : (right_to_left
502 
503  // Textline Order
504  bool is_mongolian = false; // TODO(eger): fix me
505  *textline_order = is_vertical_text
506  ? (is_mongolian
510 
511  // Deskew angle
512  FCOORD skew = block->skew(); // true horizontal for textlines
513  *deskew_angle = -skew.angle();
514 }
BLOCK * block
Definition: pageres.h:258
FCOORD skew() const
Definition: ocrblock.h:151
BLOCK_RES * block() const
Definition: pageres.h:763
Definition: ocrblock.h:31
bool right_to_left() const
Definition: ocrblock.h:84
FCOORD classify_rotation() const
Definition: ocrblock.h:145
float angle() const
find angle
Definition: points.h:249
Definition: points.h:189
FCOORD re_rotation() const
Definition: ocrblock.h:139
float x() const
Definition: points.h:209
void tesseract::PageIterator::ParagraphInfo ( tesseract::ParagraphJustification justification,
bool *  is_list_item,
bool *  is_crown,
int *  first_line_indent 
) const

Returns information about the current paragraph, if available.

justification - LEFT if ragged right, or fully justified and script is left-to-right. RIGHT if ragged left, or fully justified and script is right-to-left. unknown if it looks like source code or we have very few lines. is_list_item - true if we believe this is a member of an ordered or unordered list. is_crown - true if the first line of the paragraph is aligned with the other lines of the paragraph even though subsequent paragraphs have first line indents. This typically indicates that this is the continuation of a previous paragraph or that it is the very first paragraph in the chapter. first_line_indent - For LEFT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the left edge of the rest of the paragraph. for RIGHT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the right edge of the rest of the paragraph. NOTE 1: This value may be negative. NOTE 2: if *is_crown == true, the first line of this paragraph is actually flush, and first_line_indent is set to the "common" first_line_indent for subsequent paragraphs in this block of text.

Definition at line 516 of file pageiterator.cpp.

519  {
521  if (!it_->row() || !it_->row()->row || !it_->row()->row->para() ||
522  !it_->row()->row->para()->model)
523  return;
524 
525  PARA *para = it_->row()->row->para();
526  *is_list_item = para->is_list_item;
527  *is_crown = para->is_very_first_or_continuation;
528  *first_line_indent = para->model->first_indent() -
529  para->model->body_indent();
530 }
const ParagraphModel * model
Definition: ocrpara.h:36
ROW_RES * row() const
Definition: pageres.h:760
bool is_list_item
Definition: ocrpara.h:38
int body_indent() const
Definition: ocrpara.h:169
int first_indent() const
Definition: ocrpara.h:168
Definition: ocrpara.h:29
PARA * para() const
Definition: ocrrow.h:112
bool is_very_first_or_continuation
Definition: ocrpara.h:43
ROW * row
Definition: pageres.h:286
bool tesseract::PageIterator::PositionedAtSameWord ( const PAGE_RES_IT other) const

Are we positioned at the same location as other?

Definition at line 78 of file pageiterator.cpp.

78  {
79  return (it_ == NULL && it_ == other) ||
80  ((other != NULL) && (it_ != NULL) && (*it_ == *other));
81 }
#define NULL
Definition: host.h:144
void tesseract::PageIterator::RestartParagraph ( )
virtual

Moves the iterator to the beginning of the paragraph. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word on the first row of the paragraph.

Definition at line 91 of file pageiterator.cpp.

91  {
92  if (it_->block() == NULL) return; // At end of the document.
93  PAGE_RES_IT para(page_res_);
94  PAGE_RES_IT next_para(para);
95  next_para.forward_paragraph();
96  while (next_para.cmp(*it_) <= 0) {
97  para = next_para;
98  next_para.forward_paragraph();
99  }
100  *it_ = para;
101  BeginWord(0);
102 }
#define NULL
Definition: host.h:144
BLOCK_RES * block() const
Definition: pageres.h:763
TESS_LOCAL void BeginWord(int offset)
void tesseract::PageIterator::RestartRow ( )
virtual

Moves the iterator to the beginning of the text line. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word of the row.

Definition at line 110 of file pageiterator.cpp.

110  {
111  it_->restart_row();
112  BeginWord(0);
113 }
WERD_RES * restart_row()
Definition: pageres.cpp:1100
TESS_LOCAL void BeginWord(int offset)

Member Data Documentation

int tesseract::PageIterator::blob_index_
protected

The current blob index within the word.

Definition at line 309 of file pageiterator.h.

C_BLOB_IT* tesseract::PageIterator::cblob_it_
protected

Iterator to the blobs within the word. If NULL, then we are iterating OCR results in the box_word. Owned by this ResultIterator.

Definition at line 315 of file pageiterator.h.

PAGE_RES_IT* tesseract::PageIterator::it_
protected

The iterator to the page_res_. Owned by this ResultIterator. A pointer just to avoid dragging in Tesseract includes.

Definition at line 300 of file pageiterator.h.

PAGE_RES* tesseract::PageIterator::page_res_
protected

Pointer to the page_res owned by the API.

Definition at line 293 of file pageiterator.h.

int tesseract::PageIterator::rect_height_
protected

Definition at line 322 of file pageiterator.h.

int tesseract::PageIterator::rect_left_
protected

Definition at line 319 of file pageiterator.h.

int tesseract::PageIterator::rect_top_
protected

Definition at line 320 of file pageiterator.h.

int tesseract::PageIterator::rect_width_
protected

Definition at line 321 of file pageiterator.h.

int tesseract::PageIterator::scale_
protected

Parameters saved from the Thresholder. Needed to rebuild coordinates.

Definition at line 317 of file pageiterator.h.

int tesseract::PageIterator::scaled_yres_
protected

Definition at line 318 of file pageiterator.h.

Tesseract* tesseract::PageIterator::tesseract_
protected

Pointer to the Tesseract object owned by the API.

Definition at line 295 of file pageiterator.h.

WERD* tesseract::PageIterator::word_
protected

The current input WERD being iterated. If there is an output from OCR, then word_ is NULL. Owned by the API

Definition at line 305 of file pageiterator.h.

int tesseract::PageIterator::word_length_
protected

The length of the current word_.

Definition at line 307 of file pageiterator.h.


The documentation for this class was generated from the following files: