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