Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boxread.cpp File Reference
#include "mfcpch.h"
#include "boxread.h"
#include <string.h>
#include "rect.h"
#include "strngs.h"
#include "tprintf.h"
#include "unichar.h"

Go to the source code of this file.

Functions

FILE * OpenBoxFile (const STRING &fname)
 
bool ReadNextBox (int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
 
bool ReadNextBox (int target_page, int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
 
bool ParseBoxFileStr (const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
 
void MakeBoxFileStr (const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
 

Function Documentation

void MakeBoxFileStr ( const char *  unichar_str,
const TBOX box,
int  page_num,
STRING box_str 
)

Definition at line 155 of file boxread.cpp.

156  {
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 }
inT16 left() const
Definition: rect.h:67
inT16 right() const
Definition: rect.h:74
void add_str_int(const char *str, int number)
Definition: strngs.cpp:334
inT16 top() const
Definition: rect.h:53
inT16 bottom() const
Definition: rect.h:60
FILE* OpenBoxFile ( const STRING fname)

Definition at line 33 of file boxread.cpp.

33  {
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 }
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:156
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
Definition: strngs.h:40
const ERRCODE CANTOPENFILE
Definition: fileerr.h:25
bool ParseBoxFileStr ( const char *  boxfile_str,
int *  page_number,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 105 of file boxread.cpp.

106  {
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 }
#define NULL
Definition: host.h:144
Definition: rect.h:29
const int kBoxReadBufSize
Definition: boxread.h:30
void chomp_string(char *str)
Definition: helpers.h:32
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
Definition: rect.h:256
int count(LIST var_list)
Definition: oldlist.cpp:108
bool ReadNextBox ( int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 59 of file boxread.cpp.

60  {
61  return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
62 }
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:59
bool ReadNextBox ( int  target_page,
int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 67 of file boxread.cpp.

68  {
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 }
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:105
const int kBoxReadBufSize
Definition: boxread.h:30
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41