tesseract 4.1.1
Loading...
Searching...
No Matches
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 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
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:390
FCOORD re_rotation() const
Definition: ocrblock.h:134
void rotate(const FCOORD vec)
Definition: points.h:763

◆ DenormTransform() [2/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}
int IntCastRounded(double x)
Definition: helpers.h:175
int16_t x
Definition: blobs.h:93
int16_t y
Definition: blobs.h:94
Definition: points.h:189
float y() const
Definition: points.h:210
float x() const
Definition: points.h:207

◆ 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 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}
int binary_search(const T &target) const
float y_scale() const
Definition: normalis.h:270
void set_y(float yin)
rewrite function
Definition: points.h:218
void set_x(float xin)
rewrite function
Definition: points.h:214

◆ LocalDenormTransform() [2/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}

◆ LocalNormBlob()

void DENORM::LocalNormBlob ( TBLOB blob) const

Definition at line 412 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);
422}
void Move(const ICOORD vec)
Definition: blobs.cpp:430
void Rotate(const FCOORD rotation)
Definition: blobs.cpp:422
void Scale(float factor)
Definition: blobs.cpp:438
integer coordinate
Definition: points.h:32

◆ LocalNormTransform() [1/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}
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108
int size() const
Definition: genericvector.h:72

◆ LocalNormTransform() [2/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

◆ NormTransform() [1/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

◆ NormTransform() [2/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}

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

◆ 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 505 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 }
537}
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
void Print() const
Definition: normalis.cpp:505

◆ 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}
const DENORM * predecessor() const
Definition: normalis.h:263
int16_t width() const
Definition: rect.h:115
int16_t height() const
Definition: rect.h:108
int16_t left() const
Definition: rect.h:72
int16_t bottom() const
Definition: rect.h:65

◆ 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}
float x_scale() const
Definition: normalis.h:267

◆ 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 428 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 }
502}
const float kFinalPixelTolerance
Definition: normalis.cpp:36
const int kSloppyTolerance
Definition: normalis.cpp:34
const int kBlnBaselineOffset
Definition: normalis.h:25
const int kBlnCellHeight
Definition: normalis.h:23
const int kBlnXHeight
Definition: normalis.h:24
float pt_to_pt_dist(const FCOORD &pt) const
Distance between pts.
Definition: points.h:242
int16_t top() const
Definition: rect.h:58
int16_t right() const
Definition: rect.h:79
bool script_has_upper_lower() const
Definition: unicharset.h:897
bool top_bottom_useful() const
Definition: unicharset.h:537
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:568

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