Debian Codegraph @

src/alg/smooth.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * smooth.c - pre-filtering bitmap before splitting
3 */
4
5 #include "../base/mdjvucfg.h"
6 #include <minidjvu/minidjvu.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 /* all input rows must be 0-or-1 unpacked */
12 static void smooth_row(unsigned char *r, /* result */
13 unsigned char *u, /* upper row */
14 unsigned char *t, /* this row - must have margin 1 */
15 unsigned char *l, /* lower row */
16 int32 n)
17 {
18 int32 i;
19 for (i = 0; i < n; i++)
20 {
•21 int score = u[i] + l[i] + t[i-1] + t[i+1];
22
•23 if (t[i] == 0)
24 {
25 /* Only turn white into black for a good reason. */
26 r[i] = (score == 4);
27 }
28 else if (score == 0)
29 {
30 /* Good reason to clear the pixel. */
31 r[i] = 0;
32 }
33 else if (score == 1)
34 {
35 /* Check for weak horizontal or vertical linking. */
36 if (u[i] | l[i])
37 r[i] = (u[i-1] & l[i-1]) | (u[i+1] & l[i+1]);
38 else
39 r[i] = (u[i-1] & u[i+1]) | (l[i-1] & l[i+1]);
40 }
41 else
42 {
43 /* Keep it black. */
44 r[i] = 1;
45 }
46 }
47 }
48
49
50 MDJVU_IMPLEMENT void mdjvu_smooth(mdjvu_bitmap_t b)
51 {
52 int32 w = mdjvu_bitmap_get_width(b);
53 int32 h = mdjvu_bitmap_get_height(b);
54 int32 i;
55 unsigned char *u, *t, *l, *r;
56
57 if (h < 3) return;
58
59 u = (unsigned char *) calloc(w + 2, 1) + 1; /* upper row */
60 t = (unsigned char *) calloc(w + 2, 1) + 1; /* this row */
61 l = (unsigned char *) calloc(w + 2, 1) + 1; /* lower row */
62 r = (unsigned char *) malloc(w); /* result */
63
64 mdjvu_bitmap_unpack_row_0_or_1(b, l, 0);
65 for (i = 0; i < h; i++)
66 {
67 unsigned char *tmp = u;
68 u = t;
69 t = l;
70 l = tmp;
71
72 if (i + 1 < h)
73 mdjvu_bitmap_unpack_row_0_or_1(b, l, i + 1);
74 else
75 memset(l, 0, w);
76
77 smooth_row(r, u, t, l, w);
78 mdjvu_bitmap_pack_row(b, r, i);
79 }
80
81 free(u - 1);
82 free(t - 1);
83 free(l - 1);
84 free(r);
85 }