Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boxread.cpp
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 #include "mfcpch.h"
21 #include "boxread.h"
22 #include <string.h>
23
24 #include "rect.h"
25 #include "strngs.h"
26 #include "tprintf.h"
27 #include "unichar.h"
28
29 // Special char code used to identify multi-blob labels.
30 static const char* kMultiBlobLabelCode = "WordStr";
31
32 // Open the boxfile based on the given image filename.
33 FILE* OpenBoxFile(const STRING& fname) {
34 STRING filename = fname;
35 const char *lastdot = strrchr(filename.string(), '.');
36 if (lastdot != NULL)
37 filename[lastdot - filename.string()] = '\0';
38
39 filename += ".box";
40 FILE* box_file = NULL;
41 if (!(box_file = fopen(filename.string(), "rb"))) {
42 CANTOPENFILE.error("read_next_box", TESSEXIT,
43 "Cant open box file %s",
44 filename.string());
45 }
46 return box_file;
47 }
48
49 // Box files are used ONLY DURING TRAINING, but by both processes of
50 // creating tr files with tesseract, and unicharset_extractor.
51 // ReadNextBox factors out the code to interpret a line of a box
52 // file so that applybox and unicharset_extractor interpret the same way.
53 // This function returns the next valid box file utf8 string and coords
54 // and returns true, or false on eof (and closes the file).
55 // It ignores the utf8 file signature ByteOrderMark (U+FEFF=EF BB BF), checks
56 // for valid utf-8 and allows space or tab between fields.
57 // utf8_str is set with the unichar string, and bounding box with the box.
58 // If there are page numbers in the file, it reads them all.
59 bool ReadNextBox(int *line_number, FILE* box_file,
60 STRING* utf8_str, TBOX* bounding_box) {
61 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
62 }
63
64 // As ReadNextBox above, but get a specific page number. (0-based)
65 // Use -1 to read any page number. Files without page number all
66 // read as if they are page 0.
67 bool ReadNextBox(int target_page, int *line_number, FILE* box_file,
68 STRING* utf8_str, TBOX* bounding_box) {
69 int page = 0;
70 char buff[kBoxReadBufSize]; // boxfile read buffer
71 char *buffptr = buff;
72
73 while (fgets(buff, sizeof(buff) - 1, box_file)) {
74 (*line_number)++;
75
76 buffptr = buff;
77 const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
78 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
79 buffptr += 3; // Skip unicode file designation.
80 // Check for blank lines in box file
81 while (*buffptr == ' ' || *buffptr == '\t')
82 buffptr++;
83 if (*buffptr != '\0') {
84 if (!ParseBoxFileStr(buffptr, &page, utf8_str, bounding_box)) {
85 tprintf("Box file format error on line %i; ignored\n", *line_number);
86 continue;
87 }
88 if (target_page >= 0 && target_page != page)
89 continue; // Not on the appropriate page.
90 return true; // Successfully read a box.
91 }
92 }
93 fclose(box_file);
94 return false; // EOF
95 }
96
97 // Parses the given box file string into a page_number, utf8_str, and
98 // bounding_box. Returns true on a successful parse.
99 // The box file is assumed to contain box definitions, one per line, of the
100 // following format for blob-level boxes:
101 // <UTF8 str> <left> <bottom> <right> <top> <page id>
102 // and for word/line-level boxes:
103 // WordStr <left> <bottom> <right> <top> <page id> #<space-delimited word str>
104 // See applyybox.cpp for more information.
105 bool ParseBoxFileStr(const char* boxfile_str, int* page_number,
106 STRING* utf8_str, TBOX* bounding_box) {
107 *bounding_box = TBOX(); // Initialize it to empty.
108 *utf8_str = "";
109 char uch[kBoxReadBufSize];
110 const char *buffptr = boxfile_str;
111 // Read the unichar without messing up on Tibetan.
112 // According to issue 253 the utf-8 surrogates 85 and A0 are treated
113 // as whitespace by sscanf, so it is more reliable to just find
114 // ascii space and tab.
115 int uch_len = 0;
116 while (*buffptr != '\0' && *buffptr != ' ' && *buffptr != '\t' &&
117 uch_len < kBoxReadBufSize - 1) {
118 uch[uch_len++] = *buffptr++;
119 }
120 uch[uch_len] = '\0';
121 if (*buffptr != '\0') ++buffptr;
122 int x_min, y_min, x_max, y_max;
123 *page_number = 0;
124 int count = sscanf(buffptr, "%d %d %d %d %d",
125 &x_min, &y_min, &x_max, &y_max, page_number);
126 if (count != 5 && count != 4) {
127 tprintf("Bad box coordinates in boxfile string!\n");
128 return false;
129 }
130 // Test for long space-delimited string label.
131 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
132 (buffptr = strchr(buffptr, '#')) != NULL) {
133 strncpy(uch, buffptr + 1, kBoxReadBufSize);
134 chomp_string(uch);
135 uch_len = strlen(uch);
136 }
137 // Validate UTF8 by making unichars with it.
138 int used = 0;
139 while (used < uch_len) {
140 UNICHAR ch(uch + used, uch_len - used);
141 int new_used = ch.utf8_len();
142 if (new_used == 0) {
143 tprintf("Bad UTF-8 str %s starts with 0x%02x at col %d\n",
144 uch + used, uch[used], used + 1);
145 return false;
146 }
147 used += new_used;
148 }
149 *utf8_str = uch;
150 bounding_box->set_to_given_coords(x_min, y_min, x_max, y_max);
151 return true; // Successfully read a box.
152 }
153
154 // Creates a box file string from a unichar string, TBOX and page number.
155 void MakeBoxFileStr(const char* unichar_str, const TBOX& box, int page_num,
156 STRING* box_str) {
157 *box_str = unichar_str;
158 box_str->add_str_int(" ", box.left());
159 box_str->add_str_int(" ", box.bottom());
160 box_str->add_str_int(" ", box.right());
161 box_str->add_str_int(" ", box.top());
162 box_str->add_str_int(" ", page_num);
163 }
164