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);
157 if (pixGetColormap(src)) {
158 Pix* tmp = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
159 depth = pixGetDepth(tmp);
160 if (depth > 1 && depth < 8) {
161 pix_ = pixConvertTo8(tmp,
false);
166 }
else if (depth > 1 && depth < 8) {
167 pix_ = pixConvertTo8(src,
false);
169 pix_ = pixCopy(NULL, src);
171 depth = pixGetDepth(
pix_);
187 *pix = pixCopy(NULL, original);
188 pixDestroy(&original);
204 int width = pixGetWidth(pix_grey);
205 int height = pixGetHeight(pix_grey);
208 OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
209 pixDestroy(&pix_grey);
210 Pix* pix_thresholds = pixCreate(width, height, 8);
211 int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
212 pixSetAllArbitrary(pix_thresholds, threshold);
213 delete [] thresholds;
215 return pix_thresholds;
231 return pixClone(
pix_);
235 Pix* cropped = pixClipRectangle(
pix_, box, NULL);
247 int depth = pixGetDepth(pix);
249 Pix* result = depth < 8 ? pixConvertTo8(pix,
false)
250 : pixConvertRGBToLuminance(pix);
259 Pix** out_pix)
const {
269 if ((num_channels == 4 || num_channels == 1) &&
271 od.ThresholdRectToPixOCL((
unsigned char*)pixGetData(src_pix), num_channels,
272 pixGetWpl(src_pix) * 4, thresholds, hi_values,
281 delete [] thresholds;
293 const int* thresholds,
294 const int* hi_values,
298 uinT32* pixdata = pixGetData(*pix);
299 int wpl = pixGetWpl(*pix);
300 int src_wpl = pixGetWpl(src_pix);
301 uinT32* srcdata = pixGetData(src_pix);
304 uinT32* pixline = pixdata + y * wpl;
306 bool white_result =
true;
307 for (
int ch = 0; ch < num_channels; ++ch) {
308 int pixel = GET_DATA_BYTE(const_cast<void*>(
309 reinterpret_cast<const void *>(linedata)),
311 if (hi_values[ch] >= 0 &&
312 (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
313 white_result =
false;
318 CLEAR_DATA_BIT(pixline, x);
320 SET_DATA_BIT(pixline, x);
virtual ~ImageThresholder()
bool IsFullImage() const
Return true if we are processing the full image.
void ThresholdRectToPix(Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
int OtsuThreshold(Pix *src_pix, int left, int top, int width, int height, int **thresholds, int **hi_values)
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
void SetRectangle(int left, int top, int width, int height)
virtual void ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
#define PERF_COUNT_START(FUNCT_NAME)
virtual Pix * GetPixRectGrey()
virtual Pix * GetPixRectThresholds()
bool IsEmpty() const
Return true if no image has been set.
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 OtsuThresholdRectToPix(Pix *src_pix, Pix **out_pix) const