21#include <allheaders.h>
50 inverse_ = src.inverse_;
51 predecessor_ = src.predecessor_;
54 if (src.rotation_ ==
nullptr) {
57 rotation_ =
new FCOORD(*src.rotation_);
59 x_origin_ = src.x_origin_;
60 y_origin_ = src.y_origin_;
61 x_scale_ = src.x_scale_;
62 y_scale_ = src.y_scale_;
63 final_xshift_ = src.final_xshift_;
64 final_yshift_ = src.final_yshift_;
98 const DENORM *predecessor,
float x_origin,
float y_origin,
99 float x_scale,
float y_scale,
float final_xshift,
100 float final_yshift) {
103 if (rotation ==
nullptr) {
106 rotation_ =
new FCOORD(*rotation);
109 x_origin_ = x_origin;
110 y_origin_ = y_origin;
113 final_xshift_ = final_xshift;
114 final_yshift_ = final_yshift;
156static void ComputeRunlengthImage(
const TBOX &box,
157 const std::vector<std::vector<int>> &x_coords,
158 const std::vector<std::vector<int>> &y_coords,
160 int width = box.
width();
161 int height = box.
height();
165 for (
int ix = 0; ix < width; ++ix) {
167 for (
auto y_coord : y_coords[ix]) {
169 int gap = y_edge - y;
172 (*minruns)(ix, y) = gap;
177 int gap = height - y;
179 (*minruns)(ix, y) = gap;
184 for (
int iy = 0; iy < height; ++iy) {
186 for (
auto x_coord : x_coords[iy]) {
188 int gap = x_edge - x;
190 if (gap < (*minruns)(x, iy)) {
191 (*minruns)(x, iy) = gap;
198 if (gap < (*minruns)(x, iy)) {
199 (*minruns)(x, iy) = gap;
225static void ComputeEdgeDensityProfiles(
const TBOX &box,
const GENERIC_2D_ARRAY<int> &minruns,
226 std::vector<float> &hx, std::vector<float> &hy) {
227 int width = box.width();
228 int height = box.height();
230 hx.resize(width + 1);
232 hy.resize(height + 1);
234 for (
int iy = 0; iy < height; ++iy) {
235 for (
int ix = 0; ix < width; ++ix) {
236 int run = minruns(ix, iy);
240 float density = 1.0f / run;
248 for (
int ix = 0; ix < width; ++ix) {
251 for (
int iy = 0; iy < height; ++iy) {
272 float target_height,
float final_xshift,
float final_yshift,
273 const std::vector<std::vector<int>> &x_coords,
274 const std::vector<std::vector<int>> &y_coords) {
280 x_map_ =
new std::vector<float>;
281 y_map_ =
new std::vector<float>;
283 int width = box.
width();
284 int height = box.
height();
286 ComputeRunlengthImage(box, x_coords, y_coords, &minruns);
289 ComputeEdgeDensityProfiles(box, minruns, *x_map_, *y_map_);
292 (*x_map_)[width] = target_width;
293 for (
int x = width - 1; x >= 0; --x) {
294 (*x_map_)[x] = (*x_map_)[x + 1] - (*x_map_)[x] * target_width;
296 (*y_map_)[height] = target_height;
297 for (
int y = height - 1; y >= 0; --y) {
298 (*y_map_)[y] = (*y_map_)[y + 1] - (*y_map_)[y] * target_height;
300 x_origin_ = box.
left();
302 final_xshift_ = final_xshift;
303 final_yshift_ = final_yshift;
316 FCOORD translated(pt.
x() - x_origin_, pt.
y() - y_origin_);
317 if (x_map_ !=
nullptr && y_map_ !=
nullptr) {
319 translated.
set_x((*x_map_)[x]);
321 translated.
set_y((*y_map_)[y]);
323 translated.
set_x(translated.
x() * x_scale_);
324 translated.
set_y(translated.
y() * y_scale_);
325 if (rotation_ !=
nullptr) {
326 translated.
rotate(*rotation_);
329 transformed->
set_x(translated.
x() + final_xshift_);
330 transformed->
set_y(translated.
y() + final_yshift_);
347 if (first_norm !=
this) {
348 if (predecessor_ !=
nullptr) {
350 }
else if (block_ !=
nullptr) {
352 src_pt.
rotate(fwd_rotation);
369 FCOORD rotated(pt.
x() - final_xshift_, pt.
y() - final_yshift_);
370 if (x_map_ !=
nullptr && y_map_ !=
nullptr) {
371 auto pos = std::upper_bound(x_map_->begin(), x_map_->end(), rotated.
x());
372 if (pos > x_map_->begin()) {
375 auto x = pos - x_map_->begin();
376 original->
set_x(x + x_origin_);
377 pos = std::upper_bound(y_map_->begin(), y_map_->end(), rotated.
y());
378 if (pos > y_map_->begin()) {
381 auto y = pos - y_map_->begin();
382 original->
set_y(y + y_origin_);
384 if (rotation_ !=
nullptr) {
385 FCOORD inverse_rotation(rotation_->
x(), -rotation_->
y());
386 rotated.
rotate(inverse_rotation);
388 original->
set_x(rotated.
x() / x_scale_ + x_origin_);
408 if (last_denorm !=
this) {
409 if (predecessor_ !=
nullptr) {
411 }
else if (block_ !=
nullptr) {
421 blob->
Move(translation);
422 if (y_scale_ != 1.0f) {
423 blob->
Scale(y_scale_);
425 if (rotation_ !=
nullptr) {
430 blob->
Move(translation);
438 float *min_xht,
float *max_xht,
float *yshift)
const {
459 int min_bottom, max_bottom, min_top, max_top;
460 unicharset.
get_top_bottom(unichar_id, &min_bottom, &max_bottom, &min_top, &max_top);
463 double midx = (bbox.
left() + bbox.
right()) / 2.0;
464 double ydiff = (bbox.
top() - bbox.
bottom()) + 2.0;
466 FCOORD mid_high(midx, bbox.
bottom() + ydiff), tmid_high;
474 int bln_yshift = 0, bottom_shift = 0, top_shift = 0;
475 if (bottom < min_bottom - tolerance) {
476 bottom_shift = bottom - min_bottom;
477 }
else if (bottom > max_bottom + tolerance) {
478 bottom_shift = bottom - max_bottom;
480 if (top < min_top - tolerance) {
481 top_shift = top - min_top;
482 }
else if (top > max_top + tolerance) {
483 top_shift = top - max_top;
485 if ((top_shift >= 0 && bottom_shift > 0) || (top_shift < 0 && bottom_shift < 0)) {
486 bln_yshift = (top_shift + bottom_shift) / 2;
488 *yshift = bln_yshift * yscale;
505 float result = height *
kBlnXHeight * yscale / min_height;
507 result = height *
kBlnXHeight * yscale / max_height;
514 if (pix_ !=
nullptr) {
515 tprintf(
"Pix dimensions %d x %d x %d\n", pixGetWidth(pix_), pixGetHeight(pix_),
524 tprintf(
"Input Origin = (%g, %g)\n", x_origin_, y_origin_);
525 if (x_map_ !=
nullptr && y_map_ !=
nullptr) {
527 for (
auto x : *x_map_) {
531 for (
auto y : *y_map_) {
536 tprintf(
"Scale = (%g, %g)\n", x_scale_, y_scale_);
537 if (rotation_ !=
nullptr) {
538 tprintf(
"Rotation = (%g, %g)\n", rotation_->
x(), rotation_->
y());
541 tprintf(
"Final Origin = (%g, %g)\n", final_xshift_, final_xshift_);
542 if (predecessor_ !=
nullptr) {
544 predecessor_->
Print();
551void DENORM::Clear() {
566 predecessor_ =
nullptr;
573 final_xshift_ = 0.0f;
const int kSloppyTolerance
void tprintf(const char *format,...)
int IntCastRounded(double x)
const float kFinalPixelTolerance
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
const int kBlnBaselineOffset
void Move(const ICOORD vec)
void Rotate(const FCOORD rotation)
const DENORM * predecessor() const
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 NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
void XHeightRange(int unichar_id, const UNICHARSET &unicharset, const TBOX &bbox, float *min_xht, float *max_xht, float *yshift) const
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
void SetupNonLinear(const DENORM *predecessor, const TBOX &box, float target_width, float target_height, float final_xshift, float final_yshift, const std::vector< std::vector< int > > &x_coords, const std::vector< std::vector< int > > &y_coords)
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
void LocalNormBlob(TBLOB *blob) const
const BLOCK * block() const
DENORM & operator=(const DENORM &)
FCOORD re_rotation() const
void set_x(TDimension xin)
rewrite function
void set_y(TDimension yin)
rewrite function
void set_y(float yin)
rewrite function
float pt_to_pt_dist(const FCOORD &pt) const
Distance between pts.
void set_x(float xin)
rewrite function
void rotate(const FCOORD vec)
TDimension height() const
TDimension bottom() const
bool script_has_upper_lower() const
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
bool top_bottom_useful() const