tesseract  5.0.0
wordstrboxrenderer.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: wordstrboxrenderer.cpp
3  * Description: Renderer for creating box file with WordStr strings.
4  * based on the tsv renderer.
5  *
6  * (C) Copyright 2019, Google Inc.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #include <tesseract/baseapi.h> // for TessBaseAPI
20 #include <tesseract/renderer.h>
21 #include "tesseractclass.h" // for Tesseract
22 
23 namespace tesseract {
24 
31 char *TessBaseAPI::GetWordStrBoxText(int page_number = 0) {
32  if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0)) {
33  return nullptr;
34  }
35 
36  std::string wordstr_box_str;
37  int left = 0, top = 0, right = 0, bottom = 0;
38 
39  bool first_line = true;
40 
42  while (!res_it->Empty(RIL_BLOCK)) {
43  if (res_it->Empty(RIL_WORD)) {
44  res_it->Next(RIL_WORD);
45  continue;
46  }
47 
48  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
49  if (!first_line) {
50  wordstr_box_str += "\n\t " + std::to_string(right + 1);
51  wordstr_box_str += " " + std::to_string(image_height_ - bottom);
52  wordstr_box_str += " " + std::to_string(right + 5);
53  wordstr_box_str += " " + std::to_string(image_height_ - top);
54  wordstr_box_str += " " + std::to_string(page_number); // row for tab for EOL
55  wordstr_box_str += "\n";
56  } else {
57  first_line = false;
58  }
59  // Use bounding box for whole line for WordStr
60  res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom);
61  wordstr_box_str += "WordStr " + std::to_string(left);
62  wordstr_box_str += " " + std::to_string(image_height_ - bottom);
63  wordstr_box_str += " " + std::to_string(right);
64  wordstr_box_str += " " + std::to_string(image_height_ - top);
65  wordstr_box_str += " " + std::to_string(page_number); // word
66  wordstr_box_str += " #";
67  }
68  do {
69  wordstr_box_str += std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_WORD)).get();
70  wordstr_box_str += " ";
71  res_it->Next(RIL_WORD);
72  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
73  }
74 
75  if (left != 0 && top != 0 && right != 0 && bottom != 0) {
76  wordstr_box_str += "\n\t " + std::to_string(right + 1);
77  wordstr_box_str += " " + std::to_string(image_height_ - bottom);
78  wordstr_box_str += " " + std::to_string(right + 5);
79  wordstr_box_str += " " + std::to_string(image_height_ - top);
80  wordstr_box_str += " " + std::to_string(page_number); // row for tab for EOL
81  wordstr_box_str += "\n";
82  }
83  char *ret = new char[wordstr_box_str.length() + 1];
84  strcpy(ret, wordstr_box_str.c_str());
85  delete res_it;
86  return ret;
87 }
88 
89 /**********************************************************************
90  * WordStrBox Renderer interface implementation
91  **********************************************************************/
93  : TessResultRenderer(outputbase, "box") {}
94 
96  const std::unique_ptr<const char[]> wordstrbox(api->GetWordStrBoxText(imagenum()));
97  if (wordstrbox == nullptr) {
98  return false;
99  }
100 
101  AppendString(wordstrbox.get());
102 
103  return true;
104 }
105 
106 } // namespace tesseract.
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:831
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:774
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:767
char * GetWordStrBoxText(int page_number)
LTRResultIterator * GetLTRIterator()
Definition: baseapi.cpp:1296
char * GetUTF8Text(PageIteratorLevel level) const
virtual bool Next(PageIteratorLevel level)
virtual bool IsAtBeginningOf(PageIteratorLevel level) const
bool Empty(PageIteratorLevel level) const
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
void AppendString(const char *s)
Definition: renderer.cpp:111
bool AddImageHandler(TessBaseAPI *api) override
TessWordStrBoxRenderer(const char *outputbase)