~ubuntu-branches/ubuntu/vivid/tesseract/vivid

« back to all changes in this revision

Viewing changes to training/boxchar.cpp

  • Committer: Package Import Robot
  • Author(s): Jeff Breidenbach
  • Date: 2014-02-03 11:10:20 UTC
  • mfrom: (1.3.1) (19.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140203111020-igquodd7pjlp3uri
Tags: 3.03.01-1
* New upstream release, includes critical fix to PDF rendering
* Complete leptonlib transition (see bug #735509)
* Promote from experimental to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 * File:        boxchar.cpp
 
3
 * Description: Simple class to associate a Tesseract classification unit with
 
4
 *              its bounding box so that the boxes can be rotated as the image
 
5
 *              is rotated for degradation.  Also includes routines to output
 
6
 *              the character-tagged boxes to a boxfile.
 
7
 * Author:      Ray Smith
 
8
 * Created:     Mon Nov 18 2013
 
9
 *
 
10
 * (C) Copyright 2013, Google Inc.
 
11
 * Licensed under the Apache License, Version 2.0 (the "License");
 
12
 * you may not use this file except in compliance with the License.
 
13
 * You may obtain a copy of the License at
 
14
 * http://www.apache.org/licenses/LICENSE-2.0
 
15
 * Unless required by applicable law or agreed to in writing, software
 
16
 * distributed under the License is distributed on an "AS IS" BASIS,
 
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
18
 * See the License for the specific language governing permissions and
 
19
 * limitations under the License.
 
20
 *
 
21
 **********************************************************************/
 
22
 
 
23
#include "boxchar.h"
 
24
 
 
25
#include <stddef.h>
 
26
 
 
27
#include "fileio.h"
 
28
 
 
29
namespace tesseract {
 
30
 
 
31
BoxChar::BoxChar(const char* utf8_str, int len) : ch_(utf8_str, len) {
 
32
  box_ = NULL;
 
33
}
 
34
 
 
35
BoxChar::~BoxChar() {
 
36
  boxDestroy(&box_);
 
37
}
 
38
 
 
39
void BoxChar::AddBox(int x, int y, int width, int height) {
 
40
  box_ = boxCreate(x, y, width, height);
 
41
}
 
42
 
 
43
/* static */
 
44
void BoxChar::TranslateBoxes(int xshift, int yshift,
 
45
                             vector<BoxChar*>* boxes) {
 
46
  for (int i = 0; i < boxes->size(); ++i) {
 
47
    BOX* box = (*boxes)[i]->box_;
 
48
    if (box != NULL) {
 
49
      box->x += xshift;
 
50
      box->y += yshift;
 
51
    }
 
52
  }
 
53
}
 
54
 
 
55
// Rotate the boxes in [start_box, end_box) by the given rotation.
 
56
// The rotation is in radians clockwise about the given center.
 
57
/* static */
 
58
void BoxChar::RotateBoxes(float rotation,
 
59
                          int xcenter,
 
60
                          int ycenter,
 
61
                          int start_box,
 
62
                          int end_box,
 
63
                          vector<BoxChar*>* boxes) {
 
64
  Boxa* orig = boxaCreate(0);
 
65
  for (int i = start_box; i < end_box; ++i) {
 
66
    BOX* box = (*boxes)[i]->box_;
 
67
    if (box) boxaAddBox(orig, box, L_CLONE);
 
68
  }
 
69
  Boxa* rotated = boxaRotate(orig, xcenter, ycenter, rotation);
 
70
  boxaDestroy(&orig);
 
71
  for (int i = start_box, box_ind = 0; i < end_box; ++i) {
 
72
    if ((*boxes)[i]->box_) {
 
73
      boxDestroy(&((*boxes)[i]->box_));
 
74
      (*boxes)[i]->box_ = boxaGetBox(rotated, box_ind++, L_CLONE);
 
75
    }
 
76
  }
 
77
  boxaDestroy(&rotated);
 
78
}
 
79
 
 
80
/* static */
 
81
void BoxChar::WriteTesseractBoxFile(const string& filename, int height,
 
82
                                    const vector<BoxChar*>& boxes) {
 
83
  string output;
 
84
  const int kMaxLineLength = 1024;
 
85
  char buffer[kMaxLineLength];
 
86
  for (int i = 0; i < boxes.size(); ++i) {
 
87
    if (boxes[i]->box_ != NULL) {
 
88
      int nbytes = snprintf(buffer, kMaxLineLength,
 
89
                            "%s %d %d %d %d %d\n",
 
90
                            boxes[i]->ch_.c_str(),
 
91
                            boxes[i]->box_->x,
 
92
                            height - boxes[i]->box_->y - boxes[i]->box_->h,
 
93
                            boxes[i]->box_->x + boxes[i]->box_->w,
 
94
                            height - boxes[i]->box_->y,
 
95
                            boxes[i]->page_);
 
96
      output.append(buffer, nbytes);
 
97
    }
 
98
  }
 
99
  File::WriteStringToFileOrDie(output, filename);
 
100
}
 
101
}  // namespace tesseract