22 #include "config_auto.h"
26 #include "allheaders.h"
31 #define PROJECTION_MARGIN 10 //arbitrary
37 const
double kCosSmallAngle = 0.866;
49 cblob_ptr->
rotate(rotation);
51 compute_bounding_box();
56 int left = -box.
right();
68 ICOORD bottom_pt(top_pt.x(), base_char_bottom_);
70 base_char_top_ = top_pt.y();
71 bottom_pt.rotate(rotation);
72 base_char_bottom_ = bottom_pt.y();
90 nextblob->joined =
TRUE;
97 if (cblob_ptr !=
NULL && other->cblob_ptr !=
NULL) {
98 C_OUTLINE_IT ol_it(cblob_ptr->
out_list());
99 ol_it.add_list_after(other->cblob_ptr->
out_list());
114 BLOBNBOX_IT *start_it,
127 float test_ymin, test_ymax;
132 blobcount = (
inT16) floor (box.
width () / xheight);
133 if (blobcount > 1 && cblob_ptr !=
NULL) {
135 blobwidth = (float) (box.
width () + 1) / blobcount;
136 for (blobindex = blobcount - 1, rightx = box.
right ();
137 blobindex >= 0; blobindex--, rightx -= blobwidth) {
142 blob = blob_it.data ();
145 test_ymin, test_ymax);
149 while (blob != end_it->data ());
151 leftx = (
inT16) floor (rightx - blobwidth);
152 if (leftx < box.
left ())
161 newblob->box =
TBOX (bl, tr);
163 newblob->base_char_top_ = tr.
y();
164 newblob->base_char_bottom_ = bl.
y();
165 end_it->add_after_stay_put (newblob);
175 for (
int dir = 0; dir <
BND_COUNT; ++dir) {
178 if (neighbour !=
NULL) {
181 gaps[dir] = box.
x_gap(n_box);
183 gaps[dir] = box.
y_gap(n_box);
194 int* v_min,
int* v_max)
const {
199 *h_max =
MAX(gaps[BND_LEFT], gaps[BND_RIGHT]);
200 if (*h_max > max_dimension && *h_min < max_dimension) *h_max = *h_min;
202 *v_max =
MAX(gaps[BND_ABOVE], gaps[BND_BELOW]);
203 if (*v_max > max_dimension && *v_min < max_dimension) *v_max = *v_min;
208 for (
int dir = 0; dir <
BND_COUNT; ++dir) {
211 neighbours_[dir] =
NULL;
212 good_stroke_neighbours_[dir] =
false;
221 for (
int dir = 0; dir <
BND_COUNT; ++dir) {
232 for (
int dir = 0; dir <
BND_COUNT; ++dir) {
247 int box_perimeter = 2 * (box.
height() + box.
width());
258 perimeter -= 4 *
cblob()->
area() / perimeter;
259 perimeter -= 2 * box.
width();
273 perimeter -= 4 *
cblob()->
area() / perimeter;
274 perimeter -= 2 * box.
height();
286 if (box.
left() < other.box.
left() && box.
left() < other.left_rule_)
288 if (other.box.
left() < box.
left() && other.box.
left() < left_rule_)
290 if (box.
right() > other.box.
right() && box.
right() > other.right_rule_)
292 if (other.box.
right() > box.
right() && other.box.
right() > right_rule_)
299 double fractional_tolerance,
300 double constant_tolerance)
const {
305 float h_tolerance = horz_stroke_width_ * fractional_tolerance
306 + constant_tolerance;
307 float v_tolerance = vert_stroke_width_ * fractional_tolerance
308 + constant_tolerance;
309 double p_tolerance = p_width * fractional_tolerance
310 + constant_tolerance;
311 bool h_zero = horz_stroke_width_ == 0.0f || other.horz_stroke_width_ == 0.0f;
312 bool v_zero = vert_stroke_width_ == 0.0f || other.vert_stroke_width_ == 0.0f;
313 bool h_ok = !h_zero &&
NearlyEqual(horz_stroke_width_,
314 other.horz_stroke_width_, h_tolerance);
315 bool v_ok = !v_zero &&
NearlyEqual(vert_stroke_width_,
316 other.vert_stroke_width_, v_tolerance);
317 bool p_ok = h_zero && v_zero &&
NearlyEqual(p_width, n_p_width, p_tolerance);
321 return p_ok || ((v_ok || h_ok) && (h_ok || h_zero) && (v_ok || v_zero));
327 FCOORD no_rotation(1.0f, 0.0f);
328 float top = box.
top();
329 float bottom = box.
bottom();
330 if (cblob_ptr !=
NULL) {
332 static_cast<float>(right), no_rotation,
340 FCOORD bot_left(left, bottom);
341 FCOORD top_right(right, top);
342 TBOX shrunken_box(bot_left);
343 TBOX shrunken_box2(top_right);
344 shrunken_box += shrunken_box2;
351 baseline_y_ = box.
bottom();
352 if (cblob_ptr ==
NULL)
return;
358 BLOBNBOX_IT blob_it(blobs);
359 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
360 blob_it.data()->CleanNeighbours();
366 BLOBNBOX_IT blob_it(blobs);
367 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
370 delete blob->
cblob();
371 delete blob_it.extract();
379 BLOBNBOX_LIST* blobs) {
382 int scale_factor = 1;
383 if (thresholds !=
NULL && grey !=
NULL) {
384 grey_height = pixGetHeight(grey);
385 thr_height = pixGetHeight(thresholds);
389 BLOBNBOX_IT blob_it(blobs);
390 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
394 l_uint32 threshold = 128;
395 if (thresholds !=
NULL && grey !=
NULL) {
400 pixGetPixel(thresholds, pt.
x / scale_factor,
401 thr_height - 1 - pt.
y / scale_factor, &threshold);
409 #ifndef GRAPHICS_DISABLED
416 BLOBNBOX_IT it(list);
417 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
418 it.data()->plot(win, body_colour, child_colour);
429 BLOBNBOX_IT it(list);
430 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
433 blob->
plot(win, body_colour, child_colour);
439 switch (region_type) {
481 if (cblob_ptr !=
NULL)
482 cblob_ptr->
plot(window, blob_colour, child_colour);
504 C_OUTLINE_IT out_it = blob->
out_list ();
508 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
509 outline = out_it.data ();
512 for (stepindex = 0; stepindex < outline->
pathlength (); stepindex++) {
514 if (pos.
x () >= leftx && pos.
x () <= rightx) {
517 vec = outline->
step (stepindex);
543 C_OUTLINE_IT out_it = blob->
out_list ();
547 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
548 outline = out_it.data ();
550 for (stepindex = 0; stepindex < outline->
pathlength (); stepindex++) {
552 if (pos.
x () >= leftx && pos.
x () <= rightx) {
555 vec = outline->
step (stepindex);
580 C_OUTLINE_IT out_it = blob->
out_list ();
584 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
585 outline = out_it.data ();
587 for (stepindex = 0; stepindex < outline->
pathlength (); stepindex++) {
589 if (pos.
y () >= bottomy && pos.
y () <= topy) {
592 vec = outline->
step (stepindex);
608 C_OUTLINE_LIST out_list;
610 C_OUTLINE_IT in_it = blob->
out_list ();
612 C_OUTLINE_IT out_it = &out_list;
614 for (in_it.mark_cycle_pt (); !in_it.cycled_list (); in_it.forward ()) {
615 out_it.add_after_then_move (
new C_OUTLINE (in_it.data (), rotation));
617 return new C_BLOB (&out_list);
691 initial_y_min = bottom;
694 BLOBNBOX_IT it = &blobs;
696 it.add_to_end (blob);
697 diff = top - bottom - row_size;
703 else if ((top - bottom) * 3 < row_size) {
704 diff = row_size / 3 + bottom - top;
711 tprintf(
"pitch=%d, fp=%g, fps=%g, fpns=%g, prs=%g, prns=%g,"
712 " spacing=%g xh=%g y_origin=%g xev=%d, asc=%g, desc=%g,"
713 " body=%g, minsp=%d maxnsp=%d, thr=%d kern=%g sp=%g\n",
734 BLOBNBOX_IT it = &blobs;
736 it.add_to_end (blob);
737 allowed = row_size + y_min - y_max;
739 available = top > y_max ? top - y_max : 0;
742 available += y_min - bottom;
744 available += available;
745 if (available < allowed)
748 y_min -= (y_min - bottom) * allowed / available;
750 y_max += (top - y_max) * allowed / available;
765 BLOBNBOX_IT it = &blobs;
768 it.add_before_then_move (blob);
771 while (!it.cycled_list ()
772 && it.data ()->bounding_box ().left () <=
775 if (it.cycled_list ())
776 it.add_to_end (blob);
778 it.add_before_stay_put (blob);
795 if (blob_it.empty ())
797 row_box = blob_it.data ()->bounding_box ();
798 for (blob_it.mark_cycle_pt (); !blob_it.cycled_list (); blob_it.forward ())
799 row_box += blob_it.data ()->bounding_box ();
805 for (blob_it.mark_cycle_pt (); !blob_it.cycled_list (); blob_it.forward ()) {
806 blob = blob_it.data();
818 void TO_ROW::clear() {
850 num_repeated_sets_ = -1;
866 C_OUTLINE_IT out_it = blob->
out_list ();
868 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
889 C_OUTLINE_IT out_it = outline->
child ();
893 for (stepindex = 0; stepindex < length; stepindex++) {
894 step = outline->
step (stepindex);
896 stats->
add (pos.
x (), -pos.
y ());
897 }
else if (step.
x () < 0) {
898 stats->
add (pos.
x () - 1, pos.
y ());
903 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
922 static void clear_blobnboxes(BLOBNBOX_LIST* boxes) {
923 BLOBNBOX_IT it = boxes;
926 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
961 clear_blobnboxes(&
blobs);
973 static void SizeFilterBlobs(
int min_height,
int max_height,
974 BLOBNBOX_LIST* src_list,
975 BLOBNBOX_LIST* noise_list,
976 BLOBNBOX_LIST* small_list,
977 BLOBNBOX_LIST* medium_list,
978 BLOBNBOX_LIST* large_list) {
979 BLOBNBOX_IT noise_it(noise_list);
980 BLOBNBOX_IT small_it(small_list);
981 BLOBNBOX_IT medium_it(medium_list);
982 BLOBNBOX_IT large_it(large_list);
983 for (BLOBNBOX_IT src_it(src_list); !src_it.empty(); src_it.forward()) {
988 if (height < min_height &&
989 (width < min_height || width > max_height))
990 noise_it.add_after_then_move(blob);
991 else if (height > max_height)
992 large_it.add_after_then_move(blob);
993 else if (height < min_height)
994 small_it.add_after_then_move(blob);
996 medium_it.add_after_then_move(blob);
1008 BLOBNBOX_LIST noise_list;
1009 BLOBNBOX_LIST small_list;
1010 BLOBNBOX_LIST medium_list;
1011 BLOBNBOX_LIST large_list;
1012 SizeFilterBlobs(min_height, max_height, &
blobs,
1013 &noise_list, &small_list, &medium_list, &large_list);
1014 SizeFilterBlobs(min_height, max_height, &
large_blobs,
1015 &noise_list, &small_list, &medium_list, &large_list);
1016 SizeFilterBlobs(min_height, max_height, &
small_blobs,
1017 &noise_list, &small_list, &medium_list, &large_list);
1018 SizeFilterBlobs(min_height, max_height, &
noise_blobs,
1019 &noise_list, &small_list, &medium_list, &large_list);
1020 BLOBNBOX_IT blob_it(&
blobs);
1021 blob_it.add_list_after(&medium_list);
1023 blob_it.add_list_after(&large_list);
1025 blob_it.add_list_after(&small_list);
1027 blob_it.add_list_after(&noise_list);
1055 #ifndef GRAPHICS_DISABLED
1081 BLOBNBOX_LIST *list,
1084 BLOBNBOX_IT it = list;
1085 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1086 it.data()->plot(win, body_colour, child_colour);
1089 #endif // GRAPHICS_DISABLED
void MinMaxGapsClipped(int *h_min, int *h_max, int *v_min, int *v_max) const
#define PROJECTION_MARGIN
inT16 EstimateBaselinePosition()
PITCH_TYPE pitch_decision
static ScrollView::Color TextlineColor(BlobRegionType region_type, BlobTextFlowType flow_type)
static void PlotNoiseBlobs(BLOBNBOX_LIST *list, ScrollView::Color body_colour, ScrollView::Color child_colour, ScrollView *win)
void vertical_cblob_projection(C_BLOB *blob, STATS *stats)
bool joined_to_prev() const
int NoisyNeighbours() const
const double kDefiniteAspectRatio
C_BLOB * crotate_cblob(C_BLOB *blob, FCOORD rotation)
void vertical_coutline_projection(C_OUTLINE *outline, STATS *stats)
const ICOORD & start_pos() const
#define ELIST2IZE(CLASSNAME)
void add(inT32 value, inT32 count)
void ReSetAndReFilterBlobs()
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
void reflect_box_in_y_axis()
BLOBNBOX_LIST * blob_list()
const double kMinMediumSizeRatio
void plot_blob_list(ScrollView *win, BLOBNBOX_LIST *list, ScrollView::Color body_colour, ScrollView::Color child_colour)
void NeighbourGaps(int gaps[BND_COUNT]) const
const double kMaxMediumSizeRatio
void find_cblob_hlimits(C_BLOB *blob, float bottomy, float topy, float &xmin, float &xmax)
BLOBNBOX_LIST small_blobs
C_OUTLINE_LIST * out_list()
void add_blob(BLOBNBOX *blob, float top, float bottom, float row_size)
void rotate(const FCOORD &vec)
bool DeletableNoise() const
void DeleteUnownedNoise()
void EstimateBaselinePosition()
inT16 y() const
access_function
bool NearlyEqual(T x, T y, T tolerance)
void ComputeEdgeOffsets(Pix *thresholds, Pix *grey)
TBOX BoundsWithinLimits(int left, int right)
float area_stroke_width() const
int y_gap(const TBOX &box) const
static void DeleteNoiseBlobs(BLOBNBOX_LIST *blobs)
BlobRegionType region_type() const
void chop(BLOBNBOX_IT *start_it, BLOBNBOX_IT *blob_it, FCOORD rotation, float xheight)
static void ComputeEdgeOffsets(Pix *thresholds, Pix *grey, BLOBNBOX_LIST *blobs)
BLOBNBOX_LIST noise_blobs
bool DefiniteIndividualFlow()
const double kComplexShapePerimeterRatio
void compute_vertical_projection()
void really_merge(BLOBNBOX *other)
void compute_bounding_box()
void ComputeEdgeOffsets(int threshold, Pix *pix)
void set_diacritic_box(const TBOX &diacritic_box)
bool ConfirmNoTabViolation(const BLOBNBOX &other) const
void find_cblob_limits(C_BLOB *blob, float leftx, float rightx, FCOORD rotation, float &ymin, float &ymax)
void rotate_box(FCOORD rotation)
TBOX box_next(BLOBNBOX_IT *it)
void plot_graded_blobs(ScrollView *to_win)
bool good_stroke_neighbour(BlobNeighbourDir n) const
TBOX box_next_pre_chopped(BLOBNBOX_IT *it)
void set_horz_possible(bool value)
PITCH_TYPE pitch_decision
int x_gap(const TBOX &box) const
float horz_stroke_width() const
int IntCastRounded(double x)
BLOBNBOX * neighbour(BlobNeighbourDir n) const
inT16 x() const
access function
void set_vert_possible(bool value)
bool set_range(inT32 min_bucket_value, inT32 max_bucket_value_plus_1)
ELISTIZE(BLOBNBOX) ELIST2IZE(TO_ROW) ELISTIZE(TO_BLOCK) const double kCosSmallAngle=0.866
void insert_blob(BLOBNBOX *blob)
float vert_stroke_width() const
TBOX bounding_box() const
ICOORD step(int index) const
void plot(ScrollView *window, ScrollView::Color blob_colour, ScrollView::Color child_colour)
bool MatchingStrokeWidth(const BLOBNBOX &other, double fractional_tolerance, double constant_tolerance) const
const TBOX & bounding_box() const
void find_cblob_vlimits(C_BLOB *blob, float leftx, float rightx, float &ymin, float &ymax)
void rotate(FCOORD rotation)
void plot_noise_blobs(ScrollView *to_win)
BLOBNBOX_LIST large_blobs
void merge(BLOBNBOX *nextblob)
ScrollView::Color BoxColor() const
static void PlotBlobs(BLOBNBOX_LIST *list, ScrollView::Color body_colour, ScrollView::Color child_colour, ScrollView *win)
void plot(ScrollView *window, ScrollView::Color blob_colour, ScrollView::Color child_colour)
void rotate(const FCOORD &vec)