tesseract  5.0.0-alpha-619-ge9db
boxread.cpp File Reference
#include "boxread.h"
#include <cstring>
#include <locale>
#include <sstream>
#include <string>
#include "errcode.h"
#include "fileerr.h"
#include <tesseract/genericvector.h>
#include <tesseract/helpers.h>
#include "rect.h"
#include <tesseract/strngs.h>
#include "tprintf.h"
#include <tesseract/unichar.h>

Go to the source code of this file.

Functions

FILE * OpenBoxFile (const STRING &fname)
 
bool ReadAllBoxes (int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
 
bool ReadMemBoxes (int target_page, bool skip_blanks, const char *box_data, bool continue_on_failure, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
 
bool ReadNextBox (int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
 
bool ReadNextBox (int target_page, int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
 
bool ParseBoxFileStr (const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
 
void MakeBoxFileStr (const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
 

Function Documentation

◆ MakeBoxFileStr()

void MakeBoxFileStr ( const char *  unichar_str,
const TBOX box,
int  page_num,
STRING box_str 
)

Definition at line 249 of file boxread.cpp.

250  {
251  *box_str = unichar_str;
252  box_str->add_str_int(" ", box.left());
253  box_str->add_str_int(" ", box.bottom());
254  box_str->add_str_int(" ", box.right());
255  box_str->add_str_int(" ", box.top());
256  box_str->add_str_int(" ", page_num);
257 }

◆ OpenBoxFile()

FILE* OpenBoxFile ( const STRING fname)

Definition at line 54 of file boxread.cpp.

54  {
55  std::string filename = BoxFileName(fname.c_str());
56  FILE* box_file = nullptr;
57  if (!(box_file = fopen(filename.c_str(), "rb"))) {
58  CANTOPENFILE.error("read_next_box", TESSEXIT, "Can't open box file %s",
59  filename.c_str());
60  }
61  return box_file;
62 }

◆ ParseBoxFileStr()

bool ParseBoxFileStr ( const char *  boxfile_str,
int *  page_number,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 181 of file boxread.cpp.

182  {
183  *bounding_box = TBOX(); // Initialize it to empty.
184  *utf8_str = "";
185  char uch[kBoxReadBufSize];
186  const char *buffptr = boxfile_str;
187  // Read the unichar without messing up on Tibetan.
188  // According to issue 253 the utf-8 surrogates 85 and A0 are treated
189  // as whitespace by sscanf, so it is more reliable to just find
190  // ascii space and tab.
191  int uch_len = 0;
192  // Skip unicode file designation, if present.
193  const auto *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
194  if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
195  buffptr += 3;
196  // Allow a single blank as the UTF-8 string. Check for empty string and
197  // then blindly eat the first character.
198  if (*buffptr == '\0') return false;
199  do {
200  uch[uch_len++] = *buffptr++;
201  } while (*buffptr != '\0' && *buffptr != ' ' && *buffptr != '\t' &&
202  uch_len < kBoxReadBufSize - 1);
203  uch[uch_len] = '\0';
204  if (*buffptr != '\0') ++buffptr;
205  int x_min = INT_MAX;
206  int y_min = INT_MAX;
207  int x_max = INT_MIN;
208  int y_max = INT_MIN;
209  *page_number = 0;
210  std::stringstream stream(buffptr);
211  stream.imbue(std::locale::classic());
212  stream >> x_min;
213  stream >> y_min;
214  stream >> x_max;
215  stream >> y_max;
216  stream >> *page_number;
217  if (x_max < x_min || y_max < y_min) {
218  tprintf("Bad box coordinates in boxfile string! %s\n", ubuf);
219  return false;
220  }
221  // Test for long space-delimited string label.
222  if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
223  (buffptr = strchr(buffptr, '#')) != nullptr) {
224  strncpy(uch, buffptr + 1, kBoxReadBufSize - 1);
225  uch[kBoxReadBufSize - 1] = '\0'; // Prevent buffer overrun.
226  chomp_string(uch);
227  uch_len = strlen(uch);
228  }
229  // Validate UTF8 by making unichars with it.
230  int used = 0;
231  while (used < uch_len) {
232  tesseract::UNICHAR ch(uch + used, uch_len - used);
233  int new_used = ch.utf8_len();
234  if (new_used == 0) {
235  tprintf("Bad UTF-8 str %s starts with 0x%02x at col %d\n",
236  uch + used, uch[used], used + 1);
237  return false;
238  }
239  used += new_used;
240  }
241  *utf8_str = uch;
242  if (x_min > x_max) Swap(&x_min, &x_max);
243  if (y_min > y_max) Swap(&y_min, &y_max);
244  bounding_box->set_to_given_coords(x_min, y_min, x_max, y_max);
245  return true; // Successfully read a box.
246 }

◆ ReadAllBoxes()

bool ReadAllBoxes ( int  target_page,
bool  skip_blanks,
const STRING filename,
GenericVector< TBOX > *  boxes,
GenericVector< STRING > *  texts,
GenericVector< STRING > *  box_texts,
GenericVector< int > *  pages 
)

Definition at line 71 of file boxread.cpp.

75  {
76  GenericVector<char> box_data;
77  if (!tesseract::LoadDataFromFile(BoxFileName(filename.c_str()).c_str(), &box_data))
78  return false;
79  // Convert the array of bytes to a string, so it can be used by the parser.
80  box_data.push_back('\0');
81  return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
82  /*continue_on_failure*/ true, boxes, texts, box_texts,
83  pages);
84 }

◆ ReadMemBoxes()

bool ReadMemBoxes ( int  target_page,
bool  skip_blanks,
const char *  box_data,
bool  continue_on_failure,
GenericVector< TBOX > *  boxes,
GenericVector< STRING > *  texts,
GenericVector< STRING > *  box_texts,
GenericVector< int > *  pages 
)

Definition at line 87 of file boxread.cpp.

92  {
93  STRING box_str(box_data);
95  box_str.split('\n', &lines);
96  if (lines.empty()) return false;
97  int num_boxes = 0;
98  for (int i = 0; i < lines.size(); ++i) {
99  int page = 0;
100  STRING utf8_str;
101  TBOX box;
102  if (!ParseBoxFileStr(lines[i].c_str(), &page, &utf8_str, &box)) {
103  if (continue_on_failure)
104  continue;
105  else
106  return false;
107  }
108  if (skip_blanks && (utf8_str == " " || utf8_str == "\t")) continue;
109  if (target_page >= 0 && page != target_page) continue;
110  if (boxes != nullptr) boxes->push_back(box);
111  if (texts != nullptr) texts->push_back(utf8_str);
112  if (box_texts != nullptr) {
113  STRING full_text;
114  MakeBoxFileStr(utf8_str.c_str(), box, target_page, &full_text);
115  box_texts->push_back(full_text);
116  }
117  if (pages != nullptr) pages->push_back(page);
118  ++num_boxes;
119  }
120  return num_boxes > 0;
121 }

◆ ReadNextBox() [1/2]

bool ReadNextBox ( int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 134 of file boxread.cpp.

135  {
136  return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
137 }

◆ ReadNextBox() [2/2]

bool ReadNextBox ( int  target_page,
int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 142 of file boxread.cpp.

143  {
144  int page = 0;
145  char buff[kBoxReadBufSize]; // boxfile read buffer
146  char *buffptr = buff;
147 
148  while (fgets(buff, sizeof(buff) - 1, box_file)) {
149  (*line_number)++;
150 
151  buffptr = buff;
152  const auto *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
153  if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
154  buffptr += 3; // Skip unicode file designation.
155  // Check for blank lines in box file
156  if (*buffptr == '\n' || *buffptr == '\0') continue;
157  // Skip blank boxes.
158  if (*buffptr == ' ' || *buffptr == '\t') continue;
159  if (*buffptr != '\0') {
160  if (!ParseBoxFileStr(buffptr, &page, utf8_str, bounding_box)) {
161  tprintf("Box file format error on line %i; ignored\n", *line_number);
162  continue;
163  }
164  if (target_page >= 0 && target_page != page)
165  continue; // Not on the appropriate page.
166  return true; // Successfully read a box.
167  }
168  }
169  fclose(box_file);
170  return false; // EOF
171 }
TBOX
Definition: cleanapi_test.cc:19
string
std::string string
Definition: equationdetect_test.cc:21
TESSEXIT
Definition: errcode.h:42
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::LoadDataFromFile
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
Definition: genericvector.h:341
MakeBoxFileStr
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
Definition: boxread.cpp:249
TBOX::top
int16_t top() const
Definition: rect.h:57
STRING
Definition: strngs.h:45
TBOX::set_to_given_coords
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
Definition: rect.h:270
tesseract::UNICHAR
Definition: unichar.h:59
ReadNextBox
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:134
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
ParseBoxFileStr
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:181
ERRCODE::error
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:33
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
GenericVector::empty
bool empty() const
Definition: genericvector.h:86
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
ReadMemBoxes
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, bool continue_on_failure, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
Definition: boxread.cpp:87
GenericVector< char >
kBoxReadBufSize
const int kBoxReadBufSize
Definition: boxread.h:30
TBOX::left
int16_t left() const
Definition: rect.h:71
CANTOPENFILE
constexpr ERRCODE CANTOPENFILE("Can't open file")
TBOX::right
int16_t right() const
Definition: rect.h:78
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
Swap
void Swap(T *p1, T *p2)
Definition: helpers.h:93
GenericVector::size
int size() const
Definition: genericvector.h:71
TBOX
Definition: rect.h:33