Skip to content

Commit 77db9b4

Browse files
committed
makerow: Replace alloc_mem, free_mem by C++ new, delete, std::vector
Signed-off-by: Stefan Weil <[email protected]>
1 parent 556a1c1 commit 77db9b4

File tree

1 file changed

+40
-54
lines changed

1 file changed

+40
-54
lines changed

src/textord/makerow.cpp

+40-54
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
**********************************************************************/
1919

20+
#include <vector> // for std::vector
2021
#include "stderr.h"
2122
#include "blobbox.h"
2223
#include "ccstruct.h"
@@ -288,12 +289,10 @@ void compute_page_skew( //get average gradient
288289
float &page_m, //average gradient
289290
float &page_err //average error
290291
) {
291-
int32_t row_count; //total rows
292-
int32_t blob_count; //total_blobs
293-
int32_t row_err; //integer error
294-
float *gradients; //of rows
295-
float *errors; //of rows
296-
int32_t row_index; //of total
292+
int32_t row_count; //total rows
293+
int32_t blob_count; //total_blobs
294+
int32_t row_err; //integer error
295+
int32_t row_index; //of total
297296
TO_ROW *row; //current row
298297
TO_BLOCK_IT block_it = blocks; //iterator
299298

@@ -315,11 +314,10 @@ void compute_page_skew( //get average gradient
315314
page_err = 0.0f;
316315
return;
317316
}
318-
gradients = (float *) alloc_mem (blob_count * sizeof (float));
319-
//get mem
320-
errors = (float *) alloc_mem (blob_count * sizeof (float));
321-
if (gradients == nullptr || errors == nullptr)
322-
MEMORY_OUT.error ("compute_page_skew", ABORT, nullptr);
317+
// of rows
318+
std::vector<float> gradients(blob_count);
319+
// of rows
320+
std::vector<float> errors(blob_count);
323321

324322
row_index = 0;
325323
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
@@ -369,13 +367,11 @@ void compute_page_skew( //get average gradient
369367
}
370368
row_count = row_index;
371369
row_index = choose_nth_item ((int32_t) (row_count * textord_skew_ile),
372-
gradients, row_count);
370+
&gradients[0], row_count);
373371
page_m = gradients[row_index];
374372
row_index = choose_nth_item ((int32_t) (row_count * textord_skew_ile),
375-
errors, row_count);
373+
&errors[0], row_count);
376374
page_err = errors[row_index];
377-
free_mem(gradients);
378-
free_mem(errors);
379375
}
380376

381377
const double kNoiseSize = 0.5; // Fraction of xheight.
@@ -580,8 +576,6 @@ void delete_non_dropout_rows( //find lines
580576
bool testing_on //correct orientation
581577
) {
582578
TBOX block_box; //deskewed block
583-
int32_t *deltas; //change in occupation
584-
int32_t *occupation; //of pixel coords
585579
int32_t max_y; //in block
586580
int32_t min_y;
587581
int32_t line_index; //of scan line
@@ -610,26 +604,26 @@ void delete_non_dropout_rows( //find lines
610604
line_count = max_y - min_y + 1;
611605
if (line_count <= 0)
612606
return; //empty block
613-
deltas = (int32_t *) alloc_mem (line_count * sizeof (int32_t));
614-
occupation = (int32_t *) alloc_mem (line_count * sizeof (int32_t));
615-
if (deltas == nullptr || occupation == nullptr)
616-
MEMORY_OUT.error ("compute_line_spacing", ABORT, nullptr);
607+
// change in occupation
608+
std::vector<int32_t> deltas(line_count);
609+
// of pixel coords
610+
std::vector<int32_t> occupation(line_count);
617611

618-
compute_line_occupation(block, gradient, min_y, max_y, occupation, deltas);
612+
compute_line_occupation(block, gradient, min_y, max_y, &occupation[0], &deltas[0]);
619613
compute_occupation_threshold ((int32_t)
620614
ceil (block->line_spacing *
621615
(tesseract::CCStruct::kDescenderFraction +
622616
tesseract::CCStruct::kAscenderFraction)),
623617
(int32_t) ceil (block->line_spacing *
624618
(tesseract::CCStruct::kXHeightFraction +
625619
tesseract::CCStruct::kAscenderFraction)),
626-
max_y - min_y + 1, occupation, deltas);
620+
max_y - min_y + 1, &occupation[0], &deltas[0]);
627621
#ifndef GRAPHICS_DISABLED
628622
if (testing_on) {
629-
draw_occupation(xleft, ybottom, min_y, max_y, occupation, deltas);
623+
draw_occupation(xleft, ybottom, min_y, max_y, &occupation[0], &deltas[0]);
630624
}
631625
#endif
632-
compute_dropout_distances(occupation, deltas, line_count);
626+
compute_dropout_distances(&occupation[0], &deltas[0], line_count);
633627
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
634628
row = row_it.data ();
635629
line_index = (int32_t) floor (row->intercept ());
@@ -648,9 +642,6 @@ void delete_non_dropout_rows( //find lines
648642
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
649643
blob_it.add_list_after (row_it.data ()->blob_list ());
650644
}
651-
652-
free_mem(deltas);
653-
free_mem(occupation);
654645
}
655646

656647

@@ -779,10 +770,10 @@ TBOX deskew_block_coords( //block box
779770
void compute_line_occupation( //project blobs
780771
TO_BLOCK *block, //block to do
781772
float gradient, //global skew
782-
int32_t min_y, //min coord in block
783-
int32_t max_y, //in block
773+
int32_t min_y, //min coord in block
774+
int32_t max_y, //in block
784775
int32_t *occupation, //output projection
785-
int32_t *deltas //derivative
776+
int32_t *deltas //derivative
786777
) {
787778
int32_t line_count; //maxy-miny+1
788779
int32_t line_index; //of scan line
@@ -1162,11 +1153,8 @@ void compute_row_stats( //find lines
11621153
TO_ROW_IT row_it = block->get_rows ();
11631154
//number of rows
11641155
int16_t rowcount = row_it.length ();
1165-
TO_ROW **rows; //for choose nth
1166-
1167-
rows = (TO_ROW **) alloc_mem (rowcount * sizeof (TO_ROW *));
1168-
if (rows == nullptr)
1169-
MEMORY_OUT.error ("compute_row_stats", ABORT, nullptr);
1156+
// for choose nth
1157+
std::vector<TO_ROW*> rows(rowcount);
11701158
rowcount = 0;
11711159
prev_row = nullptr;
11721160
row_it.move_to_last (); //start at bottom
@@ -1190,13 +1178,13 @@ void compute_row_stats( //find lines
11901178
tprintf ("Blob based spacing=(%g,%g), offset=%g",
11911179
block->line_size, block->line_spacing, block->baseline_offset);
11921180
if (rowcount > 0) {
1193-
row_index = choose_nth_item (rowcount * 3 / 4, rows, rowcount,
1181+
row_index = choose_nth_item(rowcount * 3 / 4, &rows[0], rowcount,
11941182
sizeof (TO_ROW *), row_spacing_order);
11951183
iqr = rows[row_index]->spacing;
1196-
row_index = choose_nth_item (rowcount / 4, rows, rowcount,
1184+
row_index = choose_nth_item(rowcount / 4, &rows[0], rowcount,
11971185
sizeof (TO_ROW *), row_spacing_order);
11981186
iqr -= rows[row_index]->spacing;
1199-
row_index = choose_nth_item (rowcount / 2, rows, rowcount,
1187+
row_index = choose_nth_item(rowcount / 2, &rows[0], rowcount,
12001188
sizeof (TO_ROW *), row_spacing_order);
12011189
block->key_row = rows[row_index];
12021190
if (testing_on)
@@ -1232,7 +1220,6 @@ void compute_row_stats( //find lines
12321220
if (testing_on)
12331221
tprintf ("\nEstimate line size=%g, spacing=%g, offset=%g\n",
12341222
block->line_size, block->line_spacing, block->baseline_offset);
1235-
free_mem(rows);
12361223
}
12371224

12381225

@@ -2070,26 +2057,25 @@ void Textord::make_spline_rows(TO_BLOCK* block, // block to do
20702057
*/
20712058
void make_baseline_spline(TO_ROW *row, //row to fit
20722059
TO_BLOCK *block) {
2073-
int32_t *xstarts; // spline boundaries
20742060
double *coeffs; // quadratic coeffs
2075-
int32_t segments; // no of segments
2061+
int32_t segments; // no of segments
20762062

2077-
xstarts =
2078-
(int32_t *) alloc_mem((row->blob_list()->length() + 1) * sizeof(int32_t));
2063+
// spline boundaries
2064+
int32_t *xstarts = new int32_t[row->blob_list()->length() + 1];
20792065
if (segment_baseline(row, block, segments, xstarts)
20802066
&& !textord_straight_baselines && !textord_parallel_baselines) {
20812067
coeffs = linear_spline_baseline(row, block, segments, xstarts);
20822068
} else {
20832069
xstarts[1] = xstarts[segments];
20842070
segments = 1;
2085-
coeffs = (double *) alloc_mem (3 * sizeof (double));
2071+
coeffs = new double[3];
20862072
coeffs[0] = 0;
20872073
coeffs[1] = row->line_m ();
20882074
coeffs[2] = row->line_c ();
20892075
}
20902076
row->baseline = QSPLINE (segments, xstarts, coeffs);
2091-
free_mem(coeffs);
2092-
free_mem(xstarts);
2077+
delete[] coeffs;
2078+
delete[] xstarts;
20932079
}
20942080

20952081

@@ -2203,22 +2189,21 @@ double *
22032189
linear_spline_baseline ( //split baseline
22042190
TO_ROW * row, //row to fit
22052191
TO_BLOCK * block, //block it came from
2206-
int32_t & segments, //no fo segments
2207-
int32_t xstarts[] //coords of segments
2192+
int32_t & segments, //no fo segments
2193+
int32_t xstarts[] //coords of segments
22082194
) {
22092195
int blobcount; //no of blobs
22102196
int blobindex; //current blob
22112197
int index1, index2; //blob numbers
22122198
int blobs_per_segment; //blobs in each
2213-
TBOX box; //blob box
2214-
TBOX new_box; //new_it box
2199+
TBOX box; //blob box
2200+
TBOX new_box; //new_it box
22152201
//blobs
22162202
BLOBNBOX_IT blob_it = row->blob_list ();
22172203
BLOBNBOX_IT new_it = blob_it; //front end
22182204
float b, c; //fitted curve
22192205
tesseract::DetLineFit lms;
2220-
double *coeffs; //quadratic coeffs
2221-
int32_t segment; //current segment
2206+
int32_t segment; //current segment
22222207

22232208
box = box_next_pre_chopped (&blob_it);
22242209
xstarts[0] = box.left ();
@@ -2231,7 +2216,8 @@ int32_t xstarts[] //coords of segments
22312216
if (segments < 1)
22322217
segments = 1;
22332218
blobs_per_segment = blobcount / segments;
2234-
coeffs = (double *) alloc_mem (segments * 3 * sizeof (double));
2219+
// quadratic coeffs
2220+
double *coeffs = new double[segments * 3];
22352221
if (textord_oldbl_debug)
22362222
tprintf
22372223
("Linear splining baseline of %d blobs at (%d,%d), into %d segments of %d blobs\n",

0 commit comments

Comments
 (0)