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, 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 98 of file boxread.cpp.

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

◆ MakeBoxFileStr()

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

Definition at line 226 of file boxread.cpp.

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

◆ 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
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
const char * filename
Definition: ioapi.h:38
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:98

◆ ParseBoxFileStr()

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

Definition at line 166 of file boxread.cpp.

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

◆ 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], boxes, texts,
61  box_texts, pages);
62 }
int push_back(T object)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:98
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
Definition: boxread.cpp:65

◆ ReadMemBoxes()

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

Definition at line 65 of file boxread.cpp.

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

◆ ReadNextBox() [1/2]

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

Definition at line 119 of file boxread.cpp.

120  {
121  return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
122 }
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:119

◆ ReadNextBox() [2/2]

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

Definition at line 127 of file boxread.cpp.

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