tesseract  4.0.0-1-g2a2b
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:211

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

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

◆ 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  ImageData* 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 }
void resize_no_init(int size)
Definition: genericvector.h:65
int page_number() const
Definition: imagedata.h:132
const GenericVector< char > & image_data() const
Definition: imagedata.h:138
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
const STRING & box_text(int index) const
Definition: imagedata.h:156
int push_back(T object)

◆ 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 DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:161
bool DeSerialize(bool swap, FILE *fp)
bool DeSerializeClasses(bool swap, FILE *fp)

◆ Display()

void tesseract::ImageData::Display ( ) const

Definition at line 275 of file imagedata.cpp.

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

◆ GetPix()

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

Definition at line 216 of file imagedata.cpp.

216  {
217  return GetPixInternal(image_data_);
218 }

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

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

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

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

◆ 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
Definition: strngs.cpp:148
bool SerializeClasses(FILE *fp) const
bool Serialize(FILE *fp) const

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

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

◆ 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:181
int page_number() const
Definition: imagedata.h:132

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