tesseract  4.00.00dev
boxread.cpp File Reference
#include "boxread.h"
#include <string.h>
#include "fileerr.h"
#include "rect.h"
#include "strngs.h"
#include "tprintf.h"
#include "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)
 
STRING BoxFileName (const STRING &image_filename)
 
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

◆ BoxFileName()

STRING BoxFileName ( const STRING image_filename)

Definition at line 103 of file boxread.cpp.

103  {
104  STRING box_filename = image_filename;
105  const char *lastdot = strrchr(box_filename.string(), '.');
106  if (lastdot != NULL)
107  box_filename.truncate_at(lastdot - box_filename.string());
108 
109  box_filename += ".box";
110  return box_filename;
111 }
const char * string() const
Definition: strngs.cpp:198
Definition: strngs.h:45
void truncate_at(inT32 index)
Definition: strngs.cpp:269

◆ MakeBoxFileStr()

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

Definition at line 231 of file boxread.cpp.

232  {
233  *box_str = unichar_str;
234  box_str->add_str_int(" ", box.left());
235  box_str->add_str_int(" ", box.bottom());
236  box_str->add_str_int(" ", box.right());
237  box_str->add_str_int(" ", box.top());
238  box_str->add_str_int(" ", page_num);
239 }
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
inT16 top() const
Definition: rect.h:54
inT16 bottom() const
Definition: rect.h:61
inT16 left() const
Definition: rect.h:68
inT16 right() const
Definition: rect.h:75

◆ OpenBoxFile()

FILE* OpenBoxFile ( const STRING fname)

Definition at line 33 of file boxread.cpp.

33  {
34  STRING filename = BoxFileName(fname);
35  FILE* box_file = NULL;
36  if (!(box_file = fopen(filename.string(), "rb"))) {
37  CANTOPENFILE.error("read_next_box", TESSEXIT, "Can't open box file %s",
38  filename.string());
39  }
40  return box_file;
41 }
const ERRCODE CANTOPENFILE
Definition: fileerr.h:25
const char * string() const
Definition: strngs.cpp:198
Definition: strngs.h:45
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:103
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40

◆ ParseBoxFileStr()

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

Definition at line 171 of file boxread.cpp.

172  {
173  *bounding_box = TBOX(); // Initialize it to empty.
174  *utf8_str = "";
175  char uch[kBoxReadBufSize];
176  const char *buffptr = boxfile_str;
177  // Read the unichar without messing up on Tibetan.
178  // According to issue 253 the utf-8 surrogates 85 and A0 are treated
179  // as whitespace by sscanf, so it is more reliable to just find
180  // ascii space and tab.
181  int uch_len = 0;
182  // Skip unicode file designation, if present.
183  const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
184  if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
185  buffptr += 3;
186  // Allow a single blank as the UTF-8 string. Check for empty string and
187  // then blindly eat the first character.
188  if (*buffptr == '\0') return false;
189  do {
190  uch[uch_len++] = *buffptr++;
191  } while (*buffptr != '\0' && *buffptr != ' ' && *buffptr != '\t' &&
192  uch_len < kBoxReadBufSize - 1);
193  uch[uch_len] = '\0';
194  if (*buffptr != '\0') ++buffptr;
195  int x_min, y_min, x_max, y_max;
196  *page_number = 0;
197  int count = sscanf(buffptr, "%d %d %d %d %d",
198  &x_min, &y_min, &x_max, &y_max, page_number);
199  if (count != 5 && count != 4) {
200  tprintf("Bad box coordinates in boxfile string! %s\n", ubuf);
201  return false;
202  }
203  // Test for long space-delimited string label.
204  if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
205  (buffptr = strchr(buffptr, '#')) != NULL) {
206  strncpy(uch, buffptr + 1, kBoxReadBufSize - 1);
207  uch[kBoxReadBufSize - 1] = '\0'; // Prevent buffer overrun.
208  chomp_string(uch);
209  uch_len = strlen(uch);
210  }
211  // Validate UTF8 by making unichars with it.
212  int used = 0;
213  while (used < uch_len) {
214  tesseract::UNICHAR ch(uch + used, uch_len - used);
215  int new_used = ch.utf8_len();
216  if (new_used == 0) {
217  tprintf("Bad UTF-8 str %s starts with 0x%02x at col %d\n",
218  uch + used, uch[used], used + 1);
219  return false;
220  }
221  used += new_used;
222  }
223  *utf8_str = uch;
224  if (x_min > x_max) Swap(&x_min, &x_max);
225  if (y_min > y_max) Swap(&y_min, &y_max);
226  bounding_box->set_to_given_coords(x_min, y_min, x_max, y_max);
227  return true; // Successfully read a box.
228 }
void chomp_string(char *str)
Definition: helpers.h:82
#define tprintf(...)
Definition: tprintf.h:31
int count(LIST var_list)
Definition: oldlist.cpp:103
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
Definition: rect.h:263
const int kBoxReadBufSize
Definition: boxread.h:31
Definition: rect.h:30
void Swap(T *p1, T *p2)
Definition: helpers.h:97

◆ 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 50 of file boxread.cpp.

54  {
55  GenericVector<char> box_data;
56  if (!tesseract::LoadDataFromFile(BoxFileName(filename), &box_data))
57  return false;
58  // Convert the array of bytes to a string, so it can be used by the parser.
59  box_data.push_back('\0');
60  return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
61  /*continue_on_failure*/ true, boxes, texts, box_texts,
62  pages);
63 }
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:66
int push_back(T object)
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:103
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)

◆ 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 66 of file boxread.cpp.

71  {
72  STRING box_str(box_data);
74  box_str.split('\n', &lines);
75  if (lines.empty()) return false;
76  int num_boxes = 0;
77  for (int i = 0; i < lines.size(); ++i) {
78  int page = 0;
79  STRING utf8_str;
80  TBOX box;
81  if (!ParseBoxFileStr(lines[i].string(), &page, &utf8_str, &box)) {
82  if (continue_on_failure)
83  continue;
84  else
85  return false;
86  }
87  if (skip_blanks && (utf8_str == " " || utf8_str == "\t")) continue;
88  if (target_page >= 0 && page != target_page) continue;
89  if (boxes != NULL) boxes->push_back(box);
90  if (texts != NULL) texts->push_back(utf8_str);
91  if (box_texts != NULL) {
92  STRING full_text;
93  MakeBoxFileStr(utf8_str.string(), box, target_page, &full_text);
94  box_texts->push_back(full_text);
95  }
96  if (pages != NULL) pages->push_back(page);
97  ++num_boxes;
98  }
99  return num_boxes > 0;
100 }
bool empty() const
Definition: genericvector.h:91
int size() const
Definition: genericvector.h:72
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:171
const char * string() const
Definition: strngs.cpp:198
int push_back(T object)
Definition: strngs.h:45
Definition: rect.h:30
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
Definition: boxread.cpp:231

◆ ReadNextBox() [1/2]

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

Definition at line 124 of file boxread.cpp.

125  {
126  return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
127 }
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:124

◆ ReadNextBox() [2/2]

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

Definition at line 132 of file boxread.cpp.

133  {
134  int page = 0;
135  char buff[kBoxReadBufSize]; // boxfile read buffer
136  char *buffptr = buff;
137 
138  while (fgets(buff, sizeof(buff) - 1, box_file)) {
139  (*line_number)++;
140 
141  buffptr = buff;
142  const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
143  if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
144  buffptr += 3; // Skip unicode file designation.
145  // Check for blank lines in box file
146  if (*buffptr == '\n' || *buffptr == '\0') continue;
147  // Skip blank boxes.
148  if (*buffptr == ' ' || *buffptr == '\t') continue;
149  if (*buffptr != '\0') {
150  if (!ParseBoxFileStr(buffptr, &page, utf8_str, bounding_box)) {
151  tprintf("Box file format error on line %i; ignored\n", *line_number);
152  continue;
153  }
154  if (target_page >= 0 && target_page != page)
155  continue; // Not on the appropriate page.
156  return true; // Successfully read a box.
157  }
158  }
159  fclose(box_file);
160  return false; // EOF
161 }
#define tprintf(...)
Definition: tprintf.h:31
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:171
const int kBoxReadBufSize
Definition: boxread.h:31