tesseract  4.0.0-1-g2a2b
DENORM Class Reference

#include <normalis.h>

Public Member Functions

 DENORM ()
 
 DENORM (const DENORM &)
 
DENORMoperator= (const DENORM &)
 
 ~DENORM ()
 
void SetupNormalization (const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift)
 
void SetupNonLinear (const DENORM *predecessor, const TBOX &box, float target_width, float target_height, float final_xshift, float final_yshift, const GenericVector< GenericVector< int > > &x_coords, const GenericVector< GenericVector< int > > &y_coords)
 
void LocalNormTransform (const TPOINT &pt, TPOINT *transformed) const
 
void LocalNormTransform (const FCOORD &pt, FCOORD *transformed) const
 
void NormTransform (const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
 
void NormTransform (const DENORM *first_norm, const FCOORD &pt, FCOORD *transformed) const
 
void LocalDenormTransform (const TPOINT &pt, TPOINT *original) const
 
void LocalDenormTransform (const FCOORD &pt, FCOORD *original) const
 
void DenormTransform (const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
 
void DenormTransform (const DENORM *last_denorm, const FCOORD &pt, FCOORD *original) const
 
void LocalNormBlob (TBLOB *blob) const
 
void XHeightRange (int unichar_id, const UNICHARSET &unicharset, const TBOX &bbox, float *min_xht, float *max_xht, float *yshift) const
 
void Print () const
 
Pix * pix () const
 
void set_pix (Pix *pix)
 
bool inverse () const
 
void set_inverse (bool value)
 
const DENORMRootDenorm () const
 
const DENORMpredecessor () const
 
float x_scale () const
 
float y_scale () const
 
const BLOCKblock () const
 
void set_block (const BLOCK *block)
 

Detailed Description

Definition at line 50 of file normalis.h.

Constructor & Destructor Documentation

◆ DENORM() [1/2]

DENORM::DENORM ( )

Definition at line 38 of file normalis.cpp.

38  {
39  Init();
40 }

◆ DENORM() [2/2]

DENORM::DENORM ( const DENORM src)

Definition at line 42 of file normalis.cpp.

42  {
43  rotation_ = nullptr;
44  *this = src;
45 }

◆ ~DENORM()

DENORM::~DENORM ( )

Definition at line 67 of file normalis.cpp.

67  {
68  Clear();
69 }

Member Function Documentation

◆ block()

const BLOCK* DENORM::block ( ) const
inline

Definition at line 273 of file normalis.h.

273  {
274  return block_;
275  }

◆ DenormTransform() [1/2]

void DENORM::DenormTransform ( const DENORM last_denorm,
const TPOINT pt,
TPOINT original 
) const

Definition at line 390 of file normalis.cpp.

391  {
392  FCOORD src_pt(pt.x, pt.y);
393  FCOORD float_result;
394  DenormTransform(last_denorm, src_pt, &float_result);
395  original->x = IntCastRounded(float_result.x());
396  original->y = IntCastRounded(float_result.y());
397 }
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:390
int IntCastRounded(double x)
Definition: helpers.h:168
int16_t x
Definition: blobs.h:78
Definition: points.h:189
float x() const
Definition: points.h:208
int16_t y
Definition: blobs.h:79
float y() const
Definition: points.h:211

◆ DenormTransform() [2/2]

void DENORM::DenormTransform ( const DENORM last_denorm,
const FCOORD pt,
FCOORD original 
) const

Definition at line 398 of file normalis.cpp.

399  {
400  LocalDenormTransform(pt, original);
401  if (last_denorm != this) {
402  if (predecessor_ != nullptr) {
403  predecessor_->DenormTransform(last_denorm, *original, original);
404  } else if (block_ != nullptr) {
405  original->rotate(block_->re_rotation());
406  }
407  }
408 }
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:360
FCOORD re_rotation() const
Definition: ocrblock.h:136
void rotate(const FCOORD vec)
Definition: points.h:764
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:390

◆ inverse()

bool DENORM::inverse ( ) const
inline

Definition at line 252 of file normalis.h.

252  {
253  return inverse_;
254  }

◆ LocalDenormTransform() [1/2]

void DENORM::LocalDenormTransform ( const TPOINT pt,
TPOINT original 
) const

Definition at line 360 of file normalis.cpp.

360  {
361  FCOORD src_pt(pt.x, pt.y);
362  FCOORD float_result;
363  LocalDenormTransform(src_pt, &float_result);
364  original->x = IntCastRounded(float_result.x());
365  original->y = IntCastRounded(float_result.y());
366 }
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:360
int IntCastRounded(double x)
Definition: helpers.h:168
int16_t x
Definition: blobs.h:78
Definition: points.h:189
float x() const
Definition: points.h:208
int16_t y
Definition: blobs.h:79
float y() const
Definition: points.h:211

◆ LocalDenormTransform() [2/2]

void DENORM::LocalDenormTransform ( const FCOORD pt,
FCOORD original 
) const

Definition at line 367 of file normalis.cpp.

367  {
368  FCOORD rotated(pt.x() - final_xshift_, pt.y() - final_yshift_);
369  if (x_map_ != nullptr && y_map_ != nullptr) {
370  int x = x_map_->binary_search(rotated.x());
371  original->set_x(x + x_origin_);
372  int y = y_map_->binary_search(rotated.y());
373  original->set_y(y + y_origin_);
374  } else {
375  if (rotation_ != nullptr) {
376  FCOORD inverse_rotation(rotation_->x(), -rotation_->y());
377  rotated.rotate(inverse_rotation);
378  }
379  original->set_x(rotated.x() / x_scale_ + x_origin_);
380  float y_scale = y_scale_;
381  original->set_y(rotated.y() / y_scale + y_origin_);
382  }
383 }
void rotate(const FCOORD vec)
Definition: points.h:764
float y_scale() const
Definition: normalis.h:270
void set_x(float xin)
rewrite function
Definition: points.h:215
Definition: points.h:189
int binary_search(const T &target) const
float x() const
Definition: points.h:208
void set_y(float yin)
rewrite function
Definition: points.h:219
float y() const
Definition: points.h:211

◆ LocalNormBlob()

void DENORM::LocalNormBlob ( TBLOB blob) const

Definition at line 412 of file normalis.cpp.

412  {
413  TBOX blob_box = blob->bounding_box();
414  ICOORD translation(-IntCastRounded(x_origin_), -IntCastRounded(y_origin_));
415  blob->Move(translation);
416  if (y_scale_ != 1.0f)
417  blob->Scale(y_scale_);
418  if (rotation_ != nullptr)
419  blob->Rotate(*rotation_);
420  translation.set_x(IntCastRounded(final_xshift_));
421  translation.set_y(IntCastRounded(final_yshift_));
422  blob->Move(translation);
423 }
Definition: rect.h:34
integer coordinate
Definition: points.h:32
int IntCastRounded(double x)
Definition: helpers.h:168
void Rotate(const FCOORD rotation)
Definition: blobs.cpp:432
TBOX bounding_box() const
Definition: blobs.cpp:478
void Move(const ICOORD vec)
Definition: blobs.cpp:440
void Scale(float factor)
Definition: blobs.cpp:448

◆ LocalNormTransform() [1/2]

void DENORM::LocalNormTransform ( const TPOINT pt,
TPOINT transformed 
) const

Definition at line 306 of file normalis.cpp.

306  {
307  FCOORD src_pt(pt.x, pt.y);
308  FCOORD float_result;
309  LocalNormTransform(src_pt, &float_result);
310  transformed->x = IntCastRounded(float_result.x());
311  transformed->y = IntCastRounded(float_result.y());
312 }
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:306
int IntCastRounded(double x)
Definition: helpers.h:168
int16_t x
Definition: blobs.h:78
Definition: points.h:189
float x() const
Definition: points.h:208
int16_t y
Definition: blobs.h:79
float y() const
Definition: points.h:211

◆ LocalNormTransform() [2/2]

void DENORM::LocalNormTransform ( const FCOORD pt,
FCOORD transformed 
) const

Definition at line 313 of file normalis.cpp.

313  {
314  FCOORD translated(pt.x() - x_origin_, pt.y() - y_origin_);
315  if (x_map_ != nullptr && y_map_ != nullptr) {
316  int x = ClipToRange(IntCastRounded(translated.x()), 0, x_map_->size()-1);
317  translated.set_x((*x_map_)[x]);
318  int y = ClipToRange(IntCastRounded(translated.y()), 0, y_map_->size()-1);
319  translated.set_y((*y_map_)[y]);
320  } else {
321  translated.set_x(translated.x() * x_scale_);
322  translated.set_y(translated.y() * y_scale_);
323  if (rotation_ != nullptr)
324  translated.rotate(*rotation_);
325  }
326  transformed->set_x(translated.x() + final_xshift_);
327  transformed->set_y(translated.y() + final_yshift_);
328 }
int size() const
Definition: genericvector.h:71
void set_x(float xin)
rewrite function
Definition: points.h:215
int IntCastRounded(double x)
Definition: helpers.h:168
Definition: points.h:189
float x() const
Definition: points.h:208
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111
void set_y(float yin)
rewrite function
Definition: points.h:219
float y() const
Definition: points.h:211

◆ NormTransform() [1/2]

void DENORM::NormTransform ( const DENORM first_norm,
const TPOINT pt,
TPOINT transformed 
) const

Definition at line 335 of file normalis.cpp.

336  {
337  FCOORD src_pt(pt.x, pt.y);
338  FCOORD float_result;
339  NormTransform(first_norm, src_pt, &float_result);
340  transformed->x = IntCastRounded(float_result.x());
341  transformed->y = IntCastRounded(float_result.y());
342 }
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:335
int IntCastRounded(double x)
Definition: helpers.h:168
int16_t x
Definition: blobs.h:78
Definition: points.h:189
float x() const
Definition: points.h:208
int16_t y
Definition: blobs.h:79
float y() const
Definition: points.h:211

◆ NormTransform() [2/2]

void DENORM::NormTransform ( const DENORM first_norm,
const FCOORD pt,
FCOORD transformed 
) const

Definition at line 343 of file normalis.cpp.

344  {
345  FCOORD src_pt(pt);
346  if (first_norm != this) {
347  if (predecessor_ != nullptr) {
348  predecessor_->NormTransform(first_norm, pt, &src_pt);
349  } else if (block_ != nullptr) {
350  FCOORD fwd_rotation(block_->re_rotation().x(),
351  -block_->re_rotation().y());
352  src_pt.rotate(fwd_rotation);
353  }
354  }
355  LocalNormTransform(src_pt, transformed);
356 }
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:335
FCOORD re_rotation() const
Definition: ocrblock.h:136
void rotate(const FCOORD vec)
Definition: points.h:764
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:306
Definition: points.h:189
float x() const
Definition: points.h:208
float y() const
Definition: points.h:211

◆ operator=()

DENORM & DENORM::operator= ( const DENORM src)

Definition at line 48 of file normalis.cpp.

48  {
49  Clear();
50  inverse_ = src.inverse_;
51  predecessor_ = src.predecessor_;
52  pix_ = src.pix_;
53  block_ = src.block_;
54  if (src.rotation_ == nullptr)
55  rotation_ = nullptr;
56  else
57  rotation_ = new FCOORD(*src.rotation_);
58  x_origin_ = src.x_origin_;
59  y_origin_ = src.y_origin_;
60  x_scale_ = src.x_scale_;
61  y_scale_ = src.y_scale_;
62  final_xshift_ = src.final_xshift_;
63  final_yshift_ = src.final_yshift_;
64  return *this;
65 }
Definition: points.h:189

◆ pix()

Pix* DENORM::pix ( ) const
inline

Definition at line 246 of file normalis.h.

246  {
247  return pix_;
248  }

◆ predecessor()

const DENORM* DENORM::predecessor ( ) const
inline

Definition at line 263 of file normalis.h.

263  {
264  return predecessor_;
265  }

◆ Print()

void DENORM::Print ( ) const

Definition at line 506 of file normalis.cpp.

506  {
507  if (pix_ != nullptr) {
508  tprintf("Pix dimensions %d x %d x %d\n",
509  pixGetWidth(pix_), pixGetHeight(pix_), pixGetDepth(pix_));
510  }
511  if (inverse_)
512  tprintf("Inverse\n");
513  if (block_ && block_->re_rotation().x() != 1.0f) {
514  tprintf("Block rotation %g, %g\n",
515  block_->re_rotation().x(), block_->re_rotation().y());
516  }
517  tprintf("Input Origin = (%g, %g)\n", x_origin_, y_origin_);
518  if (x_map_ != nullptr && y_map_ != nullptr) {
519  tprintf("x map:\n");
520  for (int x = 0; x < x_map_->size(); ++x) {
521  tprintf("%g ", (*x_map_)[x]);
522  }
523  tprintf("\ny map:\n");
524  for (int y = 0; y < y_map_->size(); ++y) {
525  tprintf("%g ", (*y_map_)[y]);
526  }
527  tprintf("\n");
528  } else {
529  tprintf("Scale = (%g, %g)\n", x_scale_, y_scale_);
530  if (rotation_ != nullptr)
531  tprintf("Rotation = (%g, %g)\n", rotation_->x(), rotation_->y());
532  }
533  tprintf("Final Origin = (%g, %g)\n", final_xshift_, final_xshift_);
534  if (predecessor_ != nullptr) {
535  tprintf("Predecessor:\n");
536  predecessor_->Print();
537  }
538 }
int size() const
Definition: genericvector.h:71
FCOORD re_rotation() const
Definition: ocrblock.h:136
void Print() const
Definition: normalis.cpp:506
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
float x() const
Definition: points.h:208
float y() const
Definition: points.h:211

◆ RootDenorm()

const DENORM* DENORM::RootDenorm ( ) const
inline

Definition at line 258 of file normalis.h.

258  {
259  if (predecessor_ != nullptr)
260  return predecessor_->RootDenorm();
261  return this;
262  }
const DENORM * RootDenorm() const
Definition: normalis.h:258

◆ set_block()

void DENORM::set_block ( const BLOCK block)
inline

Definition at line 276 of file normalis.h.

276  {
277  block_ = block;
278  }
const BLOCK * block() const
Definition: normalis.h:273

◆ set_inverse()

void DENORM::set_inverse ( bool  value)
inline

Definition at line 255 of file normalis.h.

255  {
256  inverse_ = value;
257  }

◆ set_pix()

void DENORM::set_pix ( Pix *  pix)
inline

Definition at line 249 of file normalis.h.

249  {
250  pix_ = pix;
251  }
Pix * pix() const
Definition: normalis.h:246

◆ SetupNonLinear()

void DENORM::SetupNonLinear ( const DENORM predecessor,
const TBOX box,
float  target_width,
float  target_height,
float  final_xshift,
float  final_yshift,
const GenericVector< GenericVector< int > > &  x_coords,
const GenericVector< GenericVector< int > > &  y_coords 
)

Definition at line 268 of file normalis.cpp.

272  {
273  Clear();
274  predecessor_ = predecessor;
275  // x_map_ and y_map_ store a mapping from input x and y coordinate to output
276  // x and y coordinate, based on scaling to the supplied target_width and
277  // target_height.
278  x_map_ = new GenericVector<float>;
279  y_map_ = new GenericVector<float>;
280  // Set a 2-d image array to the run lengths at each pixel.
281  int width = box.width();
282  int height = box.height();
283  GENERIC_2D_ARRAY<int> minruns(width, height, 0);
284  ComputeRunlengthImage(box, x_coords, y_coords, &minruns);
285  // Edge density is the sum of the inverses of the run lengths. Compute
286  // edge density projection profiles.
287  ComputeEdgeDensityProfiles(box, minruns, x_map_, y_map_);
288  // Convert the edge density profiles to the coordinates by multiplying by
289  // the desired size and accumulating.
290  (*x_map_)[width] = target_width;
291  for (int x = width - 1; x >= 0; --x) {
292  (*x_map_)[x] = (*x_map_)[x + 1] - (*x_map_)[x] * target_width;
293  }
294  (*y_map_)[height] = target_height;
295  for (int y = height - 1; y >= 0; --y) {
296  (*y_map_)[y] = (*y_map_)[y + 1] - (*y_map_)[y] * target_height;
297  }
298  x_origin_ = box.left();
299  y_origin_ = box.bottom();
300  final_xshift_ = final_xshift;
301  final_yshift_ = final_yshift;
302 }
int16_t width() const
Definition: rect.h:115
int16_t left() const
Definition: rect.h:72
const DENORM * predecessor() const
Definition: normalis.h:263
int16_t bottom() const
Definition: rect.h:65
int16_t height() const
Definition: rect.h:108

◆ SetupNormalization()

void DENORM::SetupNormalization ( const BLOCK block,
const FCOORD rotation,
const DENORM predecessor,
float  x_origin,
float  y_origin,
float  x_scale,
float  y_scale,
float  final_xshift,
float  final_yshift 
)

Definition at line 96 of file normalis.cpp.

101  {
102  Clear();
103  block_ = block;
104  if (rotation == nullptr)
105  rotation_ = nullptr;
106  else
107  rotation_ = new FCOORD(*rotation);
108  predecessor_ = predecessor;
109  x_origin_ = x_origin;
110  y_origin_ = y_origin;
111  x_scale_ = x_scale;
112  y_scale_ = y_scale;
113  final_xshift_ = final_xshift;
114  final_yshift_ = final_yshift;
115 }
const BLOCK * block() const
Definition: normalis.h:273
float y_scale() const
Definition: normalis.h:270
float x_scale() const
Definition: normalis.h:267
const DENORM * predecessor() const
Definition: normalis.h:263
Definition: points.h:189

◆ x_scale()

float DENORM::x_scale ( ) const
inline

Definition at line 267 of file normalis.h.

267  {
268  return x_scale_;
269  }

◆ XHeightRange()

void DENORM::XHeightRange ( int  unichar_id,
const UNICHARSET unicharset,
const TBOX bbox,
float *  min_xht,
float *  max_xht,
float *  yshift 
) const

Definition at line 429 of file normalis.cpp.

431  {
432  // Default return -- accept anything.
433  *yshift = 0.0f;
434  *min_xht = 0.0f;
435  *max_xht = FLT_MAX;
436 
437  if (!unicharset.top_bottom_useful())
438  return;
439 
440  // Clip the top and bottom to the limit of normalized feature space.
441  int top = ClipToRange<int>(bbox.top(), 0, kBlnCellHeight - 1);
442  int bottom = ClipToRange<int>(bbox.bottom(), 0, kBlnCellHeight - 1);
443  // A tolerance of yscale corresponds to 1 pixel in the image.
444  double tolerance = y_scale();
445  // If the script doesn't have upper and lower-case characters, widen the
446  // tolerance to allow sloppy baseline/x-height estimates.
447  if (!unicharset.script_has_upper_lower())
448  tolerance = y_scale() * kSloppyTolerance;
449 
450  int min_bottom, max_bottom, min_top, max_top;
451  unicharset.get_top_bottom(unichar_id, &min_bottom, &max_bottom,
452  &min_top, &max_top);
453 
454  // Calculate the scale factor we'll use to get to image y-pixels
455  double midx = (bbox.left() + bbox.right()) / 2.0;
456  double ydiff = (bbox.top() - bbox.bottom()) + 2.0;
457  FCOORD mid_bot(midx, bbox.bottom()), tmid_bot;
458  FCOORD mid_high(midx, bbox.bottom() + ydiff), tmid_high;
459  DenormTransform(nullptr, mid_bot, &tmid_bot);
460  DenormTransform(nullptr, mid_high, &tmid_high);
461 
462  // bln_y_measure * yscale = image_y_measure
463  double yscale = tmid_high.pt_to_pt_dist(tmid_bot) / ydiff;
464 
465  // Calculate y-shift
466  int bln_yshift = 0, bottom_shift = 0, top_shift = 0;
467  if (bottom < min_bottom - tolerance) {
468  bottom_shift = bottom - min_bottom;
469  } else if (bottom > max_bottom + tolerance) {
470  bottom_shift = bottom - max_bottom;
471  }
472  if (top < min_top - tolerance) {
473  top_shift = top - min_top;
474  } else if (top > max_top + tolerance) {
475  top_shift = top - max_top;
476  }
477  if ((top_shift >= 0 && bottom_shift > 0) ||
478  (top_shift < 0 && bottom_shift < 0)) {
479  bln_yshift = (top_shift + bottom_shift) / 2;
480  }
481  *yshift = bln_yshift * yscale;
482 
483  // To help very high cap/xheight ratio fonts accept the correct x-height,
484  // and to allow the large caps in small caps to accept the xheight of the
485  // small caps, add kBlnBaselineOffset to chars with a maximum max, and have
486  // a top already at a significantly high position.
487  if (max_top == kBlnCellHeight - 1 &&
489  max_top += kBlnBaselineOffset;
490  top -= bln_yshift;
491  int height = top - kBlnBaselineOffset;
492  double min_height = min_top - kBlnBaselineOffset - tolerance;
493  double max_height = max_top - kBlnBaselineOffset + tolerance;
494 
495  // We shouldn't try calculations if the characters are very short (for example
496  // for punctuation).
497  if (min_height > kBlnXHeight / 8 && height > 0) {
498  float result = height * kBlnXHeight * yscale / min_height;
499  *max_xht = result + kFinalPixelTolerance;
500  result = height * kBlnXHeight * yscale / max_height;
501  *min_xht = result - kFinalPixelTolerance;
502  }
503 }
bool script_has_upper_lower() const
Definition: unicharset.h:891
float y_scale() const
Definition: normalis.h:270
const int kBlnXHeight
Definition: normalis.h:24
const int kBlnBaselineOffset
Definition: normalis.h:25
const int kBlnCellHeight
Definition: normalis.h:23
int16_t left() const
Definition: rect.h:72
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:390
int16_t top() const
Definition: rect.h:58
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:563
const float kFinalPixelTolerance
Definition: normalis.cpp:36
Definition: points.h:189
int16_t right() const
Definition: rect.h:79
const int kSloppyTolerance
Definition: normalis.cpp:34
int16_t bottom() const
Definition: rect.h:65
bool top_bottom_useful() const
Definition: unicharset.h:532

◆ y_scale()

float DENORM::y_scale ( ) const
inline

Definition at line 270 of file normalis.h.

270  {
271  return y_scale_;
272  }

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