]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/fitz/halftone.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / fitz / halftone.c
1 #include "mupdf/fitz.h"
2
3 fz_halftone *
4 fz_new_halftone(fz_context *ctx, int comps)
5 {
6         fz_halftone *ht;
7         int i;
8
9         ht = fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *));
10         ht->refs = 1;
11         ht->n = comps;
12         for (i = 0; i < comps; i++)
13                 ht->comp[i] = NULL;
14
15         return ht;
16 }
17
18 fz_halftone *
19 fz_keep_halftone(fz_context *ctx, fz_halftone *ht)
20 {
21         if (ht)
22                 ht->refs++;
23         return ht;
24 }
25
26 void
27 fz_drop_halftone(fz_context *ctx, fz_halftone *ht)
28 {
29         int i;
30
31         if (!ht || --ht->refs != 0)
32                 return;
33         for (i = 0; i < ht->n; i++)
34                 fz_drop_pixmap(ctx, ht->comp[i]);
35         fz_free(ctx, ht);
36 }
37
38 /* Default mono halftone, lifted from Ghostscript. */
39 /* The 0x00 entry has been changed to 0x01 to avoid problems with white
40  * pixels appearing in the output; as we use < 0 should not appear in the
41  * array. I think that gs scales this slighly and hence never actually uses
42  * the raw values here. */
43 static unsigned char mono_ht[] =
44 {
45         0x0E, 0x8E, 0x2E, 0xAE, 0x06, 0x86, 0x26, 0xA6, 0x0C, 0x8C, 0x2C, 0xAC, 0x04, 0x84, 0x24, 0xA4,
46         0xCE, 0x4E, 0xEE, 0x6E, 0xC6, 0x46, 0xE6, 0x66, 0xCC, 0x4C, 0xEC, 0x6C, 0xC4, 0x44, 0xE4, 0x64,
47         0x3E, 0xBE, 0x1E, 0x9E, 0x36, 0xB6, 0x16, 0x96, 0x3C, 0xBC, 0x1C, 0x9C, 0x34, 0xB4, 0x14, 0x94,
48         0xFE, 0x7E, 0xDE, 0x5E, 0xF6, 0x76, 0xD6, 0x56, 0xFC, 0x7C, 0xDC, 0x5C, 0xF4, 0x74, 0xD4, 0x54,
49         0x01, 0x81, 0x21, 0xA1, 0x09, 0x89, 0x29, 0xA9, 0x03, 0x83, 0x23, 0xA3, 0x0B, 0x8B, 0x2B, 0xAB,
50         0xC1, 0x41, 0xE1, 0x61, 0xC9, 0x49, 0xE9, 0x69, 0xC3, 0x43, 0xE3, 0x63, 0xCB, 0x4B, 0xEB, 0x6B,
51         0x31, 0xB1, 0x11, 0x91, 0x39, 0xB9, 0x19, 0x99, 0x33, 0xB3, 0x13, 0x93, 0x3B, 0xBB, 0x1B, 0x9B,
52         0xF1, 0x71, 0xD1, 0x51, 0xF9, 0x79, 0xD9, 0x59, 0xF3, 0x73, 0xD3, 0x53, 0xFB, 0x7B, 0xDB, 0x5B,
53         0x0D, 0x8D, 0x2D, 0xAD, 0x05, 0x85, 0x25, 0xA5, 0x0F, 0x8F, 0x2F, 0xAF, 0x07, 0x87, 0x27, 0xA7,
54         0xCD, 0x4D, 0xED, 0x6D, 0xC5, 0x45, 0xE5, 0x65, 0xCF, 0x4F, 0xEF, 0x6F, 0xC7, 0x47, 0xE7, 0x67,
55         0x3D, 0xBD, 0x1D, 0x9D, 0x35, 0xB5, 0x15, 0x95, 0x3F, 0xBF, 0x1F, 0x9F, 0x37, 0xB7, 0x17, 0x97,
56         0xFD, 0x7D, 0xDD, 0x5D, 0xF5, 0x75, 0xD5, 0x55, 0xFF, 0x7F, 0xDF, 0x5F, 0xF7, 0x77, 0xD7, 0x57,
57         0x02, 0x82, 0x22, 0xA2, 0x0A, 0x8A, 0x2A, 0xAA, 0x01 /*0x00*/, 0x80, 0x20, 0xA0, 0x08, 0x88, 0x28, 0xA8,
58         0xC2, 0x42, 0xE2, 0x62, 0xCA, 0x4A, 0xEA, 0x6A, 0xC0, 0x40, 0xE0, 0x60, 0xC8, 0x48, 0xE8, 0x68,
59         0x32, 0xB2, 0x12, 0x92, 0x3A, 0xBA, 0x1A, 0x9A, 0x30, 0xB0, 0x10, 0x90, 0x38, 0xB8, 0x18, 0x98,
60         0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58
61 };
62
63 fz_halftone *fz_default_halftone(fz_context *ctx, int num_comps)
64 {
65         fz_halftone *ht = fz_new_halftone(ctx, num_comps);
66         assert(num_comps == 1); /* Only support 1 component for now */
67         ht->comp[0] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, mono_ht);
68         return ht;
69 }
70
71 /* Finally, code to actually perform halftoning. */
72 static void make_ht_line(unsigned char *buf, fz_halftone *ht, int x, int y, int w)
73 {
74         /* FIXME: There is a potential optimisation here; in the case where
75          * the LCM of the halftone tile widths is smaller than w, we could
76          * form just one 'LCM' run, then copy it repeatedly.
77          */
78         int k, n;
79         n = ht->n;
80         for (k = 0; k < n; k++)
81         {
82                 fz_pixmap *tile = ht->comp[k];
83                 unsigned char *b = buf++;
84                 unsigned char *t;
85                 unsigned char *tbase;
86                 int px = x + tile->x;
87                 int py = y + tile->y;
88                 int tw = tile->w;
89                 int th = tile->h;
90                 int w2 = w;
91                 int len;
92                 px = px % tw;
93                 if (px < 0)
94                         px += tw;
95                 py = py % th;
96                 if (py < 0)
97                         py += th;
98
99                 assert(tile->n == 1);
100
101                 /* Left hand section; from x to tile width */
102                 tbase = tile->samples + (unsigned int)(py * tw);
103                 t = tbase + px;
104                 len = tw - px;
105                 if (len > w2)
106                         len = w2;
107                 w2 -= len;
108                 while (len--)
109                 {
110                         *b = *t++;
111                         b += n;
112                 }
113
114                 /* Centre section - complete copies */
115                 w2 -= tw;
116                 while (w2 >= 0)
117                 {
118                         len = tw;
119                         t = tbase;
120                         while (len--)
121                         {
122                                 *b = *t++;
123                                 b += n;
124                         }
125                         w2 -= tw;
126                 }
127                 w2 += tw;
128
129                 /* Right hand section - stragglers */
130                 t = tbase;
131                 while (w2--)
132                 {
133                         *b = *t++;
134                         b += n;
135                 }
136         }
137 }
138
139 /* Inner mono thresholding code */
140 static void do_threshold_1(unsigned char *ht_line, unsigned char *pixmap, unsigned char *out, int w)
141 {
142         int bit = 0x80;
143         int h = 0;
144
145         do
146         {
147                 if (*pixmap < *ht_line++)
148                         h |= bit;
149                 pixmap += 2; /* Skip the alpha */
150                 bit >>= 1;
151                 if (bit == 0)
152                 {
153                         *out++ = h;
154                         h = 0;
155                         bit = 0x80;
156                 }
157
158         }
159         while (--w);
160         if (bit != 0x80)
161                 *out = h;
162 }
163
164 fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht)
165 {
166         fz_bitmap *out;
167         unsigned char *ht_line, *o, *p;
168         int w, h, x, y, n, pstride, ostride;
169         fz_halftone *ht_orig = ht;
170
171         if (!pix)
172                 return NULL;
173
174         assert(pix->n == 2); /* Mono + Alpha */
175
176         n = pix->n-1; /* Remove alpha */
177         if (ht == NULL)
178         {
179                 ht = fz_default_halftone(ctx, n);
180         }
181         ht_line = fz_malloc(ctx, pix->w * n);
182         out = fz_new_bitmap(ctx, pix->w, pix->h, n, pix->xres, pix->yres);
183         o = out->samples;
184         p = pix->samples;
185
186         h = pix->h;
187         x = pix->x;
188         y = pix->y;
189         w = pix->w;
190         ostride = out->stride;
191         pstride = pix->w * pix->n;
192         while (h--)
193         {
194                 make_ht_line(ht_line, ht, x, y++, w);
195                 do_threshold_1(ht_line, p, o, w);
196                 o += ostride;
197                 p += pstride;
198         }
199         if (!ht_orig)
200                 fz_drop_halftone(ctx, ht);
201         return out;
202 }