20 #include "allheaders.h"
34 image_width_(0), image_height_(0),
35 pix_channels_(0), pix_wpl_(0),
36 scale_(1), yres_(300), estimated_res_(300) {
63 int width,
int height,
64 int bytes_per_pixel,
int bytes_per_line) {
65 int bpp = bytes_per_pixel * 8;
66 if (bpp == 0) bpp = 1;
67 Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
68 l_uint32* data = pixGetData(pix);
69 int wpl = pixGetWpl(pix);
72 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
73 for (
int x = 0; x < width; ++x) {
74 if (imagedata[x / 8] & (0x80 >> (x % 8)))
75 CLEAR_DATA_BIT(data, x);
77 SET_DATA_BIT(data, x);
84 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
85 for (
int x = 0; x < width; ++x)
86 SET_DATA_BYTE(data, x, imagedata[x]);
92 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line) {
93 for (
int x = 0; x < width; ++x, ++data) {
94 SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
95 SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
96 SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
103 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl) {
104 for (
int x = 0; x < width; ++x) {
105 data[x] = (imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
106 (imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3];
112 tprintf(
"Cannot convert RAW image to Pix with bpp = %d\n", bpp);
114 pixSetYRes(pix, 300);
133 int* width,
int* height,
134 int* imagewidth,
int* imageheight) {
151 Pix* src =
const_cast<Pix*
>(pix);
156 if (depth > 1 && depth < 8) {
157 pix_ = pixConvertTo8(src,
false);
158 }
else if (pixGetColormap(src)) {
159 pix_ = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
161 pix_ = pixClone(src);
163 depth = pixGetDepth(
pix_);
193 int width = pixGetWidth(pix_grey);
194 int height = pixGetHeight(pix_grey);
197 OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
198 pixDestroy(&pix_grey);
199 Pix* pix_thresholds = pixCreate(width, height, 8);
200 int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
201 pixSetAllArbitrary(pix_thresholds, threshold);
202 delete [] thresholds;
204 return pix_thresholds;
220 return pixClone(
pix_);
224 Pix* cropped = pixClipRectangle(
pix_, box,
NULL);
236 int depth = pixGetDepth(pix);
238 Pix* result = depth < 8 ? pixConvertTo8(pix,
false)
239 : pixConvertRGBToLuminance(pix);
248 Pix** out_pix)
const {
258 if ((num_channels == 4 || num_channels == 1) &&
260 od.ThresholdRectToPixOCL((
const unsigned char*)pixGetData(src_pix),
261 num_channels, pixGetWpl(src_pix) * 4,
262 thresholds, hi_values, out_pix ,
270 delete [] thresholds;
282 const int* thresholds,
283 const int* hi_values,
287 uinT32* pixdata = pixGetData(*pix);
288 int wpl = pixGetWpl(*pix);
289 int src_wpl = pixGetWpl(src_pix);
290 uinT32* srcdata = pixGetData(src_pix);
293 uinT32* pixline = pixdata + y * wpl;
295 bool white_result =
true;
296 for (
int ch = 0; ch < num_channels; ++ch) {
297 int pixel = GET_DATA_BYTE(const_cast<void*>(
298 reinterpret_cast<const void *>(linedata)),
300 if (hi_values[ch] >= 0 &&
301 (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
302 white_result =
false;
307 CLEAR_DATA_BIT(pixline, x);
309 SET_DATA_BIT(pixline, x);
virtual Pix * GetPixRectThresholds()
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
virtual ~ImageThresholder()
virtual void Init()
Common initialization shared between SetImage methods.
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
bool IsBinary() const
Returns true if the source image is binary.
void SetRectangle(int left, int top, int width, int height)
virtual Pix * GetPixRectGrey()
#define PERF_COUNT_START(FUNCT_NAME)
void OtsuThresholdRectToPix(Pix *src_pix, Pix **out_pix) const
virtual void ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
bool IsEmpty() const
Return true if no image has been set.
bool IsFullImage() const
Return true if we are processing the full image.
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
int OtsuThreshold(Pix *src_pix, int left, int top, int width, int height, int **thresholds, int **hi_values)
void ThresholdRectToPix(Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const