tesseract  4.00.00dev
oldbasel.cpp File Reference
#include "ccstruct.h"
#include "statistc.h"
#include "quadlsq.h"
#include "detlinefit.h"
#include "makerow.h"
#include "drawtord.h"
#include "oldbasel.h"
#include "textord.h"
#include "tprintf.h"

Go to the source code of this file.

Namespaces

 tesseract
 

Macros

#define EXTERN
 
#define TURNLIMIT   1 /*min size for turning point */
 
#define X_HEIGHT_FRACTION   0.7 /*x-height/caps height */
 
#define DESCENDER_FRACTION   0.5 /*descender/x-height */
 
#define MIN_ASC_FRACTION   0.20 /*min size of ascenders */
 
#define MIN_DESC_FRACTION   0.25 /*min size of descenders */
 
#define MINASCRISE   2.0 /*min ascender/desc step */
 
#define MAXHEIGHTVARIANCE   0.15 /*accepted variation in x-height */
 
#define MAXHEIGHT   300 /*max blob height */
 
#define MAXOVERLAP   0.1 /*max 10% missed overlap */
 
#define MAXBADRUN   2 /*max non best for failed */
 
#define HEIGHTBUCKETS   200 /* Num of buckets */
 
#define DELTAHEIGHT   5.0 /* Small amount of diff */
 
#define GOODHEIGHT   5
 
#define MAXLOOPS   10
 
#define MODENUM   10
 
#define MAXPARTS   6
 
#define SPLINESIZE   23
 
#define ABS(x)   ((x)<0 ? (-(x)) : (x))
 

Functions

int get_blob_coords (TO_ROW *row, inT32 lineheight, TBOX *blobcoords, BOOL8 &holed_line, int &outcount)
 
void make_first_baseline (TBOX blobcoords[], int blobcount, int xcoords[], int ycoords[], QSPLINE *spline, QSPLINE *baseline, float jumplimit)
 
void make_holed_baseline (TBOX blobcoords[], int blobcount, QSPLINE *spline, QSPLINE *baseline, float gradient)
 
int partition_line (TBOX blobcoords[], int blobcount, int *numparts, char partids[], int partsizes[], QSPLINE *spline, float jumplimit, float ydiffs[])
 
void merge_oldbl_parts (TBOX blobcoords[], int blobcount, char partids[], int partsizes[], int biggestpart, float jumplimit)
 
int get_ydiffs (TBOX blobcoords[], int blobcount, QSPLINE *spline, float ydiffs[])
 
int choose_partition (register float diff, float partdiffs[], int lastpart, float jumplimit, float *drift, float *lastdelta, int *partcount)
 
int partition_coords (TBOX blobcoords[], int blobcount, char partids[], int bestpart, int xcoords[], int ycoords[])
 
int segment_spline (TBOX blobcoords[], int blobcount, int xcoords[], int ycoords[], int degree, int pointcount, int xstarts[])
 
BOOL8 split_stepped_spline (QSPLINE *baseline, float jumplimit, int xcoords[], int xstarts[], int &segments)
 
void insert_spline_point (int xstarts[], int segment, int coord1, int coord2, int &segments)
 
void find_lesser_parts (TO_ROW *row, TBOX blobcoords[], int blobcount, char partids[], int partsizes[], int partcount, int bestpart)
 
void old_first_xheight (TO_ROW *row, TBOX blobcoords[], int initialheight, int blobcount, QSPLINE *baseline, float jumplimit)
 
void make_first_xheight (TO_ROW *row, TBOX blobcoords[], int lineheight, int init_lineheight, int blobcount, QSPLINE *baseline, float jumplimit)
 
void find_top_modes (STATS *stats, int statnum, int modelist[], int modenum)
 
void pick_x_height (TO_ROW *row, int modelist[], int lefts[], int rights[], STATS *heightstat, int mode_threshold)
 

Variables

EXTERN bool textord_really_old_xheight = FALSE
 
EXTERN bool textord_oldbl_debug = FALSE
 
EXTERN bool textord_debug_baselines = FALSE
 
EXTERN bool textord_oldbl_paradef = TRUE
 
EXTERN bool textord_oldbl_split_splines = TRUE
 
EXTERN bool textord_oldbl_merge_parts = TRUE
 
EXTERN bool oldbl_corrfix = TRUE
 
EXTERN bool oldbl_xhfix = FALSE
 
EXTERN bool textord_ocropus_mode = FALSE
 
EXTERN double oldbl_xhfract = 0.4
 
EXTERN int oldbl_holed_losscount = 10
 
EXTERN double oldbl_dot_error_size = 1.26
 
EXTERN double textord_oldbl_jumplimit = 0.15
 
const int kMinModeFactorOcropus = 32
 
const int kMinModeFactor = 12
 

Macro Definition Documentation

◆ ABS

#define ABS (   x)    ((x)<0 ? (-(x)) : (x))

Definition at line 73 of file oldbasel.cpp.

◆ DELTAHEIGHT

#define DELTAHEIGHT   5.0 /* Small amount of diff */

Definition at line 66 of file oldbasel.cpp.

◆ DESCENDER_FRACTION

#define DESCENDER_FRACTION   0.5 /*descender/x-height */

Definition at line 57 of file oldbasel.cpp.

◆ EXTERN

#define EXTERN

Definition at line 35 of file oldbasel.cpp.

◆ GOODHEIGHT

#define GOODHEIGHT   5

Definition at line 67 of file oldbasel.cpp.

◆ HEIGHTBUCKETS

#define HEIGHTBUCKETS   200 /* Num of buckets */

Definition at line 65 of file oldbasel.cpp.

◆ MAXBADRUN

#define MAXBADRUN   2 /*max non best for failed */

Definition at line 64 of file oldbasel.cpp.

◆ MAXHEIGHT

#define MAXHEIGHT   300 /*max blob height */

Definition at line 62 of file oldbasel.cpp.

◆ MAXHEIGHTVARIANCE

#define MAXHEIGHTVARIANCE   0.15 /*accepted variation in x-height */

Definition at line 61 of file oldbasel.cpp.

◆ MAXLOOPS

#define MAXLOOPS   10

Definition at line 68 of file oldbasel.cpp.

◆ MAXOVERLAP

#define MAXOVERLAP   0.1 /*max 10% missed overlap */

Definition at line 63 of file oldbasel.cpp.

◆ MAXPARTS

#define MAXPARTS   6

Definition at line 70 of file oldbasel.cpp.

◆ MIN_ASC_FRACTION

#define MIN_ASC_FRACTION   0.20 /*min size of ascenders */

Definition at line 58 of file oldbasel.cpp.

◆ MIN_DESC_FRACTION

#define MIN_DESC_FRACTION   0.25 /*min size of descenders */

Definition at line 59 of file oldbasel.cpp.

◆ MINASCRISE

#define MINASCRISE   2.0 /*min ascender/desc step */

Definition at line 60 of file oldbasel.cpp.

◆ MODENUM

#define MODENUM   10

Definition at line 69 of file oldbasel.cpp.

◆ SPLINESIZE

#define SPLINESIZE   23

Definition at line 71 of file oldbasel.cpp.

◆ TURNLIMIT

#define TURNLIMIT   1 /*min size for turning point */

Definition at line 55 of file oldbasel.cpp.

◆ X_HEIGHT_FRACTION

#define X_HEIGHT_FRACTION   0.7 /*x-height/caps height */

Definition at line 56 of file oldbasel.cpp.

Function Documentation

◆ choose_partition()

int choose_partition ( register float  diff,
float  partdiffs[],
int  lastpart,
float  jumplimit,
float *  drift,
float *  lastdelta,
int partcount 
)

Definition at line 957 of file oldbasel.cpp.

965  {
966  int partition; /*partition no */
967  int bestpart; /*best new partition */
968  float bestdelta; /*best gap from a part */
969  float delta; /*diff from part */
970 
971  if (lastpart < 0) {
972  partdiffs[0] = diff;
973  lastpart = 0; /*first point */
974  *drift = 0.0f;
975  *lastdelta = 0.0f;
976  }
977  /*adjusted diff from part */
978  delta = diff - partdiffs[lastpart] - *drift;
979  if (textord_oldbl_debug) {
980  tprintf ("Diff=%.2f, Delta=%.3f, Drift=%.3f, ", diff, delta, *drift);
981  }
982  if (ABS (delta) > jumplimit / 2) {
983  /*delta on part 0 */
984  bestdelta = diff - partdiffs[0] - *drift;
985  bestpart = 0; /*0 best so far */
986  for (partition = 1; partition < *partcount; partition++) {
987  delta = diff - partdiffs[partition] - *drift;
988  if (ABS (delta) < ABS (bestdelta)) {
989  bestdelta = delta;
990  bestpart = partition; /*part with nearest jump */
991  }
992  }
993  delta = bestdelta;
994  /*too far away */
995  if (ABS (bestdelta) > jumplimit
996  && *partcount < MAXPARTS) { /*and spare part left */
997  bestpart = (*partcount)++; /*best was new one */
998  /*start new one */
999  partdiffs[bestpart] = diff - *drift;
1000  delta = 0.0f;
1001  }
1002  }
1003  else {
1004  bestpart = lastpart; /*best was last one */
1005  }
1006 
1007  if (bestpart == lastpart
1008  && (ABS (delta - *lastdelta) < jumplimit / 2
1009  || ABS (delta) < jumplimit / 2))
1010  /*smooth the drift */
1011  *drift = (3 * *drift + delta) / 3;
1012  *lastdelta = delta;
1013 
1014  if (textord_oldbl_debug) {
1015  tprintf ("P=%d\n", bestpart);
1016  }
1017 
1018  return bestpart;
1019 }
EXTERN bool textord_oldbl_debug
Definition: oldbasel.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
#define MAXPARTS
Definition: oldbasel.cpp:70
#define ABS(x)
Definition: oldbasel.cpp:73

◆ find_lesser_parts()

void find_lesser_parts ( TO_ROW row,
TBOX  blobcoords[],
int  blobcount,
char  partids[],
int  partsizes[],
int  partcount,
int  bestpart 
)

Definition at line 1316 of file oldbasel.cpp.

1324  {
1325  int blobindex; /*index of blob */
1326  int partition; /*current partition */
1327  int xcentre; /*centre of blob */
1328  int poscount; /*count of best up step */
1329  int negcount; /*count of best down step */
1330  float partsteps[MAXPARTS]; /*average step to part */
1331  float bestneg; /*best down step */
1332  int runlength; /*length of bad run */
1333  int biggestrun; /*biggest bad run */
1334 
1335  biggestrun = 0;
1336  for (partition = 0; partition < partcount; partition++)
1337  partsteps[partition] = 0.0; /*zero accumulators */
1338  for (runlength = 0, blobindex = 0; blobindex < blobcount; blobindex++) {
1339  xcentre = (blobcoords[blobindex].left ()
1340  + blobcoords[blobindex].right ()) >> 1;
1341  /*in other parts */
1342  int part_id =
1343  static_cast<int>(static_cast<unsigned char>(partids[blobindex]));
1344  if (part_id != bestpart) {
1345  runlength++; /*run of non bests */
1346  if (runlength > biggestrun)
1347  biggestrun = runlength;
1348  partsteps[part_id] += blobcoords[blobindex].bottom()
1349  - row->baseline.y(xcentre);
1350  }
1351  else
1352  runlength = 0;
1353  }
1354  if (biggestrun > MAXBADRUN)
1355  row->xheight = -1.0f; /*failed */
1356  else
1357  row->xheight = 1.0f; /*success */
1358  poscount = negcount = 0;
1359  bestneg = 0.0; /*no step yet */
1360  for (partition = 0; partition < partcount; partition++) {
1361  if (partition != bestpart) {
1362  // by jetsoft divide by zero possible
1363  if (partsizes[partition] == 0)
1364  partsteps[partition] = 0;
1365  else
1366  partsteps[partition] /= partsizes[partition];
1367  //
1368 
1369  if (partsteps[partition] >= MINASCRISE
1370  && partsizes[partition] > poscount) {
1371  poscount = partsizes[partition];
1372  }
1373  if (partsteps[partition] <= -MINASCRISE
1374  && partsizes[partition] > negcount) {
1375  /*ascender rise */
1376  bestneg = partsteps[partition];
1377  /*2nd most popular */
1378  negcount = partsizes[partition];
1379  }
1380  }
1381  }
1382  /*average x-height */
1383  partsteps[bestpart] /= blobcount;
1384  row->descdrop = bestneg;
1385 }
float xheight
Definition: blobbox.h:653
float descdrop
Definition: blobbox.h:656
#define MAXPARTS
Definition: oldbasel.cpp:70
inT16 left() const
Definition: rect.h:68
QSPLINE baseline
Definition: blobbox.h:666
double y(double x) const
Definition: quspline.cpp:217
#define MAXBADRUN
Definition: oldbasel.cpp:64
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61
#define MINASCRISE
Definition: oldbasel.cpp:60

◆ find_top_modes()

void find_top_modes ( STATS stats,
int  statnum,
int  modelist[],
int  modenum 
)

Definition at line 1567 of file oldbasel.cpp.

1571  {
1572  int mode_count;
1573  int last_i = 0;
1574  int last_max = MAX_INT32;
1575  int i;
1576  int mode;
1577  int total_max = 0;
1578  int mode_factor = textord_ocropus_mode ?
1580 
1581  for (mode_count = 0; mode_count < modenum; mode_count++) {
1582  mode = 0;
1583  for (i = 0; i < statnum; i++) {
1584  if (stats->pile_count (i) > stats->pile_count (mode)) {
1585  if ((stats->pile_count (i) < last_max) ||
1586  ((stats->pile_count (i) == last_max) && (i > last_i))) {
1587  mode = i;
1588  }
1589  }
1590  }
1591  last_i = mode;
1592  last_max = stats->pile_count (last_i);
1593  total_max += last_max;
1594  if (last_max <= total_max / mode_factor)
1595  mode = 0;
1596  modelist[mode_count] = mode;
1597  }
1598 }
const int kMinModeFactor
Definition: oldbasel.cpp:1564
#define MAX_INT32
Definition: host.h:62
EXTERN bool textord_ocropus_mode
Definition: oldbasel.cpp:47
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
const char int mode
Definition: ioapi.h:38
const int kMinModeFactorOcropus
Definition: oldbasel.cpp:1563

◆ get_blob_coords()

int get_blob_coords ( TO_ROW row,
inT32  lineheight,
TBOX blobcoords,
BOOL8 holed_line,
int outcount 
)

Definition at line 441 of file oldbasel.cpp.

447  {
448  //blobs
449  BLOBNBOX_IT blob_it = row->blob_list ();
450  int blobindex; /*no along text line */
451  int losscount; //lost blobs
452  int maxlosscount; //greatest lost blobs
453  /*height stat collection */
454  STATS heightstat (0, MAXHEIGHT);
455 
456  if (blob_it.empty ())
457  return 0; //none
458  maxlosscount = 0;
459  losscount = 0;
460  blob_it.mark_cycle_pt ();
461  blobindex = 0;
462  do {
463  blobcoords[blobindex] = box_next_pre_chopped (&blob_it);
464  if (blobcoords[blobindex].height () > lineheight * 0.25)
465  heightstat.add (blobcoords[blobindex].height (), 1);
466  if (blobindex == 0
467  || blobcoords[blobindex].height () > lineheight * 0.25
468  || blob_it.cycled_list ()) {
469  blobindex++; /*no of merged blobs */
470  losscount = 0;
471  }
472  else {
473  if (blobcoords[blobindex].height ()
474  < blobcoords[blobindex].width () * oldbl_dot_error_size
475  && blobcoords[blobindex].width ()
476  < blobcoords[blobindex].height () * oldbl_dot_error_size) {
477  //counts as dot
478  blobindex++;
479  losscount = 0;
480  }
481  else {
482  losscount++; //lost it
483  if (losscount > maxlosscount)
484  //remember max
485  maxlosscount = losscount;
486  }
487  }
488  }
489  while (!blob_it.cycled_list ());
490 
491  holed_line = maxlosscount > oldbl_holed_losscount;
492  outcount = blobindex; /*total blobs */
493 
494  if (heightstat.get_total () > 1)
495  /*guess x-height */
496  return (int) heightstat.ile (0.25);
497  else
498  return blobcoords[0].height ();
499 }
#define MAXHEIGHT
Definition: oldbasel.cpp:62
TBOX box_next_pre_chopped(BLOBNBOX_IT *it)
Definition: blobbox.cpp:660
inT16 height() const
Definition: rect.h:104
EXTERN double oldbl_dot_error_size
Definition: oldbasel.cpp:51
inT16 width() const
Definition: rect.h:111
Definition: statistc.h:33
BLOBNBOX_LIST * blob_list()
Definition: blobbox.h:595
EXTERN int oldbl_holed_losscount
Definition: oldbasel.cpp:50

◆ get_ydiffs()

int get_ydiffs ( TBOX  blobcoords[],
int  blobcount,
QSPLINE spline,
float  ydiffs[] 
)

Definition at line 906 of file oldbasel.cpp.

911  {
912  int blobindex; /*current blob */
913  int xcentre; /*xcoord */
914  int lastx; /*last xcentre */
915  float diffsum; /*sum of diffs */
916  float diff; /*current difference */
917  float drift; /*sum of spline steps */
918  float bestsum; /*smallest diffsum */
919  int bestindex; /*index of bestsum */
920 
921  diffsum = 0.0f;
922  bestindex = 0;
923  bestsum = (float) MAX_INT32;
924  drift = 0.0f;
925  lastx = blobcoords[0].left ();
926  /*do each blob in row */
927  for (blobindex = 0; blobindex < blobcount; blobindex++) {
928  /*centre of blob */
929  xcentre = (blobcoords[blobindex].left () + blobcoords[blobindex].right ()) >> 1;
930  //step functions in spline
931  drift += spline->step (lastx, xcentre);
932  lastx = xcentre;
933  diff = blobcoords[blobindex].bottom ();
934  diff -= spline->y (xcentre);
935  diff += drift;
936  ydiffs[blobindex] = diff; /*store difference */
937  if (blobindex > 2)
938  /*remove old one */
939  diffsum -= ABS (ydiffs[blobindex - 3]);
940  diffsum += ABS (diff); /*add new one */
941  if (blobindex >= 2 && diffsum < bestsum) {
942  bestsum = diffsum; /*find min sum */
943  bestindex = blobindex - 1; /*middle of set */
944  }
945  }
946  return bestindex;
947 }
#define MAX_INT32
Definition: host.h:62
inT16 left() const
Definition: rect.h:68
double y(double x) const
Definition: quspline.cpp:217
double step(double x1, double x2)
Definition: quspline.cpp:192
#define ABS(x)
Definition: oldbasel.cpp:73
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61

◆ insert_spline_point()

void insert_spline_point ( int  xstarts[],
int  segment,
int  coord1,
int  coord2,
int segments 
)

Definition at line 1292 of file oldbasel.cpp.

1297  {
1298  int index; //for shuffling
1299 
1300  for (index = segments; index > segment; index--)
1301  xstarts[index + 1] = xstarts[index];
1302  segments++;
1303  xstarts[segment] = coord1;
1304  xstarts[segment + 1] = coord2;
1305 }

◆ make_first_baseline()

void make_first_baseline ( TBOX  blobcoords[],
int  blobcount,
int  xcoords[],
int  ycoords[],
QSPLINE spline,
QSPLINE baseline,
float  jumplimit 
)

Definition at line 511 of file oldbasel.cpp.

519  {
520  int leftedge; /*left edge of line */
521  int rightedge; /*right edge of line */
522  int blobindex; /*current blob */
523  int segment; /*current segment */
524  float prevy, thisy, nexty; /*3 y coords */
525  float y1, y2, y3; /*3 smooth blobs */
526  float maxmax, minmin; /*absolute limits */
527  int x2 = 0; /*right edge of old y3 */
528  int ycount; /*no of ycoords in use */
529  float yturns[SPLINESIZE]; /*y coords of turn pts */
530  int xturns[SPLINESIZE]; /*xcoords of turn pts */
531  int xstarts[SPLINESIZE + 1];
532  int segments; //no of segments
533  ICOORD shift; //shift of spline
534 
535  prevy = 0;
536  /*left edge of row */
537  leftedge = blobcoords[0].left ();
538  /*right edge of line */
539  rightedge = blobcoords[blobcount - 1].right ();
540  if (spline == NULL /*no given spline */
541  || spline->segments < 3 /*or trivial */
542  /*or too non-overlap */
543  || spline->xcoords[1] > leftedge + MAXOVERLAP * (rightedge - leftedge)
544  || spline->xcoords[spline->segments - 1] < rightedge
545  - MAXOVERLAP * (rightedge - leftedge)) {
547  return; //use default
548  xstarts[0] = blobcoords[0].left () - 1;
549  for (blobindex = 0; blobindex < blobcount; blobindex++) {
550  xcoords[blobindex] = (blobcoords[blobindex].left ()
551  + blobcoords[blobindex].right ()) / 2;
552  ycoords[blobindex] = blobcoords[blobindex].bottom ();
553  }
554  xstarts[1] = blobcoords[blobcount - 1].right () + 1;
555  segments = 1; /*no of segments */
556 
557  /*linear */
558  *baseline = QSPLINE (xstarts, segments, xcoords, ycoords, blobcount, 1);
559 
560  if (blobcount >= 3) {
561  y1 = y2 = y3 = 0.0f;
562  ycount = 0;
563  segment = 0; /*no of segments */
564  maxmax = minmin = 0.0f;
565  thisy = ycoords[0] - baseline->y (xcoords[0]);
566  nexty = ycoords[1] - baseline->y (xcoords[1]);
567  for (blobindex = 2; blobindex < blobcount; blobindex++) {
568  prevy = thisy; /*shift ycoords */
569  thisy = nexty;
570  nexty = ycoords[blobindex] - baseline->y (xcoords[blobindex]);
571  /*middle of smooth y */
572  if (ABS (thisy - prevy) < jumplimit && ABS (thisy - nexty) < jumplimit) {
573  y1 = y2; /*shift window */
574  y2 = y3;
575  y3 = thisy; /*middle point */
576  ycount++;
577  /*local max */
578  if (ycount >= 3 && ((y1 < y2 && y2 >= y3)
579  /*local min */
580  || (y1 > y2 && y2 <= y3))) {
581  if (segment < SPLINESIZE - 2) {
582  /*turning pt */
583  xturns[segment] = x2;
584  yturns[segment] = y2;
585  segment++; /*no of spline segs */
586  }
587  }
588  if (ycount == 1) {
589  maxmax = minmin = y3;/*initialise limits */
590  }
591  else {
592  if (y3 > maxmax)
593  maxmax = y3; /*biggest max */
594  if (y3 < minmin)
595  minmin = y3; /*smallest min */
596  }
597  /*possible turning pt */
598  x2 = blobcoords[blobindex - 1].right ();
599  }
600  }
601 
602  jumplimit *= 1.2;
603  /*must be wavy */
604  if (maxmax - minmin > jumplimit) {
605  ycount = segment; /*no of segments */
606  for (blobindex = 0, segment = 1; blobindex < ycount;
607  blobindex++) {
608  if (yturns[blobindex] > minmin + jumplimit
609  || yturns[blobindex] < maxmax - jumplimit) {
610  /*significant peak */
611  if (segment == 1
612  || yturns[blobindex] > prevy + jumplimit
613  || yturns[blobindex] < prevy - jumplimit) {
614  /*different to previous */
615  xstarts[segment] = xturns[blobindex];
616  segment++;
617  prevy = yturns[blobindex];
618  }
619  /*bigger max */
620  else if ((prevy > minmin + jumplimit && yturns[blobindex] > prevy)
621  /*smaller min */
622  || (prevy < maxmax - jumplimit && yturns[blobindex] < prevy)) {
623  xstarts[segment - 1] = xturns[blobindex];
624  /*improved previous */
625  prevy = yturns[blobindex];
626  }
627  }
628  }
629  xstarts[segment] = blobcoords[blobcount - 1].right () + 1;
630  segments = segment; /*no of segments */
631  /*linear */
632  *baseline = QSPLINE (xstarts, segments, xcoords, ycoords, blobcount, 1);
633  }
634  }
635  }
636  else {
637  *baseline = *spline; /*copy it */
638  shift = ICOORD (0, (inT16) (blobcoords[0].bottom ()
639  - spline->y (blobcoords[0].right ())));
640  baseline->move (shift);
641  }
642 }
void move(ICOORD vec)
Definition: quspline.cpp:259
int16_t inT16
Definition: host.h:36
inT16 left() const
Definition: rect.h:68
#define MAXOVERLAP
Definition: oldbasel.cpp:63
double y(double x) const
Definition: quspline.cpp:217
#define ABS(x)
Definition: oldbasel.cpp:73
#define SPLINESIZE
Definition: oldbasel.cpp:71
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61
EXTERN bool textord_oldbl_paradef
Definition: oldbasel.cpp:41
integer coordinate
Definition: points.h:30

◆ make_first_xheight()

void make_first_xheight ( TO_ROW row,
TBOX  blobcoords[],
int  lineheight,
int  init_lineheight,
int  blobcount,
QSPLINE baseline,
float  jumplimit 
)

Definition at line 1482 of file oldbasel.cpp.

1490  {
1491  STATS heightstat (0, HEIGHTBUCKETS);
1492  int lefts[HEIGHTBUCKETS];
1493  int rights[HEIGHTBUCKETS];
1494  int modelist[MODENUM];
1495  int blobindex;
1496  int mode_count; //blobs to count in thr
1497  int sign_bit;
1498  int mode_threshold;
1499  const int kBaselineTouch = 2; // This really should change with resolution.
1500  const int kGoodStrength = 8; // Strength of baseline-touching heights.
1501  const float kMinHeight = 0.25; // Min fraction of lineheight to use.
1502 
1503  sign_bit = row->xheight > 0 ? 1 : -1;
1504 
1505  memset(lefts, 0, HEIGHTBUCKETS * sizeof(lefts[0]));
1506  memset(rights, 0, HEIGHTBUCKETS * sizeof(rights[0]));
1507  mode_count = 0;
1508  for (blobindex = 0; blobindex < blobcount; blobindex++) {
1509  int xcenter = (blobcoords[blobindex].left () +
1510  blobcoords[blobindex].right ()) / 2;
1511  float base = baseline->y(xcenter);
1512  float bottomdiff = fabs(base - blobcoords[blobindex].bottom());
1513  int strength = textord_ocropus_mode &&
1514  bottomdiff <= kBaselineTouch ? kGoodStrength : 1;
1515  int height = static_cast<int>(blobcoords[blobindex].top () - base + 0.5);
1516  if (blobcoords[blobindex].height () > init_lineheight * kMinHeight) {
1517  if (height > lineheight * oldbl_xhfract
1518  && height > textord_min_xheight) {
1519  heightstat.add (height, strength);
1520  if (height < HEIGHTBUCKETS) {
1521  if (xcenter > rights[height])
1522  rights[height] = xcenter;
1523  if (xcenter > 0 && (lefts[height] == 0 || xcenter < lefts[height]))
1524  lefts[height] = xcenter;
1525  }
1526  }
1527  mode_count += strength;
1528  }
1529  }
1530 
1531  mode_threshold = (int) (blobcount * 0.1);
1532  if (oldbl_dot_error_size > 1 || oldbl_xhfix)
1533  mode_threshold = (int) (mode_count * 0.1);
1534 
1535  if (textord_oldbl_debug) {
1536  tprintf ("blobcount=%d, mode_count=%d, mode_t=%d\n",
1537  blobcount, mode_count, mode_threshold);
1538  }
1539  find_top_modes(&heightstat, HEIGHTBUCKETS, modelist, MODENUM);
1540  if (textord_oldbl_debug) {
1541  for (blobindex = 0; blobindex < MODENUM; blobindex++)
1542  tprintf ("mode[%d]=%d ", blobindex, modelist[blobindex]);
1543  tprintf ("\n");
1544  }
1545  pick_x_height(row, modelist, lefts, rights, &heightstat, mode_threshold);
1546 
1547  if (textord_oldbl_debug)
1548  tprintf ("Output xheight=%g\n", row->xheight);
1549  if (row->xheight < 0 && textord_oldbl_debug)
1550  tprintf ("warning: Row Line height < 0; %4.2f\n", row->xheight);
1551 
1552  if (sign_bit < 0)
1553  row->xheight = -row->xheight;
1554 }
#define MODENUM
Definition: oldbasel.cpp:69
EXTERN bool oldbl_xhfix
Definition: oldbasel.cpp:46
float xheight
Definition: blobbox.h:653
EXTERN bool textord_ocropus_mode
Definition: oldbasel.cpp:47
EXTERN bool textord_oldbl_debug
Definition: oldbasel.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
inT16 left() const
Definition: rect.h:68
void find_top_modes(STATS *stats, int statnum, int modelist[], int modenum)
Definition: oldbasel.cpp:1567
double y(double x) const
Definition: quspline.cpp:217
inT16 top() const
Definition: rect.h:54
int textord_min_xheight
Definition: makerow.cpp:69
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
inT16 right() const
Definition: rect.h:75
EXTERN double oldbl_dot_error_size
Definition: oldbasel.cpp:51
Definition: statistc.h:33
void pick_x_height(TO_ROW *row, int modelist[], int lefts[], int rights[], STATS *heightstat, int mode_threshold)
Definition: oldbasel.cpp:1607
EXTERN double oldbl_xhfract
Definition: oldbasel.cpp:48
#define HEIGHTBUCKETS
Definition: oldbasel.cpp:65

◆ make_holed_baseline()

void make_holed_baseline ( TBOX  blobcoords[],
int  blobcount,
QSPLINE spline,
QSPLINE baseline,
float  gradient 
)

Definition at line 654 of file oldbasel.cpp.

660  {
661  int leftedge; /*left edge of line */
662  int rightedge; /*right edge of line */
663  int blobindex; /*current blob */
664  float x; //centre of row
665  ICOORD shift; //shift of spline
666 
667  tesseract::DetLineFit lms; // straight baseline
668  inT32 xstarts[2]; //straight line
669  double coeffs[3];
670  float c; //line parameter
671 
672  /*left edge of row */
673  leftedge = blobcoords[0].left ();
674  /*right edge of line */
675  rightedge = blobcoords[blobcount - 1].right();
676  for (blobindex = 0; blobindex < blobcount; blobindex++) {
677  lms.Add(ICOORD((blobcoords[blobindex].left() +
678  blobcoords[blobindex].right()) / 2,
679  blobcoords[blobindex].bottom()));
680  }
681  lms.ConstrainedFit(gradient, &c);
682  xstarts[0] = leftedge;
683  xstarts[1] = rightedge;
684  coeffs[0] = 0;
685  coeffs[1] = gradient;
686  coeffs[2] = c;
687  *baseline = QSPLINE (1, xstarts, coeffs);
688  if (spline != NULL /*no given spline */
689  && spline->segments >= 3 /*or trivial */
690  /*or too non-overlap */
691  && spline->xcoords[1] <= leftedge + MAXOVERLAP * (rightedge - leftedge)
692  && spline->xcoords[spline->segments - 1] >= rightedge
693  - MAXOVERLAP * (rightedge - leftedge)) {
694  *baseline = *spline; /*copy it */
695  x = (leftedge + rightedge) / 2.0;
696  shift = ICOORD (0, (inT16) (gradient * x + c - spline->y (x)));
697  baseline->move (shift);
698  }
699 }
void move(ICOORD vec)
Definition: quspline.cpp:259
int32_t inT32
Definition: host.h:38
int16_t inT16
Definition: host.h:36
inT16 left() const
Definition: rect.h:68
#define MAXOVERLAP
Definition: oldbasel.cpp:63
double y(double x) const
Definition: quspline.cpp:217
void Add(const ICOORD &pt)
Definition: detlinefit.cpp:52
inT16 right() const
Definition: rect.h:75
double ConstrainedFit(const FCOORD &direction, double min_dist, double max_dist, bool debug, ICOORD *line_pt)
Definition: detlinefit.cpp:131
integer coordinate
Definition: points.h:30

◆ merge_oldbl_parts()

void merge_oldbl_parts ( TBOX  blobcoords[],
int  blobcount,
char  partids[],
int  partsizes[],
int  biggestpart,
float  jumplimit 
)

Definition at line 793 of file oldbasel.cpp.

800  {
801  BOOL8 found_one; //found a bestpart blob
802  BOOL8 close_one; //found was close enough
803  int blobindex; /*no along text line */
804  int prevpart; //previous iteration
805  int runlength; //no in this part
806  float diff; /*difference from line */
807  int startx; /*index of start blob */
808  int test_blob; //another index
809  FCOORD coord; //blob coordinate
810  float m, c; //fitted line
811  QLSQ stats; //line stuff
812 
813  prevpart = biggestpart;
814  runlength = 0;
815  startx = 0;
816  for (blobindex = 0; blobindex < blobcount; blobindex++) {
817  if (partids[blobindex] != prevpart) {
818  // tprintf("Partition change at (%d,%d) from %d to %d after run of %d\n",
819  // blobcoords[blobindex].left(),blobcoords[blobindex].bottom(),
820  // prevpart,partids[blobindex],runlength);
821  if (prevpart != biggestpart && runlength > MAXBADRUN) {
822  stats.clear ();
823  for (test_blob = startx; test_blob < blobindex; test_blob++) {
824  coord = FCOORD ((blobcoords[test_blob].left ()
825  + blobcoords[test_blob].right ()) / 2.0,
826  blobcoords[test_blob].bottom ());
827  stats.add (coord.x (), coord.y ());
828  }
829  stats.fit (1);
830  m = stats.get_b ();
831  c = stats.get_c ();
833  tprintf ("Fitted line y=%g x + %g\n", m, c);
834  found_one = FALSE;
835  close_one = FALSE;
836  for (test_blob = 1; !found_one
837  && (startx - test_blob >= 0
838  || blobindex + test_blob <= blobcount); test_blob++) {
839  if (startx - test_blob >= 0
840  && partids[startx - test_blob] == biggestpart) {
841  found_one = TRUE;
842  coord = FCOORD ((blobcoords[startx - test_blob].left ()
843  + blobcoords[startx -
844  test_blob].right ()) /
845  2.0,
846  blobcoords[startx -
847  test_blob].bottom ());
848  diff = m * coord.x () + c - coord.y ();
850  tprintf
851  ("Diff of common blob to suspect part=%g at (%g,%g)\n",
852  diff, coord.x (), coord.y ());
853  if (diff < jumplimit && -diff < jumplimit)
854  close_one = TRUE;
855  }
856  if (blobindex + test_blob <= blobcount
857  && partids[blobindex + test_blob - 1] == biggestpart) {
858  found_one = TRUE;
859  coord =
860  FCOORD ((blobcoords[blobindex + test_blob - 1].
861  left () + blobcoords[blobindex + test_blob -
862  1].right ()) / 2.0,
863  blobcoords[blobindex + test_blob -
864  1].bottom ());
865  diff = m * coord.x () + c - coord.y ();
867  tprintf
868  ("Diff of common blob to suspect part=%g at (%g,%g)\n",
869  diff, coord.x (), coord.y ());
870  if (diff < jumplimit && -diff < jumplimit)
871  close_one = TRUE;
872  }
873  }
874  if (close_one) {
876  tprintf
877  ("Merged %d blobs back into part %d from %d starting at (%d,%d)\n",
878  runlength, biggestpart, prevpart,
879  blobcoords[startx].left (),
880  blobcoords[startx].bottom ());
881  //switch sides
882  partsizes[prevpart] -= runlength;
883  for (test_blob = startx; test_blob < blobindex; test_blob++)
884  partids[test_blob] = biggestpart;
885  }
886  }
887  prevpart = partids[blobindex];
888  runlength = 1;
889  startx = blobindex;
890  }
891  else
892  runlength++;
893  }
894 }
double get_c()
Definition: quadlsq.h:51
double get_b()
Definition: quadlsq.h:48
#define TRUE
Definition: capi.h:45
Definition: points.h:189
EXTERN bool textord_oldbl_debug
Definition: oldbasel.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
unsigned char BOOL8
Definition: host.h:44
#define FALSE
Definition: capi.h:46
void add(double x, double y)
Definition: quadlsq.cpp:56
Definition: quadlsq.h:25
void fit(int degree)
Definition: quadlsq.cpp:100
#define MAXBADRUN
Definition: oldbasel.cpp:64
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209
void clear()
Definition: quadlsq.cpp:34

◆ old_first_xheight()

void old_first_xheight ( TO_ROW row,
TBOX  blobcoords[],
int  initialheight,
int  blobcount,
QSPLINE baseline,
float  jumplimit 
)

Definition at line 1397 of file oldbasel.cpp.

1404  {
1405  int blobindex; /*current blob */
1406  /*height statistics */
1407  STATS heightstat (0, MAXHEIGHT);
1408  int height; /*height of blob */
1409  int xcentre; /*centre of blob */
1410  int lineheight; /*approx xheight */
1411  float ascenders; /*ascender sum */
1412  int asccount; /*no of ascenders */
1413  float xsum; /*xheight sum */
1414  int xcount; /*xheight count */
1415  float diff; /*height difference */
1416 
1417  if (blobcount > 1) {
1418  for (blobindex = 0; blobindex < blobcount; blobindex++) {
1419  xcentre = (blobcoords[blobindex].left ()
1420  + blobcoords[blobindex].right ()) / 2;
1421  /*height of blob */
1422  height = (int) (blobcoords[blobindex].top () - baseline->y (xcentre) + 0.5);
1423  if (height > initialheight * oldbl_xhfract
1424  && height > textord_min_xheight)
1425  heightstat.add (height, 1);
1426  }
1427  if (heightstat.get_total () > 3) {
1428  lineheight = (int) heightstat.ile (0.25);
1429  if (lineheight <= 0)
1430  lineheight = (int) heightstat.ile (0.5);
1431  }
1432  else
1433  lineheight = initialheight;
1434  }
1435  else {
1436  lineheight = (int) (blobcoords[0].top ()
1437  - baseline->y ((blobcoords[0].left ()
1438  + blobcoords[0].right ()) / 2) +
1439  0.5);
1440  }
1441 
1442  xsum = 0.0f;
1443  xcount = 0;
1444  for (ascenders = 0.0f, asccount = 0, blobindex = 0; blobindex < blobcount;
1445  blobindex++) {
1446  xcentre = (blobcoords[blobindex].left ()
1447  + blobcoords[blobindex].right ()) / 2;
1448  diff = blobcoords[blobindex].top () - baseline->y (xcentre);
1449  /*is it ascender */
1450  if (diff > lineheight + jumplimit) {
1451  ascenders += diff;
1452  asccount++; /*count ascenders */
1453  }
1454  else if (diff > lineheight - jumplimit) {
1455  xsum += diff; /*mean xheight */
1456  xcount++;
1457  }
1458  }
1459  if (xcount > 0)
1460  xsum /= xcount; /*average xheight */
1461  else
1462  xsum = (float) lineheight; /*guess it */
1463  row->xheight *= xsum;
1464  if (asccount > 0)
1465  row->ascrise = ascenders / asccount - xsum;
1466  else
1467  row->ascrise = 0.0f; /*had none */
1468  if (row->xheight == 0)
1469  row->xheight = -1.0f;
1470 }
#define MAXHEIGHT
Definition: oldbasel.cpp:62
float xheight
Definition: blobbox.h:653
inT16 left() const
Definition: rect.h:68
double y(double x) const
Definition: quspline.cpp:217
inT16 top() const
Definition: rect.h:54
int textord_min_xheight
Definition: makerow.cpp:69
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
inT16 right() const
Definition: rect.h:75
Definition: statistc.h:33
EXTERN double oldbl_xhfract
Definition: oldbasel.cpp:48
float ascrise
Definition: blobbox.h:655

◆ partition_coords()

int partition_coords ( TBOX  blobcoords[],
int  blobcount,
char  partids[],
int  bestpart,
int  xcoords[],
int  ycoords[] 
)

Definition at line 1029 of file oldbasel.cpp.

1036  {
1037  int blobindex; /*no along text line */
1038  int pointcount; /*no of points */
1039 
1040  pointcount = 0;
1041  for (blobindex = 0; blobindex < blobcount; blobindex++) {
1042  if (partids[blobindex] == bestpart) {
1043  /*centre of blob */
1044  xcoords[pointcount] = (blobcoords[blobindex].left () + blobcoords[blobindex].right ()) >> 1;
1045  ycoords[pointcount++] = blobcoords[blobindex].bottom ();
1046  }
1047  }
1048  return pointcount; /*no of points found */
1049 }
inT16 left() const
Definition: rect.h:68
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61

◆ partition_line()

int partition_line ( TBOX  blobcoords[],
int  blobcount,
int numparts,
char  partids[],
int  partsizes[],
QSPLINE spline,
float  jumplimit,
float  ydiffs[] 
)

Definition at line 712 of file oldbasel.cpp.

721  {
722  int blobindex; /*no along text line */
723  int bestpart; /*best new partition */
724  int biggestpart; /*part with most members */
725  float diff; /*difference from line */
726  int startx; /*index of start blob */
727  float partdiffs[MAXPARTS]; /*step between parts */
728 
729  for (bestpart = 0; bestpart < MAXPARTS; bestpart++)
730  partsizes[bestpart] = 0; /*zero them all */
731 
732  startx = get_ydiffs (blobcoords, blobcount, spline, ydiffs);
733  *numparts = 1; /*1 partition */
734  bestpart = -1; /*first point */
735  float drift = 0.0f;
736  float last_delta = 0.0f;
737  for (blobindex = startx; blobindex < blobcount; blobindex++) {
738  /*do each blob in row */
739  diff = ydiffs[blobindex]; /*diff from line */
740  if (textord_oldbl_debug) {
741  tprintf ("%d(%d,%d), ", blobindex,
742  blobcoords[blobindex].left (),
743  blobcoords[blobindex].bottom ());
744  }
745  bestpart = choose_partition(diff, partdiffs, bestpart, jumplimit,
746  &drift, &last_delta, numparts);
747  /*record partition */
748  partids[blobindex] = bestpart;
749  partsizes[bestpart]++; /*another in it */
750  }
751 
752  bestpart = -1; /*first point */
753  drift = 0.0f;
754  last_delta = 0.0f;
755  partsizes[0]--; /*doing 1st pt again */
756  /*do each blob in row */
757  for (blobindex = startx; blobindex >= 0; blobindex--) {
758  diff = ydiffs[blobindex]; /*diff from line */
759  if (textord_oldbl_debug) {
760  tprintf ("%d(%d,%d), ", blobindex,
761  blobcoords[blobindex].left (),
762  blobcoords[blobindex].bottom ());
763  }
764  bestpart = choose_partition(diff, partdiffs, bestpart, jumplimit,
765  &drift, &last_delta, numparts);
766  /*record partition */
767  partids[blobindex] = bestpart;
768  partsizes[bestpart]++; /*another in it */
769  }
770 
771  for (biggestpart = 0, bestpart = 1; bestpart < *numparts; bestpart++)
772  if (partsizes[bestpart] >= partsizes[biggestpart])
773  biggestpart = bestpart; /*new biggest */
775  merge_oldbl_parts(blobcoords,
776  blobcount,
777  partids,
778  partsizes,
779  biggestpart,
780  jumplimit);
781  return biggestpart; /*biggest partition */
782 }
void merge_oldbl_parts(TBOX blobcoords[], int blobcount, char partids[], int partsizes[], int biggestpart, float jumplimit)
Definition: oldbasel.cpp:793
EXTERN bool textord_oldbl_debug
Definition: oldbasel.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
#define MAXPARTS
Definition: oldbasel.cpp:70
int choose_partition(register float diff, float partdiffs[], int lastpart, float jumplimit, float *drift, float *lastdelta, int *partcount)
Definition: oldbasel.cpp:957
int get_ydiffs(TBOX blobcoords[], int blobcount, QSPLINE *spline, float ydiffs[])
Definition: oldbasel.cpp:906
EXTERN bool textord_oldbl_merge_parts
Definition: oldbasel.cpp:43

◆ pick_x_height()

void pick_x_height ( TO_ROW row,
int  modelist[],
int  lefts[],
int  rights[],
STATS heightstat,
int  mode_threshold 
)

Definition at line 1607 of file oldbasel.cpp.

1611  {
1612  int x;
1613  int y;
1614  int z;
1615  float ratio;
1616  int found_one_bigger = FALSE;
1617  int best_x_height = 0;
1618  int best_asc = 0;
1619  int num_in_best;
1620 
1621  for (x = 0; x < MODENUM; x++) {
1622  for (y = 0; y < MODENUM; y++) {
1623  /* Check for two modes */
1624  if (modelist[x] && modelist[y] &&
1625  heightstat->pile_count (modelist[x]) > mode_threshold &&
1627  MIN(rights[modelist[x]], rights[modelist[y]]) >
1628  MAX(lefts[modelist[x]], lefts[modelist[y]]))) {
1629  ratio = (float) modelist[y] / (float) modelist[x];
1630  if (1.2 < ratio && ratio < 1.8) {
1631  /* Two modes found */
1632  best_x_height = modelist[x];
1633  num_in_best = heightstat->pile_count (modelist[x]);
1634 
1635  /* Try to get one higher */
1636  do {
1637  found_one_bigger = FALSE;
1638  for (z = 0; z < MODENUM; z++) {
1639  if (modelist[z] == best_x_height + 1 &&
1641  MIN(rights[modelist[x]], rights[modelist[y]]) >
1642  MAX(lefts[modelist[x]], lefts[modelist[y]]))) {
1643  ratio = (float) modelist[y] / (float) modelist[z];
1644  if ((1.2 < ratio && ratio < 1.8) &&
1645  /* Should be half of best */
1646  heightstat->pile_count (modelist[z]) >
1647  num_in_best * 0.5) {
1648  best_x_height++;
1649  found_one_bigger = TRUE;
1650  break;
1651  }
1652  }
1653  }
1654  }
1655  while (found_one_bigger);
1656 
1657  /* try to get a higher ascender */
1658 
1659  best_asc = modelist[y];
1660  num_in_best = heightstat->pile_count (modelist[y]);
1661 
1662  /* Try to get one higher */
1663  do {
1664  found_one_bigger = FALSE;
1665  for (z = 0; z < MODENUM; z++) {
1666  if (modelist[z] > best_asc &&
1668  MIN(rights[modelist[x]], rights[modelist[y]]) >
1669  MAX(lefts[modelist[x]], lefts[modelist[y]]))) {
1670  ratio = (float) modelist[z] / (float) best_x_height;
1671  if ((1.2 < ratio && ratio < 1.8) &&
1672  /* Should be half of best */
1673  heightstat->pile_count (modelist[z]) >
1674  num_in_best * 0.5) {
1675  best_asc = modelist[z];
1676  found_one_bigger = TRUE;
1677  break;
1678  }
1679  }
1680  }
1681  }
1682  while (found_one_bigger);
1683 
1684  row->xheight = (float) best_x_height;
1685  row->ascrise = (float) best_asc - best_x_height;
1686  return;
1687  }
1688  }
1689  }
1690  }
1691 
1692  best_x_height = modelist[0]; /* Single Mode found */
1693  num_in_best = heightstat->pile_count (best_x_height);
1694  do {
1695  /* Try to get one higher */
1696  found_one_bigger = FALSE;
1697  for (z = 1; z < MODENUM; z++) {
1698  /* Should be half of best */
1699  if ((modelist[z] == best_x_height + 1) &&
1700  (heightstat->pile_count (modelist[z]) > num_in_best * 0.5)) {
1701  best_x_height++;
1702  found_one_bigger = TRUE;
1703  break;
1704  }
1705  }
1706  }
1707  while (found_one_bigger);
1708 
1709  row->ascrise = 0.0f;
1710  row->xheight = (float) best_x_height;
1711  if (row->xheight == 0)
1712  row->xheight = -1.0f;
1713 }
#define MODENUM
Definition: oldbasel.cpp:69
#define TRUE
Definition: capi.h:45
float xheight
Definition: blobbox.h:653
EXTERN bool textord_ocropus_mode
Definition: oldbasel.cpp:47
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
#define FALSE
Definition: capi.h:46
#define MAX(x, y)
Definition: ndminx.h:24
#define MIN(x, y)
Definition: ndminx.h:28
float ascrise
Definition: blobbox.h:655

◆ segment_spline()

int segment_spline ( TBOX  blobcoords[],
int  blobcount,
int  xcoords[],
int  ycoords[],
int  degree,
int  pointcount,
int  xstarts[] 
)

Definition at line 1060 of file oldbasel.cpp.

1067  {
1068  int ptindex; /*no along text line */
1069  int segment; /*partition no */
1070  int lastmin, lastmax; /*possible turn points */
1071  int turnpoints[SPLINESIZE]; /*good turning points */
1072  int turncount; /*no of turning points */
1073  int max_x; //max specified coord
1074 
1075  xstarts[0] = xcoords[0] - 1; //leftmost defined pt
1076  max_x = xcoords[pointcount - 1] + 1;
1077  if (degree < 2)
1078  pointcount = 0;
1079  turncount = 0; /*no turning points yet */
1080  if (pointcount > 3) {
1081  ptindex = 1;
1082  lastmax = lastmin = 0; /*start with first one */
1083  while (ptindex < pointcount - 1 && turncount < SPLINESIZE - 1) {
1084  /*minimum */
1085  if (ycoords[ptindex - 1] > ycoords[ptindex] && ycoords[ptindex] <= ycoords[ptindex + 1]) {
1086  if (ycoords[ptindex] < ycoords[lastmax] - TURNLIMIT) {
1087  if (turncount == 0 || turnpoints[turncount - 1] != lastmax)
1088  /*new max point */
1089  turnpoints[turncount++] = lastmax;
1090  lastmin = ptindex; /*latest minimum */
1091  }
1092  else if (ycoords[ptindex] < ycoords[lastmin]) {
1093  lastmin = ptindex; /*lower minimum */
1094  }
1095  }
1096 
1097  /*maximum */
1098  if (ycoords[ptindex - 1] < ycoords[ptindex] && ycoords[ptindex] >= ycoords[ptindex + 1]) {
1099  if (ycoords[ptindex] > ycoords[lastmin] + TURNLIMIT) {
1100  if (turncount == 0 || turnpoints[turncount - 1] != lastmin)
1101  /*new min point */
1102  turnpoints[turncount++] = lastmin;
1103  lastmax = ptindex; /*latest maximum */
1104  }
1105  else if (ycoords[ptindex] > ycoords[lastmax]) {
1106  lastmax = ptindex; /*higher maximum */
1107  }
1108  }
1109  ptindex++;
1110  }
1111  /*possible global min */
1112  if (ycoords[ptindex] < ycoords[lastmax] - TURNLIMIT
1113  && (turncount == 0 || turnpoints[turncount - 1] != lastmax)) {
1114  if (turncount < SPLINESIZE - 1)
1115  /*2 more turns */
1116  turnpoints[turncount++] = lastmax;
1117  if (turncount < SPLINESIZE - 1)
1118  turnpoints[turncount++] = ptindex;
1119  }
1120  else if (ycoords[ptindex] > ycoords[lastmin] + TURNLIMIT
1121  /*possible global max */
1122  && (turncount == 0 || turnpoints[turncount - 1] != lastmin)) {
1123  if (turncount < SPLINESIZE - 1)
1124  /*2 more turns */
1125  turnpoints[turncount++] = lastmin;
1126  if (turncount < SPLINESIZE - 1)
1127  turnpoints[turncount++] = ptindex;
1128  }
1129  else if (turncount > 0 && turnpoints[turncount - 1] == lastmin
1130  && turncount < SPLINESIZE - 1) {
1131  if (ycoords[ptindex] > ycoords[lastmax])
1132  turnpoints[turncount++] = ptindex;
1133  else
1134  turnpoints[turncount++] = lastmax;
1135  }
1136  else if (turncount > 0 && turnpoints[turncount - 1] == lastmax
1137  && turncount < SPLINESIZE - 1) {
1138  if (ycoords[ptindex] < ycoords[lastmin])
1139  turnpoints[turncount++] = ptindex;
1140  else
1141  turnpoints[turncount++] = lastmin;
1142  }
1143  }
1144 
1145  if (textord_oldbl_debug && turncount > 0)
1146  tprintf ("First turn is %d at (%d,%d)\n",
1147  turnpoints[0], xcoords[turnpoints[0]], ycoords[turnpoints[0]]);
1148  for (segment = 1; segment < turncount; segment++) {
1149  /*centre y coord */
1150  lastmax = (ycoords[turnpoints[segment - 1]] + ycoords[turnpoints[segment]]) / 2;
1151 
1152  /* fix alg so that it works with both rising and falling sections */
1153  if (ycoords[turnpoints[segment - 1]] < ycoords[turnpoints[segment]])
1154  /*find rising y centre */
1155  for (ptindex = turnpoints[segment - 1] + 1; ptindex < turnpoints[segment] && ycoords[ptindex + 1] <= lastmax; ptindex++);
1156  else
1157  /*find falling y centre */
1158  for (ptindex = turnpoints[segment - 1] + 1; ptindex < turnpoints[segment] && ycoords[ptindex + 1] >= lastmax; ptindex++);
1159 
1160  /*centre x */
1161  xstarts[segment] = (xcoords[ptindex - 1] + xcoords[ptindex]
1162  + xcoords[turnpoints[segment - 1]]
1163  + xcoords[turnpoints[segment]] + 2) / 4;
1164  /*halfway between turns */
1165  if (textord_oldbl_debug)
1166  tprintf ("Turn %d is %d at (%d,%d), mid pt is %d@%d, final @%d\n",
1167  segment, turnpoints[segment],
1168  xcoords[turnpoints[segment]], ycoords[turnpoints[segment]],
1169  ptindex - 1, xcoords[ptindex - 1], xstarts[segment]);
1170  }
1171 
1172  xstarts[segment] = max_x;
1173  return segment; /*no of splines */
1174 }
EXTERN bool textord_oldbl_debug
Definition: oldbasel.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
#define TURNLIMIT
Definition: oldbasel.cpp:55
#define SPLINESIZE
Definition: oldbasel.cpp:71

◆ split_stepped_spline()

BOOL8 split_stepped_spline ( QSPLINE baseline,
float  jumplimit,
int  xcoords[],
int  xstarts[],
int segments 
)

Definition at line 1185 of file oldbasel.cpp.

1191  {
1192  BOOL8 doneany; //return value
1193  int segment; /*partition no */
1194  int startindex, centreindex, endindex;
1195  float leftcoord, rightcoord;
1196  int leftindex, rightindex;
1197  float step; //spline step
1198 
1199  doneany = FALSE;
1200  startindex = 0;
1201  for (segment = 1; segment < segments - 1; segment++) {
1202  step = baseline->step ((xstarts[segment - 1] + xstarts[segment]) / 2.0,
1203  (xstarts[segment] + xstarts[segment + 1]) / 2.0);
1204  if (step < 0)
1205  step = -step;
1206  if (step > jumplimit) {
1207  while (xcoords[startindex] < xstarts[segment - 1])
1208  startindex++;
1209  centreindex = startindex;
1210  while (xcoords[centreindex] < xstarts[segment])
1211  centreindex++;
1212  endindex = centreindex;
1213  while (xcoords[endindex] < xstarts[segment + 1])
1214  endindex++;
1215  if (segments >= SPLINESIZE) {
1217  tprintf ("Too many segments to resegment spline!!\n");
1218  }
1219  else if (endindex - startindex >= textord_spline_medianwin * 3) {
1220  while (centreindex - startindex <
1221  textord_spline_medianwin * 3 / 2)
1222  centreindex++;
1223  while (endindex - centreindex <
1224  textord_spline_medianwin * 3 / 2)
1225  centreindex--;
1226  leftindex = (startindex + startindex + centreindex) / 3;
1227  rightindex = (centreindex + endindex + endindex) / 3;
1228  leftcoord =
1229  (xcoords[startindex] * 2 + xcoords[centreindex]) / 3.0;
1230  rightcoord =
1231  (xcoords[centreindex] + xcoords[endindex] * 2) / 3.0;
1232  while (xcoords[leftindex] > leftcoord
1233  && leftindex - startindex > textord_spline_medianwin)
1234  leftindex--;
1235  while (xcoords[leftindex] < leftcoord
1236  && centreindex - leftindex >
1238  leftindex++;
1239  if (xcoords[leftindex] - leftcoord >
1240  leftcoord - xcoords[leftindex - 1])
1241  leftindex--;
1242  while (xcoords[rightindex] > rightcoord
1243  && rightindex - centreindex >
1245  rightindex--;
1246  while (xcoords[rightindex] < rightcoord
1247  && endindex - rightindex > textord_spline_medianwin)
1248  rightindex++;
1249  if (xcoords[rightindex] - rightcoord >
1250  rightcoord - xcoords[rightindex - 1])
1251  rightindex--;
1253  tprintf ("Splitting spline at %d with step %g at (%d,%d)\n",
1254  xstarts[segment],
1255  baseline->
1256  step ((xstarts[segment - 1] +
1257  xstarts[segment]) / 2.0,
1258  (xstarts[segment] +
1259  xstarts[segment + 1]) / 2.0),
1260  (xcoords[leftindex - 1] + xcoords[leftindex]) / 2,
1261  (xcoords[rightindex - 1] + xcoords[rightindex]) / 2);
1262  insert_spline_point (xstarts, segment,
1263  (xcoords[leftindex - 1] +
1264  xcoords[leftindex]) / 2,
1265  (xcoords[rightindex - 1] +
1266  xcoords[rightindex]) / 2, segments);
1267  doneany = TRUE;
1268  }
1269  else if (textord_debug_baselines) {
1270  tprintf
1271  ("Resegmenting spline failed - insufficient pts (%d,%d,%d,%d)\n",
1272  startindex, centreindex, endindex,
1274  }
1275  }
1276  // else tprintf("Spline step at %d is %g\n",
1277  // xstarts[segment],
1278  // baseline->step((xstarts[segment-1]+xstarts[segment])/2.0,
1279  // (xstarts[segment]+xstarts[segment+1])/2.0));
1280  }
1281  return doneany;
1282 }
#define TRUE
Definition: capi.h:45
int32_t inT32
Definition: host.h:38
#define tprintf(...)
Definition: tprintf.h:31
void insert_spline_point(int xstarts[], int segment, int coord1, int coord2, int &segments)
Definition: oldbasel.cpp:1292
EXTERN bool textord_debug_baselines
Definition: oldbasel.cpp:40
unsigned char BOOL8
Definition: host.h:44
#define FALSE
Definition: capi.h:46
double step(double x1, double x2)
Definition: quspline.cpp:192
#define SPLINESIZE
Definition: oldbasel.cpp:71
int textord_spline_medianwin
Definition: makerow.cpp:66

Variable Documentation

◆ kMinModeFactor

const int kMinModeFactor = 12

Definition at line 1564 of file oldbasel.cpp.

◆ kMinModeFactorOcropus

const int kMinModeFactorOcropus = 32

Definition at line 1563 of file oldbasel.cpp.

◆ oldbl_corrfix

EXTERN bool oldbl_corrfix = TRUE

"Improve correlation of heights"

Definition at line 44 of file oldbasel.cpp.

◆ oldbl_dot_error_size

EXTERN double oldbl_dot_error_size = 1.26

"Max aspect ratio of a dot"

Definition at line 51 of file oldbasel.cpp.

◆ oldbl_holed_losscount

EXTERN int oldbl_holed_losscount = 10

"Max lost before fallback line used"

Definition at line 50 of file oldbasel.cpp.

◆ oldbl_xhfix

EXTERN bool oldbl_xhfix = FALSE

"Fix bug in modes threshold for xheights"

Definition at line 46 of file oldbasel.cpp.

◆ oldbl_xhfract

EXTERN double oldbl_xhfract = 0.4

"Fraction of est allowed in calc"

Definition at line 48 of file oldbasel.cpp.

◆ textord_debug_baselines

EXTERN bool textord_debug_baselines = FALSE

"Debug baseline generation"

Definition at line 40 of file oldbasel.cpp.

◆ textord_ocropus_mode

EXTERN bool textord_ocropus_mode = FALSE

"Make baselines for ocropus"

Definition at line 47 of file oldbasel.cpp.

◆ textord_oldbl_debug

EXTERN bool textord_oldbl_debug = FALSE

"Debug old baseline generation"

Definition at line 39 of file oldbasel.cpp.

◆ textord_oldbl_jumplimit

EXTERN double textord_oldbl_jumplimit = 0.15

"X fraction for new partition"

Definition at line 53 of file oldbasel.cpp.

◆ textord_oldbl_merge_parts

EXTERN bool textord_oldbl_merge_parts = TRUE

"Merge suspect partitions"

Definition at line 43 of file oldbasel.cpp.

◆ textord_oldbl_paradef

EXTERN bool textord_oldbl_paradef = TRUE

"Use para default mechanism"

Definition at line 41 of file oldbasel.cpp.

◆ textord_oldbl_split_splines

EXTERN bool textord_oldbl_split_splines = TRUE

"Split stepped splines"

Definition at line 42 of file oldbasel.cpp.

◆ textord_really_old_xheight

EXTERN bool textord_really_old_xheight = FALSE

"Use original wiseowl xheight"

Definition at line 38 of file oldbasel.cpp.