30 static const char* kMultiBlobLabelCode =
"WordStr";
35 FILE* box_file = NULL;
36 if (!(box_file = fopen(
filename.string(),
"rb"))) {
60 return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
65 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
72 box_str.
split(
'\n', &lines);
73 if (lines.
empty())
return false;
75 for (
int i = 0; i < lines.
size(); ++i) {
82 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
83 if (target_page >= 0 && page != target_page)
continue;
85 if (texts != NULL) texts->
push_back(utf8_str);
86 if (box_texts != NULL) {
91 if (pages != NULL) pages->
push_back(page);
99 STRING box_filename = image_filename;
100 const char *lastdot = strrchr(box_filename.
string(),
'.');
104 box_filename +=
".box";
121 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
127 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
131 char *buffptr = buff;
133 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
137 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
138 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
141 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
143 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
144 if (*buffptr !=
'\0') {
146 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
149 if (target_page >= 0 && target_page != page)
168 *bounding_box =
TBOX();
171 const char *buffptr = boxfile_str;
178 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
179 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
183 if (*buffptr ==
'\0')
return false;
185 uch[uch_len++] = *buffptr++;
186 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
189 if (*buffptr !=
'\0') ++buffptr;
190 int x_min, y_min, x_max, y_max;
192 int count = sscanf(buffptr,
"%d %d %d %d %d",
193 &x_min, &y_min, &x_max, &y_max, page_number);
195 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
199 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
200 (buffptr = strchr(buffptr,
'#')) != NULL) {
204 uch_len = strlen(uch);
208 while (used < uch_len) {
209 UNICHAR ch(uch + used, uch_len - used);
212 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
213 uch + used, uch[used], used + 1);
219 if (x_min > x_max)
Swap(&x_min, &x_max);
220 if (y_min > y_max)
Swap(&y_min, &y_max);
228 *box_str = unichar_str;
const int kBoxReadBufSize
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
void split(const char c, GenericVector< STRING > *splited)
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
void add_str_int(const char *str, int number)
void truncate_at(inT32 index)
const char * string() const
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
STRING BoxFileName(const STRING &image_filename)
const ERRCODE CANTOPENFILE
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
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)
FILE * OpenBoxFile(const STRING &fname)
void chomp_string(char *str)
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)