tesseract  5.0.0-alpha-619-ge9db
tesseract::ImageData Class Reference

#include <imagedata.h>

Public Member Functions

 ImageData ()
 
 ImageData (bool vertical, Pix *pix)
 
 ~ImageData ()
 
bool Serialize (TFile *fp) const
 
bool DeSerialize (TFile *fp)
 
const STRINGimagefilename () const
 
void set_imagefilename (const STRING &name)
 
int page_number () const
 
void set_page_number (int num)
 
const GenericVector< char > & image_data () const
 
const STRINGlanguage () const
 
void set_language (const STRING &lang)
 
const STRINGtranscription () const
 
const GenericVector< TBOX > & boxes () const
 
const GenericVector< STRING > & box_texts () const
 
const STRINGbox_text (int index) const
 
void SetPix (Pix *pix)
 
Pix * GetPix () const
 
Pix * PreScale (int target_height, int max_height, float *scale_factor, int *scaled_width, int *scaled_height, GenericVector< TBOX > *boxes) const
 
int MemoryUsed () const
 
void Display () const
 
void AddBoxes (const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< 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 104 of file imagedata.h.

Constructor & Destructor Documentation

◆ ImageData() [1/2]

tesseract::ImageData::ImageData ( )

Definition at line 119 of file imagedata.cpp.

119  : page_number_(-1), vertical_text_(false) {
120 }

◆ ImageData() [2/2]

tesseract::ImageData::ImageData ( bool  vertical,
Pix *  pix 
)

Definition at line 122 of file imagedata.cpp.

123  : page_number_(0), vertical_text_(vertical) {
124  SetPix(pix);
125 }

◆ ~ImageData()

tesseract::ImageData::~ImageData ( )

Definition at line 126 of file imagedata.cpp.

126  {
127 }

Member Function Documentation

◆ AddBoxes()

void tesseract::ImageData::AddBoxes ( const GenericVector< TBOX > &  boxes,
const GenericVector< STRING > &  texts,
const GenericVector< int > &  box_pages 
)

Definition at line 311 of file imagedata.cpp.

313  {
314  // Copy the boxes and make the transcription.
315  for (int i = 0; i < box_pages.size(); ++i) {
316  if (page_number_ >= 0 && box_pages[i] != page_number_) continue;
317  transcription_ += texts[i];
318  boxes_.push_back(boxes[i]);
319  box_texts_.push_back(texts[i]);
320  }
321 }

◆ box_text()

const STRING& tesseract::ImageData::box_text ( int  index) const
inline

Definition at line 155 of file imagedata.h.

155  {
156  return box_texts_[index];
157  }

◆ box_texts()

const GenericVector<STRING>& tesseract::ImageData::box_texts ( ) const
inline

Definition at line 152 of file imagedata.h.

152  {
153  return box_texts_;
154  }

◆ boxes()

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

Definition at line 149 of file imagedata.h.

149  {
150  return boxes_;
151  }

◆ 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 131 of file imagedata.cpp.

133  {
134  auto* image_data = new ImageData();
135  image_data->imagefilename_ = name;
136  image_data->page_number_ = page_number;
137  image_data->language_ = lang;
138  // Save the imagedata.
139  image_data->image_data_.resize_no_init(imagedatasize);
140  memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
141  if (!image_data->AddBoxes(box_text)) {
142  if (truth_text == nullptr || truth_text[0] == '\0') {
143  tprintf("Error: No text corresponding to page %d from image %s!\n",
144  page_number, name);
145  delete image_data;
146  return nullptr;
147  }
148  image_data->transcription_ = truth_text;
149  // If we have no boxes, the transcription is in the 0th box_texts_.
150  image_data->box_texts_.push_back(truth_text);
151  // We will create a box for the whole image on PreScale, to save unpacking
152  // the image now.
153  } else if (truth_text != nullptr && truth_text[0] != '\0' &&
154  image_data->transcription_ != truth_text) {
155  // Save the truth text as it is present and disagrees with the box text.
156  image_data->transcription_ = truth_text;
157  }
158  return image_data;
159 }

◆ DeSerialize()

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

Definition at line 177 of file imagedata.cpp.

177  {
178  if (!imagefilename_.DeSerialize(fp)) return false;
179  if (!fp->DeSerialize(&page_number_)) return false;
180  if (!image_data_.DeSerialize(fp)) return false;
181  if (!language_.DeSerialize(fp)) return false;
182  if (!transcription_.DeSerialize(fp)) return false;
183  // WARNING: Will not work across different endian machines.
184  if (!boxes_.DeSerialize(fp)) return false;
185  if (!box_texts_.DeSerializeClasses(fp)) return false;
186  int8_t vertical = 0;
187  if (!fp->DeSerialize(&vertical)) return false;
188  vertical_text_ = vertical != 0;
189  return true;
190 }

◆ Display()

void tesseract::ImageData::Display ( ) const

Definition at line 273 of file imagedata.cpp.

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

◆ GetPix()

Pix * tesseract::ImageData::GetPix ( ) const

Definition at line 214 of file imagedata.cpp.

214  {
215  return GetPixInternal(image_data_);
216 }

◆ image_data()

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

Definition at line 137 of file imagedata.h.

137  {
138  return image_data_;
139  }

◆ imagefilename()

const STRING& tesseract::ImageData::imagefilename ( ) const
inline

Definition at line 125 of file imagedata.h.

125  {
126  return imagefilename_;
127  }

◆ language()

const STRING& tesseract::ImageData::language ( ) const
inline

Definition at line 140 of file imagedata.h.

140  {
141  return language_;
142  }

◆ 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 131 of file imagedata.h.

131  {
132  return page_number_;
133  }

◆ PreScale()

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

Definition at line 224 of file imagedata.cpp.

226  {
227  int input_width = 0;
228  int input_height = 0;
229  Pix* src_pix = GetPix();
230  ASSERT_HOST(src_pix != nullptr);
231  input_width = pixGetWidth(src_pix);
232  input_height = pixGetHeight(src_pix);
233  if (target_height == 0) {
234  target_height = std::min(input_height, max_height);
235  }
236  float im_factor = static_cast<float>(target_height) / input_height;
237  if (scaled_width != nullptr)
238  *scaled_width = IntCastRounded(im_factor * input_width);
239  if (scaled_height != nullptr)
240  *scaled_height = target_height;
241  // Get the scaled image.
242  Pix* pix = pixScale(src_pix, im_factor, im_factor);
243  if (pix == nullptr) {
244  tprintf("Scaling pix of size %d, %d by factor %g made null pix!!\n",
245  input_width, input_height, im_factor);
246  }
247  if (scaled_width != nullptr) *scaled_width = pixGetWidth(pix);
248  if (scaled_height != nullptr) *scaled_height = pixGetHeight(pix);
249  pixDestroy(&src_pix);
250  if (boxes != nullptr) {
251  // Get the boxes.
252  boxes->truncate(0);
253  for (int b = 0; b < boxes_.size(); ++b) {
254  TBOX box = boxes_[b];
255  box.scale(im_factor);
256  boxes->push_back(box);
257  }
258  if (boxes->empty()) {
259  // Make a single box for the whole image.
260  TBOX box(0, 0, im_factor * input_width, target_height);
261  boxes->push_back(box);
262  }
263  }
264  if (scale_factor != nullptr) *scale_factor = im_factor;
265  return pix;
266 }

◆ Serialize()

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

Definition at line 162 of file imagedata.cpp.

162  {
163  if (!imagefilename_.Serialize(fp)) return false;
164  if (!fp->Serialize(&page_number_)) return false;
165  if (!image_data_.Serialize(fp)) return false;
166  if (!language_.Serialize(fp)) return false;
167  if (!transcription_.Serialize(fp)) return false;
168  // WARNING: Will not work across different endian machines.
169  if (!boxes_.Serialize(fp)) return false;
170  if (!box_texts_.SerializeClasses(fp)) return false;
171  int8_t vertical = vertical_text_;
172  return fp->Serialize(&vertical);
173 }

◆ set_imagefilename()

void tesseract::ImageData::set_imagefilename ( const STRING name)
inline

Definition at line 128 of file imagedata.h.

128  {
129  imagefilename_ = name;
130  }

◆ set_language()

void tesseract::ImageData::set_language ( const STRING lang)
inline

Definition at line 143 of file imagedata.h.

143  {
144  language_ = lang;
145  }

◆ set_page_number()

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

Definition at line 134 of file imagedata.h.

134  {
135  page_number_ = num;
136  }

◆ SetPix()

void tesseract::ImageData::SetPix ( Pix *  pix)

Definition at line 209 of file imagedata.cpp.

209  {
210  SetPixInternal(pix, &image_data_);
211 }

◆ SkipDeSerialize()

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

Definition at line 193 of file imagedata.cpp.

193  {
194  if (!STRING::SkipDeSerialize(fp)) return false;
195  int32_t page_number;
196  if (!fp->DeSerialize(&page_number)) return false;
197  if (!GenericVector<char>::SkipDeSerialize(fp)) return false;
198  if (!STRING::SkipDeSerialize(fp)) return false;
199  if (!STRING::SkipDeSerialize(fp)) return false;
200  if (!GenericVector<TBOX>::SkipDeSerialize(fp)) return false;
201  if (!GenericVector<STRING>::SkipDeSerializeClasses(fp)) return false;
202  int8_t vertical = 0;
203  return fp->DeSerialize(&vertical);
204 }

◆ transcription()

const STRING& tesseract::ImageData::transcription ( ) const
inline

Definition at line 146 of file imagedata.h.

146  {
147  return transcription_;
148  }

The documentation for this class was generated from the following files:
ScrollView
Definition: scrollview.h:97
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
ScrollView::CYAN
Definition: scrollview.h:107
ScrollView::NONE
Definition: scrollview.h:101
GenericVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:929
IntCastRounded
int IntCastRounded(double x)
Definition: helpers.h:173
STRING::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:157
STRING::Serialize
bool Serialize(FILE *fp) const
Definition: strngs.cpp:144
GenericVector::DeSerializeClasses
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: genericvector.h:1038
tesseract::ImageData::boxes
const GenericVector< TBOX > & boxes() const
Definition: imagedata.h:149
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
GenericVector::SerializeClasses
bool SerializeClasses(FILE *fp) const
Definition: genericvector.h:1008
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
GenericVector::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: genericvector.h:954
tesseract::ImageData::box_text
const STRING & box_text(int index) const
Definition: imagedata.h:155
GenericVector::resize_no_init
void resize_no_init(int size)
Definition: genericvector.h:65
GenericVector::empty
bool empty() const
Definition: genericvector.h:86
tesseract::ImageData::image_data
const GenericVector< char > & image_data() const
Definition: imagedata.h:137
tesseract::ImageData::page_number
int page_number() const
Definition: imagedata.h:131
tesseract::ImageData::ImageData
ImageData()
Definition: imagedata.cpp:119
ScrollView::RED
Definition: scrollview.h:104
GenericVector
Definition: baseapi.h:40
STRING::SkipDeSerialize
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: strngs.cpp:177
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
GenericVector::size
int size() const
Definition: genericvector.h:71
window_wait
char window_wait(ScrollView *win)
Definition: callcpp.cpp:103
tesseract::ImageData::GetPix
Pix * GetPix() const
Definition: imagedata.cpp:214
TBOX::scale
void scale(const float f)
Definition: rect.h:174
tesseract::ImageData::SetPix
void SetPix(Pix *pix)
Definition: imagedata.cpp:209
TBOX
Definition: rect.h:33