tesseract  4.00.00dev
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 52 of file normalis.h.

Constructor & Destructor Documentation

◆ DENORM() [1/2]

DENORM::DENORM ( )

Definition at line 37 of file normalis.cpp.

37  {
38  Init();
39 }

◆ DENORM() [2/2]

DENORM::DENORM ( const DENORM src)

Definition at line 41 of file normalis.cpp.

41  {
42  rotation_ = NULL;
43  *this = src;
44 }

◆ ~DENORM()

DENORM::~DENORM ( )

Definition at line 66 of file normalis.cpp.

66  {
67  Clear();
68 }

Member Function Documentation

◆ block()

const BLOCK* DENORM::block ( ) const
inline

Definition at line 275 of file normalis.h.

275  {
276  return block_;
277  }

◆ DenormTransform() [1/2]

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

Definition at line 389 of file normalis.cpp.

390  {
391  FCOORD src_pt(pt.x, pt.y);
392  FCOORD float_result;
393  DenormTransform(last_denorm, src_pt, &float_result);
394  original->x = IntCastRounded(float_result.x());
395  original->y = IntCastRounded(float_result.y());
396 }
Definition: points.h:189
int IntCastRounded(double x)
Definition: helpers.h:179
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:389
inT16 x
Definition: blobs.h:71
inT16 y
Definition: blobs.h:72
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209

◆ DenormTransform() [2/2]

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

Definition at line 397 of file normalis.cpp.

398  {
399  LocalDenormTransform(pt, original);
400  if (last_denorm != this) {
401  if (predecessor_ != NULL) {
402  predecessor_->DenormTransform(last_denorm, *original, original);
403  } else if (block_ != NULL) {
404  original->rotate(block_->re_rotation());
405  }
406  }
407 }
void rotate(const FCOORD vec)
Definition: ipoints.h:471
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:389
FCOORD re_rotation() const
Definition: ocrblock.h:138
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:359

◆ inverse()

bool DENORM::inverse ( ) const
inline

Definition at line 254 of file normalis.h.

254  {
255  return inverse_;
256  }

◆ LocalDenormTransform() [1/2]

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

Definition at line 359 of file normalis.cpp.

359  {
360  FCOORD src_pt(pt.x, pt.y);
361  FCOORD float_result;
362  LocalDenormTransform(src_pt, &float_result);
363  original->x = IntCastRounded(float_result.x());
364  original->y = IntCastRounded(float_result.y());
365 }
Definition: points.h:189
int IntCastRounded(double x)
Definition: helpers.h:179
inT16 x
Definition: blobs.h:71
inT16 y
Definition: blobs.h:72
float y() const
Definition: points.h:212
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:359
float x() const
Definition: points.h:209

◆ LocalDenormTransform() [2/2]

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

Definition at line 366 of file normalis.cpp.

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

◆ LocalNormBlob()

void DENORM::LocalNormBlob ( TBLOB blob) const

Definition at line 411 of file normalis.cpp.

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

◆ LocalNormTransform() [1/2]

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

Definition at line 305 of file normalis.cpp.

305  {
306  FCOORD src_pt(pt.x, pt.y);
307  FCOORD float_result;
308  LocalNormTransform(src_pt, &float_result);
309  transformed->x = IntCastRounded(float_result.x());
310  transformed->y = IntCastRounded(float_result.y());
311 }
Definition: points.h:189
int IntCastRounded(double x)
Definition: helpers.h:179
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:305
inT16 x
Definition: blobs.h:71
inT16 y
Definition: blobs.h:72
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209

◆ LocalNormTransform() [2/2]

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

Definition at line 312 of file normalis.cpp.

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

◆ NormTransform() [1/2]

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

Definition at line 334 of file normalis.cpp.

335  {
336  FCOORD src_pt(pt.x, pt.y);
337  FCOORD float_result;
338  NormTransform(first_norm, src_pt, &float_result);
339  transformed->x = IntCastRounded(float_result.x());
340  transformed->y = IntCastRounded(float_result.y());
341 }
Definition: points.h:189
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:334
int IntCastRounded(double x)
Definition: helpers.h:179
inT16 x
Definition: blobs.h:71
inT16 y
Definition: blobs.h:72
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209

◆ NormTransform() [2/2]

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

Definition at line 342 of file normalis.cpp.

343  {
344  FCOORD src_pt(pt);
345  if (first_norm != this) {
346  if (predecessor_ != NULL) {
347  predecessor_->NormTransform(first_norm, pt, &src_pt);
348  } else if (block_ != NULL) {
349  FCOORD fwd_rotation(block_->re_rotation().x(),
350  -block_->re_rotation().y());
351  src_pt.rotate(fwd_rotation);
352  }
353  }
354  LocalNormTransform(src_pt, transformed);
355 }
Definition: points.h:189
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:334
void rotate(const FCOORD vec)
Definition: ipoints.h:471
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:305
float y() const
Definition: points.h:212
FCOORD re_rotation() const
Definition: ocrblock.h:138
float x() const
Definition: points.h:209

◆ operator=()

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

Definition at line 47 of file normalis.cpp.

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

◆ pix()

Pix* DENORM::pix ( ) const
inline

Definition at line 248 of file normalis.h.

248  {
249  return pix_;
250  }

◆ predecessor()

const DENORM* DENORM::predecessor ( ) const
inline

Definition at line 265 of file normalis.h.

265  {
266  return predecessor_;
267  }

◆ Print()

void DENORM::Print ( ) const

Definition at line 505 of file normalis.cpp.

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

◆ RootDenorm()

const DENORM* DENORM::RootDenorm ( ) const
inline

Definition at line 260 of file normalis.h.

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

◆ set_block()

void DENORM::set_block ( const BLOCK block)
inline

Definition at line 278 of file normalis.h.

278  {
279  block_ = block;
280  }
const BLOCK * block() const
Definition: normalis.h:275

◆ set_inverse()

void DENORM::set_inverse ( bool  value)
inline

Definition at line 257 of file normalis.h.

257  {
258  inverse_ = value;
259  }

◆ set_pix()

void DENORM::set_pix ( Pix *  pix)
inline

Definition at line 251 of file normalis.h.

251  {
252  pix_ = pix;
253  }
Pix * pix() const
Definition: normalis.h:248

◆ 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 267 of file normalis.cpp.

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

◆ 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 95 of file normalis.cpp.

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

◆ x_scale()

float DENORM::x_scale ( ) const
inline

Definition at line 269 of file normalis.h.

269  {
270  return x_scale_;
271  }

◆ 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 428 of file normalis.cpp.

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

◆ y_scale()

float DENORM::y_scale ( ) const
inline

Definition at line 272 of file normalis.h.

272  {
273  return y_scale_;
274  }

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