tesseract  5.0.0
tesseract::ImageData Class Reference

#include <imagedata.h>

Public Member Functions

 ImageData ()
 
 ImageData (bool vertical, Image pix)
 
 ~ImageData ()
 
bool Serialize (TFile *fp) const
 
bool DeSerialize (TFile *fp)
 
const std::string & imagefilename () const
 
void set_imagefilename (const std::string &name)
 
int page_number () const
 
void set_page_number (int num)
 
const std::vector< char > & image_data () const
 
const std::string & language () const
 
void set_language (const std::string &lang)
 
const std::string & transcription () const
 
const std::vector< TBOX > & boxes () const
 
const std::vector< std::string > & box_texts () const
 
const std::string & box_text (int index) const
 
void SetPix (Image pix)
 
Image GetPix () const
 
Image PreScale (int target_height, int max_height, float *scale_factor, int *scaled_width, int *scaled_height, std::vector< TBOX > *boxes) const
 
int MemoryUsed () const
 
void Display () const
 
void AddBoxes (const std::vector< TBOX > &boxes, const std::vector< std::string > &texts, const std::vector< int > &box_pages)
 

Static Public Member Functions

static ImageDataBuild (const char *name, int page_number, const char *lang, const char *imagedata, int imagedatasize, const char *truth_text, const char *box_text)
 
static bool SkipDeSerialize (TFile *fp)
 

Detailed Description

Definition at line 62 of file imagedata.h.

Constructor & Destructor Documentation

◆ ImageData() [1/2]

tesseract::ImageData::ImageData ( )

Definition at line 44 of file imagedata.cpp.

44 : page_number_(-1), vertical_text_(false) {}

◆ ImageData() [2/2]

tesseract::ImageData::ImageData ( bool  vertical,
Image  pix 
)

Definition at line 46 of file imagedata.cpp.

47  : page_number_(0), vertical_text_(vertical) {
48  SetPix(pix);
49 }
void SetPix(Image pix)
Definition: imagedata.cpp:184

◆ ~ImageData()

tesseract::ImageData::~ImageData ( )

Definition at line 50 of file imagedata.cpp.

50  {
51 #ifdef TESSERACT_IMAGEDATA_AS_PIX
52  internal_pix_.destroy();
53 #endif
54 }

Member Function Documentation

◆ AddBoxes()

void tesseract::ImageData::AddBoxes ( const std::vector< TBOX > &  boxes,
const std::vector< std::string > &  texts,
const std::vector< int > &  box_pages 
)

Definition at line 315 of file imagedata.cpp.

317  {
318  // Copy the boxes and make the transcription.
319  for (unsigned i = 0; i < box_pages.size(); ++i) {
320  if (page_number_ >= 0 && box_pages[i] != page_number_) {
321  continue;
322  }
323  transcription_ += texts[i];
324  boxes_.push_back(boxes[i]);
325  box_texts_.push_back(texts[i]);
326  }
327 }
const std::vector< TBOX > & boxes() const
Definition: imagedata.h:107

◆ box_text()

const std::string& tesseract::ImageData::box_text ( int  index) const
inline

Definition at line 113 of file imagedata.h.

113  {
114  return box_texts_[index];
115  }

◆ box_texts()

const std::vector<std::string>& tesseract::ImageData::box_texts ( ) const
inline

Definition at line 110 of file imagedata.h.

110  {
111  return box_texts_;
112  }

◆ boxes()

const std::vector<TBOX>& tesseract::ImageData::boxes ( ) const
inline

Definition at line 107 of file imagedata.h.

107  {
108  return boxes_;
109  }

◆ Build()

ImageData * tesseract::ImageData::Build ( const char *  name,
int  page_number,
const char *  lang,
const char *  imagedata,
int  imagedatasize,
const char *  truth_text,
const char *  box_text 
)
static

Definition at line 58 of file imagedata.cpp.

60  {
61  auto *image_data = new ImageData();
62  image_data->imagefilename_ = name;
63  image_data->page_number_ = page_number;
64  image_data->language_ = lang;
65  // Save the imagedata.
66  // TODO: optimize resize (no init).
67  image_data->image_data_.resize(imagedatasize);
68  memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
69  if (!image_data->AddBoxes(box_text)) {
70  if (truth_text == nullptr || truth_text[0] == '\0') {
71  tprintf("Error: No text corresponding to page %d from image %s!\n",
72  page_number, name);
73  delete image_data;
74  return nullptr;
75  }
76  image_data->transcription_ = truth_text;
77  // If we have no boxes, the transcription is in the 0th box_texts_.
78  image_data->box_texts_.emplace_back(truth_text);
79  // We will create a box for the whole image on PreScale, to save unpacking
80  // the image now.
81  } else if (truth_text != nullptr && truth_text[0] != '\0' &&
82  image_data->transcription_ != truth_text) {
83  // Save the truth text as it is present and disagrees with the box text.
84  image_data->transcription_ = truth_text;
85  }
86  return image_data;
87 }
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int page_number() const
Definition: imagedata.h:89
const std::vector< char > & image_data() const
Definition: imagedata.h:95
const std::string & box_text(int index) const
Definition: imagedata.h:113

◆ DeSerialize()

bool tesseract::ImageData::DeSerialize ( TFile fp)

Definition at line 117 of file imagedata.cpp.

117  {
118  if (!fp->DeSerialize(imagefilename_)) {
119  return false;
120  }
121  if (!fp->DeSerialize(&page_number_)) {
122  return false;
123  }
124  if (!fp->DeSerialize(image_data_)) {
125  return false;
126  }
127  if (!fp->DeSerialize(language_)) {
128  return false;
129  }
130  if (!fp->DeSerialize(transcription_)) {
131  return false;
132  }
133  if (!fp->DeSerialize(boxes_)) {
134  return false;
135  }
136  if (!fp->DeSerialize(box_texts_)) {
137  return false;
138  }
139  int8_t vertical = 0;
140  if (!fp->DeSerialize(&vertical)) {
141  return false;
142  }
143  vertical_text_ = vertical != 0;
144  return true;
145 }

◆ Display()

void tesseract::ImageData::Display ( ) const

Definition at line 275 of file imagedata.cpp.

275  {
276  const int kTextSize = 64;
277  // Draw the image.
278  Image pix = GetPix();
279  if (pix == nullptr) {
280  return;
281  }
282  int width = pixGetWidth(pix);
283  int height = pixGetHeight(pix);
284  auto *win = new ScrollView("Imagedata", 100, 100, 2 * (width + 2 * kTextSize),
285  2 * (height + 4 * kTextSize), width + 10,
286  height + 3 * kTextSize, true);
287  win->Draw(pix, 0, height - 1);
288  pix.destroy();
289  // Draw the boxes.
290  win->Pen(ScrollView::RED);
291  win->Brush(ScrollView::NONE);
292  int text_size = kTextSize;
293  if (!boxes_.empty() && boxes_[0].height() * 2 < text_size) {
294  text_size = boxes_[0].height() * 2;
295  }
296  win->TextAttributes("Arial", text_size, false, false, false);
297  if (!boxes_.empty()) {
298  for (unsigned b = 0; b < boxes_.size(); ++b) {
299  boxes_[b].plot(win);
300  win->Text(boxes_[b].left(), height + kTextSize, box_texts_[b].c_str());
301  }
302  } else {
303  // The full transcription.
304  win->Pen(ScrollView::CYAN);
305  win->Text(0, height + kTextSize * 2, transcription_.c_str());
306  }
307  win->Update();
308  win->Wait();
309 }
Image GetPix() const
Definition: imagedata.cpp:193

◆ GetPix()

Image tesseract::ImageData::GetPix ( ) const

Definition at line 193 of file imagedata.cpp.

193  {
194 #ifdef TESSERACT_IMAGEDATA_AS_PIX
195 # ifdef GRAPHICS_DISABLED
196  /* The only caller of this is the scaling functions to prescale the
197  * source. Thus we can just return a new pointer to the same data. */
198  return internal_pix_.clone();
199 # else
200  /* pixCopy always does an actual copy, so the caller can modify the
201  * changed data. */
202  return internal_pix_.copy();
203 # endif
204 #else
205  return GetPixInternal(image_data_);
206 #endif
207 }

◆ image_data()

const std::vector<char>& tesseract::ImageData::image_data ( ) const
inline

Definition at line 95 of file imagedata.h.

95  {
96  return image_data_;
97  }

◆ imagefilename()

const std::string& tesseract::ImageData::imagefilename ( ) const
inline

Definition at line 83 of file imagedata.h.

83  {
84  return imagefilename_;
85  }

◆ language()

const std::string& tesseract::ImageData::language ( ) const
inline

Definition at line 98 of file imagedata.h.

98  {
99  return language_;
100  }

◆ MemoryUsed()

int tesseract::ImageData::MemoryUsed ( ) const

Definition at line 268 of file imagedata.cpp.

268  {
269  return image_data_.size();
270 }

◆ page_number()

int tesseract::ImageData::page_number ( ) const
inline

Definition at line 89 of file imagedata.h.

89  {
90  return page_number_;
91  }

◆ PreScale()

Image tesseract::ImageData::PreScale ( int  target_height,
int  max_height,
float *  scale_factor,
int *  scaled_width,
int *  scaled_height,
std::vector< TBOX > *  boxes 
) const

Definition at line 215 of file imagedata.cpp.

217  {
218  int input_width = 0;
219  int input_height = 0;
220  Image src_pix = GetPix();
221  ASSERT_HOST(src_pix != nullptr);
222  input_width = pixGetWidth(src_pix);
223  input_height = pixGetHeight(src_pix);
224  if (target_height == 0) {
225  target_height = std::min(input_height, max_height);
226  }
227  float im_factor = static_cast<float>(target_height) / input_height;
228  if (scaled_width != nullptr) {
229  *scaled_width = IntCastRounded(im_factor * input_width);
230  }
231  if (scaled_height != nullptr) {
232  *scaled_height = target_height;
233  }
234  // Get the scaled image.
235  Image pix = pixScale(src_pix, im_factor, im_factor);
236  if (pix == nullptr) {
237  tprintf("Scaling pix of size %d, %d by factor %g made null pix!!\n",
238  input_width, input_height, im_factor);
239  src_pix.destroy();
240  return nullptr;
241  }
242  if (scaled_width != nullptr) {
243  *scaled_width = pixGetWidth(pix);
244  }
245  if (scaled_height != nullptr) {
246  *scaled_height = pixGetHeight(pix);
247  }
248  src_pix.destroy();
249  if (boxes != nullptr) {
250  // Get the boxes.
251  boxes->clear();
252  for (auto box : boxes_) {
253  box.scale(im_factor);
254  boxes->push_back(box);
255  }
256  if (boxes->empty()) {
257  // Make a single box for the whole image.
258  TBOX box(0, 0, im_factor * input_width, target_height);
259  boxes->push_back(box);
260  }
261  }
262  if (scale_factor != nullptr) {
263  *scale_factor = im_factor;
264  }
265  return pix;
266 }
#define ASSERT_HOST(x)
Definition: errcode.h:59
@ TBOX
int IntCastRounded(double x)
Definition: helpers.h:175

◆ Serialize()

bool tesseract::ImageData::Serialize ( TFile fp) const

Definition at line 90 of file imagedata.cpp.

90  {
91  if (!fp->Serialize(imagefilename_)) {
92  return false;
93  }
94  if (!fp->Serialize(&page_number_)) {
95  return false;
96  }
97  if (!fp->Serialize(image_data_)) {
98  return false;
99  }
100  if (!fp->Serialize(language_)) {
101  return false;
102  }
103  if (!fp->Serialize(transcription_)) {
104  return false;
105  }
106  if (!fp->Serialize(boxes_)) {
107  return false;
108  }
109  if (!fp->Serialize(box_texts_)) {
110  return false;
111  }
112  int8_t vertical = vertical_text_;
113  return fp->Serialize(&vertical);
114 }

◆ set_imagefilename()

void tesseract::ImageData::set_imagefilename ( const std::string &  name)
inline

Definition at line 86 of file imagedata.h.

86  {
87  imagefilename_ = name;
88  }

◆ set_language()

void tesseract::ImageData::set_language ( const std::string &  lang)
inline

Definition at line 101 of file imagedata.h.

101  {
102  language_ = lang;
103  }

◆ set_page_number()

void tesseract::ImageData::set_page_number ( int  num)
inline

Definition at line 92 of file imagedata.h.

92  {
93  page_number_ = num;
94  }

◆ SetPix()

void tesseract::ImageData::SetPix ( Image  pix)

Definition at line 184 of file imagedata.cpp.

184  {
185 #ifdef TESSERACT_IMAGEDATA_AS_PIX
186  internal_pix_ = pix;
187 #else
188  SetPixInternal(pix, &image_data_);
189 #endif
190 }

◆ SkipDeSerialize()

bool tesseract::ImageData::SkipDeSerialize ( TFile fp)
static

Definition at line 148 of file imagedata.cpp.

148  {
149  if (!fp->DeSerializeSkip()) {
150  return false;
151  }
152  int32_t page_number;
153  if (!fp->DeSerialize(&page_number)) {
154  return false;
155  }
156  if (!fp->DeSerializeSkip()) {
157  return false;
158  }
159  if (!fp->DeSerializeSkip()) {
160  return false;
161  }
162  if (!fp->DeSerializeSkip()) {
163  return false;
164  }
165  if (!fp->DeSerializeSkip(sizeof(TBOX))) {
166  return false;
167  }
168  int32_t number;
169  if (!fp->DeSerialize(&number)) {
170  return false;
171  }
172  for (int i = 0; i < number; i++) {
173  if (!fp->DeSerializeSkip()) {
174  return false;
175  }
176  }
177  int8_t vertical = 0;
178  return fp->DeSerialize(&vertical);
179 }

◆ transcription()

const std::string& tesseract::ImageData::transcription ( ) const
inline

Definition at line 104 of file imagedata.h.

104  {
105  return transcription_;
106  }

The documentation for this class was generated from the following files: