Skip to content

Commit 3032b65

Browse files
committed
pithsync: Replace alloc_mem, free_mem by C++ std::vector
Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent cb9eec3 commit 3032b65

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

src/textord/pithsync.cpp

+37-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**********************************************************************
22
* File: pithsync.cpp (Formerly pitsync2.c)
33
* 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
66
*
77
* (C) Copyright 1992, Hewlett-Packard Ltd.
88
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,7 +18,7 @@
1818
**********************************************************************/
1919

2020
#include <math.h>
21-
#include "memry.h"
21+
#include <vector> // for std::vector
2222
#include "makerow.h"
2323
#include "pitsync1.h"
2424
#include "topitch.h"
@@ -320,7 +320,6 @@ double check_pitch_sync2( //find segmentation
320320
TBOX this_box; //bounding box
321321
TBOX next_box; //box of next blob
322322
FPSEGPT *segpt; //segment point
323-
FPCUTPT *cutpts; //array of points
324323
double best_cost; //best path
325324
double mean_sum; //computes result
326325
FPCUTPT *best_end; //end of best path
@@ -359,14 +358,16 @@ double check_pitch_sync2( //find segmentation
359358
projection_scale, occupation_count, seg_list,
360359
start, end);
361360
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);
364363
for (x = array_origin; x < left_edge; x++)
365364
//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);
367367
for (offset = 0; offset <= pitch_error; offset++, x++)
368368
//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);
370371

371372
this_it = *blob_it;
372373
best_cost = MAX_FLOAT32;
@@ -404,7 +405,7 @@ double check_pitch_sync2( //find segmentation
404405
faking = true;
405406
offset = projection->pile_count (x);
406407
}
407-
cutpts[x - array_origin].assign (cutpts, array_origin, x,
408+
cutpts[x - array_origin].assign (&cutpts[0], array_origin, x,
408409
faking, mid_cut, offset, projection,
409410
projection_scale, zero_count, pitch,
410411
pitch_error);
@@ -416,7 +417,7 @@ double check_pitch_sync2( //find segmentation
416417
best_count = INT16_MAX;
417418
while (x < right_edge + pitch) {
418419
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,
420421
false, false, offset, projection,
421422
projection_scale, zero_count, pitch,
422423
pitch_error);
@@ -473,7 +474,6 @@ double check_pitch_sync2( //find segmentation
473474
if (seg_it.data ()->squares () - mean_sum < 0)
474475
tprintf ("Impossible sqsum=%g, mean=%g, total=%d\n",
475476
seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
476-
free_mem(cutpts);
477477
// tprintf("blob_count=%d, pitch=%d, sync=%g, occ=%d\n",
478478
// blob_count,pitch,seg_it.data()->squares()-mean_sum,
479479
// occupation_count);
@@ -502,29 +502,27 @@ double check_pitch_sync3( //find segmentation
502502
int16_t start, //start of good range
503503
int16_t end //end of good range
504504
) {
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
518518
FPSEGPT *segpt; //segment point
519-
FPCUTPT *cutpts; //array of points
520-
BOOL8 *mins; //local min results
521519
int minindex; //next input position
522520
int test_index; //index to mins
523521
double best_cost; //best path
524522
double mean_sum; //computes result
525523
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
528526
FPSEGPT_IT seg_it = seg_list; //output iterator
529527

530528
end = (end - start) % pitch;
@@ -539,16 +537,19 @@ double check_pitch_sync3( //find segmentation
539537
for (right_edge = projection_right; projection->pile_count (right_edge) == 0
540538
&& right_edge > left_edge; right_edge--);
541539
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);
545544
for (x = array_origin; x < left_edge; x++)
546545
//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);
548548
prev_zero = left_edge - 1;
549549
for (offset = 0; offset <= pitch_error; offset++, x++)
550550
//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);
552553

553554
best_cost = MAX_FLOAT32;
554555
best_end = nullptr;
@@ -609,12 +610,12 @@ double check_pitch_sync3( //find segmentation
609610
if ((start == 0 && end == 0)
610611
|| !textord_fast_pitch_test
611612
|| (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,
613614
faking, mid_cut, offset, projection,
614615
projection_scale, zero_count, pitch,
615616
pitch_error);
616617
else
617-
cutpts[x - array_origin].assign_cheap (cutpts, array_origin, x,
618+
cutpts[x - array_origin].assign_cheap(&cutpts[0], array_origin, x,
618619
faking, mid_cut, offset,
619620
projection, projection_scale,
620621
zero_count, pitch,
@@ -631,7 +632,7 @@ double check_pitch_sync3( //find segmentation
631632
best_count = INT16_MAX;
632633
while (x < right_edge + pitch) {
633634
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,
635636
false, false, offset, projection,
636637
projection_scale, zero_count, pitch,
637638
pitch_error);
@@ -686,7 +687,5 @@ double check_pitch_sync3( //find segmentation
686687
if (seg_it.data ()->squares () - mean_sum < 0)
687688
tprintf ("Impossible sqsum=%g, mean=%g, total=%d\n",
688689
seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
689-
free_mem(mins);
690-
free_mem(cutpts);
691690
return seg_it.data ()->squares () - mean_sum;
692691
}

0 commit comments

Comments
 (0)