tesseract  3.05.02
tesseract::ImageData Class Reference

#include <imagedata.h>

Public Member Functions

 ImageData ()
 
 ImageData (bool vertical, Pix *pix)
 
 ~ImageData ()
 
bool Serialize (TFile *fp) const
 
bool DeSerialize (bool swap, 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 (bool swap, tesseract::TFile *fp)
 

Detailed Description

Definition at line 103 of file imagedata.h.

Constructor & Destructor Documentation

◆ ImageData() [1/2]

tesseract::ImageData::ImageData ( )

Definition at line 122 of file imagedata.cpp.

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

◆ ImageData() [2/2]

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

Definition at line 125 of file imagedata.cpp.

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

◆ ~ImageData()

tesseract::ImageData::~ImageData ( )

Definition at line 129 of file imagedata.cpp.

129  {
130 }

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 }
const GenericVector< TBOX > & boxes() const
Definition: imagedata.h:149
int push_back(T object)
int size() const
Definition: genericvector.h:72

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

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

◆ DeSerialize()

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

Definition at line 180 of file imagedata.cpp.

180  {
181  if (!imagefilename_.DeSerialize(swap, fp)) return false;
182  if (fp->FRead(&page_number_, sizeof(page_number_), 1) != 1) return false;
183  if (swap) ReverseN(&page_number_, sizeof(page_number_));
184  if (!image_data_.DeSerialize(swap, fp)) return false;
185  if (!transcription_.DeSerialize(swap, fp)) return false;
186  // WARNING: Will not work across different endian machines.
187  if (!boxes_.DeSerialize(swap, fp)) return false;
188  if (!box_texts_.DeSerializeClasses(swap, fp)) return false;
189  inT8 vertical = 0;
190  if (fp->FRead(&vertical, sizeof(vertical), 1) != 1) return false;
191  vertical_text_ = vertical != 0;
192  return true;
193 }
bool DeSerializeClasses(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:163
SIGNED char inT8
Definition: host.h:31
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
bool DeSerialize(bool swap, FILE *fp)

◆ 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 == NULL) return;
279  int width = pixGetWidth(pix);
280  int height = pixGetHeight(pix);
281  ScrollView* 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].string());
298  }
299  } else {
300  // The full transcription.
301  win->Pen(ScrollView::CYAN);
302  win->Text(0, height + kTextSize * 2, transcription_.string());
303  }
304  win->Update();
305  window_wait(win);
306 #endif
307 }
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
Pix * GetPix() const
Definition: imagedata.cpp:214
static void Update()
Definition: scrollview.cpp:715
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:641
void Brush(Color color)
Definition: scrollview.cpp:732
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
const char * string() const
Definition: strngs.cpp:201
void Pen(Color color)
Definition: scrollview.cpp:726
int size() const
Definition: genericvector.h:72
char window_wait(ScrollView *win)
Definition: callcpp.cpp:111
bool empty() const
Definition: genericvector.h:84

◆ 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 }
int size() const
Definition: genericvector.h:72

◆ 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 != NULL);
231  input_width = pixGetWidth(src_pix);
232  input_height = pixGetHeight(src_pix);
233  if (target_height == 0) {
234  target_height = MIN(input_height, max_height);
235  }
236  float im_factor = static_cast<float>(target_height) / input_height;
237  if (scaled_width != NULL)
238  *scaled_width = IntCastRounded(im_factor * input_width);
239  if (scaled_height != NULL)
240  *scaled_height = target_height;
241  // Get the scaled image.
242  Pix* pix = pixScale(src_pix, im_factor, im_factor);
243  if (pix == NULL) {
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 != NULL) *scaled_width = pixGetWidth(pix);
248  if (scaled_height != NULL) *scaled_height = pixGetHeight(pix);
249  pixDestroy(&src_pix);
250  if (boxes != NULL) {
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 != NULL) *scale_factor = im_factor;
265  return pix;
266 }
Pix * GetPix() const
Definition: imagedata.cpp:214
void scale(const float f)
Definition: rect.h:171
int IntCastRounded(double x)
Definition: helpers.h:172
const GenericVector< TBOX > & boxes() const
Definition: imagedata.h:149
#define MIN(x, y)
Definition: ndminx.h:28
int push_back(T object)
void truncate(int size)
#define tprintf(...)
Definition: tprintf.h:31
int size() const
Definition: genericvector.h:72
Definition: rect.h:30
bool empty() const
Definition: genericvector.h:84
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Serialize()

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

Definition at line 165 of file imagedata.cpp.

165  {
166  if (!imagefilename_.Serialize(fp)) return false;
167  if (fp->FWrite(&page_number_, sizeof(page_number_), 1) != 1) return false;
168  if (!image_data_.Serialize(fp)) return false;
169  if (!transcription_.Serialize(fp)) return false;
170  // WARNING: Will not work across different endian machines.
171  if (!boxes_.Serialize(fp)) return false;
172  if (!box_texts_.SerializeClasses(fp)) return false;
173  inT8 vertical = vertical_text_;
174  if (fp->FWrite(&vertical, sizeof(vertical), 1) != 1) return false;
175  return true;
176 }
bool Serialize(FILE *fp) const
SIGNED char inT8
Definition: host.h:31
bool Serialize(FILE *fp) const
Definition: strngs.cpp:148
bool SerializeClasses(FILE *fp) const

◆ 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 ( bool  swap,
tesseract::TFile fp 
)
static

Definition at line 196 of file imagedata.cpp.

196  {
197  if (!STRING::SkipDeSerialize(swap, fp)) return false;
199  if (fp->FRead(&page_number, sizeof(page_number), 1) != 1) return false;
200  if (!GenericVector<char>::SkipDeSerialize(swap, fp)) return false;
201  if (!STRING::SkipDeSerialize(swap, fp)) return false;
202  if (!GenericVector<TBOX>::SkipDeSerialize(swap, fp)) return false;
203  if (!GenericVector<STRING>::SkipDeSerializeClasses(swap, fp)) return false;
204  inT8 vertical = 0;
205  return fp->FRead(&vertical, sizeof(vertical), 1) == 1;
206 }
static bool SkipDeSerialize(bool swap, tesseract::TFile *fp)
Definition: strngs.cpp:185
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
SIGNED char inT8
Definition: host.h:31
int page_number() const
Definition: imagedata.h:131
int inT32
Definition: host.h:35

◆ 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: