tesseract  3.05.01
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 96 of file boxread.cpp.

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

◆ MakeBoxFileStr()

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

Definition at line 224 of file boxread.cpp.

225  {
226  *box_str = unichar_str;
227  box_str->add_str_int(" ", box.left());
228  box_str->add_str_int(" ", box.bottom());
229  box_str->add_str_int(" ", box.right());
230  box_str->add_str_int(" ", box.top());
231  box_str->add_str_int(" ", page_num);
232 }
void add_str_int(const char *str, int number)
Definition: strngs.cpp:384
inT16 bottom() const
Definition: rect.h:61
inT16 left() const
Definition: rect.h:68
inT16 top() const
Definition: rect.h:54
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 }
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:96
const ERRCODE CANTOPENFILE
Definition: fileerr.h:25
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
Definition: strngs.h:44

◆ ParseBoxFileStr()

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

Definition at line 164 of file boxread.cpp.

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

◆ 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;
57  return false;
58  return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
59  box_texts, pages);
60 }
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:96
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:63

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

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

◆ ReadNextBox() [1/2]

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

Definition at line 117 of file boxread.cpp.

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

◆ ReadNextBox() [2/2]

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

Definition at line 125 of file boxread.cpp.

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