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);
306 pixSetXRes(*pix, pixGetXRes(src_pix));
307 pixSetYRes(*pix, pixGetYRes(src_pix));
309 const uint32_t* linedata = srcdata + (y +
rect_top_) * src_wpl;
310 uint32_t* pixline = pixdata + y * wpl;
312 bool white_result =
true;
313 for (
int ch = 0; ch < num_channels; ++ch) {
315 GET_DATA_BYTE(linedata, (x +
rect_left_) * num_channels + ch);
316 if (hi_values[ch] >= 0 &&
317 (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
318 white_result =
false;
323 CLEAR_DATA_BIT(pixline, x);
325 SET_DATA_BIT(pixline, x);