tesseract  5.0.0-alpha-619-ge9db
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 49 of file normalis.h.

Constructor & Destructor Documentation

◆ DENORM() [1/2]

DENORM::DENORM ( )

Definition at line 37 of file normalis.cpp.

38  {
39  Init();

◆ DENORM() [2/2]

DENORM::DENORM ( const DENORM src)

Definition at line 41 of file normalis.cpp.

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

◆ ~DENORM()

DENORM::~DENORM ( )

Definition at line 66 of file normalis.cpp.

67  {
68  Clear();

Member Function Documentation

◆ block()

const BLOCK* DENORM::block ( ) const
inline

Definition at line 272 of file normalis.h.

273  {
274  return block_;

◆ DenormTransform() [1/2]

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

Definition at line 397 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  }

◆ DenormTransform() [2/2]

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

Definition at line 389 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());

◆ inverse()

bool DENORM::inverse ( ) const
inline

Definition at line 251 of file normalis.h.

252  {
253  return inverse_;

◆ LocalDenormTransform() [1/2]

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

Definition at line 366 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  }

◆ LocalDenormTransform() [2/2]

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

Definition at line 359 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());

◆ LocalNormBlob()

void DENORM::LocalNormBlob ( TBLOB blob) const

Definition at line 411 of file normalis.cpp.

412  {
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_ != nullptr)
418  blob->Rotate(*rotation_);
419  translation.set_x(IntCastRounded(final_xshift_));
420  translation.set_y(IntCastRounded(final_yshift_));
421  blob->Move(translation);

◆ LocalNormTransform() [1/2]

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

Definition at line 312 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_);

◆ LocalNormTransform() [2/2]

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

Definition at line 305 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());

◆ NormTransform() [1/2]

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

Definition at line 342 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);

◆ NormTransform() [2/2]

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

Definition at line 334 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());

◆ operator=()

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

Definition at line 47 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;

◆ pix()

Pix* DENORM::pix ( ) const
inline

Definition at line 245 of file normalis.h.

246  {
247  return pix_;

◆ predecessor()

const DENORM* DENORM::predecessor ( ) const
inline

Definition at line 262 of file normalis.h.

263  {
264  return predecessor_;

◆ Print()

void DENORM::Print ( ) const

Definition at line 504 of file normalis.cpp.

505  {
506  if (pix_ != nullptr) {
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_ != nullptr && y_map_ != nullptr) {
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_ != nullptr)
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_ != nullptr) {
534  tprintf("Predecessor:\n");
535  predecessor_->Print();
536  }

◆ RootDenorm()

const DENORM* DENORM::RootDenorm ( ) const
inline

Definition at line 257 of file normalis.h.

258  {
259  if (predecessor_ != nullptr)
260  return predecessor_->RootDenorm();
261  return this;

◆ set_block()

void DENORM::set_block ( const BLOCK block)
inline

Definition at line 275 of file normalis.h.

276  {
277  block_ = block;

◆ set_inverse()

void DENORM::set_inverse ( bool  value)
inline

Definition at line 254 of file normalis.h.

255  {
256  inverse_ = value;

◆ set_pix()

void DENORM::set_pix ( Pix *  pix)
inline

Definition at line 248 of file normalis.h.

249  {
250  pix_ = pix;

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

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;

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

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;

◆ x_scale()

float DENORM::x_scale ( ) const
inline

Definition at line 266 of file normalis.h.

267  {
268  return x_scale_;

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

430  {
431  // Default return -- accept anything.
432  *yshift = 0.0f;
433  *min_xht = 0.0f;
434  *max_xht = FLT_MAX;
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(nullptr, mid_bot, &tmid_bot);
459  DenormTransform(nullptr, 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  }

◆ y_scale()

float DENORM::y_scale ( ) const
inline

Definition at line 269 of file normalis.h.

270  {
271  return y_scale_;

The documentation for this class was generated from the following files:
TBLOB::Scale
void Scale(float factor)
Definition: blobs.cpp:437
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
kBlnXHeight
const int kBlnXHeight
Definition: normalis.h:23
DENORM::LocalDenormTransform
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:359
DENORM::NormTransform
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:334
FCOORD::set_x
void set_x(float xin)
rewrite function
Definition: points.h:213
kSloppyTolerance
const int kSloppyTolerance
Definition: normalis.cpp:33
FCOORD::y
float y() const
Definition: points.h:209
ICOORD
integer coordinate
Definition: points.h:30
FCOORD::x
float x() const
Definition: points.h:206
TBOX::top
int16_t top() const
Definition: rect.h:57
IntCastRounded
int IntCastRounded(double x)
Definition: helpers.h:173
DENORM::RootDenorm
const DENORM * RootDenorm() const
Definition: normalis.h:257
DENORM::predecessor
const DENORM * predecessor() const
Definition: normalis.h:262
FCOORD
Definition: points.h:187
FCOORD::set_y
void set_y(float yin)
rewrite function
Definition: points.h:217
GENERIC_2D_ARRAY< int >
TBOX::height
int16_t height() const
Definition: rect.h:107
DENORM::block
const BLOCK * block() const
Definition: normalis.h:272
DENORM::Print
void Print() const
Definition: normalis.cpp:504
UNICHARSET::get_top_bottom
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:558
TPOINT::x
int16_t x
Definition: blobs.h:91
DENORM::DenormTransform
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:389
DENORM::y_scale
float y_scale() const
Definition: normalis.h:269
TPOINT::y
int16_t y
Definition: blobs.h:92
kFinalPixelTolerance
const float kFinalPixelTolerance
Definition: normalis.cpp:35
TBOX::width
int16_t width() const
Definition: rect.h:114
UNICHARSET::top_bottom_useful
bool top_bottom_useful() const
Definition: unicharset.h:527
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
UNICHARSET::script_has_upper_lower
bool script_has_upper_lower() const
Definition: unicharset.h:887
FCOORD::rotate
void rotate(const FCOORD vec)
Definition: points.h:736
GenericVector< float >
TBLOB::Rotate
void Rotate(const FCOORD rotation)
Definition: blobs.cpp:421
TBOX::left
int16_t left() const
Definition: rect.h:71
DENORM::x_scale
float x_scale() const
Definition: normalis.h:266
TBOX::right
int16_t right() const
Definition: rect.h:78
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
GenericVector::binary_search
int binary_search(const T &target) const
Definition: genericvector.h:252
DENORM::LocalNormTransform
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:305
kBlnCellHeight
const int kBlnCellHeight
Definition: normalis.h:22
BLOCK::re_rotation
FCOORD re_rotation() const
Definition: ocrblock.h:133
DENORM::pix
Pix * pix() const
Definition: normalis.h:245
GenericVector::size
int size() const
Definition: genericvector.h:71
TBLOB::Move
void Move(const ICOORD vec)
Definition: blobs.cpp:429
kBlnBaselineOffset
const int kBlnBaselineOffset
Definition: normalis.h:24