20 #include "allheaders.h" 35 image_width_(0), image_height_(0),
36 pix_channels_(0), pix_wpl_(0),
37 scale_(1), yres_(300), estimated_res_(300) {
52 return pix_ ==
nullptr;
64 int width,
int height,
65 int bytes_per_pixel,
int bytes_per_line) {
66 int bpp = bytes_per_pixel * 8;
67 if (bpp == 0) bpp = 1;
68 Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
69 l_uint32* data = pixGetData(pix);
70 int wpl = pixGetWpl(pix);
73 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
74 for (
int x = 0; x < width; ++x) {
75 if (imagedata[x / 8] & (0x80 >> (x % 8)))
76 CLEAR_DATA_BIT(data, x);
78 SET_DATA_BIT(data, x);
85 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
86 for (
int x = 0; x < width; ++x)
87 SET_DATA_BYTE(data, x, imagedata[x]);
93 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line) {
94 for (
int x = 0; x < width; ++x, ++data) {
95 SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
96 SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
97 SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
104 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl) {
105 for (
int x = 0; x < width; ++x) {
106 data[x] = (imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
107 (imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3];
113 tprintf(
"Cannot convert RAW image to Pix with bpp = %d\n", bpp);
115 pixSetYRes(pix, 300);
134 int* width,
int* height,
135 int* imagewidth,
int* imageheight) {
152 Pix* src =
const_cast<Pix*
>(pix);
158 if (pixGetColormap(src)) {
159 Pix* tmp = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
160 depth = pixGetDepth(tmp);
161 if (depth > 1 && depth < 8) {
162 pix_ = pixConvertTo8(tmp,
false);
167 }
else if (depth > 1 && depth < 8) {
168 pix_ = pixConvertTo8(src,
false);
170 pix_ = pixCopy(
nullptr, src);
172 depth = pixGetDepth(
pix_);
193 *pix = pixCopy(
nullptr, original);
194 pixDestroy(&original);
211 int width = pixGetWidth(pix_grey);
212 int height = pixGetHeight(pix_grey);
215 OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
216 pixDestroy(&pix_grey);
217 Pix* pix_thresholds = pixCreate(width, height, 8);
218 int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
219 pixSetAllArbitrary(pix_thresholds, threshold);
220 delete [] thresholds;
222 return pix_thresholds;
238 return pixClone(
pix_);
242 Pix* cropped = pixClipRectangle(
pix_, box,
nullptr);
254 int depth = pixGetDepth(pix);
256 Pix* result = depth < 8 ? pixConvertTo8(pix,
false)
257 : pixConvertRGBToLuminance(pix);
266 Pix** out_pix)
const {
276 if (num_channels == 4 &&
278 od.ThresholdRectToPixOCL((
unsigned char*)pixGetData(src_pix), num_channels,
279 pixGetWpl(src_pix) * 4, thresholds, hi_values,
288 delete [] thresholds;
300 const int* thresholds,
301 const int* hi_values,
305 uint32_t* pixdata = pixGetData(*pix);
306 int wpl = pixGetWpl(*pix);
307 int src_wpl = pixGetWpl(src_pix);
308 uint32_t* srcdata = pixGetData(src_pix);
310 const uint32_t* linedata = srcdata + (y +
rect_top_) * src_wpl;
311 uint32_t* pixline = pixdata + y * wpl;
313 bool white_result =
true;
314 for (
int ch = 0; ch < num_channels; ++ch) {
316 GET_DATA_BYTE(linedata, (x +
rect_left_) * num_channels + ch);
317 if (hi_values[ch] >= 0 &&
318 (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
319 white_result =
false;
324 CLEAR_DATA_BIT(pixline, x);
326 SET_DATA_BIT(pixline, x);
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
virtual Pix * GetPixRectGrey()
#define PERF_COUNT_START(FUNCT_NAME)
virtual Pix * GetPixRectThresholds()
bool IsEmpty() const
Return true if no image has been set.
bool IsFullImage() const
Return true if we are processing the full image.
void OtsuThresholdRectToPix(Pix *src_pix, Pix **out_pix) const
DLLSYM void tprintf(const char *format,...)
bool IsBinary() const
Returns true if the source image is binary.
void SetRectangle(int left, int top, int width, int height)
virtual void Init()
Common initialization shared between SetImage methods.
virtual ~ImageThresholder()
void ThresholdRectToPix(Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const
int OtsuThreshold(Pix *src_pix, int left, int top, int width, int height, int **thresholds, int **hi_values)
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
Returns false on error.