17
17
*
18
18
**********************************************************************/
19
19
20
+ #include < vector> // for std::vector
20
21
#include " stderr.h"
21
22
#include " blobbox.h"
22
23
#include " ccstruct.h"
@@ -288,12 +289,10 @@ void compute_page_skew( //get average gradient
288
289
float &page_m, // average gradient
289
290
float &page_err // average error
290
291
) {
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
297
296
TO_ROW *row; // current row
298
297
TO_BLOCK_IT block_it = blocks; // iterator
299
298
@@ -315,11 +314,10 @@ void compute_page_skew( //get average gradient
315
314
page_err = 0 .0f ;
316
315
return ;
317
316
}
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);
323
321
324
322
row_index = 0 ;
325
323
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
@@ -369,13 +367,11 @@ void compute_page_skew( //get average gradient
369
367
}
370
368
row_count = row_index;
371
369
row_index = choose_nth_item ((int32_t ) (row_count * textord_skew_ile),
372
- gradients, row_count);
370
+ & gradients[ 0 ] , row_count);
373
371
page_m = gradients[row_index];
374
372
row_index = choose_nth_item ((int32_t ) (row_count * textord_skew_ile),
375
- errors, row_count);
373
+ & errors[ 0 ] , row_count);
376
374
page_err = errors[row_index];
377
- free_mem (gradients);
378
- free_mem (errors);
379
375
}
380
376
381
377
const double kNoiseSize = 0.5 ; // Fraction of xheight.
@@ -580,8 +576,6 @@ void delete_non_dropout_rows( //find lines
580
576
bool testing_on // correct orientation
581
577
) {
582
578
TBOX block_box; // deskewed block
583
- int32_t *deltas; // change in occupation
584
- int32_t *occupation; // of pixel coords
585
579
int32_t max_y; // in block
586
580
int32_t min_y;
587
581
int32_t line_index; // of scan line
@@ -610,26 +604,26 @@ void delete_non_dropout_rows( //find lines
610
604
line_count = max_y - min_y + 1 ;
611
605
if (line_count <= 0 )
612
606
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 );
617
611
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 ] );
619
613
compute_occupation_threshold ((int32_t )
620
614
ceil (block->line_spacing *
621
615
(tesseract::CCStruct::kDescenderFraction +
622
616
tesseract::CCStruct::kAscenderFraction )),
623
617
(int32_t ) ceil (block->line_spacing *
624
618
(tesseract::CCStruct::kXHeightFraction +
625
619
tesseract::CCStruct::kAscenderFraction )),
626
- max_y - min_y + 1 , occupation, deltas);
620
+ max_y - min_y + 1 , & occupation[ 0 ], & deltas[ 0 ] );
627
621
#ifndef GRAPHICS_DISABLED
628
622
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 ] );
630
624
}
631
625
#endif
632
- compute_dropout_distances (occupation, deltas, line_count);
626
+ compute_dropout_distances (& occupation[ 0 ], & deltas[ 0 ] , line_count);
633
627
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
634
628
row = row_it.data ();
635
629
line_index = (int32_t ) floor (row->intercept ());
@@ -648,9 +642,6 @@ void delete_non_dropout_rows( //find lines
648
642
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
649
643
blob_it.add_list_after (row_it.data ()->blob_list ());
650
644
}
651
-
652
- free_mem (deltas);
653
- free_mem (occupation);
654
645
}
655
646
656
647
@@ -779,10 +770,10 @@ TBOX deskew_block_coords( //block box
779
770
void compute_line_occupation ( // project blobs
780
771
TO_BLOCK *block, // block to do
781
772
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
784
775
int32_t *occupation, // output projection
785
- int32_t *deltas // derivative
776
+ int32_t *deltas // derivative
786
777
) {
787
778
int32_t line_count; // maxy-miny+1
788
779
int32_t line_index; // of scan line
@@ -1162,11 +1153,8 @@ void compute_row_stats( //find lines
1162
1153
TO_ROW_IT row_it = block->get_rows ();
1163
1154
// number of rows
1164
1155
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);
1170
1158
rowcount = 0 ;
1171
1159
prev_row = nullptr ;
1172
1160
row_it.move_to_last (); // start at bottom
@@ -1190,13 +1178,13 @@ void compute_row_stats( //find lines
1190
1178
tprintf (" Blob based spacing=(%g,%g), offset=%g" ,
1191
1179
block->line_size , block->line_spacing , block->baseline_offset );
1192
1180
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,
1194
1182
sizeof (TO_ROW *), row_spacing_order);
1195
1183
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,
1197
1185
sizeof (TO_ROW *), row_spacing_order);
1198
1186
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,
1200
1188
sizeof (TO_ROW *), row_spacing_order);
1201
1189
block->key_row = rows[row_index];
1202
1190
if (testing_on)
@@ -1232,7 +1220,6 @@ void compute_row_stats( //find lines
1232
1220
if (testing_on)
1233
1221
tprintf (" \n Estimate line size=%g, spacing=%g, offset=%g\n " ,
1234
1222
block->line_size , block->line_spacing , block->baseline_offset );
1235
- free_mem (rows);
1236
1223
}
1237
1224
1238
1225
@@ -2070,26 +2057,25 @@ void Textord::make_spline_rows(TO_BLOCK* block, // block to do
2070
2057
*/
2071
2058
void make_baseline_spline (TO_ROW *row, // row to fit
2072
2059
TO_BLOCK *block) {
2073
- int32_t *xstarts; // spline boundaries
2074
2060
double *coeffs; // quadratic coeffs
2075
- int32_t segments; // no of segments
2061
+ int32_t segments; // no of segments
2076
2062
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 ] ;
2079
2065
if (segment_baseline (row, block, segments, xstarts)
2080
2066
&& !textord_straight_baselines && !textord_parallel_baselines) {
2081
2067
coeffs = linear_spline_baseline (row, block, segments, xstarts);
2082
2068
} else {
2083
2069
xstarts[1 ] = xstarts[segments];
2084
2070
segments = 1 ;
2085
- coeffs = ( double *) alloc_mem ( 3 * sizeof ( double )) ;
2071
+ coeffs = new double [ 3 ] ;
2086
2072
coeffs[0 ] = 0 ;
2087
2073
coeffs[1 ] = row->line_m ();
2088
2074
coeffs[2 ] = row->line_c ();
2089
2075
}
2090
2076
row->baseline = QSPLINE (segments, xstarts, coeffs);
2091
- free_mem ( coeffs) ;
2092
- free_mem ( xstarts) ;
2077
+ delete[] coeffs;
2078
+ delete[] xstarts;
2093
2079
}
2094
2080
2095
2081
@@ -2203,22 +2189,21 @@ double *
2203
2189
linear_spline_baseline ( // split baseline
2204
2190
TO_ROW * row, // row to fit
2205
2191
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
2208
2194
) {
2209
2195
int blobcount; // no of blobs
2210
2196
int blobindex; // current blob
2211
2197
int index1, index2; // blob numbers
2212
2198
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
2215
2201
// blobs
2216
2202
BLOBNBOX_IT blob_it = row->blob_list ();
2217
2203
BLOBNBOX_IT new_it = blob_it; // front end
2218
2204
float b, c; // fitted curve
2219
2205
tesseract::DetLineFit lms;
2220
- double *coeffs; // quadratic coeffs
2221
- int32_t segment; // current segment
2206
+ int32_t segment; // current segment
2222
2207
2223
2208
box = box_next_pre_chopped (&blob_it);
2224
2209
xstarts[0 ] = box.left ();
@@ -2231,7 +2216,8 @@ int32_t xstarts[] //coords of segments
2231
2216
if (segments < 1 )
2232
2217
segments = 1 ;
2233
2218
blobs_per_segment = blobcount / segments;
2234
- coeffs = (double *) alloc_mem (segments * 3 * sizeof (double ));
2219
+ // quadratic coeffs
2220
+ double *coeffs = new double [segments * 3 ];
2235
2221
if (textord_oldbl_debug)
2236
2222
tprintf
2237
2223
(" Linear splining baseline of %d blobs at (%d,%d), into %d segments of %d blobs\n " ,
0 commit comments