Debian Codegraph @

src/alg/erosion.c

raw
1 /*
2 * erosion.c - determine candidates for flipping by image encoder
3 */
4
5
6 #include "../base/mdjvucfg.h"
7 #include <minidjvu/minidjvu.h>
8 #include <stdlib.h>
9
10
11 /* ERROR ALERT:
12 * We should never declare border pixels
13 * as erosion candidates or otherwise bounding boxes may change
14 * with unpredictable consequences.
15 * (this implementation is OK)
16 */
17
18 /*
19 * it all resembles `smooth' algorithm very much.
20 */
21
22 /*
23 * This implementation permits flipping of a pixel
24 * when it has exactly 2 of 4 edge neighbors black
25 * and exactly 2 of 4 vertex (diagonal) neighbors black.
26 */
27
28
29 /* all input rows must be 0-or-1 unpacked */
30 static void get_erosion_candidates_in_a_row(
31 unsigned char *r, /* result */
32 unsigned char *u, /* upper row */
33 unsigned char *t, /* this row */
34 unsigned char *l, /* lower row */
35 int32 n)
36 {
37 int32 i;
38 n--;
39 r[0] = 0;
40 r[n] = 0;
41 for (i = 1; i < n; i++)
42 {
43 int score_4 = u[i] + l[i] + t[i-1] + t[i+1] - 2;
44 int score_d = u[i-1] + l[i-1] + u[i+1] + l[i+1] - 2;
45
46 r[i] = !score_4 && !score_d ? 1 : 0;
47 }
48 }
49
50 MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_get_erosion_mask(mdjvu_bitmap_t bmp)
51 {
52 int32 w = mdjvu_bitmap_get_width(bmp);
53 int32 h = mdjvu_bitmap_get_height(bmp);
54 mdjvu_bitmap_t result = mdjvu_bitmap_create(w, h);
55 int32 i;
56 unsigned char *u, *t, *l, *r;
57
58 if (h < 3) return result;
59
60 u = (unsigned char *) malloc(w); /* upper row */
61 t = (unsigned char *) malloc(w); /* this row */
62 l = (unsigned char *) malloc(w); /* lower row */
63 r = (unsigned char *) malloc(w); /* result */
64
65 mdjvu_bitmap_unpack_row_0_or_1(bmp, t, 0);
66 mdjvu_bitmap_unpack_row_0_or_1(bmp, l, 1);
67 for (i = 1; i < h - 1; i++)
68 {
69 unsigned char *tmp = u;
70 u = t;
71 t = l;
72 l = tmp;
73
74 mdjvu_bitmap_unpack_row_0_or_1(bmp, l, i + 1);
75
76 get_erosion_candidates_in_a_row(r, u, t, l, w);
77 mdjvu_bitmap_pack_row(result, r, i);
78 }
79
80 free(u);
81 free(t);
82 free(l);
83 free(r);
84
85 return result;
86 }