19 #include "allheaders.h" 29 #if defined(USE_OPENCL) 37 image_width_(0), image_height_(0),
38 pix_channels_(0), pix_wpl_(0),
39 scale_(1), yres_(300), estimated_res_(300) {
54 return pix_ ==
nullptr;
66 int width,
int height,
67 int bytes_per_pixel,
int bytes_per_line) {
68 int bpp = bytes_per_pixel * 8;
69 if (bpp == 0) bpp = 1;
70 Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
71 l_uint32* data = pixGetData(pix);
72 int wpl = pixGetWpl(pix);
75 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
76 for (
int x = 0; x < width; ++x) {
77 if (imagedata[x / 8] & (0x80 >> (x % 8)))
78 CLEAR_DATA_BIT(data, x);
80 SET_DATA_BIT(data, x);
87 for (
int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
88 for (
int x = 0; x < width; ++x)
89 SET_DATA_BYTE(data, x, imagedata[x]);
95 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line) {
96 for (
int x = 0; x < width; ++x, ++data) {
97 SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
98 SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
99 SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
106 for (
int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl) {
107 for (
int x = 0; x < width; ++x) {
108 data[x] = (imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
109 (imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3];
115 tprintf(
"Cannot convert RAW image to Pix with bpp = %d\n", bpp);
135 int* width,
int* height,
136 int* imagewidth,
int* imageheight) {
153 Pix* src =
const_cast<Pix*
>(pix);
159 if (pixGetColormap(src)) {
160 Pix* tmp = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
161 depth = pixGetDepth(tmp);
162 if (depth > 1 && depth < 8) {
163 pix_ = pixConvertTo8(tmp,
false);
168 }
else if (depth > 1 && depth < 8) {
169 pix_ = pixConvertTo8(src,
false);
171 pix_ = pixCopy(
nullptr, src);
173 depth = pixGetDepth(
pix_);
194 *pix = pixCopy(
nullptr, original);
195 pixDestroy(&original);
212 int width = pixGetWidth(pix_grey);
213 int height = pixGetHeight(pix_grey);
216 OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
217 pixDestroy(&pix_grey);
218 Pix* pix_thresholds = pixCreate(width, height, 8);
219 int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
220 pixSetAllArbitrary(pix_thresholds, threshold);
221 delete [] thresholds;
223 return pix_thresholds;
239 return pixClone(
pix_);
243 Pix* cropped = pixClipRectangle(
pix_, box,
nullptr);
255 int depth = pixGetDepth(pix);
257 Pix* result = depth < 8 ? pixConvertTo8(pix,
false)
258 : pixConvertRGBToLuminance(pix);
267 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;
298 const int* thresholds,
299 const int* hi_values,
302 uint32_t* pixdata = pixGetData(*pix);
303 int wpl = pixGetWpl(*pix);
304 int src_wpl = pixGetWpl(src_pix);
305 uint32_t* srcdata = pixGetData(src_pix);
307 const uint32_t* linedata = srcdata + (y +
rect_top_) * src_wpl;
308 uint32_t* pixline = pixdata + y * wpl;
310 bool white_result =
true;
311 for (
int ch = 0; ch < num_channels; ++ch) {
313 GET_DATA_BYTE(linedata, (x +
rect_left_) * num_channels + ch);
314 if (hi_values[ch] >= 0 &&
315 (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
316 white_result =
false;
321 CLEAR_DATA_BIT(pixline, x);
323 SET_DATA_BIT(pixline, x);
int estimated_res_
Resolution estimate from text size.
void ThresholdRectToPix(Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const
bool IsEmpty() const
Return true if no image has been set.
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
bool IsBinary() const
Returns true if the source image is binary.
int pix_channels_
Number of 8-bit channels in pix_.
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
int yres_
y pixels/inch in source image.
bool IsFullImage() const
Return true if we are processing the full image.
int pix_wpl_
Words per line of pix_.
virtual void Init()
Common initialization shared between SetImage methods.
DLLSYM void tprintf(const char *format,...)
int image_height_
Height of source pix_.
virtual Pix * GetPixRectGrey()
int OtsuThreshold(Pix *src_pix, int left, int top, int width, int height, int **thresholds, int **hi_values)
void SetRectangle(int left, int top, int width, int height)
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
Returns false on error.
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
virtual ~ImageThresholder()
virtual Pix * GetPixRectThresholds()
int scale_
Scale factor from original image.
void OtsuThresholdRectToPix(Pix *src_pix, Pix **out_pix) const
int image_width_
Width of source pix_.