Skip to content

Commit 96772c5

Browse files
committed
Reduce scope of some local variables (reported by Codacy)
Apply also some smaller optimizations and add TODO comments for local variables which require further examination. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 5605293 commit 96772c5

File tree

9 files changed

+38
-54
lines changed

9 files changed

+38
-54
lines changed

src/api/pdfrenderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ static void GetWordBaseline(int writing_direction, int ppi, int height, int word
242242
double word_length;
243243
double x, y;
244244
{
245-
int px = word_x1;
246-
int py = word_y1;
247245
double l2 = dist2(line_x1, line_y1, line_x2, line_y2);
248246
if (l2 == 0) {
249247
x = line_x1;
250248
y = line_y1;
251249
} else {
250+
int px = word_x1;
251+
int py = word_y1;
252252
double t = ((px - line_x2) * (line_x2 - line_x1) + (py - line_y2) * (line_y2 - line_y1)) / l2;
253253
x = line_x2 + t * (line_x2 - line_x1);
254254
y = line_y2 + t * (line_y2 - line_y1);

src/ccmain/applybox.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ void Tesseract::MaximallyChopWord(const std::vector<TBOX> &boxes, BLOCK *block,
258258
}
259259
const double e = exp(1.0); // The base of natural logs.
260260
unsigned blob_number;
261-
int right_chop_index = 0;
262261
if (!assume_fixed_pitch_char_segment) {
263262
// We only chop if the language is not fixed pitch like CJK.
264263
SEAM *seam = nullptr;
264+
int right_chop_index = 0;
265265
while ((seam = chop_one_blob(boxes, blob_choices, word_res, &blob_number)) != nullptr) {
266266
word_res->InsertSeam(blob_number, seam);
267267
BLOB_CHOICE *left_choice = blob_choices[blob_number];
@@ -685,6 +685,7 @@ void Tesseract::SearchForText(const std::vector<BLOB_CHOICE_LIST *> *choices, in
685685
void Tesseract::TidyUp(PAGE_RES *page_res) {
686686
int ok_blob_count = 0;
687687
int bad_blob_count = 0;
688+
// TODO: check usage of ok_word_count.
688689
int ok_word_count = 0;
689690
int unlabelled_words = 0;
690691
PAGE_RES_IT pr_it(page_res);

src/ccmain/control.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ bool Tesseract::ReassignDiacritics(int pass, PAGE_RES_IT *pr_it, bool *make_next
949949
}
950950
real_word->AddSelectedOutlines(wanted, wanted_blobs, wanted_outlines, nullptr);
951951
AssignDiacriticsToNewBlobs(outlines, pass, real_word, pr_it, &word_wanted, &target_blobs);
952+
// TODO: check code.
952953
int non_overlapped = 0;
953954
int non_overlapped_used = 0;
954955
for (unsigned i = 0; i < word_wanted.size(); ++i) {
@@ -1121,9 +1122,9 @@ bool Tesseract::SelectGoodDiacriticOutlines(int pass, float certainty_threshold,
11211122
C_BLOB *blob,
11221123
const std::vector<C_OUTLINE *> &outlines,
11231124
int num_outlines, std::vector<bool> *ok_outlines) {
1124-
std::string best_str;
11251125
float target_cert = certainty_threshold;
11261126
if (blob != nullptr) {
1127+
std::string best_str;
11271128
float target_c2;
11281129
target_cert = ClassifyBlobAsWord(pass, pr_it, blob, best_str, &target_c2);
11291130
if (debug_noise_removal) {
@@ -1797,9 +1798,6 @@ Allow a single hyphen in a lower case word
17971798
}
17981799

17991800
bool Tesseract::check_debug_pt(WERD_RES *word, int location) {
1800-
bool show_map_detail = false;
1801-
int16_t i;
1802-
18031801
if (!test_pt) {
18041802
return false;
18051803
}
@@ -1811,6 +1809,7 @@ bool Tesseract::check_debug_pt(WERD_RES *word, int location) {
18111809
if (location < 0) {
18121810
return true; // For breakpoint use
18131811
}
1812+
bool show_map_detail = false;
18141813
tessedit_rejection_debug.set_value(true);
18151814
debug_x_ht_level.set_value(2);
18161815
tprintf("\n\nTESTWD::");
@@ -1864,7 +1863,7 @@ bool Tesseract::check_debug_pt(WERD_RES *word, int location) {
18641863
tprintf("\n");
18651864
if (show_map_detail) {
18661865
tprintf("\"%s\"\n", word->best_choice->unichar_string().c_str());
1867-
for (i = 0; word->best_choice->unichar_string()[i] != '\0'; i++) {
1866+
for (unsigned i = 0; word->best_choice->unichar_string()[i] != '\0'; i++) {
18681867
tprintf("**** \"%c\" ****\n", word->best_choice->unichar_string()[i]);
18691868
word->reject_map[i].full_print(debug_fp);
18701869
}
@@ -1891,13 +1890,12 @@ static void find_modal_font( // good chars in word
18911890
int16_t *font_out, // output font
18921891
int8_t *font_count // output count
18931892
) {
1894-
int16_t font; // font index
1895-
int32_t count; // pile count
1896-
18971893
if (fonts->get_total() > 0) {
1898-
font = static_cast<int16_t>(fonts->mode());
1894+
// font index
1895+
int16_t font = static_cast<int16_t>(fonts->mode());
18991896
*font_out = font;
1900-
count = fonts->pile_count(font);
1897+
// pile count
1898+
int32_t count = fonts->pile_count(font);
19011899
*font_count = count < INT8_MAX ? count : INT8_MAX;
19021900
fonts->add(font, -*font_count);
19031901
} else {

src/ccmain/docqual.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ int16_t Tesseract::word_blob_quality(WERD_RES *word) {
6060
}
6161

6262
int16_t Tesseract::word_outline_errs(WERD_RES *word) {
63-
int16_t i = 0;
6463
int16_t err_count = 0;
6564

6665
if (word->rebuild_word != nullptr) {
66+
int16_t i = 0;
6767
for (unsigned b = 0; b < word->rebuild_word->NumBlobs(); ++b) {
6868
TBLOB *blob = word->rebuild_word->blobs[b];
6969
err_count += count_outline_errs(word->best_choice->unichar_string()[i], blob->NumOutlines());
@@ -209,13 +209,8 @@ void Tesseract::unrej_good_quality_words( // unreject potential
209209

210210
void Tesseract::doc_and_block_rejection( // reject big chunks
211211
PAGE_RES_IT &page_res_it, bool good_quality_doc) {
212-
int16_t block_no = 0;
213-
int16_t row_no = 0;
214212
BLOCK_RES *current_block;
215-
ROW_RES *current_row;
216213

217-
bool rej_word;
218-
bool prev_word_rejected;
219214
int16_t char_quality = 0;
220215
int16_t accepted_char_quality;
221216

@@ -238,16 +233,17 @@ void Tesseract::doc_and_block_rejection( // reject big chunks
238233
WERD_RES *word;
239234
while ((word = page_res_it.word()) != nullptr) {
240235
current_block = page_res_it.block();
241-
block_no = current_block->block->pdblk.index();
236+
int16_t block_no = current_block->block->pdblk.index();
242237
if (current_block->char_count > 0 &&
243238
(current_block->rej_count * 100.0 / current_block->char_count) >
244239
tessedit_reject_block_percent) {
245240
if (tessedit_debug_block_rejection) {
246241
tprintf("REJECTING BLOCK %d #chars: %d; #Rejects: %d\n", block_no,
247242
current_block->char_count, current_block->rej_count);
248243
}
249-
prev_word_rejected = false;
244+
bool prev_word_rejected = false;
250245
while ((word = page_res_it.word()) != nullptr && (page_res_it.block() == current_block)) {
246+
bool rej_word;
251247
if (tessedit_preserve_blk_rej_perfect_wds) {
252248
rej_word = word->reject_map.reject_count() > 0 ||
253249
word->reject_map.length() < tessedit_preserve_min_wd_len;
@@ -284,9 +280,9 @@ void Tesseract::doc_and_block_rejection( // reject big chunks
284280
}
285281

286282
/* Walk rows in block testing for row rejection */
287-
row_no = 0;
283+
int16_t row_no = 0;
288284
while (page_res_it.word() != nullptr && page_res_it.block() == current_block) {
289-
current_row = page_res_it.row();
285+
ROW_RES *current_row = page_res_it.row();
290286
row_no++;
291287
/* Reject whole row if:
292288
fraction of chars on row which are rejected exceed a limit AND
@@ -302,9 +298,10 @@ void Tesseract::doc_and_block_rejection( // reject big chunks
302298
tprintf("REJECTING ROW %d #chars: %d; #Rejects: %d\n", row_no,
303299
current_row->char_count, current_row->rej_count);
304300
}
305-
prev_word_rejected = false;
301+
bool prev_word_rejected = false;
306302
while ((word = page_res_it.word()) != nullptr && page_res_it.row() == current_row) {
307303
/* Preserve words on good docs unless they are mostly rejected*/
304+
bool rej_word;
308305
if (!tessedit_row_rej_good_docs && good_quality_doc) {
309306
rej_word = word->reject_map.reject_count() /
310307
static_cast<float>(word->reject_map.length()) >
@@ -448,20 +445,18 @@ void Tesseract::tilde_crunch(PAGE_RES_IT &page_res_it) {
448445
}
449446

450447
bool Tesseract::terrible_word_crunch(WERD_RES *word, GARBAGE_LEVEL garbage_level) {
451-
float rating_per_ch;
452-
int adjusted_len;
453448
int crunch_mode = 0;
454449

455450
if (word->best_choice->unichar_string().empty() ||
456451
(strspn(word->best_choice->unichar_string().c_str(), " ") ==
457452
word->best_choice->unichar_string().size())) {
458453
crunch_mode = 1;
459454
} else {
460-
adjusted_len = word->reject_map.length();
455+
int adjusted_len = word->reject_map.length();
461456
if (adjusted_len > crunch_rating_max) {
462457
adjusted_len = crunch_rating_max;
463458
}
464-
rating_per_ch = word->best_choice->rating() / adjusted_len;
459+
float rating_per_ch = word->best_choice->rating() / adjusted_len;
465460

466461
if (rating_per_ch > crunch_terrible_rating) {
467462
crunch_mode = 2;
@@ -528,7 +523,6 @@ bool Tesseract::potential_word_crunch(WERD_RES *word, GARBAGE_LEVEL garbage_leve
528523
}
529524

530525
void Tesseract::tilde_delete(PAGE_RES_IT &page_res_it) {
531-
WERD_RES *word;
532526
PAGE_RES_IT copy_it;
533527
bool deleting_from_bol = false;
534528
bool marked_delete_point = false;
@@ -539,7 +533,7 @@ void Tesseract::tilde_delete(PAGE_RES_IT &page_res_it) {
539533

540534
page_res_it.restart_page();
541535
while (page_res_it.word() != nullptr) {
542-
word = page_res_it.word();
536+
WERD_RES *word = page_res_it.word();
543537

544538
delete_mode = word_deletable(word, debug_delete_mode);
545539
if (delete_mode != CR_NONE) {

src/ccmain/fixspace.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ void Tesseract::fix_fuzzy_spaces(ETEXT_DESC *monitor, int32_t word_count, PAGE_R
171171
void Tesseract::fix_fuzzy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK *block) {
172172
int16_t best_score;
173173
WERD_RES_LIST current_perm;
174-
int16_t current_score;
175174
bool improved = false;
176175

177176
best_score = eval_word_spacing(best_perm); // default score
@@ -183,7 +182,7 @@ void Tesseract::fix_fuzzy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK *
183182

184183
while ((best_score != PERFECT_WERDS) && !current_perm.empty()) {
185184
match_current_words(current_perm, row, block);
186-
current_score = eval_word_spacing(current_perm);
185+
int16_t current_score = eval_word_spacing(current_perm);
187186
dump_words(current_perm, current_score, 2, improved);
188187
if (current_score > best_score) {
189188
best_perm.clear();
@@ -201,11 +200,10 @@ void Tesseract::fix_fuzzy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK *
201200
void initialise_search(WERD_RES_LIST &src_list, WERD_RES_LIST &new_list) {
202201
WERD_RES_IT src_it(&src_list);
203202
WERD_RES_IT new_it(&new_list);
204-
WERD_RES *src_wd;
205203
WERD_RES *new_wd;
206204

207205
for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) {
208-
src_wd = src_it.data();
206+
WERD_RES *src_wd = src_it.data();
209207
if (!src_wd->combination) {
210208
new_wd = WERD_RES::deep_copy(src_wd);
211209
new_wd->combination = false;
@@ -393,8 +391,6 @@ void transform_to_next_perm(WERD_RES_LIST &words) {
393391
WERD_RES_IT prev_word_it(&words);
394392
WERD_RES *word;
395393
WERD_RES *prev_word;
396-
WERD_RES *combo;
397-
WERD *copy_word;
398394
int16_t prev_right = -INT16_MAX;
399395
TBOX box;
400396
int16_t gap;
@@ -425,12 +421,13 @@ void transform_to_next_perm(WERD_RES_LIST &words) {
425421
gap = box.left() - prev_right;
426422
if (gap <= min_gap) {
427423
prev_word = prev_word_it.data();
424+
WERD_RES *combo;
428425
if (prev_word->combination) {
429426
combo = prev_word;
430427
} else {
431428
/* Make a new combination and insert before
432429
* the first word being joined. */
433-
copy_word = new WERD;
430+
auto *copy_word = new WERD;
434431
*copy_word = *(prev_word->word);
435432
// deep copy
436433
combo = new WERD_RES(copy_word);
@@ -546,7 +543,6 @@ void Tesseract::fix_sp_fp_word(WERD_RES_IT &word_res_it, ROW *row, BLOCK *block)
546543
WERD_RES *word_res;
547544
WERD_RES_LIST sub_word_list;
548545
WERD_RES_IT sub_word_list_it(&sub_word_list);
549-
int16_t blob_index;
550546
int16_t new_length;
551547
float junk;
552548

@@ -556,7 +552,7 @@ void Tesseract::fix_sp_fp_word(WERD_RES_IT &word_res_it, ROW *row, BLOCK *block)
556552
return;
557553
}
558554

559-
blob_index = worst_noise_blob(word_res, &junk);
555+
auto blob_index = worst_noise_blob(word_res, &junk);
560556
if (blob_index < 0) {
561557
return;
562558
}
@@ -623,7 +619,6 @@ void Tesseract::break_noisiest_blob_word(WERD_RES_LIST &words) {
623619
WERD_RES_IT worst_word_it;
624620
float worst_noise_score = 9999;
625621
int worst_blob_index = -1; // Noisiest blob of noisiest wd
626-
int blob_index; // of wds noisiest blob
627622
float noise_score; // of wds noisiest blob
628623
WERD_RES *word_res;
629624
C_BLOB_IT blob_it;
@@ -636,7 +631,7 @@ void Tesseract::break_noisiest_blob_word(WERD_RES_LIST &words) {
636631
int16_t i;
637632

638633
for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
639-
blob_index = worst_noise_blob(word_it.data(), &noise_score);
634+
auto blob_index = worst_noise_blob(word_it.data(), &noise_score);
640635
if (blob_index > -1 && worst_noise_score > noise_score) {
641636
worst_noise_score = noise_score;
642637
worst_blob_index = blob_index;
@@ -806,7 +801,6 @@ float Tesseract::blob_noise_score(TBLOB *blob) {
806801
void fixspace_dbg(WERD_RES *word) {
807802
TBOX box = word->word->bounding_box();
808803
const bool show_map_detail = false;
809-
int16_t i;
810804

811805
box.print();
812806
tprintf(" \"%s\" ", word->best_choice->unichar_string().c_str());
@@ -816,7 +810,7 @@ void fixspace_dbg(WERD_RES *word) {
816810
tprintf("\n");
817811
if (show_map_detail) {
818812
tprintf("\"%s\"\n", word->best_choice->unichar_string().c_str());
819-
for (i = 0; word->best_choice->unichar_string()[i] != '\0'; i++) {
813+
for (unsigned i = 0; word->best_choice->unichar_string()[i] != '\0'; i++) {
820814
tprintf("**** \"%c\" ****\n", word->best_choice->unichar_string()[i]);
821815
word->reject_map[i].full_print(debug_fp);
822816
}

src/ccmain/output.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ void Tesseract::write_results(PAGE_RES_IT &page_res_it,
101101
bool force_eol) { // override tilde crunch?
102102
WERD_RES *word = page_res_it.word();
103103
const UNICHARSET &uchset = *word->uch_set;
104-
bool need_reject = false;
105104
UNICHAR_ID space = uchset.unichar_to_id(" ");
106105

107106
if ((word->unlv_crunch_mode != CR_NONE || word->best_choice->empty()) &&
108107
!tessedit_zero_kelvin_rejection && !tessedit_word_for_word) {
108+
bool need_reject = false;
109109
if ((word->unlv_crunch_mode != CR_DELETE) &&
110110
(!stats_.tilde_crunch_written ||
111111
((word->unlv_crunch_mode == CR_KEEP_SPACE) && (word->word->space() > 0) &&

src/ccmain/paragraphs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2407,8 +2407,8 @@ static void InitializeTextAndBoxesPreRecognition(const MutableIterator &it, RowI
24072407
// Set up text, lword_text, and rword_text (mostly for debug printing).
24082408
std::string fake_text;
24092409
PageIterator pit(static_cast<const PageIterator &>(it));
2410-
bool first_word = true;
24112410
if (!pit.Empty(RIL_WORD)) {
2411+
bool first_word = true;
24122412
do {
24132413
fake_text += "x";
24142414
if (first_word) {

src/ccmain/pgedit.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,7 @@ bool Tesseract::word_display(PAGE_RES_IT *pr_it) {
703703
WERD_RES *word_res = pr_it->word();
704704
WERD *word = word_res->word;
705705
TBOX word_bb; // word bounding box
706-
int word_height; // ht of word BB
707706
bool displayed_something = false;
708-
float shift; // from bot left
709707

710708
if (color_mode != CM_RAINBOW && word_res->box_word != nullptr) {
711709
# ifndef DISABLED_LEGACY_ENGINE
@@ -842,13 +840,14 @@ bool Tesseract::word_display(PAGE_RES_IT *pr_it) {
842840
if (text.length() > 0) {
843841
word_bb = word->bounding_box();
844842
image_win->Pen(ScrollView::RED);
845-
word_height = word_bb.height();
846-
int text_height = 0.50 * word_height;
843+
auto word_height = word_bb.height();
844+
int text_height = word_height / 2;
847845
if (text_height > 20) {
848846
text_height = 20;
849847
}
850848
image_win->TextAttributes("Arial", text_height, false, false, false);
851-
shift = (word_height < word_bb.width()) ? 0.25 * word_height : 0.0f;
849+
// from bot left
850+
float shift = (word_height < word_bb.width()) ? 0.25f * word_height : 0.0f;
852851
image_win->Text(word_bb.left() + shift, word_bb.bottom() + 0.25 * word_height, text.c_str());
853852
if (blame.length() > 0) {
854853
image_win->Text(word_bb.left() + shift, word_bb.bottom() + 0.25 * word_height - text_height,

src/ccmain/reject.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,6 @@ bool Tesseract::one_ell_conflict(WERD_RES *word_res, bool update_map) {
293293
int16_t i;
294294
int16_t offset;
295295
bool non_conflict_set_char; // non conf set a/n?
296-
bool conflict = false;
297-
bool allow_1s;
298296
ACCEPTABLE_WERD_TYPE word_type;
299297
bool dict_perm_type;
300298
bool dict_word_ok;
@@ -411,11 +409,11 @@ bool Tesseract::one_ell_conflict(WERD_RES *word_res, bool update_map) {
411409
Else reject all conflict chs
412410
*/
413411
if (word_contains_non_1_digit(word, lengths)) {
414-
allow_1s =
412+
bool allow_1s =
415413
(alpha_count(word, lengths) == 0) || (word_res->best_choice->permuter() == NUMBER_PERM);
416414

417415
int16_t offset;
418-
conflict = false;
416+
bool conflict = false;
419417
for (i = 0, offset = 0; word[offset] != '\0';
420418
offset += word_res->best_choice->unichar_lengths()[i++]) {
421419
if ((!allow_1s || (word[offset] != '1')) &&

0 commit comments

Comments
 (0)