Skip to content

Commit

Permalink
Support image width and height larger than 32767
Browse files Browse the repository at this point in the history
Builds now can define LARGE_IMAGES to use 32 bit for
image dimensions instead of 16 bit.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Jul 25, 2021
1 parent e538cd7 commit e829fe3
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 154 deletions.
10 changes: 5 additions & 5 deletions src/ccstruct/blobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <tesseract/publictypes.h> // for OcrEngineMode

#include <cstdint> // for int16_t
#include "tesstypes.h" // for TDimension

struct Pix;

Expand All @@ -46,8 +46,8 @@ class WERD;
----------------------------------------------------------------------*/

struct TPOINT {
TPOINT() : x(0), y(0) {}
TPOINT(int16_t vx, int16_t vy) : x(vx), y(vy) {}
TPOINT() = default;
TPOINT(TDimension vx, TDimension vy) : x(vx), y(vy) {}
TPOINT(const ICOORD &ic) : x(ic.x()), y(ic.y()) {}

void operator+=(const TPOINT &other) {
Expand Down Expand Up @@ -86,8 +86,8 @@ struct TPOINT {
return x * x + y * y;
}

int16_t x; // absolute x coord.
int16_t y; // absolute y coord.
TDimension x = 0; // absolute x coord.
TDimension y = 0; // absolute y coord.
};

using VECTOR = TPOINT; // structure for coordinates.
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/mod128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace tesseract {

static const int16_t idirtab[] = {
static const TDimension idirtab[] = {
1000, 0, 998, 49, 995, 98, 989, 146, 980, 195, 970, 242, 956, 290, 941,
336, 923, 382, 903, 427, 881, 471, 857, 514, 831, 555, 803, 595, 773, 634,
740, 671, 707, 707, 671, 740, 634, 773, 595, 803, 555, 831, 514, 857, 471,
Expand Down
15 changes: 8 additions & 7 deletions src/ccstruct/ocrblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ namespace tesseract {
*
* Constructor for a simple rectangular block.
*/
BLOCK::BLOCK(const char *name, ///< filename
bool prop, ///< proportional
int16_t kern, ///< kerning
int16_t space, ///< spacing
int16_t xmin, ///< bottom left
int16_t ymin, int16_t xmax, ///< top right
int16_t ymax)
BLOCK::BLOCK(const char *name, ///< filename
bool prop, ///< proportional
int16_t kern, ///< kerning
int16_t space, ///< spacing
TDimension xmin, ///< bottom left
TDimension ymin,
TDimension xmax, ///< top right
TDimension ymax)
: pdblk(xmin, ymin, xmax, ymax)
, filename(name)
, re_rotation_(1.0f, 0.0f)
Expand Down
8 changes: 4 additions & 4 deletions src/ccstruct/ocrblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class TESS_API BLOCK : public ELIST_LINK
bool prop, ///< proportional
int16_t kern, ///< kerning
int16_t space, ///< spacing
int16_t xmin, ///< bottom left
int16_t ymin,
int16_t xmax, ///< top right
int16_t ymax);
TDimension xmin, ///< bottom left
TDimension ymin,
TDimension xmax, ///< top right
TDimension ymax);

~BLOCK() = default;

Expand Down
13 changes: 7 additions & 6 deletions src/ccstruct/pdblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ constexpr ERRCODE LOSTBLOCKLINE("Can't find rectangle for line");
* Constructor for a simple rectangular block.
**********************************************************************/
PDBLK::PDBLK( // rectangular block
int16_t xmin, // bottom left
int16_t ymin, int16_t xmax, // top right
int16_t ymax)
TDimension xmin, // bottom left
TDimension ymin,
TDimension xmax, // top right
TDimension ymax)
: box(ICOORD(xmin, ymin), ICOORD(xmax, ymax)) {
// boundaries
ICOORDELT_IT left_it = &leftside;
Expand Down Expand Up @@ -349,9 +350,9 @@ void BLOCK_RECT_IT::forward() { // next rectangle
* Get the the start and width of a line in the block.
**********************************************************************/

int16_t BLOCK_LINE_IT::get_line( // get a line
int16_t y, // line to get
int16_t &xext // output extent
TDimension BLOCK_LINE_IT::get_line( // get a line
TDimension y, // line to get
TDimension &xext // output extent
) {
ICOORD bleft; // bounding box
ICOORD tright; // of block & rect
Expand Down
14 changes: 7 additions & 7 deletions src/ccstruct/pdblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class PDBLK {
index_ = 0;
}
/// simple constructor
PDBLK(int16_t xmin, ///< bottom left
int16_t ymin,
int16_t xmax, ///< top right
int16_t ymax);
PDBLK(TDimension xmin, ///< bottom left
TDimension ymin,
TDimension xmax, ///< top right
TDimension ymax);

/// set vertex lists
///@param left list of left vertices
Expand Down Expand Up @@ -145,8 +145,8 @@ class BLOCK_RECT_IT // rectangle iterator
}

private:
int16_t ymin = 0; ///< bottom of rectangle
int16_t ymax = 0; ///< top of rectangle
TDimension ymin = 0; ///< bottom of rectangle
TDimension ymax = 0; ///< top of rectangle
PDBLK *block = nullptr; ///< block to iterate
ICOORDELT_IT left_it; ///< boundary iterators
ICOORDELT_IT right_it;
Expand All @@ -172,7 +172,7 @@ class BLOCK_LINE_IT {
/// get a line
///@param y line to get
///@param xext output extent
int16_t get_line(int16_t y, int16_t &xext);
TDimension get_line(TDimension y, TDimension &xext);

private:
PDBLK *block; ///< block to iterate
Expand Down
24 changes: 22 additions & 2 deletions src/ccstruct/points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,32 @@ bool FCOORD::normalise() { // Convert to unit vec
return true;
}

// Deserialize an ICOORD.
// For compatibility reasons it uses unsigned 16 bit coordinates
// instead of 32 bit coordinates.
bool ICOORD::DeSerialize(TFile *f) {
return f->DeSerialize(&xcoord) && f->DeSerialize(&ycoord);
bool success = false;
uint16_t coord;
if (f->DeSerialize(&coord)) {
xcoord = coord;
if (f->DeSerialize(&coord)) {
ycoord = coord;
success = true;
}
}
return success;
}

// Serialize an ICOORD.
// For compatibility reasons it uses unsigned 16 bit coordinates
// instead of 32 bit coordinates.
bool ICOORD::Serialize(TFile *f) const {
return f->Serialize(&xcoord) && f->Serialize(&ycoord);
uint16_t coord;
coord = xcoord;
auto success = f->Serialize(&coord);
coord = ycoord;
success = success && f->Serialize(&coord);
return success;
}

// Set from the given x,y, shrinking the vector to fit if needed.
Expand Down
41 changes: 21 additions & 20 deletions src/ccstruct/points.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "elst.h"
#include "errcode.h" // for ASSERT_HOST
#include "tesstypes.h" // for TDimension

#include <tesseract/export.h> // for DLLSYM

Expand All @@ -43,7 +44,7 @@ class ICOORD {
/// constructor
///@param xin x value
///@param yin y value
ICOORD(int16_t xin, int16_t yin) {
ICOORD(TDimension xin, TDimension yin) {
xcoord = xin;
ycoord = yin;
}
Expand All @@ -54,20 +55,20 @@ class ICOORD {
bool Serialize(TFile *f) const;

/// access function
int16_t x() const {
TDimension x() const {
return xcoord;
}
/// access_function
int16_t y() const {
TDimension y() const {
return ycoord;
}

/// rewrite function
void set_x(int16_t xin) {
void set_x(TDimension xin) {
xcoord = xin; // write new value
}
/// rewrite function
void set_y(int16_t yin) { // value to set
void set_y(TDimension yin) { // value to set
ycoord = yin;
}

Expand Down Expand Up @@ -128,15 +129,15 @@ class ICOORD {
/// cross product
friend int32_t operator*(const ICOORD &, const ICOORD &);
/// multiply
friend ICOORD operator*(const ICOORD &, int16_t);
friend ICOORD operator*(const ICOORD &, TDimension);
/// multiply
friend ICOORD operator*(int16_t, const ICOORD &);
friend ICOORD operator*(TDimension, const ICOORD &);
/// multiply
friend ICOORD &operator*=(ICOORD &, int16_t);
friend ICOORD &operator*=(ICOORD &, TDimension);
/// divide
friend ICOORD operator/(const ICOORD &, int16_t);
friend ICOORD operator/(const ICOORD &, TDimension);
/// divide
friend ICOORD &operator/=(ICOORD &, int16_t);
friend ICOORD &operator/=(ICOORD &, TDimension);
/// rotate
///@param vec by vector
void rotate(const FCOORD &vec);
Expand All @@ -155,8 +156,8 @@ class ICOORD {
bool DeSerialize(bool swap, FILE *fp);

protected:
int16_t xcoord; ///< x value
int16_t ycoord; ///< y value
TDimension xcoord; ///< x value
TDimension ycoord; ///< y value
};

class ICOORDELT : public ELIST_LINK,
Expand All @@ -171,7 +172,7 @@ class ICOORDELT : public ELIST_LINK,
/// constructor
///@param xin x value
///@param yin y value
ICOORDELT(int16_t xin, int16_t yin) {
ICOORDELT(TDimension xin, TDimension yin) {
xcoord = xin;
ycoord = yin;
}
Expand Down Expand Up @@ -438,7 +439,7 @@ inline int32_t operator*( // cross product

inline ICOORD operator*( // scalar multiply
const ICOORD &op1, // operands
int16_t scale) {
TDimension scale) {
ICOORD result; // output

result.xcoord = op1.xcoord * scale;
Expand All @@ -447,7 +448,7 @@ inline ICOORD operator*( // scalar multiply
}

inline ICOORD operator*( // scalar multiply
int16_t scale,
TDimension scale,
const ICOORD &op1 // operands
) {
ICOORD result; // output
Expand All @@ -465,7 +466,7 @@ inline ICOORD operator*( // scalar multiply

inline ICOORD &operator*=( // scalar multiply
ICOORD &op1, // operands
int16_t scale) {
TDimension scale) {
op1.xcoord *= scale;
op1.ycoord *= scale;
return op1;
Expand All @@ -479,7 +480,7 @@ inline ICOORD &operator*=( // scalar multiply

inline ICOORD operator/( // scalar divide
const ICOORD &op1, // operands
int16_t scale) {
TDimension scale) {
ICOORD result; // output

result.xcoord = op1.xcoord / scale;
Expand All @@ -495,7 +496,7 @@ inline ICOORD operator/( // scalar divide

inline ICOORD &operator/=( // scalar divide
ICOORD &op1, // operands
int16_t scale) {
TDimension scale) {
op1.xcoord /= scale;
op1.ycoord /= scale;
return op1;
Expand All @@ -509,8 +510,8 @@ inline ICOORD &operator/=( // scalar divide

inline void ICOORD::rotate( // rotate by vector
const FCOORD &vec) {
auto tmp = static_cast<int16_t>(std::floor(xcoord * vec.x() - ycoord * vec.y() + 0.5f));
ycoord = static_cast<int16_t>(std::floor(ycoord * vec.x() + xcoord * vec.y() + 0.5f));
auto tmp = static_cast<TDimension>(std::floor(xcoord * vec.x() - ycoord * vec.y() + 0.5f));
ycoord = static_cast<TDimension>(std::floor(ycoord * vec.x() + xcoord * vec.y() + 0.5f));
xcoord = tmp;
}

Expand Down
19 changes: 7 additions & 12 deletions src/ccstruct/polyblk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ void POLY_BLOCK::rotate(FCOORD rotation) {
pos.set_x(pt->x());
pos.set_y(pt->y());
pos.rotate(rotation);
pt->set_x(static_cast<int16_t>(floor(pos.x() + 0.5)));
pt->set_y(static_cast<int16_t>(floor(pos.y() + 0.5)));
pt->set_x(static_cast<TDimension>(floor(pos.x() + 0.5)));
pt->set_y(static_cast<TDimension>(floor(pos.y() + 0.5)));
pts.forward();
} while (!pts.at_first());
compute_bb();
Expand Down Expand Up @@ -270,30 +270,25 @@ void POLY_BLOCK::plot(ScrollView *window, int32_t num) {
}

void POLY_BLOCK::fill(ScrollView *window, ScrollView::Color colour) {
int16_t y;
int16_t width;
PB_LINE_IT *lines;
ICOORDELT_IT s_it;

lines = new PB_LINE_IT(this);
std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT(this));
window->Pen(colour);

for (y = this->bounding_box()->bottom(); y <= this->bounding_box()->top(); y++) {
for (auto y = this->bounding_box()->bottom(); y <= this->bounding_box()->top(); y++) {
const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(lines->get_line(y));
if (!segments->empty()) {
s_it.set_to_list(segments.get());
for (s_it.mark_cycle_pt(); !s_it.cycled_list(); s_it.forward()) {
// Note different use of ICOORDELT, x coord is x coord of pixel
// at the start of line segment, y coord is length of line segment
// Last pixel is start pixel + length.
width = s_it.data()->y();
auto width = s_it.data()->y();
window->SetCursor(s_it.data()->x(), y);
window->DrawTo(s_it.data()->x() + static_cast<float>(width), y);
}
}
}

delete lines;
}
#endif

Expand Down Expand Up @@ -339,7 +334,7 @@ bool POLY_BLOCK::overlap(POLY_BLOCK *other) {
return false;
}

ICOORDELT_LIST *PB_LINE_IT::get_line(int16_t y) {
ICOORDELT_LIST *PB_LINE_IT::get_line(TDimension y) {
ICOORDELT_IT v, r;
ICOORDELT_LIST *result;
ICOORDELT *x, *current, *previous;
Expand All @@ -356,7 +351,7 @@ ICOORDELT_LIST *PB_LINE_IT::get_line(int16_t y) {
float fx =
0.5f + previous->x() +
(current->x() - previous->x()) * (fy - previous->y()) / (current->y() - previous->y());
x = new ICOORDELT(static_cast<int16_t>(fx), 0);
x = new ICOORDELT(static_cast<TDimension>(fx), 0);
r.add_to_end(x);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/polyblk.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class PB_LINE_IT {
// Each element of the returned list is the start (x) and extent(y) of
// a run inside the region.
// Delete the returned list after use.
ICOORDELT_LIST *get_line(int16_t y);
ICOORDELT_LIST *get_line(TDimension y);

private:
POLY_BLOCK *block;
Expand Down
8 changes: 4 additions & 4 deletions src/ccstruct/rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ void TBOX::rotate_large(const FCOORD &vec) {

TBOX TBOX::intersection( // shared area box
const TBOX &box) const {
int16_t left;
int16_t bottom;
int16_t right;
int16_t top;
TDimension left;
TDimension bottom;
TDimension right;
TDimension top;
if (overlap(box)) {
if (box.bot_left.x() > bot_left.x()) {
left = box.bot_left.x();
Expand Down
Loading

0 comments on commit e829fe3

Please sign in to comment.