Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pitsync1.cpp File Reference
#include "mfcpch.h"
#include <math.h>
#include "memry.h"
#include "pitsync1.h"
#include "notdll.h"

Go to the source code of this file.

Macros

#define EXTERN
 

Functions

 ELISTIZE (FPSEGPT) CLISTIZE(FPSEGPT_LIST) EXTERN int pitsync_linear_version
 
double check_pitch_sync (BLOBNBOX_IT *blob_it, inT16 blob_count, inT16 pitch, inT16 pitch_error, STATS *projection, FPSEGPT_LIST *seg_list)
 
void make_illegal_segment (FPSEGPT_LIST *prev_list, TBOX blob_box, BLOBNBOX_IT blob_it, inT16 region_index, inT16 pitch, inT16 pitch_error, FPSEGPT_LIST *seg_list)
 

Variables

EXTERN double pitsync_joined_edge = 0.75
 
EXTERN double pitsync_offset_freecut_fraction = 0.25
 
EXTERN int pitsync_fake_depth = 1
 

Macro Definition Documentation

#define EXTERN

Function Documentation

double check_pitch_sync ( BLOBNBOX_IT *  blob_it,
inT16  blob_count,
inT16  pitch,
inT16  pitch_error,
STATS projection,
FPSEGPT_LIST *  seg_list 
)

Definition at line 151 of file pitsync1.cpp.

158  {
159  inT16 x; //current coord
160  inT16 min_index; //blob number
161  inT16 max_index; //blob number
162  inT16 left_edge; //of word
163  inT16 right_edge; //of word
164  inT16 right_max; //max allowed x
165  inT16 min_x; //in this region
166  inT16 max_x;
167  inT16 region_index;
168  inT16 best_region_index = 0; //for best result
169  inT16 offset; //dist to legal area
170  inT16 left_best_x; //edge of good region
171  inT16 right_best_x; //right edge
172  TBOX min_box; //bounding box
173  TBOX max_box; //bounding box
174  TBOX next_box; //box of next blob
175  FPSEGPT *segpt; //segment point
176  FPSEGPT_LIST *segpts; //points in a segment
177  double best_cost; //best path
178  double mean_sum; //computes result
179  FPSEGPT *best_end; //end of best path
180  BLOBNBOX_IT min_it; //copy iterator
181  BLOBNBOX_IT max_it; //copy iterator
182  FPSEGPT_IT segpt_it; //iterator
183  //output segments
184  FPSEGPT_IT outseg_it = seg_list;
185  FPSEGPT_LIST_CLIST lattice; //list of lists
186  //region iterator
187  FPSEGPT_LIST_C_IT lattice_it = &lattice;
188 
189  // tprintf("Computing sync on word of %d blobs with pitch %d\n",
190  // blob_count, pitch);
191  // if (blob_count==8 && pitch==27)
192  // projection->print(stdout,TRUE);
193  if (pitch < 3)
194  pitch = 3; //nothing ludicrous
195  if ((pitch - 3) / 2 < pitch_error)
196  pitch_error = (pitch - 3) / 2;
197  min_it = *blob_it;
198  min_box = box_next (&min_it); //get box
199  // if (blob_count==8 && pitch==27)
200  // tprintf("1st box at (%d,%d)->(%d,%d)\n",
201  // min_box.left(),min_box.bottom(),
202  // min_box.right(),min_box.top());
203  //left of word
204  left_edge = min_box.left () + pitch_error;
205  for (min_index = 1; min_index < blob_count; min_index++) {
206  min_box = box_next (&min_it);
207  // if (blob_count==8 && pitch==27)
208  // tprintf("Box at (%d,%d)->(%d,%d)\n",
209  // min_box.left(),min_box.bottom(),
210  // min_box.right(),min_box.top());
211  }
212  right_edge = min_box.right (); //end of word
213  max_x = left_edge;
214  //min permissible
215  min_x = max_x - pitch + pitch_error * 2 + 1;
216  right_max = right_edge + pitch - pitch_error - 1;
217  segpts = new FPSEGPT_LIST; //list of points
218  segpt_it.set_to_list (segpts);
219  for (x = min_x; x <= max_x; x++) {
220  segpt = new FPSEGPT (x); //make a new one
221  //put in list
222  segpt_it.add_after_then_move (segpt);
223  }
224  //first segment
225  lattice_it.add_before_then_move (segpts);
226  min_index = 0;
227  region_index = 1;
228  best_cost = MAX_FLOAT32;
229  best_end = NULL;
230  min_it = *blob_it;
231  min_box = box_next (&min_it); //first box
232  do {
233  left_best_x = -1;
234  right_best_x = -1;
235  segpts = new FPSEGPT_LIST; //list of points
236  segpt_it.set_to_list (segpts);
237  min_x += pitch - pitch_error;//next limits
238  max_x += pitch + pitch_error;
239  while (min_box.right () < min_x && min_index < blob_count) {
240  min_index++;
241  min_box = box_next (&min_it);
242  }
243  max_it = min_it;
244  max_index = min_index;
245  max_box = min_box;
246  next_box = box_next (&max_it);
247  for (x = min_x; x <= max_x && x <= right_max; x++) {
248  while (x < right_edge && max_index < blob_count
249  && x > max_box.right ()) {
250  max_index++;
251  max_box = next_box;
252  next_box = box_next (&max_it);
253  }
254  if (x <= max_box.left () + pitch_error
255  || x >= max_box.right () - pitch_error || x >= right_edge
256  || (max_index < blob_count - 1 && x >= next_box.left ())
257  || (x - max_box.left () > pitch * pitsync_joined_edge
258  && max_box.right () - x > pitch * pitsync_joined_edge)) {
259  // || projection->local_min(x))
260  if (x - max_box.left () > 0
261  && x - max_box.left () <= pitch_error)
262  //dist to real break
263  offset = x - max_box.left ();
264  else if (max_box.right () - x > 0
265  && max_box.right () - x <= pitch_error
266  && (max_index >= blob_count - 1
267  || x < next_box.left ()))
268  offset = max_box.right () - x;
269  else
270  offset = 0;
271  // offset=pitsync_offset_freecut_fraction*projection->pile_count(x);
272  segpt = new FPSEGPT (x, FALSE, offset, region_index,
273  pitch, pitch_error, lattice_it.data ());
274  }
275  else {
276  offset = projection->pile_count (x);
277  segpt = new FPSEGPT (x, TRUE, offset, region_index,
278  pitch, pitch_error, lattice_it.data ());
279  }
280  if (segpt->previous () != NULL) {
281  segpt_it.add_after_then_move (segpt);
282  if (x >= right_edge - pitch_error) {
283  segpt->terminal = TRUE;//no more wanted
284  if (segpt->cost_function () < best_cost) {
285  best_cost = segpt->cost_function ();
286  //find least
287  best_end = segpt;
288  best_region_index = region_index;
289  left_best_x = x;
290  right_best_x = x;
291  }
292  else if (segpt->cost_function () == best_cost
293  && right_best_x == x - 1)
294  right_best_x = x;
295  }
296  }
297  else {
298  delete segpt; //no good
299  }
300  }
301  if (segpts->empty ()) {
302  if (best_end != NULL)
303  break; //already found one
304  make_illegal_segment (lattice_it.data (), min_box, min_it,
305  region_index, pitch, pitch_error, segpts);
306  }
307  else {
308  if (right_best_x > left_best_x + 1) {
309  left_best_x = (left_best_x + right_best_x + 1) / 2;
310  for (segpt_it.mark_cycle_pt (); !segpt_it.cycled_list ()
311  && segpt_it.data ()->position () != left_best_x;
312  segpt_it.forward ());
313  if (segpt_it.data ()->position () == left_best_x)
314  //middle of region
315  best_end = segpt_it.data ();
316  }
317  }
318  //new segment
319  lattice_it.add_before_then_move (segpts);
320  region_index++;
321  }
322  while (min_x < right_edge);
323  ASSERT_HOST (best_end != NULL);//must always find some
324 
325  for (lattice_it.mark_cycle_pt (); !lattice_it.cycled_list ();
326  lattice_it.forward ()) {
327  segpts = lattice_it.data ();
328  segpt_it.set_to_list (segpts);
329  // if (blob_count==8 && pitch==27)
330  // {
331  // for (segpt_it.mark_cycle_pt();!segpt_it.cycled_list();segpt_it.forward())
332  // {
333  // segpt=segpt_it.data();
334  // tprintf("At %d, (%x) cost=%g, m=%g, sq=%g, pred=%x\n",
335  // segpt->position(),segpt,segpt->cost_function(),
336  // segpt->sum(),segpt->squares(),segpt->previous());
337  // }
338  // tprintf("\n");
339  // }
340  for (segpt_it.mark_cycle_pt (); !segpt_it.cycled_list ()
341  && segpt_it.data () != best_end; segpt_it.forward ());
342  if (segpt_it.data () == best_end) {
343  //save good one
344  segpt = segpt_it.extract ();
345  outseg_it.add_before_then_move (segpt);
346  best_end = segpt->previous ();
347  }
348  }
349  ASSERT_HOST (best_end == NULL);
350  ASSERT_HOST (!outseg_it.empty ());
351  outseg_it.move_to_last ();
352  mean_sum = outseg_it.data ()->sum ();
353  mean_sum = mean_sum * mean_sum / best_region_index;
354  if (outseg_it.data ()->squares () - mean_sum < 0)
355  tprintf ("Impossible sqsum=%g, mean=%g, total=%d\n",
356  outseg_it.data ()->squares (), outseg_it.data ()->sum (),
357  best_region_index);
358  lattice.deep_clear (); //shift the lot
359  return outseg_it.data ()->squares () - mean_sum;
360 }
BOOL8 terminal
Definition: pitsync1.h:72
#define NULL
Definition: host.h:144
inT16 left() const
Definition: rect.h:67
TBOX box_next(BLOBNBOX_IT *it)
Definition: blobbox.cpp:585
Definition: rect.h:29
#define FALSE
Definition: capi.h:28
inT16 right() const
Definition: rect.h:74
void make_illegal_segment(FPSEGPT_LIST *prev_list, TBOX blob_box, BLOBNBOX_IT blob_it, inT16 region_index, inT16 pitch, inT16 pitch_error, FPSEGPT_LIST *seg_list)
Definition: pitsync1.cpp:369
inT32 pile_count(inT32 value) const
Definition: statistc.h:74
#define MAX_FLOAT32
Definition: host.h:124
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
short inT16
Definition: host.h:100
FPSEGPT * previous()
Definition: pitsync1.h:63
#define ASSERT_HOST(x)
Definition: errcode.h:84
double cost_function()
Definition: pitsync1.h:54
EXTERN double pitsync_joined_edge
Definition: pitsync1.cpp:36
#define TRUE
Definition: capi.h:27
ELISTIZE ( FPSEGPT  )

"Use new fast algorithm"

void make_illegal_segment ( FPSEGPT_LIST *  prev_list,
TBOX  blob_box,
BLOBNBOX_IT  blob_it,
inT16  region_index,
inT16  pitch,
inT16  pitch_error,
FPSEGPT_LIST *  seg_list 
)

Definition at line 369 of file pitsync1.cpp.

377  {
378  inT16 x; //current coord
379  inT16 min_x = 0; //in this region
380  inT16 max_x = 0;
381  inT16 offset; //dist to edge
382  FPSEGPT *segpt; //segment point
383  FPSEGPT *prevpt; //previous point
384  float best_cost; //best path
385  FPSEGPT_IT segpt_it = seg_list;//iterator
386  //previous points
387  FPSEGPT_IT prevpt_it = prev_list;
388 
389  best_cost = MAX_FLOAT32;
390  for (prevpt_it.mark_cycle_pt (); !prevpt_it.cycled_list ();
391  prevpt_it.forward ()) {
392  prevpt = prevpt_it.data ();
393  if (prevpt->cost_function () < best_cost) {
394  //find least
395  best_cost = prevpt->cost_function ();
396  min_x = prevpt->position ();
397  max_x = min_x; //limits on coords
398  }
399  else if (prevpt->cost_function () == best_cost) {
400  max_x = prevpt->position ();
401  }
402  }
403  min_x += pitch - pitch_error;
404  max_x += pitch + pitch_error;
405  for (x = min_x; x <= max_x; x++) {
406  while (x > blob_box.right ()) {
407  blob_box = box_next (&blob_it);
408  }
409  offset = x - blob_box.left ();
410  if (blob_box.right () - x < offset)
411  offset = blob_box.right () - x;
412  segpt = new FPSEGPT (x, FALSE, offset,
413  region_index, pitch, pitch_error, prev_list);
414  if (segpt->previous () != NULL) {
415  ASSERT_HOST (offset >= 0);
416  fprintf (stderr, "made fake at %d\n", x);
417  //make one up
418  segpt_it.add_after_then_move (segpt);
419  segpt->faked = TRUE;
420  segpt->fake_count++;
421  }
422  else
423  delete segpt;
424  }
425 }
BOOL8 faked
Definition: pitsync1.h:71
#define NULL
Definition: host.h:144
inT32 position()
Definition: pitsync1.h:51
inT16 left() const
Definition: rect.h:67
TBOX box_next(BLOBNBOX_IT *it)
Definition: blobbox.cpp:585
#define FALSE
Definition: capi.h:28
inT16 right() const
Definition: rect.h:74
inT16 fake_count
Definition: pitsync1.h:73
#define MAX_FLOAT32
Definition: host.h:124
short inT16
Definition: host.h:100
FPSEGPT * previous()
Definition: pitsync1.h:63
#define ASSERT_HOST(x)
Definition: errcode.h:84
double cost_function()
Definition: pitsync1.h:54
#define TRUE
Definition: capi.h:27

Variable Documentation

EXTERN int pitsync_fake_depth = 1

"Max advance fake generation"

Definition at line 41 of file pitsync1.cpp.

EXTERN double pitsync_joined_edge = 0.75

"Dist inside big blob for chopping"

Definition at line 36 of file pitsync1.cpp.

EXTERN double pitsync_offset_freecut_fraction = 0.25

"Fraction of cut for free cuts"

Definition at line 39 of file pitsync1.cpp.