All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boxread.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: boxread.cpp
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 <stdio.h>
24 #include "genericvector.h"
25 #include "strngs.h"
26 
27 class STRING;
28 class TBOX;
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 NULL 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 NULL).
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 bool ReadMemBoxes(int target_page, bool skip_blanks, const char* box_data,
52  GenericVector<TBOX>* boxes,
53  GenericVector<STRING>* texts,
54  GenericVector<STRING>* box_texts,
55  GenericVector<int>* pages);
56 
57 // Returns the box file name corresponding to the given image_filename.
58 STRING BoxFileName(const STRING& image_filename);
59 
60 // ReadNextBox factors out the code to interpret a line of a box
61 // file so that applybox and unicharset_extractor interpret the same way.
62 // This function returns the next valid box file utf8 string and coords
63 // and returns true, or false on eof (and closes the file).
64 // It ignores the utf8 file signature ByteOrderMark (U+FEFF=EF BB BF), checks
65 // for valid utf-8 and allows space or tab between fields.
66 // utf8_str is set with the unichar string, and bounding box with the box.
67 // If there are page numbers in the file, it reads them all.
68 bool ReadNextBox(int *line_number, FILE* box_file,
69  STRING* utf8_str, TBOX* bounding_box);
70 // As ReadNextBox above, but get a specific page number. (0-based)
71 // Use -1 to read any page number. Files without page number all
72 // read as if they are page 0.
73 bool ReadNextBox(int target_page, int *line_number, FILE* box_file,
74  STRING* utf8_str, TBOX* bounding_box);
75 
76 // Parses the given box file string into a page_number, utf8_str, and
77 // bounding_box. Returns true on a successful parse.
78 bool ParseBoxFileStr(const char* boxfile_str, int* page_number,
79  STRING* utf8_str, TBOX* bounding_box);
80 
81 // Creates a box file string from a unichar string, TBOX and page number.
82 void MakeBoxFileStr(const char* unichar_str, const TBOX& box, int page_num,
83  STRING* box_str);
84 
85 #endif // TESSERACT_CCUTIL_BOXREAD_H__
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:51
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:64
const int kBoxReadBufSize
Definition: boxread.h:31
STRING BoxFileName(const STRING &image_filename)
Definition: boxread.cpp:97
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
Definition: boxread.cpp:225
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:165
FILE * OpenBoxFile(const STRING &fname)
Definition: boxread.cpp:33
Definition: rect.h:30
Definition: strngs.h:44
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:118