1
1
/* *********************************************************************
2
2
* File: pithsync.cpp (Formerly pitsync2.c)
3
3
* Description: Code to find the optimum fixed pitch segmentation of some blobs.
4
- * Author: Ray Smith
5
- * Created: Thu Nov 19 11:48:05 GMT 1992
4
+ * Author: Ray Smith
5
+ * Created: Thu Nov 19 11:48:05 GMT 1992
6
6
*
7
7
* (C) Copyright 1992, Hewlett-Packard Ltd.
8
8
** Licensed under the Apache License, Version 2.0 (the "License");
18
18
**********************************************************************/
19
19
20
20
#include < math.h>
21
- #include " memry.h "
21
+ #include < vector > // for std::vector
22
22
#include " makerow.h"
23
23
#include " pitsync1.h"
24
24
#include " topitch.h"
@@ -320,7 +320,6 @@ double check_pitch_sync2( //find segmentation
320
320
TBOX this_box; // bounding box
321
321
TBOX next_box; // box of next blob
322
322
FPSEGPT *segpt; // segment point
323
- FPCUTPT *cutpts; // array of points
324
323
double best_cost; // best path
325
324
double mean_sum; // computes result
326
325
FPCUTPT *best_end; // end of best path
@@ -359,14 +358,16 @@ double check_pitch_sync2( //find segmentation
359
358
projection_scale, occupation_count, seg_list,
360
359
start, end);
361
360
array_origin = left_edge - pitch;
362
- cutpts = (FPCUTPT *) alloc_mem ((right_edge - left_edge + pitch * 2 + 1 )
363
- * sizeof (FPCUTPT) );
361
+ // array of points
362
+ std::vector<FPCUTPT> cutpts (right_edge - left_edge + pitch * 2 + 1 );
364
363
for (x = array_origin; x < left_edge; x++)
365
364
// free cuts
366
- cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, 0 );
365
+ cutpts[x - array_origin].setup (&cutpts[0 ], array_origin, projection,
366
+ zero_count, pitch, x, 0 );
367
367
for (offset = 0 ; offset <= pitch_error; offset++, x++)
368
368
// not quite free
369
- cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, offset);
369
+ cutpts[x - array_origin].setup (&cutpts[0 ], array_origin, projection,
370
+ zero_count, pitch, x, offset);
370
371
371
372
this_it = *blob_it;
372
373
best_cost = MAX_FLOAT32;
@@ -404,7 +405,7 @@ double check_pitch_sync2( //find segmentation
404
405
faking = true ;
405
406
offset = projection->pile_count (x);
406
407
}
407
- cutpts[x - array_origin].assign (cutpts, array_origin, x,
408
+ cutpts[x - array_origin].assign (& cutpts[ 0 ] , array_origin, x,
408
409
faking, mid_cut, offset, projection,
409
410
projection_scale, zero_count, pitch,
410
411
pitch_error);
@@ -416,7 +417,7 @@ double check_pitch_sync2( //find segmentation
416
417
best_count = INT16_MAX;
417
418
while (x < right_edge + pitch) {
418
419
offset = x < right_edge ? right_edge - x : 0 ;
419
- cutpts[x - array_origin].assign (cutpts, array_origin, x,
420
+ cutpts[x - array_origin].assign (& cutpts[ 0 ] , array_origin, x,
420
421
false , false , offset, projection,
421
422
projection_scale, zero_count, pitch,
422
423
pitch_error);
@@ -473,7 +474,6 @@ double check_pitch_sync2( //find segmentation
473
474
if (seg_it.data ()->squares () - mean_sum < 0 )
474
475
tprintf (" Impossible sqsum=%g, mean=%g, total=%d\n " ,
475
476
seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
476
- free_mem (cutpts);
477
477
// tprintf("blob_count=%d, pitch=%d, sync=%g, occ=%d\n",
478
478
// blob_count,pitch,seg_it.data()->squares()-mean_sum,
479
479
// occupation_count);
@@ -502,29 +502,27 @@ double check_pitch_sync3( //find segmentation
502
502
int16_t start, // start of good range
503
503
int16_t end // end of good range
504
504
) {
505
- bool faking; // illegal cut pt
506
- bool mid_cut; // cheap cut pt.
507
- int16_t left_edge; // of word
508
- int16_t right_edge; // of word
509
- int16_t x; // current coord
510
- int16_t array_origin; // x coord of array
511
- int16_t offset; // dist to legal area
512
- int16_t projection_offset; // from scaled projection
513
- int16_t prev_zero; // previous zero dist
514
- int16_t next_zero; // next zero dist
515
- int16_t zero_offset; // scan window
516
- int16_t best_left_x = 0 ; // for equals
517
- int16_t best_right_x = 0 ; // right edge
505
+ bool faking; // illegal cut pt
506
+ bool mid_cut; // cheap cut pt.
507
+ int16_t left_edge; // of word
508
+ int16_t right_edge; // of word
509
+ int16_t x; // current coord
510
+ int16_t array_origin; // x coord of array
511
+ int16_t offset; // dist to legal area
512
+ int16_t projection_offset; // from scaled projection
513
+ int16_t prev_zero; // previous zero dist
514
+ int16_t next_zero; // next zero dist
515
+ int16_t zero_offset; // scan window
516
+ int16_t best_left_x = 0 ; // for equals
517
+ int16_t best_right_x = 0 ; // right edge
518
518
FPSEGPT *segpt; // segment point
519
- FPCUTPT *cutpts; // array of points
520
- BOOL8 *mins; // local min results
521
519
int minindex; // next input position
522
520
int test_index; // index to mins
523
521
double best_cost; // best path
524
522
double mean_sum; // computes result
525
523
FPCUTPT *best_end; // end of best path
526
- int16_t best_fake; // best fake level
527
- int16_t best_count; // no of cuts
524
+ int16_t best_fake; // best fake level
525
+ int16_t best_count; // no of cuts
528
526
FPSEGPT_IT seg_it = seg_list; // output iterator
529
527
530
528
end = (end - start) % pitch;
@@ -539,16 +537,19 @@ double check_pitch_sync3( //find segmentation
539
537
for (right_edge = projection_right; projection->pile_count (right_edge) == 0
540
538
&& right_edge > left_edge; right_edge--);
541
539
array_origin = left_edge - pitch;
542
- cutpts = (FPCUTPT *) alloc_mem ((right_edge - left_edge + pitch * 2 + 1 )
543
- * sizeof (FPCUTPT));
544
- mins = (BOOL8 *) alloc_mem ((pitch_error * 2 + 1 ) * sizeof (BOOL8));
540
+ // array of points
541
+ std::vector<FPCUTPT> cutpts (right_edge - left_edge + pitch * 2 + 1 );
542
+ // local min results
543
+ std::vector<BOOL8> mins (pitch_error * 2 + 1 );
545
544
for (x = array_origin; x < left_edge; x++)
546
545
// free cuts
547
- cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, 0 );
546
+ cutpts[x - array_origin].setup (&cutpts[0 ], array_origin, projection,
547
+ zero_count, pitch, x, 0 );
548
548
prev_zero = left_edge - 1 ;
549
549
for (offset = 0 ; offset <= pitch_error; offset++, x++)
550
550
// not quite free
551
- cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, offset);
551
+ cutpts[x - array_origin].setup (&cutpts[0 ], array_origin, projection,
552
+ zero_count, pitch, x, offset);
552
553
553
554
best_cost = MAX_FLOAT32;
554
555
best_end = nullptr ;
@@ -609,12 +610,12 @@ double check_pitch_sync3( //find segmentation
609
610
if ((start == 0 && end == 0 )
610
611
|| !textord_fast_pitch_test
611
612
|| (x - projection_left - start) % pitch <= end)
612
- cutpts[x - array_origin].assign ( cutpts, array_origin, x,
613
+ cutpts[x - array_origin].assign (& cutpts[ 0 ] , array_origin, x,
613
614
faking, mid_cut, offset, projection,
614
615
projection_scale, zero_count, pitch,
615
616
pitch_error);
616
617
else
617
- cutpts[x - array_origin].assign_cheap ( cutpts, array_origin, x,
618
+ cutpts[x - array_origin].assign_cheap (& cutpts[ 0 ] , array_origin, x,
618
619
faking, mid_cut, offset,
619
620
projection, projection_scale,
620
621
zero_count, pitch,
@@ -631,7 +632,7 @@ double check_pitch_sync3( //find segmentation
631
632
best_count = INT16_MAX;
632
633
while (x < right_edge + pitch) {
633
634
offset = x < right_edge ? right_edge - x : 0 ;
634
- cutpts[x - array_origin].assign ( cutpts, array_origin, x,
635
+ cutpts[x - array_origin].assign (& cutpts[ 0 ] , array_origin, x,
635
636
false , false , offset, projection,
636
637
projection_scale, zero_count, pitch,
637
638
pitch_error);
@@ -686,7 +687,5 @@ double check_pitch_sync3( //find segmentation
686
687
if (seg_it.data ()->squares () - mean_sum < 0 )
687
688
tprintf (" Impossible sqsum=%g, mean=%g, total=%d\n " ,
688
689
seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
689
- free_mem (mins);
690
- free_mem (cutpts);
691
690
return seg_it.data ()->squares () - mean_sum;
692
691
}
0 commit comments