forked from unpaper/unpaper
/
tools.c
561 lines (513 loc) · 17.2 KB
/
tools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright © 2005-2007 Jens Gulden
// Copyright © 2011-2011 Diego Elio Pettenò
// SPDX-FileCopyrightText: 2005 The unpaper authors
//
// SPDX-License-Identifier: GPL-2.0-only
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libavutil/avutil.h>
#include <libavutil/pixfmt.h>
#include "tools.h"
#include "unpaper.h"
/****************************************************************************
* tool functions *
* - arithmetic tool functions *
* - tool functions for image handling *
****************************************************************************/
static inline uint8_t pixelGrayscale(uint8_t r, uint8_t g, uint8_t b) {
return (r + g + b) / 3;
}
/* --- tool functions for image handling ---------------------------------- */
static void getPixelComponents(AVFrame *image, int x, int y, uint8_t *r,
uint8_t *g, uint8_t *b, uint8_t defval) {
uint8_t *pix;
if ((x < 0) || (x >= image->width) || (y < 0) || (y >= image->height)) {
*r = *g = *b = defval;
return;
}
switch (image->format) {
case AV_PIX_FMT_GRAY8:
pix = image->data[0] + (y * image->linesize[0] + x);
*r = *g = *b = *pix;
break;
case AV_PIX_FMT_Y400A:
pix = image->data[0] + (y * image->linesize[0] + x * 2);
*r = *g = *b = *pix;
break;
case AV_PIX_FMT_RGB24:
pix = image->data[0] + (y * image->linesize[0] + x * 3);
*r = pix[0];
*g = pix[1];
*b = pix[2];
break;
case AV_PIX_FMT_MONOWHITE:
pix = image->data[0] + (y * image->linesize[0] + x / 8);
if (*pix & (128 >> (x % 8)))
*r = *g = *b = BLACK;
else
*r = *g = *b = WHITE;
break;
case AV_PIX_FMT_MONOBLACK:
pix = image->data[0] + (y * image->linesize[0] + x / 8);
if (*pix & (128 >> (x % 8)))
*r = *g = *b = WHITE;
else
*r = *g = *b = BLACK;
break;
default:
errOutput("unknown pixel format.");
}
}
/**
* Allocates a memory block for storing image data and fills the IMAGE-struct
* with the specified values.
*/
void initImage(AVFrame **image, int width, int height, int pixel_format,
bool fill) {
int ret;
(*image) = av_frame_alloc();
(*image)->width = width;
(*image)->height = height;
(*image)->format = pixel_format;
ret = av_frame_get_buffer(*image, 8);
if (ret < 0) {
char errbuff[1024];
av_strerror(ret, errbuff, sizeof(errbuff));
errOutput("unable to allocate buffer: %s", errbuff);
}
if (fill) {
for (int y = 0; y < (*image)->height; y++) {
for (int x = 0; x < (*image)->width; x++) {
setPixel(sheetBackground, x, y, *image);
}
}
}
}
/**
* Sets the color/grayscale value of a single pixel.
*
* @return true if the pixel has been changed, false if the original color was
* the one to set
*/
bool setPixel(int pixel, int x, int y, AVFrame *image) {
uint8_t *pix;
if ((x < 0) || (x >= image->width) || (y < 0) || (y >= image->height)) {
return false; // nop
}
uint8_t r = (pixel >> 16) & 0xff;
uint8_t g = (pixel >> 8) & 0xff;
uint8_t b = pixel & 0xff;
uint8_t pixelbw = pixelGrayscale(r, g, b) < absBlackThreshold ? BLACK : WHITE;
switch (image->format) {
case AV_PIX_FMT_GRAY8:
pix = image->data[0] + (y * image->linesize[0] + x);
*pix = pixelGrayscale(r, g, b);
break;
case AV_PIX_FMT_Y400A:
pix = image->data[0] + (y * image->linesize[0] + x * 2);
pix[0] = pixelGrayscale(r, g, b);
pix[1] = 0xFF; // no alpha.
break;
case AV_PIX_FMT_RGB24:
pix = image->data[0] + (y * image->linesize[0] + x * 3);
pix[0] = r;
pix[1] = g;
pix[2] = b;
break;
case AV_PIX_FMT_MONOWHITE:
pixelbw = ~pixelbw; // reverse compared to following case
case AV_PIX_FMT_MONOBLACK:
pix = image->data[0] + (y * image->linesize[0] + x / 8);
if (pixelbw == WHITE) {
*pix = *pix | (128 >> (x % 8));
} else if (pixelbw == BLACK) {
*pix = *pix & ~(128 >> (x % 8));
}
break;
default:
errOutput("unknown pixel format.");
}
return true;
}
/**
* Returns the color or grayscale value of a single pixel.
* Always returns a color-compatible value (which may be interpreted as 8-bit
* grayscale)
*
* @return color or grayscale-value of the requested pixel, or WHITE if the
* coordinates are outside the image
*/
int getPixel(int x, int y, AVFrame *image) {
uint8_t r, g, b;
getPixelComponents(image, x, y, &r, &g, &b, WHITE);
return pixelValue(r, g, b);
}
/**
* Returns the grayscale (=brightness) value of a single pixel.
*
* @return grayscale-value of the requested pixel, or WHITE if the coordinates
* are outside the image
*/
static uint8_t getPixelGrayscale(int x, int y, AVFrame *image) {
uint8_t r, g, b;
getPixelComponents(image, x, y, &r, &g, &b, WHITE);
return pixelGrayscale(r, g, b);
}
/**
* Returns the 'lightness' value of a single pixel. For color images, this
* value denotes the minimum brightness of a single color-component in the
* total color, which means that any color is considered 'dark' which has
* either the red, the green or the blue component (or, of course, several
* of them) set to a high value. In some way, this is a measure how close a
* color is to white.
* For grayscale images, this value is equal to the pixel brightness.
*
* @return lightness-value (the higher, the lighter) of the requested pixel, or
* WHITE if the coordinates are outside the image
*/
static uint8_t getPixelLightness(int x, int y, AVFrame *image) {
uint8_t r, g, b;
getPixelComponents(image, x, y, &r, &g, &b, WHITE);
return min3(r, g, b);
}
/**
* Returns the 'inverse-darkness' value of a single pixel. For color images,
* this value denotes the maximum brightness of a single color-component in the
* total color, which means that any color is considered 'light' which has
* either the red, the green or the blue component (or, of course, several
* of them) set to a high value. In some way, this is a measure how far away a
* color is to black.
* For grayscale images, this value is equal to the pixel brightness.
*
* @return inverse-darkness-value (the LOWER, the darker) of the requested
* pixel, or WHITE if the coordinates are outside the image
*/
uint8_t getPixelDarknessInverse(int x, int y, AVFrame *image) {
uint8_t r, g, b;
getPixelComponents(image, x, y, &r, &g, &b, WHITE);
return max3(r, g, b);
}
/**
* Sets the color/grayscale value of a single pixel to white.
*
* @return true if the pixel has been changed, false if the original color was
* the one to set
*/
static bool clearPixel(int x, int y, AVFrame *image) {
return setPixel(WHITE24, x, y, image);
}
/**
* Clears a rectangular area of pixels with either black or white.
* @return The number of pixels actually changed from black (dark) to white.
*/
int clearRect(const int left, const int top, const int right, const int bottom,
AVFrame *image, const int blackwhite) {
int count = 0;
for (int y = top; y <= bottom; y++) {
for (int x = left; x <= right; x++) {
if (setPixel(blackwhite, x, y, image)) {
count++;
}
}
}
return count;
}
/**
* Copies one area of an image into another.
*/
void copyImageArea(const int x, const int y, const int width, const int height,
AVFrame *source, const int toX, const int toY,
AVFrame *target) {
// naive but generic implementation
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
const int pixel = getPixel(x + col, y + row, source);
setPixel(pixel, toX + col, toY + row, target);
}
}
}
/**
* Centers one area of an image inside an area of another image.
* If the source area is smaller than the target area, is is equally
* surrounded by a white border, if it is bigger, it gets equally cropped
* at the edges.
*/
static void centerImageArea(int x, int y, int w, int h, AVFrame *source,
int toX, int toY, int ww, int hh, AVFrame *target) {
if ((w < ww) || (h < hh)) { // white rest-border will remain, so clear first
clearRect(toX, toY, toX + ww - 1, toY + hh - 1, target, sheetBackground);
}
if (w < ww) {
toX += (ww - w) / 2;
}
if (h < hh) {
toY += (hh - h) / 2;
}
if (w > ww) {
x += (w - ww) / 2;
w = ww;
}
if (h > hh) {
y += (h - hh) / 2;
h = hh;
}
copyImageArea(x, y, w, h, source, toX, toY, target);
}
/**
* Centers a whole image inside an area of another image.
*/
void centerImage(AVFrame *source, int toX, int toY, int ww, int hh,
AVFrame *target) {
centerImageArea(0, 0, source->width, source->height, source, toX, toY, ww, hh,
target);
}
/**
* Returns the average brightness of a rectangular area.
*/
uint8_t inverseBrightnessRect(const int x1, const int y1, const int x2,
const int y2, AVFrame *image) {
unsigned int total = 0;
const int count = (x2 - x1 + 1) * (y2 - y1 + 1);
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
total += getPixelGrayscale(x, y, image);
}
}
return WHITE - (total / count);
}
/**
* Returns the inverseaverage lightness of a rectangular area.
*/
uint8_t inverseLightnessRect(const int x1, const int y1, const int x2,
const int y2, AVFrame *image) {
unsigned int total = 0;
const int count = (x2 - x1 + 1) * (y2 - y1 + 1);
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
total += getPixelLightness(x, y, image);
}
}
return WHITE - (total / count);
}
/**
* Returns the average darkness of a rectangular area.
*/
uint8_t darknessRect(const int x1, const int y1, const int x2, const int y2,
AVFrame *image) {
unsigned int total = 0;
const int count = (x2 - x1 + 1) * (y2 - y1 + 1);
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
total += getPixelDarknessInverse(x, y, image);
}
}
return WHITE - (total / count);
}
/**
* Counts the number of pixels in a rectangular area whose grayscale
* values ranges between minColor and maxBrightness. Optionally, the area can
* get cleared with white color while counting.
*/
int countPixelsRect(int left, int top, int right, int bottom, int minColor,
int maxBrightness, bool clear, AVFrame *image) {
int count = 0;
for (int y = top; y <= bottom; y++) {
for (int x = left; x <= right; x++) {
const int pixel = getPixelGrayscale(x, y, image);
if ((pixel >= minColor) && (pixel <= maxBrightness)) {
if (clear == true) {
clearPixel(x, y, image);
}
count++;
}
}
}
return count;
}
/**
* Counts the number of dark pixels around the pixel at (x,y), who have a
* square-metric distance of 'level' to the (x,y) (thus, testing the values
* of 9 pixels if level==1, 16 pixels if level==2 and so on).
* Optionally, the pixels can get cleared after counting.
*/
static int countPixelNeighborsLevel(int x, int y, bool clear, int level,
int whiteMin, AVFrame *image) {
int count = 0;
// upper and lower rows
for (int xx = x - level; xx <= x + level; xx++) {
// upper row
uint8_t pixel = getPixelLightness(xx, y - level, image);
if (pixel < whiteMin) { // non-light pixel found
if (clear == true) {
clearPixel(xx, y - level, image);
}
count++;
}
// lower row
pixel = getPixelLightness(xx, y + level, image);
if (pixel < whiteMin) {
if (clear == true) {
clearPixel(xx, y + level, image);
}
count++;
}
}
// middle rows
for (int yy = y - (level - 1); yy <= y + (level - 1); yy++) {
// first col
uint8_t pixel = getPixelLightness(x - level, yy, image);
if (pixel < whiteMin) {
if (clear == true) {
clearPixel(x - level, yy, image);
}
count++;
}
// last col
pixel = getPixelLightness(x + level, yy, image);
if (pixel < whiteMin) {
if (clear == true) {
clearPixel(x + level, yy, image);
}
count++;
}
}
/* old version, not optimized:
for (yy = y-level; yy <= y+level; yy++) {
for (xx = x-level; xx <= x+level; xx++) {
if (abs(xx-x)==level || abs(yy-y)==level) {
pixel = getPixelLightness(xx, yy, image);
if (pixel < whiteMin) {
if (clear==true) {
clearPixel(xx, yy, image);
}
count++;
}
}
}
}*/
return count;
}
/**
* Count all dark pixels in the distance 0..intensity that are directly
* reachable from the dark pixel at (x,y), without having to cross bright
* pixels.
*/
int countPixelNeighbors(int x, int y, int intensity, int whiteMin,
AVFrame *image) {
int count = 1; // assume self as set
int lCount = -1;
// can finish when one level is completely zero
for (int level = 1; (lCount != 0) && (level <= intensity); level++) {
lCount = countPixelNeighborsLevel(x, y, false, level, whiteMin, image);
count += lCount;
}
return count;
}
/**
* Clears all dark pixels that are directly reachable from the dark pixel at
* (x,y). This should be called only if it has previously been detected that
* the amount of pixels to clear will be reasonable small.
*/
void clearPixelNeighbors(int x, int y, int whiteMin, AVFrame *image) {
int lCount = -1;
clearPixel(x, y, image);
// lCount will become 0, otherwise countPixelNeighbors() would previously have
// delivered a bigger value (and this here would not have been called)
for (int level = 1; lCount != 0; level++) {
lCount = countPixelNeighborsLevel(x, y, true, level, whiteMin, image);
}
}
/**
* Solidly fills a line of pixels heading towards a specified direction
* until color-changes in the pixels to overwrite exceed the 'intensity'
* parameter.
*
* @param stepX either -1 or 1, if stepY is 0, else 0
* @param stepY either -1 or 1, if stepX is 0, else 0
*/
static int fillLine(int x, int y, int stepX, int stepY, int color,
uint8_t maskMin, uint8_t maskMax, int intensity,
AVFrame *image) {
int distance = 0;
int intensityCount = 1; // first pixel must match, otherwise directly exit
const int w = image->width;
const int h = image->height;
while (true) {
x += stepX;
y += stepY;
uint8_t pixel = getPixelGrayscale(x, y, image);
if ((pixel >= maskMin) && (pixel <= maskMax)) {
intensityCount = intensity; // reset counter
} else {
intensityCount--; // allow maximum of 'intensity' pixels to be bright,
// until stop
}
if ((intensityCount > 0) && (x >= 0) && (x < w) && (y >= 0) && (y < h)) {
setPixel(color, x, y, image);
distance++;
} else {
return distance; // exit here
}
}
}
/**
* Start flood-filling around the edges of a line which has previously been
* filled using fillLine(). Here, the flood-fill algorithm performs its
* indirect recursion.
*
* @param stepX either -1 or 1, if stepY is 0, else 0
* @param stepY either -1 or 1, if stepX is 0, else 0
* @see fillLine()
*/
static void floodFillAroundLine(int x, int y, int stepX, int stepY,
int distance, int color, int maskMin,
int maskMax, int intensity, AVFrame *image) {
for (int d = 0; d < distance; d++) {
if (stepX != 0) {
x += stepX;
floodFill(x, y + 1, color, maskMin, maskMax, intensity,
image); // indirect recursion
floodFill(x, y - 1, color, maskMin, maskMax, intensity,
image); // indirect recursion
} else { // stepY != 0
y += stepY;
floodFill(x + 1, y, color, maskMin, maskMax, intensity,
image); // indirect recursion
floodFill(x - 1, y, color, maskMin, maskMax, intensity,
image); // indirect recursion
}
}
}
/**
* Flood-fill an area of pixels. (Naive implementation, optimizable.)
*
* @see earlier header-declaration to enable indirect recursive calls
*/
void floodFill(int x, int y, int color, int maskMin, int maskMax, int intensity,
AVFrame *image) {
// is current pixel to be filled?
const int pixel = getPixelGrayscale(x, y, image);
if ((pixel >= maskMin) && (pixel <= maskMax)) {
// first, fill a 'cross' (both vertical, horizontal line)
setPixel(color, x, y, image);
const int left =
fillLine(x, y, -1, 0, color, maskMin, maskMax, intensity, image);
const int top =
fillLine(x, y, 0, -1, color, maskMin, maskMax, intensity, image);
const int right =
fillLine(x, y, 1, 0, color, maskMin, maskMax, intensity, image);
const int bottom =
fillLine(x, y, 0, 1, color, maskMin, maskMax, intensity, image);
// now recurse on each neighborhood-pixel of the cross (most recursions will
// immediately return)
floodFillAroundLine(x, y, -1, 0, left, color, maskMin, maskMax, intensity,
image);
floodFillAroundLine(x, y, 0, -1, top, color, maskMin, maskMax, intensity,
image);
floodFillAroundLine(x, y, 1, 0, right, color, maskMin, maskMax, intensity,
image);
floodFillAroundLine(x, y, 0, 1, bottom, color, maskMin, maskMax, intensity,
image);
}
}