tesseract  5.0.0-alpha-619-ge9db
boxread.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: boxread.h
3  * Description: Read data from a box file.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 2007, Google Inc.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #ifndef TESSERACT_CCUTIL_BOXREAD_H_
20 #define TESSERACT_CCUTIL_BOXREAD_H_
21 
22 #include <cstdio> // for FILE
23 #include <tesseract/strngs.h> // for STRING
24 
25 class TBOX;
26 
27 template <typename T> class GenericVector;
28 template <typename T> class GenericVector;
29 
30 // Size of buffer used to read a line from a box file.
31 const int kBoxReadBufSize = 1024;
32 
33 // Open the boxfile based on the given image filename.
34 // Returns nullptr if the box file cannot be opened.
35 FILE* OpenBoxFile(const STRING& fname);
36 
37 // Reads all boxes from the given filename.
38 // Reads a specific target_page number if >= 0, or all pages otherwise.
39 // Skips blanks if skip_blanks is true.
40 // The UTF-8 label of the box is put in texts, and the full box definition as
41 // a string is put in box_texts, with the corresponding page number in pages.
42 // Each of the output vectors is optional (may be nullptr).
43 // Returns false if no boxes are found.
44 bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING& filename,
45  GenericVector<TBOX>* boxes,
46  GenericVector<STRING>* texts,
47  GenericVector<STRING>* box_texts,
48  GenericVector<int>* pages);
49 
50 // Reads all boxes from the string. Otherwise, as ReadAllBoxes.
51 // continue_on_failure allows reading to continue even if an invalid box is
52 // encountered and will return true if it succeeds in reading some boxes.
53 // It otherwise gives up and returns false on encountering an invalid box.
54 bool ReadMemBoxes(int target_page, bool skip_blanks, const char* box_data,
55  bool continue_on_failure,
56  GenericVector<TBOX>* boxes,
57  GenericVector<STRING>* texts,
58  GenericVector<STRING>* box_texts,
59  GenericVector<int>* pages);
60 
61 // ReadNextBox factors out the code to interpret a line of a box
62 // file so that applybox and unicharset_extractor interpret the same way.
63 // This function returns the next valid box file utf8 string and coords
64 // and returns true, or false on eof (and closes the file).
65 // It ignores the utf8 file signature ByteOrderMark (U+FEFF=EF BB BF), checks
66 // for valid utf-8 and allows space or tab between fields.
67 // utf8_str is set with the unichar string, and bounding box with the box.
68 // If there are page numbers in the file, it reads them all.
69 bool ReadNextBox(int *line_number, FILE* box_file,
70  STRING* utf8_str, TBOX* bounding_box);
71 // As ReadNextBox above, but get a specific page number. (0-based)
72 // Use -1 to read any page number. Files without page number all
73 // read as if they are page 0.
74 bool ReadNextBox(int target_page, int *line_number, FILE* box_file,
75  STRING* utf8_str, TBOX* bounding_box);
76 
77 // Parses the given box file string into a page_number, utf8_str, and
78 // bounding_box. Returns true on a successful parse.
79 bool ParseBoxFileStr(const char* boxfile_str, int* page_number,
80  STRING* utf8_str, TBOX* bounding_box);
81 
82 // Creates a box file string from a unichar string, TBOX and page number.
83 void MakeBoxFileStr(const char* unichar_str, const TBOX& box, int page_num,
84  STRING* box_str);
85 
86 #endif // TESSERACT_CCUTIL_BOXREAD_H_
strngs.h
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: boxread.cpp:87
MakeBoxFileStr
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
Definition: boxread.cpp:249
ReadNextBox
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:134
STRING
Definition: strngs.h:45
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: boxread.cpp:71
ParseBoxFileStr
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:181
GenericVector
Definition: baseapi.h:40
kBoxReadBufSize
const int kBoxReadBufSize
Definition: boxread.h:30
OpenBoxFile
FILE * OpenBoxFile(const STRING &fname)
Definition: boxread.cpp:54
TBOX
Definition: rect.h:33