Commit 6d67d0fd for tesseract
commit 6d67d0fd3c07d7d4328b594b5a237ce25420fff8
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 13 21:26:18 2026 +0100
textord: use 32 bit coordinates in the fixed pitch code
The fixed pitch code kept the edges of the row projection and the
positions of the cuts in int16_t. projection_right is the right edge
of the row plus a margin, and the search for cuts looks up to one
pitch beyond it, so the values wrapped for text which ends near
x = 32767, the largest width of a page. check_pitch_sync3() then
wrote cut points in front of its array and did not terminate, and
other pages failed ASSERT_HOST(right_edge >= left_edge).
Use int32_t for TO_ROW::projection_left and projection_right, for the
parameters which pass them on, and for the local coordinates in
topitch.cpp, pithsync.cpp and pitsync1.cpp.
Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-FY9O67E6).
Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/ccstruct/blobbox.h b/src/ccstruct/blobbox.h
index 53aa379f..f2fa54fb 100644
--- a/src/ccstruct/blobbox.h
+++ b/src/ccstruct/blobbox.h
@@ -651,8 +651,8 @@ public:
bool merged = false;
bool all_caps; // had no ascenders
bool used_dm_model; // in guessing pitch
- int16_t projection_left; // start of projection
- int16_t projection_right; // start of projection
+ int32_t projection_left; // start of projection
+ int32_t projection_right; // start of projection
PITCH_TYPE pitch_decision; // how strong is decision
float fixed_pitch; // pitch or 0
float fp_space; // sp if fixed pitch
diff --git a/src/textord/drawtord.cpp b/src/textord/drawtord.cpp
index 91d52ebe..f05897bb 100644
--- a/src/textord/drawtord.cpp
+++ b/src/textord/drawtord.cpp
@@ -313,8 +313,8 @@ void plot_fp_cells( // draw words
int16_t pitch, // of block
int16_t blob_count, // no of real blobs
STATS *projection, // vertical
- int16_t projection_left, // edges //scale factor
- int16_t projection_right, float projection_scale) {
+ int32_t projection_left, // edges //scale factor
+ int32_t projection_right, float projection_scale) {
int16_t occupation; // occupied cells
TBOX word_box; // bounding box
FPSEGPT_LIST seg_list; // list of cuts
diff --git a/src/textord/drawtord.h b/src/textord/drawtord.h
index d1ad4768..c62de82b 100644
--- a/src/textord/drawtord.h
+++ b/src/textord/drawtord.h
@@ -78,8 +78,8 @@ void plot_fp_cells( // draw words
int16_t pitch, // of block
int16_t blob_count, // no of real blobs
STATS *projection, // vertical
- int16_t projection_left, // edges //scale factor
- int16_t projection_right, float projection_scale);
+ int32_t projection_left, // edges //scale factor
+ int32_t projection_right, float projection_scale);
void plot_fp_cells2( // draw words
ScrollView *win, // window tro draw in
ScrollView::Color colour, // colour of lines
diff --git a/src/textord/pithsync.cpp b/src/textord/pithsync.cpp
index cb92de8c..346dd603 100644
--- a/src/textord/pithsync.cpp
+++ b/src/textord/pithsync.cpp
@@ -37,11 +37,11 @@ namespace tesseract {
void FPCUTPT::setup( // constructor
FPCUTPT *cutpts, // predecessors
- int16_t array_origin, // start coord
+ int32_t array_origin, // start coord
STATS *projection, // vertical occupation
int16_t zero_count, // official zero
int16_t pitch, // proposed pitch
- int16_t x, // position
+ int32_t x, // position
int16_t offset // dist to gap
) {
// half of pitch
@@ -96,8 +96,8 @@ void FPCUTPT::setup( // constructor
void FPCUTPT::assign( // constructor
FPCUTPT *cutpts, // predecessors
- int16_t array_origin, // start coord
- int16_t x, // position
+ int32_t array_origin, // start coord
+ int32_t x, // position
bool faking, // faking this one
bool mid_cut, // cheap cut.
int16_t offset, // dist to gap
@@ -198,8 +198,8 @@ void FPCUTPT::assign( // constructor
void FPCUTPT::assign_cheap( // constructor
FPCUTPT *cutpts, // predecessors
- int16_t array_origin, // start coord
- int16_t x, // position
+ int32_t array_origin, // start coord
+ int32_t x, // position
bool faking, // faking this one
bool mid_cut, // cheap cut.
int16_t offset, // dist to gap
@@ -294,8 +294,8 @@ double check_pitch_sync2( // find segmentation
int16_t pitch, // pitch estimate
int16_t pitch_error, // tolerance
STATS *projection, // vertical
- int16_t projection_left, // edges //scale factor
- int16_t projection_right, float projection_scale,
+ int32_t projection_left, // edges //scale factor
+ int32_t projection_right, float projection_scale,
int16_t &occupation_count, // no of occupied cells
FPSEGPT_LIST *seg_list, // output list
int16_t start, // start of good range
@@ -303,15 +303,15 @@ double check_pitch_sync2( // find segmentation
) {
bool faking; // illegal cut pt
bool mid_cut; // cheap cut pt.
- int16_t x; // current coord
+ int32_t x; // current coord
int16_t blob_index; // blob number
- int16_t left_edge; // of word
- int16_t right_edge; // of word
- int16_t array_origin; // x coord of array
+ int32_t left_edge; // of word
+ int32_t right_edge; // of word
+ int32_t array_origin; // x coord of array
int16_t offset; // dist to legal area
int16_t zero_count; // projection zero
- int16_t best_left_x = 0; // for equals
- int16_t best_right_x = 0; // right edge
+ int32_t best_left_x = 0; // for equals
+ int32_t best_right_x = 0; // right edge
TBOX this_box; // bounding box
TBOX next_box; // box of next blob
FPSEGPT *segpt; // segment point
@@ -479,8 +479,8 @@ double check_pitch_sync2( // find segmentation
**********************************************************************/
double check_pitch_sync3( // find segmentation
- int16_t projection_left, // edges //to be considered 0
- int16_t projection_right, int16_t zero_count,
+ int32_t projection_left, // edges //to be considered 0
+ int32_t projection_right, int16_t zero_count,
int16_t pitch, // pitch estimate
int16_t pitch_error, // tolerance
STATS *projection, // vertical
@@ -492,17 +492,17 @@ double check_pitch_sync3( // find segmentation
) {
bool faking; // illegal cut pt
bool mid_cut; // cheap cut pt.
- int16_t left_edge; // of word
- int16_t right_edge; // of word
- int16_t x; // current coord
- int16_t array_origin; // x coord of array
- int16_t offset; // dist to legal area
+ int32_t left_edge; // of word
+ int32_t right_edge; // of word
+ int32_t x; // current coord
+ int32_t array_origin; // x coord of array
+ int32_t offset; // dist to legal area
int16_t projection_offset; // from scaled projection
- int16_t prev_zero; // previous zero dist
- int16_t next_zero; // next zero dist
+ int32_t prev_zero; // previous zero dist
+ int32_t next_zero; // next zero dist
int16_t zero_offset; // scan window
- int16_t best_left_x = 0; // for equals
- int16_t best_right_x = 0; // right edge
+ int32_t best_left_x = 0; // for equals
+ int32_t best_right_x = 0; // right edge
FPSEGPT *segpt; // segment point
int minindex; // next input position
int test_index; // index to mins
diff --git a/src/textord/pithsync.h b/src/textord/pithsync.h
index 16db4723..a2507c07 100644
--- a/src/textord/pithsync.h
+++ b/src/textord/pithsync.h
@@ -32,17 +32,17 @@ public:
FPCUTPT() = default;
void setup( // start of cut
FPCUTPT cutpts[], // predecessors
- int16_t array_origin, // start coord
+ int32_t array_origin, // start coord
STATS *projection, // occupation
int16_t zero_count, // official zero
int16_t pitch, // proposed pitch
- int16_t x, // position
+ int32_t x, // position
int16_t offset); // dist to gap
void assign( // evaluate cut
FPCUTPT cutpts[], // predecessors
- int16_t array_origin, // start coord
- int16_t x, // position
+ int32_t array_origin, // start coord
+ int32_t x, // position
bool faking, // faking this one
bool mid_cut, // doing free cut
int16_t offset, // extra cost dist
@@ -54,8 +54,8 @@ public:
void assign_cheap( // evaluate cut
FPCUTPT cutpts[], // predecessors
- int16_t array_origin, // start coord
- int16_t x, // position
+ int32_t array_origin, // start coord
+ int32_t x, // position
bool faking, // faking this one
bool mid_cut, // doing free cut
int16_t offset, // extra cost dist
@@ -107,16 +107,16 @@ double check_pitch_sync2( // find segmentation
int16_t pitch, // pitch estimate
int16_t pitch_error, // tolerance
STATS *projection, // vertical
- int16_t projection_left, // edges //scale factor
- int16_t projection_right, float projection_scale,
+ int32_t projection_left, // edges //scale factor
+ int32_t projection_right, float projection_scale,
int16_t &occupation_count, // no of occupied cells
FPSEGPT_LIST *seg_list, // output list
int16_t start, // start of good range
int16_t end // end of good range
);
double check_pitch_sync3( // find segmentation
- int16_t projection_left, // edges //to be considered 0
- int16_t projection_right, int16_t zero_count,
+ int32_t projection_left, // edges //to be considered 0
+ int32_t projection_right, int16_t zero_count,
int16_t pitch, // pitch estimate
int16_t pitch_error, // tolerance
STATS *projection, // vertical
diff --git a/src/textord/pitsync1.cpp b/src/textord/pitsync1.cpp
index 7999558e..13834afd 100644
--- a/src/textord/pitsync1.cpp
+++ b/src/textord/pitsync1.cpp
@@ -55,7 +55,7 @@ FPSEGPT::FPSEGPT( // constructor
**********************************************************************/
FPSEGPT::FPSEGPT( // constructor
- int16_t x // position
+ int32_t x // position
)
: xpos(x) {
pred = nullptr;
@@ -75,7 +75,7 @@ FPSEGPT::FPSEGPT( // constructor
**********************************************************************/
FPSEGPT::FPSEGPT( // constructor
- int16_t x, // position
+ int32_t x, // position
bool faking, // faking this one
int16_t offset, // dist to gap
int16_t region_index, // segment number
@@ -143,19 +143,19 @@ double check_pitch_sync( // find segmentation
STATS *projection, // vertical
FPSEGPT_LIST *seg_list // output list
) {
- int16_t x; // current coord
+ int32_t x; // current coord
int16_t min_index; // blob number
int16_t max_index; // blob number
- int16_t left_edge; // of word
- int16_t right_edge; // of word
- int16_t right_max; // max allowed x
- int16_t min_x; // in this region
- int16_t max_x;
+ int32_t left_edge; // of word
+ int32_t right_edge; // of word
+ int32_t right_max; // max allowed x
+ int32_t min_x; // in this region
+ int32_t max_x;
int16_t region_index;
int16_t best_region_index = 0; // for best result
int16_t offset; // dist to legal area
- int16_t left_best_x; // edge of good region
- int16_t right_best_x; // right edge
+ int32_t left_best_x; // edge of good region
+ int32_t right_best_x; // right edge
TBOX min_box; // bounding box
TBOX max_box; // bounding box
TBOX next_box; // box of next blob
@@ -359,9 +359,9 @@ void make_illegal_segment( // find segmentation
int16_t pitch_error, // tolerance
FPSEGPT_LIST *seg_list // output list
) {
- int16_t x; // current coord
- int16_t min_x = 0; // in this region
- int16_t max_x = 0;
+ int32_t x; // current coord
+ int32_t min_x = 0; // in this region
+ int32_t max_x = 0;
int16_t offset; // dist to edge
FPSEGPT *segpt; // segment point
FPSEGPT *prevpt; // previous point
diff --git a/src/textord/pitsync1.h b/src/textord/pitsync1.h
index d81512d6..837b746f 100644
--- a/src/textord/pitsync1.h
+++ b/src/textord/pitsync1.h
@@ -35,9 +35,9 @@ class FPSEGPT : public ELIST<FPSEGPT>::LINK {
public:
FPSEGPT() = default;
FPSEGPT( // constructor
- int16_t x); // position
+ int32_t x); // position
FPSEGPT( // constructor
- int16_t x, // position
+ int32_t x, // position
bool faking, // faking this one
int16_t offset, // extra cost dist
int16_t region_index, // segment number
diff --git a/src/textord/topitch.cpp b/src/textord/topitch.cpp
index f83abcff..ade07e7a 100644
--- a/src/textord/topitch.cpp
+++ b/src/textord/topitch.cpp
@@ -364,7 +364,7 @@ bool try_doc_fixed( // determine pitch
TO_BLOCK_LIST *port_blocks, // input list
float gradient // page skew
) {
- int16_t master_x; // uniform shifts
+ int32_t master_x; // uniform shifts
int16_t pitch; // median pitch.
int x; // profile coord
int prop_blocks; // correct counts
@@ -374,10 +374,10 @@ bool try_doc_fixed( // determine pitch
TO_BLOCK_IT block_it = port_blocks;
TO_BLOCK *block; // current block;
TO_ROW *row; // current row
- int16_t projection_left; // edges
- int16_t projection_right;
- int16_t row_left; // edges of row
- int16_t row_right;
+ int32_t projection_left; // edges
+ int32_t projection_right;
+ int32_t row_left; // edges of row
+ int32_t row_right;
float master_y; // uniform shifts
float shift_factor; // page skew correction
float final_pitch; // output pitch
@@ -398,8 +398,8 @@ bool try_doc_fixed( // determine pitch
TO_ROW_IT row_it(block_it.data()->get_rows());
master_x = row_it.data()->projection_left;
master_y = row_it.data()->baseline.y(master_x);
- projection_left = INT16_MAX;
- projection_right = -INT16_MAX;
+ projection_left = INT32_MAX;
+ projection_right = -INT32_MAX;
prop_blocks = 0;
fixed_blocks = 0;
total_row_count = 0;
@@ -415,8 +415,8 @@ bool try_doc_fixed( // determine pitch
}
// find median
row_y = row->baseline.y(master_x);
- row_left = static_cast<int16_t>(row->projection_left - shift_factor * (master_y - row_y));
- row_right = static_cast<int16_t>(row->projection_right - shift_factor * (master_y - row_y));
+ row_left = static_cast<int32_t>(row->projection_left - shift_factor * (master_y - row_y));
+ row_right = static_cast<int32_t>(row->projection_right - shift_factor * (master_y - row_y));
if (row_left < projection_left) {
projection_left = row_left;
}
@@ -436,7 +436,7 @@ bool try_doc_fixed( // determine pitch
for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
row = row_it.data();
row_y = row->baseline.y(master_x);
- row_left = static_cast<int16_t>(row->projection_left - shift_factor * (master_y - row_y));
+ row_left = static_cast<int32_t>(row->projection_left - shift_factor * (master_y - row_y));
for (x = row->projection_left; x < row->projection_right; x++, row_left++) {
projection.add(row_left, row->projection.pile_count(x));
}
@@ -1072,8 +1072,8 @@ bool count_pitch_stats( // find lines
float tune_row_pitch( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge of projection
- int16_t projection_right, // edge of projection
+ int32_t projection_left, // edge of projection
+ int32_t projection_right, // edge of projection
float space_size, // size of blank
float &initial_pitch, // guess at pitch
float &best_sp_sd, // space sd
@@ -1171,8 +1171,8 @@ float tune_row_pitch( // find fp cells
float tune_row_pitch2( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge of projection
- int16_t projection_right, // edge of projection
+ int32_t projection_left, // edge of projection
+ int32_t projection_right, // edge of projection
float space_size, // size of blank
float &initial_pitch, // guess at pitch
float &best_sp_sd, // space sd
@@ -1181,7 +1181,7 @@ float tune_row_pitch2( // find fp cells
bool testing_on // individual words
) {
int pitch_delta; // offset pitch
- int16_t pixel; // pixel coord
+ int32_t pixel; // pixel coord
int16_t best_pixel; // pixel coord
int16_t best_delta; // best pitch
int16_t best_pitch; // best pitch
@@ -1264,8 +1264,8 @@ float tune_row_pitch2( // find fp cells
float compute_pitch_sd( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge
- int16_t projection_right, // edge
+ int32_t projection_left, // edge
+ int32_t projection_right, // edge
float space_size, // size of blank
float initial_pitch, // guess at pitch
float &sp_sd, // space sd
@@ -1379,7 +1379,8 @@ float compute_pitch_sd( // find fp cells
sp_count++;
}
for (seg_it.mark_cycle_pt(); !seg_it.cycled_list(); seg_it.forward()) {
- segpos = seg_it.data()->position();
+ // cuts may lie up to a pitch outside the image
+ segpos = ClipToRange<int>(seg_it.data()->position(), INT16_MIN, INT16_MAX);
if (cell_it.empty() || segpos > cellpos + initial_pitch / 2) {
// big gap
while (!cell_it.empty() && segpos > cellpos + initial_pitch * 3 / 2) {
@@ -1426,8 +1427,8 @@ float compute_pitch_sd( // find fp cells
float compute_pitch_sd2( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge
- int16_t projection_right, // edge
+ int32_t projection_left, // edge
+ int32_t projection_right, // edge
float initial_pitch, // guess at pitch
int16_t &occupation, // no of occupied cells
int16_t &mid_cuts, // no of free cuts
@@ -1493,7 +1494,8 @@ float compute_pitch_sd2( // find fp cells
#endif
seg_it.set_to_list(&seg_list);
for (seg_it.mark_cycle_pt(); !seg_it.cycled_list(); seg_it.forward()) {
- segpos = seg_it.data()->position();
+ // cuts may lie up to a pitch outside the image
+ segpos = ClipToRange<int>(seg_it.data()->position(), INT16_MIN, INT16_MAX);
// make new one
cell = new ICOORDELT(segpos, 0);
cell_it.add_after_then_move(cell);
@@ -1515,8 +1517,8 @@ float compute_pitch_sd2( // find fp cells
void print_pitch_sd( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edges //size of blank
- int16_t projection_right, float space_size,
+ int32_t projection_left, // edges //size of blank
+ int32_t projection_right, float space_size,
float initial_pitch // guess at pitch
) {
const char *res2; // pitch result
diff --git a/src/textord/topitch.h b/src/textord/topitch.h
index 204759b6..46a71944 100644
--- a/src/textord/topitch.h
+++ b/src/textord/topitch.h
@@ -98,8 +98,8 @@ bool count_pitch_stats( // find lines
float tune_row_pitch( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge of projection
- int16_t projection_right, // edge of projection
+ int32_t projection_left, // edge of projection
+ int32_t projection_right, // edge of projection
float space_size, // size of blank
float &initial_pitch, // guess at pitch
float &best_sp_sd, // space sd
@@ -110,8 +110,8 @@ float tune_row_pitch( // find fp cells
float tune_row_pitch2( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge of projection
- int16_t projection_right, // edge of projection
+ int32_t projection_left, // edge of projection
+ int32_t projection_right, // edge of projection
float space_size, // size of blank
float &initial_pitch, // guess at pitch
float &best_sp_sd, // space sd
@@ -122,8 +122,8 @@ float tune_row_pitch2( // find fp cells
float compute_pitch_sd( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge
- int16_t projection_right, // edge
+ int32_t projection_left, // edge
+ int32_t projection_right, // edge
float space_size, // size of blank
float initial_pitch, // guess at pitch
float &sp_sd, // space sd
@@ -136,8 +136,8 @@ float compute_pitch_sd( // find fp cells
float compute_pitch_sd2( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edge
- int16_t projection_right, // edge
+ int32_t projection_left, // edge
+ int32_t projection_right, // edge
float initial_pitch, // guess at pitch
int16_t &occupation, // no of occupied cells
int16_t &mid_cuts, // no of free cuts
@@ -149,8 +149,8 @@ float compute_pitch_sd2( // find fp cells
void print_pitch_sd( // find fp cells
TO_ROW *row, // row to do
STATS *projection, // vertical projection
- int16_t projection_left, // edges //size of blank
- int16_t projection_right, float space_size,
+ int32_t projection_left, // edges //size of blank
+ int32_t projection_right, float space_size,
float initial_pitch // guess at pitch
);
void find_repeated_chars(TO_BLOCK *block); // Block to search.