tesseract 4.1.1
Loading...
Searching...
No Matches
boxread.h File Reference
#include <cstdio>
#include "strngs.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)
 

Variables

const int kBoxReadBufSize = 1024
 

Function Documentation

◆ BoxFileName()

STRING BoxFileName ( const STRING image_filename)

Definition at line 106 of file boxread.cpp.

106 {
107 STRING box_filename = image_filename;
108 const char *lastdot = strrchr(box_filename.string(), '.');
109 if (lastdot != nullptr)
110 box_filename.truncate_at(lastdot - box_filename.string());
111
112 box_filename += ".box";
113 return box_filename;
114}
Definition: strngs.h:45
void truncate_at(int32_t index)
Definition: strngs.cpp:265
const char * string() const
Definition: strngs.cpp:194

◆ MakeBoxFileStr()

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

Definition at line 242 of file boxread.cpp.

243 {
244 *box_str = unichar_str;
245 box_str->add_str_int(" ", box.left());
246 box_str->add_str_int(" ", box.bottom());
247 box_str->add_str_int(" ", box.right());
248 box_str->add_str_int(" ", box.top());
249 box_str->add_str_int(" ", page_num);
250}
int16_t top() const
Definition: rect.h:58
int16_t left() const
Definition: rect.h:72
int16_t bottom() const
Definition: rect.h:65
int16_t right() const
Definition: rect.h:79
void add_str_int(const char *str, int number)
Definition: strngs.cpp:377

◆ OpenBoxFile()

FILE * OpenBoxFile ( const STRING fname)

Definition at line 36 of file boxread.cpp.

36 {
37 STRING filename = BoxFileName(fname);
38 FILE* box_file = nullptr;
39 if (!(box_file = fopen(filename.string(), "rb"))) {
40 CANTOPENFILE.error("read_next_box", TESSEXIT, "Can't open box file %s",
41 filename.string());
42 }
43 return box_file;
44}
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:106
@ TESSEXIT
Definition: errcode.h:28
constexpr ERRCODE CANTOPENFILE("Can't open file")
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:35

◆ ParseBoxFileStr()

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

Definition at line 174 of file boxread.cpp.

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

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

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

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

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

◆ ReadNextBox() [1/2]

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

Definition at line 127 of file boxread.cpp.

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

◆ ReadNextBox() [2/2]

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

Definition at line 135 of file boxread.cpp.

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

Variable Documentation

◆ kBoxReadBufSize

const int kBoxReadBufSize = 1024

Definition at line 32 of file boxread.h.