All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
 
float PreScale (int target_height, Pix **pix, 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)
 

Detailed Description

Definition at line 88 of file imagedata.h.

Constructor & Destructor Documentation

tesseract::ImageData::ImageData ( )

Definition at line 108 of file imagedata.cpp.

108  : page_number_(-1), vertical_text_(false) {
109 }
tesseract::ImageData::ImageData ( bool  vertical,
Pix *  pix 
)

Definition at line 111 of file imagedata.cpp.

112  : page_number_(0), vertical_text_(vertical) {
113  SetPix(pix);
114 }
void SetPix(Pix *pix)
Definition: imagedata.cpp:182
tesseract::ImageData::~ImageData ( )

Definition at line 115 of file imagedata.cpp.

115  {
116 }

Member Function Documentation

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

Definition at line 285 of file imagedata.cpp.

287  {
288  // Copy the boxes and make the transcription.
289  for (int i = 0; i < box_pages.size(); ++i) {
290  if (page_number_ >= 0 && box_pages[i] != page_number_) continue;
291  transcription_ += texts[i];
292  boxes_.push_back(boxes[i]);
293  box_texts_.push_back(texts[i]);
294  }
295 }
int size() const
Definition: genericvector.h:72
int push_back(T object)
const STRING& tesseract::ImageData::box_text ( int  index) const
inline

Definition at line 138 of file imagedata.h.

138  {
139  return box_texts_[index];
140  }
const GenericVector<STRING>& tesseract::ImageData::box_texts ( ) const
inline

Definition at line 135 of file imagedata.h.

135  {
136  return box_texts_;
137  }
const GenericVector<TBOX>& tesseract::ImageData::boxes ( ) const
inline

Definition at line 132 of file imagedata.h.

132  {
133  return boxes_;
134  }
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 120 of file imagedata.cpp.

122  {
123  ImageData* image_data = new ImageData();
124  image_data->imagefilename_ = name;
125  image_data->page_number_ = page_number;
126  image_data->language_ = lang;
127  // Save the imagedata.
128  image_data->image_data_.init_to_size(imagedatasize, 0);
129  memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
130  if (!image_data->AddBoxes(box_text)) {
131  if (truth_text == NULL || truth_text[0] == '\0') {
132  tprintf("Error: No text corresponding to page %d from image %s!\n",
133  page_number, name);
134  delete image_data;
135  return NULL;
136  }
137  image_data->transcription_ = truth_text;
138  // If we have no boxes, the transcription is in the 0th box_texts_.
139  image_data->box_texts_.push_back(truth_text);
140  // We will create a box for the whole image on PreScale, to save unpacking
141  // the image now.
142  } else if (truth_text != NULL && truth_text[0] != '\0' &&
143  image_data->transcription_ != truth_text) {
144  // Save the truth text as it is present and disagrees with the box text.
145  image_data->transcription_ = truth_text;
146  }
147  return image_data;
148 }
#define tprintf(...)
Definition: tprintf.h:31
name_table name
int page_number() const
Definition: imagedata.h:114
#define NULL
Definition: host.h:144
const GenericVector< char > & image_data() const
Definition: imagedata.h:120
const STRING & box_text(int index) const
Definition: imagedata.h:138
bool tesseract::ImageData::DeSerialize ( bool  swap,
TFile fp 
)

Definition at line 166 of file imagedata.cpp.

166  {
167  if (!imagefilename_.DeSerialize(swap, fp)) return false;
168  if (fp->FRead(&page_number_, sizeof(page_number_), 1) != 1) return false;
169  if (swap) ReverseN(&page_number_, sizeof(page_number_));
170  if (!image_data_.DeSerialize(swap, fp)) return false;
171  if (!transcription_.DeSerialize(swap, fp)) return false;
172  // WARNING: Will not work across different endian machines.
173  if (!boxes_.DeSerialize(swap, fp)) return false;
174  if (!box_texts_.DeSerializeClasses(swap, fp)) return false;
175  inT8 vertical = 0;
176  if (fp->FRead(&vertical, sizeof(vertical), 1) != 1) return false;
177  vertical_text_ = vertical != 0;
178  return true;
179 }
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:163
bool DeSerializeClasses(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
SIGNED char inT8
Definition: host.h:98
void tesseract::ImageData::Display ( ) const

Definition at line 248 of file imagedata.cpp.

248  {
249 #ifndef GRAPHICS_DISABLED
250  const int kTextSize = 64;
251  // Draw the image.
252  Pix* pix = GetPix();
253  if (pix == NULL) return;
254  int width = pixGetWidth(pix);
255  int height = pixGetHeight(pix);
256  ScrollView* win = new ScrollView("Imagedata", 100, 100,
257  2 * (width + 2 * kTextSize),
258  2 * (height + 4 * kTextSize),
259  width + 10, height + 3 * kTextSize, true);
260  win->Image(pix, 0, height - 1);
261  pixDestroy(&pix);
262  // Draw the boxes.
263  win->Pen(ScrollView::RED);
264  win->Brush(ScrollView::NONE);
265  win->TextAttributes("Arial", kTextSize, false, false, false);
266  for (int b = 0; b < boxes_.size(); ++b) {
267  boxes_[b].plot(win);
268  win->Text(boxes_[b].left(), height + kTextSize, box_texts_[b].string());
269  TBOX scaled(boxes_[b]);
270  scaled.scale(256.0 / height);
271  scaled.plot(win);
272  }
273  // The full transcription.
274  win->Pen(ScrollView::CYAN);
275  win->Text(0, height + kTextSize * 2, transcription_.string());
276  // Add the features.
277  win->Pen(ScrollView::GREEN);
278  win->Update();
279  window_wait(win);
280 #endif
281 }
void Pen(Color color)
Definition: scrollview.cpp:726
int size() const
Definition: genericvector.h:72
static void Update()
Definition: scrollview.cpp:715
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:641
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
void Brush(Color color)
Definition: scrollview.cpp:732
Pix * GetPix() const
Definition: imagedata.cpp:187
char window_wait(ScrollView *win)
Definition: callcpp.cpp:111
Definition: rect.h:30
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193
Pix * tesseract::ImageData::GetPix ( ) const

Definition at line 187 of file imagedata.cpp.

187  {
188  return GetPixInternal(image_data_);
189 }
const GenericVector<char>& tesseract::ImageData::image_data ( ) const
inline

Definition at line 120 of file imagedata.h.

120  {
121  return image_data_;
122  }
const STRING& tesseract::ImageData::imagefilename ( ) const
inline

Definition at line 108 of file imagedata.h.

108  {
109  return imagefilename_;
110  }
const STRING& tesseract::ImageData::language ( ) const
inline

Definition at line 123 of file imagedata.h.

123  {
124  return language_;
125  }
int tesseract::ImageData::MemoryUsed ( ) const

Definition at line 243 of file imagedata.cpp.

243  {
244  return image_data_.size();
245 }
int size() const
Definition: genericvector.h:72
int tesseract::ImageData::page_number ( ) const
inline

Definition at line 114 of file imagedata.h.

114  {
115  return page_number_;
116  }
float tesseract::ImageData::PreScale ( int  target_height,
Pix **  pix,
int *  scaled_width,
int *  scaled_height,
GenericVector< TBOX > *  boxes 
) const

Definition at line 196 of file imagedata.cpp.

198  {
199  int input_width = 0;
200  int input_height = 0;
201  Pix* src_pix = GetPix();
202  ASSERT_HOST(src_pix != NULL);
203  input_width = pixGetWidth(src_pix);
204  input_height = pixGetHeight(src_pix);
205  if (target_height == 0)
206  target_height = input_height;
207  float im_factor = static_cast<float>(target_height) / input_height;
208  if (scaled_width != NULL)
209  *scaled_width = IntCastRounded(im_factor * input_width);
210  if (scaled_height != NULL)
211  *scaled_height = target_height;
212  if (pix != NULL) {
213  // Get the scaled image.
214  pixDestroy(pix);
215  *pix = pixScale(src_pix, im_factor, im_factor);
216  if (*pix == NULL) {
217  tprintf("Scaling pix of size %d, %d by factor %g made null pix!!\n",
218  input_width, input_height, im_factor);
219  }
220  if (scaled_width != NULL)
221  *scaled_width = pixGetWidth(*pix);
222  if (scaled_height != NULL)
223  *scaled_height = pixGetHeight(*pix);
224  }
225  pixDestroy(&src_pix);
226  if (boxes != NULL) {
227  // Get the boxes.
228  boxes->truncate(0);
229  for (int b = 0; b < boxes_.size(); ++b) {
230  TBOX box = boxes_[b];
231  box.scale(im_factor);
232  boxes->push_back(box);
233  }
234  if (boxes->empty()) {
235  // Make a single box for the whole image.
236  TBOX box(0, 0, im_factor * input_width, target_height);
237  boxes->push_back(box);
238  }
239  }
240  return im_factor;
241 }
int size() const
Definition: genericvector.h:72
void truncate(int size)
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
#define ASSERT_HOST(x)
Definition: errcode.h:84
Pix * GetPix() const
Definition: imagedata.cpp:187
bool empty() const
Definition: genericvector.h:84
int IntCastRounded(double x)
Definition: helpers.h:172
Definition: rect.h:30
#define NULL
Definition: host.h:144
void scale(const float f)
Definition: rect.h:171
bool tesseract::ImageData::Serialize ( TFile fp) const

Definition at line 151 of file imagedata.cpp.

151  {
152  if (!imagefilename_.Serialize(fp)) return false;
153  if (fp->FWrite(&page_number_, sizeof(page_number_), 1) != 1) return false;
154  if (!image_data_.Serialize(fp)) return false;
155  if (!transcription_.Serialize(fp)) return false;
156  // WARNING: Will not work across different endian machines.
157  if (!boxes_.Serialize(fp)) return false;
158  if (!box_texts_.SerializeClasses(fp)) return false;
159  inT8 vertical = vertical_text_;
160  if (fp->FWrite(&vertical, sizeof(vertical), 1) != 1) return false;
161  return true;
162 }
bool Serialize(FILE *fp) const
bool SerializeClasses(FILE *fp) const
bool Serialize(FILE *fp) const
Definition: strngs.cpp:148
SIGNED char inT8
Definition: host.h:98
void tesseract::ImageData::set_imagefilename ( const STRING name)
inline

Definition at line 111 of file imagedata.h.

111  {
112  imagefilename_ = name;
113  }
name_table name
void tesseract::ImageData::set_language ( const STRING lang)
inline

Definition at line 126 of file imagedata.h.

126  {
127  language_ = lang;
128  }
void tesseract::ImageData::set_page_number ( int  num)
inline

Definition at line 117 of file imagedata.h.

117  {
118  page_number_ = num;
119  }
void tesseract::ImageData::SetPix ( Pix *  pix)

Definition at line 182 of file imagedata.cpp.

182  {
183  SetPixInternal(pix, &image_data_);
184 }
const STRING& tesseract::ImageData::transcription ( ) const
inline

Definition at line 129 of file imagedata.h.

129  {
130  return transcription_;
131  }

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