Skip to content

Commit

Permalink
Modernize code by replacing C type casts
Browse files Browse the repository at this point in the history
This was done using clang-tidy.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Apr 7, 2019
1 parent 95a15a7 commit 72c8741
Show file tree
Hide file tree
Showing 60 changed files with 411 additions and 418 deletions.
6 changes: 3 additions & 3 deletions src/api/pdfrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ bool TessPDFRenderer::BeginDocumentHandler() {
" /Length %lu /Filter /FlateDecode\n"
">>\n"
"stream\n",
(unsigned long)len);
static_cast<unsigned long>(len));
if (n >= sizeof(buf)) {
lept_free(comp);
return false;
Expand Down Expand Up @@ -624,7 +624,7 @@ bool TessPDFRenderer::BeginDocumentHandler() {
"stream\n"
"%s"
"endstream\n"
"endobj\n", (unsigned long) strlen(stream), stream);
"endobj\n", static_cast<unsigned long>(strlen(stream)), stream);
if (n >= sizeof(buf)) return false;
AppendPDFObject(buf);

Expand Down Expand Up @@ -781,7 +781,7 @@ bool TessPDFRenderer::imageToPDFObj(Pix *pix,
"<<\n"
" /Length %ld\n"
" /Subtype /Image\n",
objnum, (unsigned long) cid->nbytescomp);
objnum, static_cast<unsigned long>(cid->nbytescomp));
if (n >= sizeof(b1)) {
l_CIDataDestroy(&cid);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,7 @@ static void find_modal_font( // good chars in word
int32_t count; //pile count

if (fonts->get_total () > 0) {
font = (int16_t) fonts->mode ();
font = static_cast<int16_t>(fonts->mode ());
*font_out = font;
count = fonts->pile_count (font);
*font_count = count < INT8_MAX ? count : INT8_MAX;
Expand Down
4 changes: 2 additions & 2 deletions src/ccmain/docqual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void Tesseract::unrej_good_quality_words( //unreject potential
}
else if ((page_res_it.row ()->char_count > 0) &&
((page_res_it.row ()->rej_count /
(float) page_res_it.row ()->char_count) <=
static_cast<float>(page_res_it.row ()->char_count)) <=
quality_rowrej_pc)) {
word = page_res_it.word ();
if (word->reject_map.quality_recoverable_rejects() &&
Expand Down Expand Up @@ -863,7 +863,7 @@ GARBAGE_LEVEL Tesseract::garbage_word(WERD_RES *word, bool ok_dict_word) {
if (len > 4) {
dodgy_chars = 2 * tess_rejs + bad_char_count + isolated_digits +
isolated_alphas;
if (dodgy_chars > 5 || (dodgy_chars / (float) len) > 0.5)
if (dodgy_chars > 5 || (dodgy_chars / static_cast<float>(len)) > 0.5)
return G_DODGY;
else
return G_OK;
Expand Down
4 changes: 2 additions & 2 deletions src/ccmain/fixspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ void Tesseract::dump_words(WERD_RES_LIST &perm, int16_t score,
if (!word_res_it.data()->part_of_combo) {
tprintf("%s/%1d ",
word_res_it.data()->best_choice->unichar_string().string(),
(int)word_res_it.data()->best_choice->permuter());
static_cast<int>(word_res_it.data()->best_choice->permuter()));
}
}
tprintf("\"\n");
Expand All @@ -520,7 +520,7 @@ void Tesseract::dump_words(WERD_RES_LIST &perm, int16_t score,
if (!word_res_it.data()->part_of_combo) {
tprintf("%s/%1d ",
word_res_it.data()->best_choice->unichar_string().string(),
(int)word_res_it.data()->best_choice->permuter());
static_cast<int>(word_res_it.data()->best_choice->permuter()));
}
}
tprintf("\"\n");
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ char determine_newline_type( //test line ends
block_box = block->pdblk.bounding_box ();
//gap to eol
end_gap = block_box.right () - word_box.right ();
end_gap -= (int32_t) block->space ();
end_gap -= static_cast<int32_t>(block->space ());
width = next_box.right () - next_box.left ();
// tprintf("end_gap=%d-%d=%d, width=%d-%d=%d, nl=%d\n",
// block_box.right(),word_box.right(),end_gap,
Expand Down
3 changes: 1 addition & 2 deletions src/ccmain/paramsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// File: paramsd.cpp
// Description: Tesseract parameter Editor
// Author: Joern Wanke
// Created: Wed Jul 18 10:05:01 PDT 2007
//
// (C) Copyright 2007, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -140,7 +139,7 @@ STRING ParamContent::GetValue() const {
} else if (param_type_ == VT_DOUBLE) {
result.add_str_double("", *dIt);
} else if (param_type_ == VT_STRING) {
if (((STRING) * (sIt)).string() != nullptr) {
if (STRING(*(sIt)).string() != nullptr) {
result = sIt->string();
} else {
result = "Null";
Expand Down
13 changes: 6 additions & 7 deletions src/ccmain/pgedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ bool Tesseract::process_cmd_win_event( // UI command semantics
case RECOG_WERDS:
case RECOG_PSEUDO:
case SHOW_BLOB_FEATURES:
mode =(CMD_EVENTS) cmd_event;
mode =static_cast<CMD_EVENTS>(cmd_event);
break;
case DEBUG_WERD_CMD_EVENT:
mode = DEBUG_WERD_CMD_EVENT;
Expand Down Expand Up @@ -822,13 +822,12 @@ bool Tesseract::word_display(PAGE_RES_IT* pr_it) {
// display bounding box
if (word->display_flag(DF_BOX)) {
word->bounding_box().plot(image_win,
(ScrollView::Color)((int32_t)
static_cast<ScrollView::Color>((int32_t)
editor_image_word_bb_color),
(ScrollView::Color)((int32_t)
static_cast<ScrollView::Color>((int32_t)
editor_image_word_bb_color));

auto c = (ScrollView::Color)
((int32_t) editor_image_blob_bb_color);
auto c = static_cast<ScrollView::Color>((int32_t) editor_image_blob_bb_color);
image_win->Pen(c);
// cblob iterator
C_BLOB_IT c_it(word->cblob_list());
Expand Down Expand Up @@ -904,8 +903,8 @@ bool Tesseract::word_display(PAGE_RES_IT* pr_it) {

if (!displayed_something) // display BBox anyway
word->bounding_box().plot(image_win,
(ScrollView::Color)((int32_t) editor_image_word_bb_color),
(ScrollView::Color)((int32_t)
static_cast<ScrollView::Color>((int32_t) editor_image_word_bb_color),
static_cast<ScrollView::Color>((int32_t)
editor_image_word_bb_color));
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ccmain/reject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ int16_t Tesseract::count_alphanums(WERD_RES *word_res) {
void Tesseract::reject_mostly_rejects(WERD_RES *word) {
/* Reject the whole of the word if the fraction of rejects exceeds a limit */

if ((float) word->reject_map.reject_count() / word->reject_map.length() >=
if (static_cast<float>(word->reject_map.reject_count()) / word->reject_map.length() >=
rej_whole_of_mostly_reject_word_fract)
word->reject_map.rej_word_mostly_rej();
}
Expand Down Expand Up @@ -640,7 +640,7 @@ void Tesseract::flip_hyphens(WERD_RES *word_res) {
// Don't touch small or touching blobs - it is too dangerous.
if ((out_box.width() > 8 * word_res->denorm.x_scale()) &&
(out_box.left() > prev_right) && (out_box.right() < next_left)) {
aspect_ratio = out_box.width() / (float) out_box.height();
aspect_ratio = out_box.width() / static_cast<float>(out_box.height());
if (word_res->uch_set->eq(best_choice->unichar_id(i), ".")) {
if (aspect_ratio >= tessedit_upper_flip_hyphen &&
word_res->uch_set->contains_unichar_id(unichar_dash) &&
Expand Down
26 changes: 13 additions & 13 deletions src/ccstruct/blobbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ void BLOBNBOX::chop( //chop blobs
BLOBNBOX_IT blob_it; //blob iterator

//get no of chops
blobcount = (int16_t) floor (box.width () / xheight);
blobcount = static_cast<int16_t>(floor (box.width () / xheight));
if (blobcount > 1 && cblob_ptr != nullptr) {
//width of each
blobwidth = (float) (box.width () + 1) / blobcount;
blobwidth = static_cast<float>(box.width () + 1) / blobcount;
for (blobindex = blobcount - 1, rightx = box.right ();
blobindex >= 0; blobindex--, rightx -= blobwidth) {
ymin = (float) INT32_MAX;
ymax = (float) -INT32_MAX;
ymin = static_cast<float>(INT32_MAX);
ymax = static_cast<float>(-INT32_MAX);
blob_it = *start_it;
do {
blob = blob_it.data ();
Expand All @@ -155,11 +155,11 @@ void BLOBNBOX::chop( //chop blobs
}
while (blob != end_it->data ());
if (ymin < ymax) {
leftx = (int16_t) floor (rightx - blobwidth);
leftx = static_cast<int16_t>(floor (rightx - blobwidth));
if (leftx < box.left ())
leftx = box.left (); //clip to real box
bl = ICOORD (leftx, (int16_t) floor (ymin));
tr = ICOORD ((int16_t) ceil (rightx), (int16_t) ceil (ymax));
bl = ICOORD (leftx, static_cast<int16_t>(floor (ymin)));
tr = ICOORD (static_cast<int16_t>(ceil (rightx)), static_cast<int16_t>(ceil (ymax)));
if (blobindex == 0)
box = TBOX (bl, tr); //change box
else {
Expand Down Expand Up @@ -510,8 +510,8 @@ void find_cblob_limits( //get y limits
//outlines
C_OUTLINE_IT out_it = blob->out_list ();

ymin = (float) INT32_MAX;
ymax = (float) -INT32_MAX;
ymin = static_cast<float>(INT32_MAX);
ymax = static_cast<float>(-INT32_MAX);
for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
outline = out_it.data ();
pos = outline->start_pos (); //get coords
Expand Down Expand Up @@ -549,8 +549,8 @@ void find_cblob_vlimits( //get y limits
//outlines
C_OUTLINE_IT out_it = blob->out_list ();

ymin = (float) INT32_MAX;
ymax = (float) -INT32_MAX;
ymin = static_cast<float>(INT32_MAX);
ymax = static_cast<float>(-INT32_MAX);
for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
outline = out_it.data ();
pos = outline->start_pos (); //get coords
Expand Down Expand Up @@ -586,8 +586,8 @@ void find_cblob_hlimits( //get x limits
//outlines
C_OUTLINE_IT out_it = blob->out_list ();

xmin = (float) INT32_MAX;
xmax = (float) -INT32_MAX;
xmin = static_cast<float>(INT32_MAX);
xmax = static_cast<float>(-INT32_MAX);
for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
outline = out_it.data ();
pos = outline->start_pos (); //get coords
Expand Down
4 changes: 2 additions & 2 deletions src/ccstruct/blread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ bool read_unlv_file( //print list of sides
while (tfscanf(pdfp, "%d %d %d %d %*s", &x, &y, &width, &height) >= 4) {
//make rect block
block = new BLOCK (name.string (), true, 0, 0,
(int16_t) x, (int16_t) (ysize - y - height),
(int16_t) (x + width), (int16_t) (ysize - y));
static_cast<int16_t>(x), static_cast<int16_t>(ysize - y - height),
static_cast<int16_t>(x + width), static_cast<int16_t>(ysize - y));
//on end of list
block_it.add_to_end (block);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ccstruct/coutln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ C_OUTLINE::C_OUTLINE(CRACKEDGE* startpt, ICOORD bot_left, ICOORD top_right,
return;
}
//get memory
steps = (uint8_t *)calloc(step_mem(), 1);
steps = static_cast<uint8_t *>(calloc(step_mem(), 1));
edgept = startpt;

for (stepindex = 0; stepindex < length; stepindex++) {
Expand Down Expand Up @@ -159,7 +159,7 @@ C_OUTLINE::C_OUTLINE(C_OUTLINE* srcline, FCOORD rotation) : offsets(nullptr) {
return;
}
//get memory
steps = (uint8_t *)calloc(step_mem(), 1);
steps = static_cast<uint8_t *>(calloc(step_mem(), 1));

for (int iteration = 0; iteration < 2; ++iteration) {
DIR128 round1 = iteration == 0 ? 32 : 0;
Expand Down Expand Up @@ -1012,7 +1012,7 @@ C_OUTLINE& C_OUTLINE::operator=(const C_OUTLINE& source) {
start = source.start;
free(steps);
stepcount = source.stepcount;
steps = (uint8_t *)malloc(step_mem());
steps = static_cast<uint8_t *>(malloc(step_mem()));
memmove (steps, source.steps, step_mem());
if (!children.empty ())
children.clear ();
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/coutln.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class DLLSYM C_OUTLINE:public ELIST_LINK {
}
// Return step at a given index as a DIR128.
DIR128 step_dir(int index) const {
return DIR128((int16_t)(((steps[index/4] >> (index%4 * 2)) & STEP_MASK) <<
return DIR128(static_cast<int16_t>(((steps[index/4] >> (index%4 * 2)) & STEP_MASK) <<
(DIRBITS - 2)));
}
// Return the step vector for the given outline position.
Expand Down
6 changes: 3 additions & 3 deletions src/ccstruct/mod128.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DLLSYM DIR128
value %= MODULUS; //modulo arithmetic
if (value < 0)
value += MODULUS; //done properly
dir = (int8_t) value;
dir = static_cast<int8_t>(value);
}
DIR128(const FCOORD fc); //quantize vector

Expand All @@ -45,7 +45,7 @@ class DLLSYM DIR128
value %= MODULUS; //modulo arithmetic
if (value < 0)
value += MODULUS; //done properly
dir = (int8_t) value;
dir = static_cast<int8_t>(value);
return *this;
}
int8_t operator- ( //subtraction
Expand All @@ -58,7 +58,7 @@ class DLLSYM DIR128
result -= MODULUS; //get in range
else if (result < -MODULUS / 2)
result += MODULUS;
return (int8_t) result;
return static_cast<int8_t>(result);
}
DIR128 operator+ ( //addition
const DIR128 & add) const //of itself
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/ocrblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BLOCK:public ELIST_LINK
int16_t space,
int16_t ch_pitch) {
proportional = prop;
kerning = (int8_t) kern;
kerning = static_cast<int8_t>(kern);
spacing = space;
pitch = ch_pitch;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/ocrrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ROW:public ELIST_LINK
float base_line( //compute baseline
float xpos) const { //at the position
//get spline value
return (float) baseline.y (xpos);
return static_cast<float>(baseline.y (xpos));
}
float x_height() const { //return x height
return xheight;
Expand Down
6 changes: 3 additions & 3 deletions src/ccstruct/quadratc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class QUAD_COEFFS

float y( //evaluate
float x) const { //at x
return (float) ((a * x + b) * x + c);
return static_cast<float>((a * x + b) * x + c);
}

void move( // reposition word
Expand All @@ -50,8 +50,8 @@ class QUAD_COEFFS
int16_t p = vec.x ();
int16_t q = vec.y ();

c = (float) (c - b * p + a * p * p + q);
b = (float) (b - 2 * a * p);
c = static_cast<float>(c - b * p + a * p * p + q);
b = static_cast<float>(b - 2 * a * p);
}

double a; //x squared
Expand Down
6 changes: 3 additions & 3 deletions src/ccstruct/quspline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ double QSPLINE::step( //find step functions
total = 0;
while (index1 < index2) {
total +=
(double) quadratics[index1 + 1].y ((float) xcoords[index1 + 1]);
total -= (double) quadratics[index1].y ((float) xcoords[index1 + 1]);
static_cast<double>(quadratics[index1 + 1].y (static_cast<float>(xcoords[index1 + 1])));
total -= static_cast<double>(quadratics[index1].y (static_cast<float>(xcoords[index1 + 1])));
index1++; /*next segment */
}
return total; /*total steps */
Expand Down Expand Up @@ -356,7 +356,7 @@ void QSPLINE::plot( //draw it
window->Pen(colour);
for (segment = 0; segment < segments; segment++) {
increment =
(double) (xcoords[segment + 1] -
static_cast<double>(xcoords[segment + 1] -
xcoords[segment]) / QSPLINE_PRECISION;
x = xcoords[segment];
for (step = 0; step <= QSPLINE_PRECISION; step++) {
Expand Down
12 changes: 6 additions & 6 deletions src/ccstruct/statistc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ int32_t choose_nth_item(int32_t index, float *array, int32_t count) {
index = 0; // ensure legal
else if (index >= count)
index = count - 1;
equal_count = (int32_t) (rand() % count);
equal_count = static_cast<int32_t>(rand() % count);
pivot = array[equal_count];
// fill gap
array[equal_count] = array[0];
Expand Down Expand Up @@ -712,7 +712,7 @@ int32_t choose_nth_item(int32_t index, void *array, int32_t count, size_t size,
if (count <= 1)
return 0;
if (count == 2) {
if (compar (array, (char *) array + size) < 0) {
if (compar (array, static_cast<char *>(array) + size) < 0) {
return index >= 1 ? 1 : 0;
}
else {
Expand All @@ -723,15 +723,15 @@ int32_t choose_nth_item(int32_t index, void *array, int32_t count, size_t size,
index = 0; // ensure legal
else if (index >= count)
index = count - 1;
pivot = (int32_t) (rand () % count);
pivot = static_cast<int32_t>(rand () % count);
swap_entries (array, size, pivot, 0);
next_lesser = 0;
prev_greater = count;
equal_count = 1;
for (next_sample = 1; next_sample < prev_greater;) {
result =
compar ((char *) array + size * next_sample,
(char *) array + size * next_lesser);
compar (static_cast<char *>(array) + size * next_sample,
static_cast<char *>(array) + size * next_lesser);
if (result < 0) {
swap_entries (array, size, next_lesser++, next_sample++);
// shuffle
Expand All @@ -751,7 +751,7 @@ int32_t choose_nth_item(int32_t index, void *array, int32_t count, size_t size,
return next_lesser; // in equal bracket
else
return choose_nth_item (index - prev_greater,
(char *) array + size * prev_greater,
static_cast<char *>(array) + size * prev_greater,
count - prev_greater, size,
compar) + prev_greater;
}
Expand Down
Loading

0 comments on commit 72c8741

Please sign in to comment.