All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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

STRING BoxFileName ( const STRING image_filename)

Definition at line 97 of file boxread.cpp.

97  {
98  STRING box_filename = image_filename;
99  const char *lastdot = strrchr(box_filename.string(), '.');
100  if (lastdot != NULL)
101  box_filename.truncate_at(lastdot - box_filename.string());
102 
103  box_filename += ".box";
104  return box_filename;
105 }
void truncate_at(inT32 index)
Definition: strngs.cpp:264
Definition: strngs.h:44
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193
void MakeBoxFileStr ( const char *  unichar_str,
const TBOX box,
int  page_num,
STRING box_str 
)

Definition at line 225 of file boxread.cpp.

226  {
227  *box_str = unichar_str;
228  box_str->add_str_int(" ", box.left());
229  box_str->add_str_int(" ", box.bottom());
230  box_str->add_str_int(" ", box.right());
231  box_str->add_str_int(" ", box.top());
232  box_str->add_str_int(" ", page_num);
233 }
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
inT16 top() const
Definition: rect.h:54
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,
38  "Cant open box file %s",
39  filename.string());
40  }
41  return box_file;
42 }
const ERRCODE CANTOPENFILE
Definition: fileerr.h:25
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:97
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
Definition: strngs.h:44
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193
bool ParseBoxFileStr ( const char *  boxfile_str,
int *  page_number,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 165 of file boxread.cpp.

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

55  {
56  GenericVector<char> box_data;
57  if (!tesseract::LoadDataFromFile(BoxFileName(filename), &box_data))
58  return false;
59  return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
60  box_texts, pages);
61 }
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:64
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:97
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 64 of file boxread.cpp.

68  {
69  STRING box_str(box_data);
71  box_str.split('\n', &lines);
72  if (lines.empty()) return false;
73  int num_boxes = 0;
74  for (int i = 0; i < lines.size(); ++i) {
75  int page = 0;
76  STRING utf8_str;
77  TBOX box;
78  if (!ParseBoxFileStr(lines[i].string(), &page, &utf8_str, &box)) {
79  continue;
80  }
81  if (skip_blanks && (utf8_str == " " || utf8_str == "\t")) continue;
82  if (target_page >= 0 && page != target_page) continue;
83  if (boxes != NULL) boxes->push_back(box);
84  if (texts != NULL) texts->push_back(utf8_str);
85  if (box_texts != NULL) {
86  STRING full_text;
87  MakeBoxFileStr(utf8_str.string(), box, target_page, &full_text);
88  box_texts->push_back(full_text);
89  }
90  if (pages != NULL) pages->push_back(page);
91  ++num_boxes;
92  }
93  return num_boxes > 0;
94 }
int size() const
Definition: genericvector.h:72
int push_back(T object)
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:165
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
Definition: boxread.cpp:225
bool empty() const
Definition: genericvector.h:84
Definition: rect.h:30
Definition: strngs.h:44
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193
bool ReadNextBox ( int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 118 of file boxread.cpp.

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

Definition at line 126 of file boxread.cpp.

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