|
tesseract 3.04.01
|
00001 /********************************************************************** 00002 * File: boxread.cpp 00003 * Description: Read data from a box file. 00004 * Author: Ray Smith 00005 * Created: Fri Aug 24 17:47:23 PDT 2007 00006 * 00007 * (C) Copyright 2007, Google Inc. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #ifndef TESSERACT_CCUTIL_BOXREAD_H__ 00021 #define TESSERACT_CCUTIL_BOXREAD_H__ 00022 00023 #include <stdio.h> 00024 #include "genericvector.h" 00025 #include "strngs.h" 00026 00027 class STRING; 00028 class TBOX; 00029 00030 // Size of buffer used to read a line from a box file. 00031 const int kBoxReadBufSize = 1024; 00032 00033 // Open the boxfile based on the given image filename. 00034 // Returns NULL if the box file cannot be opened. 00035 FILE* OpenBoxFile(const STRING& fname); 00036 00037 // Reads all boxes from the given filename. 00038 // Reads a specific target_page number if >= 0, or all pages otherwise. 00039 // Skips blanks if skip_blanks is true. 00040 // The UTF-8 label of the box is put in texts, and the full box definition as 00041 // a string is put in box_texts, with the corresponding page number in pages. 00042 // Each of the output vectors is optional (may be NULL). 00043 // Returns false if no boxes are found. 00044 bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING& filename, 00045 GenericVector<TBOX>* boxes, 00046 GenericVector<STRING>* texts, 00047 GenericVector<STRING>* box_texts, 00048 GenericVector<int>* pages); 00049 00050 // Reads all boxes from the string. Otherwise, as ReadAllBoxes. 00051 bool ReadMemBoxes(int target_page, bool skip_blanks, const char* box_data, 00052 GenericVector<TBOX>* boxes, 00053 GenericVector<STRING>* texts, 00054 GenericVector<STRING>* box_texts, 00055 GenericVector<int>* pages); 00056 00057 // Returns the box file name corresponding to the given image_filename. 00058 STRING BoxFileName(const STRING& image_filename); 00059 00060 // ReadNextBox factors out the code to interpret a line of a box 00061 // file so that applybox and unicharset_extractor interpret the same way. 00062 // This function returns the next valid box file utf8 string and coords 00063 // and returns true, or false on eof (and closes the file). 00064 // It ignores the utf8 file signature ByteOrderMark (U+FEFF=EF BB BF), checks 00065 // for valid utf-8 and allows space or tab between fields. 00066 // utf8_str is set with the unichar string, and bounding box with the box. 00067 // If there are page numbers in the file, it reads them all. 00068 bool ReadNextBox(int *line_number, FILE* box_file, 00069 STRING* utf8_str, TBOX* bounding_box); 00070 // As ReadNextBox above, but get a specific page number. (0-based) 00071 // Use -1 to read any page number. Files without page number all 00072 // read as if they are page 0. 00073 bool ReadNextBox(int target_page, int *line_number, FILE* box_file, 00074 STRING* utf8_str, TBOX* bounding_box); 00075 00076 // Parses the given box file string into a page_number, utf8_str, and 00077 // bounding_box. Returns true on a successful parse. 00078 bool ParseBoxFileStr(const char* boxfile_str, int* page_number, 00079 STRING* utf8_str, TBOX* bounding_box); 00080 00081 // Creates a box file string from a unichar string, TBOX and page number. 00082 void MakeBoxFileStr(const char* unichar_str, const TBOX& box, int page_num, 00083 STRING* box_str); 00084 00085 #endif // TESSERACT_CCUTIL_BOXREAD_H__