tesseract  4.00.00dev
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 (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:212

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

316  {
317  // Copy the boxes and make the transcription.
318  for (int i = 0; i < box_pages.size(); ++i) {
319  if (page_number_ >= 0 && box_pages[i] != page_number_) continue;
320  transcription_ += texts[i];
321  boxes_.push_back(boxes[i]);
322  box_texts_.push_back(texts[i]);
323  }
324 }
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 154 of file imagedata.h.

154  {
155  return box_texts_[index];
156  }

◆ box_texts()

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

Definition at line 151 of file imagedata.h.

151  {
152  return box_texts_;
153  }

◆ boxes()

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

Definition at line 148 of file imagedata.h.

148  {
149  return boxes_;
150  }

◆ 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 }
const GenericVector< char > & image_data() const
Definition: imagedata.h:136
int page_number() const
Definition: imagedata.h:130
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
const STRING & box_text(int index) const
Definition: imagedata.h:154

◆ 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->FReadEndian(&page_number_, sizeof(page_number_), 1) != 1)
184  return false;
185  if (!image_data_.DeSerialize(fp)) return false;
186  if (!language_.DeSerialize(fp)) return false;
187  if (!transcription_.DeSerialize(fp)) return false;
188  // WARNING: Will not work across different endian machines.
189  if (!boxes_.DeSerialize(fp)) return false;
190  if (!box_texts_.DeSerializeClasses(fp)) return false;
191  inT8 vertical = 0;
192  if (fp->FRead(&vertical, sizeof(vertical), 1) != 1) return false;
193  vertical_text_ = vertical != 0;
194  return true;
195 }
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:163
int8_t inT8
Definition: host.h:34
bool DeSerializeClasses(bool swap, FILE *fp)

◆ Display()

void tesseract::ImageData::Display ( ) const

Definition at line 276 of file imagedata.cpp.

276  {
277 #ifndef GRAPHICS_DISABLED
278  const int kTextSize = 64;
279  // Draw the image.
280  Pix* pix = GetPix();
281  if (pix == NULL) return;
282  int width = pixGetWidth(pix);
283  int height = pixGetHeight(pix);
284  ScrollView* win = new ScrollView("Imagedata", 100, 100,
285  2 * (width + 2 * kTextSize),
286  2 * (height + 4 * kTextSize),
287  width + 10, height + 3 * kTextSize, true);
288  win->Image(pix, 0, height - 1);
289  pixDestroy(&pix);
290  // Draw the boxes.
291  win->Pen(ScrollView::RED);
292  win->Brush(ScrollView::NONE);
293  int text_size = kTextSize;
294  if (!boxes_.empty() && boxes_[0].height() * 2 < text_size)
295  text_size = boxes_[0].height() * 2;
296  win->TextAttributes("Arial", text_size, false, false, false);
297  if (!boxes_.empty()) {
298  for (int b = 0; b < boxes_.size(); ++b) {
299  boxes_[b].plot(win);
300  win->Text(boxes_[b].left(), height + kTextSize, box_texts_[b].string());
301  }
302  } else {
303  // The full transcription.
304  win->Pen(ScrollView::CYAN);
305  win->Text(0, height + kTextSize * 2, transcription_.string());
306  }
307  win->Update();
308  window_wait(win);
309 #endif
310 }
void Brush(Color color)
Definition: scrollview.cpp:732
const char * string() const
Definition: strngs.cpp:198
bool empty() const
Definition: genericvector.h:90
int size() const
Definition: genericvector.h:72
Pix * GetPix() const
Definition: imagedata.cpp:217
static void Update()
Definition: scrollview.cpp:715
char window_wait(ScrollView *win)
Definition: callcpp.cpp:111
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
void Pen(Color color)
Definition: scrollview.cpp:726
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:641

◆ GetPix()

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

Definition at line 217 of file imagedata.cpp.

217  {
218  return GetPixInternal(image_data_);
219 }

◆ image_data()

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

Definition at line 136 of file imagedata.h.

136  {
137  return image_data_;
138  }

◆ imagefilename()

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

Definition at line 124 of file imagedata.h.

124  {
125  return imagefilename_;
126  }

◆ language()

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

Definition at line 139 of file imagedata.h.

139  {
140  return language_;
141  }

◆ MemoryUsed()

int tesseract::ImageData::MemoryUsed ( ) const

Definition at line 271 of file imagedata.cpp.

271  {
272  return image_data_.size();
273 }
int size() const
Definition: genericvector.h:72

◆ page_number()

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

Definition at line 130 of file imagedata.h.

130  {
131  return page_number_;
132  }

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

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

◆ 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 (!language_.Serialize(fp)) return false;
170  if (!transcription_.Serialize(fp)) return false;
171  // WARNING: Will not work across different endian machines.
172  if (!boxes_.Serialize(fp)) return false;
173  if (!box_texts_.SerializeClasses(fp)) return false;
174  inT8 vertical = vertical_text_;
175  if (fp->FWrite(&vertical, sizeof(vertical), 1) != 1) return false;
176  return true;
177 }
bool Serialize(FILE *fp) const
Definition: strngs.cpp:148
int8_t inT8
Definition: host.h:34
bool Serialize(FILE *fp) const
bool SerializeClasses(FILE *fp) const

◆ set_imagefilename()

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

Definition at line 127 of file imagedata.h.

127  {
128  imagefilename_ = name;
129  }

◆ set_language()

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

Definition at line 142 of file imagedata.h.

142  {
143  language_ = lang;
144  }

◆ set_page_number()

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

Definition at line 133 of file imagedata.h.

133  {
134  page_number_ = num;
135  }

◆ SetPix()

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

Definition at line 212 of file imagedata.cpp.

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

◆ SkipDeSerialize()

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

Definition at line 198 of file imagedata.cpp.

198  {
199  if (!STRING::SkipDeSerialize(fp)) return false;
201  if (fp->FRead(&page_number, sizeof(page_number), 1) != 1) return false;
202  if (!GenericVector<char>::SkipDeSerialize(fp)) return false;
203  if (!STRING::SkipDeSerialize(fp)) return false;
204  if (!STRING::SkipDeSerialize(fp)) return false;
205  if (!GenericVector<TBOX>::SkipDeSerialize(fp)) return false;
206  if (!GenericVector<STRING>::SkipDeSerializeClasses(fp)) return false;
207  inT8 vertical = 0;
208  return fp->FRead(&vertical, sizeof(vertical), 1) == 1;
209 }
int32_t inT32
Definition: host.h:38
int page_number() const
Definition: imagedata.h:130
int8_t inT8
Definition: host.h:34
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: strngs.cpp:183
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ transcription()

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

Definition at line 145 of file imagedata.h.

145  {
146  return transcription_;
147  }

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