tesseract  4.1.1
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 105 of file imagedata.h.

Constructor & Destructor Documentation

◆ ImageData() [1/2]

tesseract::ImageData::ImageData ( )

Definition at line 123 of file imagedata.cpp.

123  : page_number_(-1), vertical_text_(false) {
124 }

◆ ImageData() [2/2]

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

Definition at line 126 of file imagedata.cpp.

127  : page_number_(0), vertical_text_(vertical) {
128  SetPix(pix);
129 }
void SetPix(Pix *pix)
Definition: imagedata.cpp:213

◆ ~ImageData()

tesseract::ImageData::~ImageData ( )

Definition at line 130 of file imagedata.cpp.

130  {
131 }

Member Function Documentation

◆ AddBoxes()

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

Definition at line 315 of file imagedata.cpp.

317  {
318  // Copy the boxes and make the transcription.
319  for (int i = 0; i < box_pages.size(); ++i) {
320  if (page_number_ >= 0 && box_pages[i] != page_number_) continue;
321  transcription_ += texts[i];
322  boxes_.push_back(boxes[i]);
323  box_texts_.push_back(texts[i]);
324  }
325 }
int push_back(T object)
int size() const
Definition: genericvector.h:72
const GenericVector< TBOX > & boxes() const
Definition: imagedata.h:150

◆ box_text()

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

Definition at line 156 of file imagedata.h.

156  {
157  return box_texts_[index];
158  }

◆ box_texts()

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

Definition at line 153 of file imagedata.h.

153  {
154  return box_texts_;
155  }

◆ boxes()

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

Definition at line 150 of file imagedata.h.

150  {
151  return boxes_;
152  }

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

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

◆ DeSerialize()

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

Definition at line 181 of file imagedata.cpp.

181  {
182  if (!imagefilename_.DeSerialize(fp)) return false;
183  if (!fp->DeSerialize(&page_number_)) return false;
184  if (!image_data_.DeSerialize(fp)) return false;
185  if (!language_.DeSerialize(fp)) return false;
186  if (!transcription_.DeSerialize(fp)) return false;
187  // WARNING: Will not work across different endian machines.
188  if (!boxes_.DeSerialize(fp)) return false;
189  if (!box_texts_.DeSerializeClasses(fp)) return false;
190  int8_t vertical = 0;
191  if (!fp->DeSerialize(&vertical)) return false;
192  vertical_text_ = vertical != 0;
193  return true;
194 }
bool DeSerializeClasses(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:159

◆ Display()

void tesseract::ImageData::Display ( ) const

Definition at line 277 of file imagedata.cpp.

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

◆ GetPix()

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

Definition at line 218 of file imagedata.cpp.

218  {
219  return GetPixInternal(image_data_);
220 }

◆ image_data()

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

Definition at line 138 of file imagedata.h.

138  {
139  return image_data_;
140  }

◆ imagefilename()

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

Definition at line 126 of file imagedata.h.

126  {
127  return imagefilename_;
128  }

◆ language()

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

Definition at line 141 of file imagedata.h.

141  {
142  return language_;
143  }

◆ MemoryUsed()

int tesseract::ImageData::MemoryUsed ( ) const

Definition at line 272 of file imagedata.cpp.

272  {
273  return image_data_.size();
274 }

◆ page_number()

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

Definition at line 132 of file imagedata.h.

132  {
133  return page_number_;
134  }

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

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

◆ Serialize()

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

Definition at line 166 of file imagedata.cpp.

166  {
167  if (!imagefilename_.Serialize(fp)) return false;
168  if (!fp->Serialize(&page_number_)) return false;
169  if (!image_data_.Serialize(fp)) return false;
170  if (!language_.Serialize(fp)) return false;
171  if (!transcription_.Serialize(fp)) return false;
172  // WARNING: Will not work across different endian machines.
173  if (!boxes_.Serialize(fp)) return false;
174  if (!box_texts_.SerializeClasses(fp)) return false;
175  int8_t vertical = vertical_text_;
176  return fp->Serialize(&vertical);
177 }
bool Serialize(FILE *fp) const
bool SerializeClasses(FILE *fp) const
bool Serialize(FILE *fp) const
Definition: strngs.cpp:146

◆ set_imagefilename()

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

Definition at line 129 of file imagedata.h.

129  {
130  imagefilename_ = name;
131  }

◆ set_language()

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

Definition at line 144 of file imagedata.h.

144  {
145  language_ = lang;
146  }

◆ set_page_number()

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

Definition at line 135 of file imagedata.h.

135  {
136  page_number_ = num;
137  }

◆ SetPix()

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

Definition at line 213 of file imagedata.cpp.

213  {
214  SetPixInternal(pix, &image_data_);
215 }

◆ SkipDeSerialize()

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

Definition at line 197 of file imagedata.cpp.

197  {
198  if (!STRING::SkipDeSerialize(fp)) return false;
199  int32_t page_number;
200  if (!fp->DeSerialize(&page_number)) return false;
201  if (!GenericVector<char>::SkipDeSerialize(fp)) return false;
202  if (!STRING::SkipDeSerialize(fp)) return false;
203  if (!STRING::SkipDeSerialize(fp)) return false;
204  if (!GenericVector<TBOX>::SkipDeSerialize(fp)) return false;
205  if (!GenericVector<STRING>::SkipDeSerializeClasses(fp)) return false;
206  int8_t vertical = 0;
207  return fp->DeSerialize(&vertical);
208 }
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: strngs.cpp:179

◆ transcription()

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

Definition at line 147 of file imagedata.h.

147  {
148  return transcription_;
149  }

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