tesseract 3.04.01

ccstruct/boxread.cpp File Reference

#include "boxread.h"
#include <string.h>
#include "fileerr.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 ReadAllBoxes (int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
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)
STRING BoxFileName (const STRING &image_filename)
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

STRING BoxFileName ( const STRING image_filename)

Definition at line 97 of file boxread.cpp.

                                                 {
  STRING box_filename = image_filename;
  const char *lastdot = strrchr(box_filename.string(), '.');
  if (lastdot != NULL)
    box_filename.truncate_at(lastdot - box_filename.string());

  box_filename += ".box";
  return box_filename;
}
void MakeBoxFileStr ( const char *  unichar_str,
const TBOX box,
int  page_num,
STRING box_str 
)

Definition at line 225 of file boxread.cpp.

                                     {
  *box_str = unichar_str;
  box_str->add_str_int(" ", box.left());
  box_str->add_str_int(" ", box.bottom());
  box_str->add_str_int(" ", box.right());
  box_str->add_str_int(" ", box.top());
  box_str->add_str_int(" ", page_num);
}
FILE* OpenBoxFile ( const STRING fname)

Definition at line 33 of file boxread.cpp.

                                       {
  STRING filename = BoxFileName(fname);
  FILE* box_file = NULL;
  if (!(box_file = fopen(filename.string(), "rb"))) {
    CANTOPENFILE.error("read_next_box", TESSEXIT,
                       "Can't open box file %s",
                       filename.string());
  }
  return box_file;
}
bool ParseBoxFileStr ( const char *  boxfile_str,
int *  page_number,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 165 of file boxread.cpp.

                                                           {
  *bounding_box = TBOX();       // Initialize it to empty.
  *utf8_str = "";
  char uch[kBoxReadBufSize];
  const char *buffptr = boxfile_str;
  // Read the unichar without messing up on Tibetan.
  // According to issue 253 the utf-8 surrogates 85 and A0 are treated
  // as whitespace by sscanf, so it is more reliable to just find
  // ascii space and tab.
  int uch_len = 0;
  // Skip unicode file designation, if present.
  const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
  if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
      buffptr += 3;
  // Allow a single blank as the UTF-8 string. Check for empty string and
  // then blindly eat the first character.
  if (*buffptr == '\0') return false;
  do {
    uch[uch_len++] = *buffptr++;
  } while (*buffptr != '\0' && *buffptr != ' ' && *buffptr != '\t' &&
           uch_len < kBoxReadBufSize - 1);
  uch[uch_len] = '\0';
  if (*buffptr != '\0') ++buffptr;
  int x_min, y_min, x_max, y_max;
  *page_number = 0;
  int count = sscanf(buffptr, "%d %d %d %d %d",
                 &x_min, &y_min, &x_max, &y_max, page_number);
  if (count != 5 && count != 4) {
    tprintf("Bad box coordinates in boxfile string! %s\n", ubuf);
    return false;
  }
  // Test for long space-delimited string label.
  if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
      (buffptr = strchr(buffptr, '#')) != NULL) {
    strncpy(uch, buffptr + 1, kBoxReadBufSize - 1);
    uch[kBoxReadBufSize - 1] = '\0';  // Prevent buffer overrun.
    chomp_string(uch);
    uch_len = strlen(uch);
  }
  // Validate UTF8 by making unichars with it.
  int used = 0;
  while (used < uch_len) {
    UNICHAR ch(uch + used, uch_len - used);
    int new_used = ch.utf8_len();
    if (new_used == 0) {
      tprintf("Bad UTF-8 str %s starts with 0x%02x at col %d\n",
              uch + used, uch[used], used + 1);
      return false;
    }
    used += new_used;
  }
  *utf8_str = uch;
  if (x_min > x_max) Swap(&x_min, &x_max);
  if (y_min > y_max) Swap(&y_min, &y_max);
  bounding_box->set_to_given_coords(x_min, y_min, x_max, y_max);
  return true;  // Successfully read a box.
}
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 at line 51 of file boxread.cpp.

                                             {
  GenericVector<char> box_data;
  if (!tesseract::LoadDataFromFile(BoxFileName(filename), &box_data))
    return false;
  return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
                      box_texts, pages);
}
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 at line 64 of file boxread.cpp.

                                             {
  STRING box_str(box_data);
  GenericVector<STRING> lines;
  box_str.split('\n', &lines);
  if (lines.empty()) return false;
  int num_boxes = 0;
  for (int i = 0; i < lines.size(); ++i) {
    int page = 0;
    STRING utf8_str;
    TBOX box;
    if (!ParseBoxFileStr(lines[i].string(), &page, &utf8_str, &box)) {
      continue;
    }
    if (skip_blanks && (utf8_str == " " || utf8_str == "\t")) continue;
    if (target_page >= 0 && page != target_page) continue;
    if (boxes != NULL) boxes->push_back(box);
    if (texts != NULL) texts->push_back(utf8_str);
    if (box_texts != NULL) {
      STRING full_text;
      MakeBoxFileStr(utf8_str.string(), box, target_page, &full_text);
      box_texts->push_back(full_text);
    }
    if (pages != NULL) pages->push_back(page);
    ++num_boxes;
  }
  return num_boxes > 0;
}
bool ReadNextBox ( int  target_page,
int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 126 of file boxread.cpp.

                                                       {
  int page = 0;
  char buff[kBoxReadBufSize];   // boxfile read buffer
  char *buffptr = buff;

  while (fgets(buff, sizeof(buff) - 1, box_file)) {
    (*line_number)++;

    buffptr = buff;
    const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
    if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
      buffptr += 3;  // Skip unicode file designation.
    // Check for blank lines in box file
    if (*buffptr == '\n' || *buffptr == '\0') continue;
    // Skip blank boxes.
    if (*buffptr == ' ' || *buffptr == '\t') continue;
    if (*buffptr != '\0') {
      if (!ParseBoxFileStr(buffptr, &page, utf8_str, bounding_box)) {
        tprintf("Box file format error on line %i; ignored\n", *line_number);
        continue;
      }
      if (target_page >= 0 && target_page != page)
        continue;  // Not on the appropriate page.
      return true;  // Successfully read a box.
    }
  }
  fclose(box_file);
  return false;  // EOF
}
bool ReadNextBox ( int *  line_number,
FILE *  box_file,
STRING utf8_str,
TBOX bounding_box 
)

Definition at line 118 of file boxread.cpp.

                                                       {
  return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines