tesseract  5.0.0-alpha-619-ge9db
werdit.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: werdit.cpp (Formerly wordit.c)
3  * Description: An iterator for passing over all the words in a document.
4  * Author: Ray Smith
5  * Created: Mon Apr 27 08:51:22 BST 1992
6  *
7  * (C) Copyright 1992, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "werdit.h"
21 #include "errcode.h" // for ASSERT_HOST
22 #include "pageres.h" // for PAGE_RES_IT, PAGE_RES (ptr only), WERD_RES
23 #include "stepblob.h" // for C_BLOB_IT, C_BLOB, C_BLOB_LIST
24 #include "werd.h" // for WERD
25 
26 /**********************************************************************
27  * make_pseudo_word
28  *
29  * Make all the blobs inside a selection into a single word.
30  * The returned PAGE_RES_IT* it points to the new word. After use, call
31  * it->DeleteCurrentWord() to delete the fake word, and then
32  * delete it to get rid of the iterator itself.
33  **********************************************************************/
34 
35 PAGE_RES_IT* make_pseudo_word(PAGE_RES* page_res, const TBOX& selection_box) {
36  PAGE_RES_IT pr_it(page_res);
37  C_BLOB_LIST new_blobs; // list of gathered blobs
38  C_BLOB_IT new_blob_it = &new_blobs; // iterator
39 
40  for (WERD_RES* word_res = pr_it.word(); word_res != nullptr;
41  word_res = pr_it.forward()) {
42  WERD* word = word_res->word;
43  if (word->bounding_box().overlap(selection_box)) {
44  C_BLOB_IT blob_it(word->cblob_list());
45  for (blob_it.mark_cycle_pt();
46  !blob_it.cycled_list(); blob_it.forward()) {
47  C_BLOB* blob = blob_it.data();
48  if (blob->bounding_box().overlap(selection_box)) {
49  new_blob_it.add_after_then_move(C_BLOB::deep_copy(blob));
50  }
51  }
52  if (!new_blobs.empty()) {
53  WERD* pseudo_word = new WERD(&new_blobs, 1, nullptr);
54  word_res = pr_it.InsertSimpleCloneWord(*word_res, pseudo_word);
55  auto* it = new PAGE_RES_IT(page_res);
56  while (it->word() != word_res && it->word() != nullptr) it->forward();
57  ASSERT_HOST(it->word() == word_res);
58  return it;
59  }
60  }
61  }
62  return nullptr;
63 }
C_BLOB::bounding_box
TBOX bounding_box() const
Definition: stepblob.cpp:247
PAGE_RES_IT::forward
WERD_RES * forward()
Definition: pageres.h:728
werdit.h
pageres.h
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
WERD::bounding_box
TBOX bounding_box() const
Definition: werd.cpp:147
TBOX::overlap
bool overlap(const TBOX &box) const
Definition: rect.h:350
WERD_RES
Definition: pageres.h:160
C_BLOB
Definition: stepblob.h:36
PAGE_RES_IT::InsertSimpleCloneWord
WERD_RES * InsertSimpleCloneWord(const WERD_RES &clone_res, WERD *new_word)
Definition: pageres.cpp:1209
werd.h
WERD::cblob_list
C_BLOB_LIST * cblob_list()
Definition: werd.h:94
C_BLOB::deep_copy
static C_BLOB * deep_copy(const C_BLOB *src)
Definition: stepblob.h:118
stepblob.h
make_pseudo_word
PAGE_RES_IT * make_pseudo_word(PAGE_RES *page_res, const TBOX &selection_box)
Definition: werdit.cpp:33
PAGE_RES_IT::word
WERD_RES * word() const
Definition: pageres.h:748
PAGE_RES
Definition: pageres.h:73
PAGE_RES_IT
Definition: pageres.h:668
WERD
Definition: werd.h:55
errcode.h
TBOX
Definition: rect.h:33