24 #include "allheaders.h" 49 inverse_ = src.inverse_;
50 predecessor_ = src.predecessor_;
53 if (src.rotation_ == NULL)
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_;
98 float x_origin,
float y_origin,
99 float x_scale,
float y_scale,
100 float final_xshift,
float final_yshift) {
103 if (rotation == NULL)
106 rotation_ =
new FCOORD(*rotation);
108 x_origin_ = x_origin;
109 y_origin_ = y_origin;
112 final_xshift_ = final_xshift;
113 final_yshift_ = final_yshift;
155 static void ComputeRunlengthImage(
160 int width = box.
width();
161 int height = box.
height();
165 for (
int ix = 0; ix < width; ++ix) {
167 for (
int i = 0; i < y_coords[ix].size(); ++i) {
168 int y_edge =
ClipToRange(y_coords[ix][i], 0, height);
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 (
int i = 0; i < x_coords[iy].size(); ++i) {
187 int x_edge =
ClipToRange(x_coords[iy][i], 0, width);
188 int gap = x_edge - x;
190 if (gap < (*minruns)(x, iy))
191 (*minruns)(x, iy) = gap;
197 if (gap < (*minruns)(x, iy))
198 (*minruns)(x, iy) = gap;
223 static void ComputeEdgeDensityProfiles(
const TBOX& box,
227 int width = box.
width();
228 int height = box.
height();
232 for (
int iy = 0; iy < height; ++iy) {
233 for (
int ix = 0; ix < width; ++ix) {
234 int run = minruns(ix, iy);
235 if (run == 0) run = 1;
236 float density = 1.0f / run;
237 (*hx)[ix] += density;
238 (*hy)[iy] += density;
244 for (
int ix = 0; ix < width; ++ix) {
247 for (
int iy = 0; iy < height; ++iy) {
253 (*hy)[height] = 1.0f;
268 const DENORM* predecessor,
const TBOX& box,
float target_width,
269 float target_height,
float final_xshift,
float final_yshift,
280 int width = box.
width();
281 int height = box.
height();
283 ComputeRunlengthImage(box, x_coords, y_coords, &minruns);
286 ComputeEdgeDensityProfiles(box, minruns, x_map_, y_map_);
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;
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;
297 x_origin_ = box.
left();
299 final_xshift_ = final_xshift;
300 final_yshift_ = final_yshift;
313 FCOORD translated(pt.
x() - x_origin_, pt.
y() - y_origin_);
314 if (x_map_ != NULL && y_map_ != NULL) {
316 translated.set_x((*x_map_)[x]);
318 translated.set_y((*y_map_)[y]);
320 translated.set_x(translated.x() * x_scale_);
321 translated.set_y(translated.y() * y_scale_);
322 if (rotation_ != NULL)
323 translated.rotate(*rotation_);
325 transformed->
set_x(translated.x() + final_xshift_);
326 transformed->
set_y(translated.y() + final_yshift_);
335 TPOINT* transformed)
const {
343 FCOORD* transformed)
const {
345 if (first_norm !=
this) {
346 if (predecessor_ != NULL) {
348 }
else if (block_ != NULL) {
351 src_pt.
rotate(fwd_rotation);
367 FCOORD rotated(pt.
x() - final_xshift_, pt.
y() - final_yshift_);
368 if (x_map_ != NULL && y_map_ != NULL) {
370 original->
set_x(x + x_origin_);
372 original->
set_y(y + y_origin_);
374 if (rotation_ != NULL) {
375 FCOORD inverse_rotation(rotation_->
x(), -rotation_->
y());
376 rotated.
rotate(inverse_rotation);
378 original->
set_x(rotated.x() / x_scale_ + x_origin_);
400 if (last_denorm !=
this) {
401 if (predecessor_ != NULL) {
403 }
else if (block_ != NULL) {
414 blob->
Move(translation);
415 if (y_scale_ != 1.0f)
416 blob->
Scale(y_scale_);
417 if (rotation_ != NULL)
421 blob->
Move(translation);
430 float* min_xht,
float* max_xht,
float* yshift)
const {
449 int min_bottom, max_bottom, min_top, max_top;
454 double midx = (bbox.
left() + bbox.
right()) / 2.0;
455 double ydiff = (bbox.
top() - bbox.
bottom()) + 2.0;
457 FCOORD mid_high(midx, bbox.
bottom() + ydiff), tmid_high;
462 double yscale = tmid_high.pt_to_pt_dist(tmid_bot) / ydiff;
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;
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;
476 if ((top_shift >= 0 && bottom_shift > 0) ||
477 (top_shift < 0 && bottom_shift < 0)) {
478 bln_yshift = (top_shift + bottom_shift) / 2;
480 *yshift = bln_yshift * yscale;
497 float result = height *
kBlnXHeight * yscale / min_height;
499 result = height *
kBlnXHeight * yscale / max_height;
507 tprintf(
"Pix dimensions %d x %d x %d\n",
508 pixGetWidth(pix_), pixGetHeight(pix_), pixGetDepth(pix_));
513 tprintf(
"Block rotation %g, %g\n",
516 tprintf(
"Input Origin = (%g, %g)\n", x_origin_, y_origin_);
517 if (x_map_ != NULL && y_map_ != NULL) {
519 for (
int x = 0; x < x_map_->
size(); ++x) {
523 for (
int y = 0; y < y_map_->
size(); ++y) {
528 tprintf(
"Scale = (%g, %g)\n", x_scale_, y_scale_);
529 if (rotation_ != NULL)
530 tprintf(
"Rotation = (%g, %g)\n", rotation_->
x(), rotation_->
y());
532 tprintf(
"Final Origin = (%g, %g)\n", final_xshift_, final_xshift_);
533 if (predecessor_ != NULL) {
535 predecessor_->
Print();
543 void DENORM::Clear() {
544 if (x_map_ != NULL) {
548 if (y_map_ != NULL) {
552 if (rotation_ != NULL) {
559 void DENORM::Init() {
571 final_xshift_ = 0.0f;
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
const int kBlnBaselineOffset
int IntCastRounded(double x)
bool top_bottom_useful() const
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)
DENORM & operator=(const DENORM &)
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
const DENORM * predecessor() const
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) 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)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
TBOX bounding_box() const
FCOORD re_rotation() const
void set_x(float xin)
rewrite function
const float kFinalPixelTolerance
void rotate(const FCOORD vec)
void Move(const ICOORD vec)
const BLOCK * block() const
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
void set_y(float yin)
rewrite function
const int kSloppyTolerance
void LocalNormBlob(TBLOB *blob) const
void set_y(inT16 yin)
rewrite function
void Rotate(const FCOORD rotation)
void set_x(inT16 xin)
rewrite function
int binary_search(const T &target) const
void XHeightRange(int unichar_id, const UNICHARSET &unicharset, const TBOX &bbox, float *min_xht, float *max_xht, float *yshift) const
bool script_has_upper_lower() const
void init_to_size(int size, T t)