1 | #include "mupdf/fitz.h" |
2 | #include "fitz-imp.h" |
3 | |
4 | #include <assert.h> |
5 | |
6 | struct fz_halftone_s |
7 | { |
8 | int refs; |
9 | int n; |
10 | fz_pixmap *comp[1]; |
11 | }; |
12 | |
13 | static fz_halftone * |
14 | fz_new_halftone(fz_context *ctx, int comps) |
15 | { |
16 | fz_halftone *ht; |
17 | int i; |
18 | |
19 | ht = fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)); |
20 | ht->refs = 1; |
21 | ht->n = comps; |
22 | for (i = 0; i < comps; i++) |
23 | ht->comp[i] = NULL; |
24 | |
25 | return ht; |
26 | } |
27 | |
28 | fz_halftone * |
29 | fz_keep_halftone(fz_context *ctx, fz_halftone *ht) |
30 | { |
31 | return fz_keep_imp(ctx, ht, &ht->refs); |
32 | } |
33 | |
34 | void |
35 | fz_drop_halftone(fz_context *ctx, fz_halftone *ht) |
36 | { |
37 | int i; |
38 | if (fz_drop_imp(ctx, ht, &ht->refs)) |
39 | { |
40 | for (i = 0; i < ht->n; i++) |
41 | fz_drop_pixmap(ctx, ht->comp[i]); |
42 | fz_free(ctx, ht); |
43 | } |
44 | } |
45 | |
46 | /* Default mono halftone, lifted from Ghostscript. */ |
47 | /* The 0x00 entry has been changed to 0x01 to avoid problems with white |
48 | * pixels appearing in the output; as we use < 0 should not appear in the |
49 | * array. I think that gs scales this slightly and hence never actually uses |
50 | * the raw values here. */ |
51 | static unsigned char mono_ht[] = |
52 | { |
53 | 0x0E, 0x8E, 0x2E, 0xAE, 0x06, 0x86, 0x26, 0xA6, 0x0C, 0x8C, 0x2C, 0xAC, 0x04, 0x84, 0x24, 0xA4, |
54 | 0xCE, 0x4E, 0xEE, 0x6E, 0xC6, 0x46, 0xE6, 0x66, 0xCC, 0x4C, 0xEC, 0x6C, 0xC4, 0x44, 0xE4, 0x64, |
55 | 0x3E, 0xBE, 0x1E, 0x9E, 0x36, 0xB6, 0x16, 0x96, 0x3C, 0xBC, 0x1C, 0x9C, 0x34, 0xB4, 0x14, 0x94, |
56 | 0xFE, 0x7E, 0xDE, 0x5E, 0xF6, 0x76, 0xD6, 0x56, 0xFC, 0x7C, 0xDC, 0x5C, 0xF4, 0x74, 0xD4, 0x54, |
57 | 0x01, 0x81, 0x21, 0xA1, 0x09, 0x89, 0x29, 0xA9, 0x03, 0x83, 0x23, 0xA3, 0x0B, 0x8B, 0x2B, 0xAB, |
58 | 0xC1, 0x41, 0xE1, 0x61, 0xC9, 0x49, 0xE9, 0x69, 0xC3, 0x43, 0xE3, 0x63, 0xCB, 0x4B, 0xEB, 0x6B, |
59 | 0x31, 0xB1, 0x11, 0x91, 0x39, 0xB9, 0x19, 0x99, 0x33, 0xB3, 0x13, 0x93, 0x3B, 0xBB, 0x1B, 0x9B, |
60 | 0xF1, 0x71, 0xD1, 0x51, 0xF9, 0x79, 0xD9, 0x59, 0xF3, 0x73, 0xD3, 0x53, 0xFB, 0x7B, 0xDB, 0x5B, |
61 | 0x0D, 0x8D, 0x2D, 0xAD, 0x05, 0x85, 0x25, 0xA5, 0x0F, 0x8F, 0x2F, 0xAF, 0x07, 0x87, 0x27, 0xA7, |
62 | 0xCD, 0x4D, 0xED, 0x6D, 0xC5, 0x45, 0xE5, 0x65, 0xCF, 0x4F, 0xEF, 0x6F, 0xC7, 0x47, 0xE7, 0x67, |
63 | 0x3D, 0xBD, 0x1D, 0x9D, 0x35, 0xB5, 0x15, 0x95, 0x3F, 0xBF, 0x1F, 0x9F, 0x37, 0xB7, 0x17, 0x97, |
64 | 0xFD, 0x7D, 0xDD, 0x5D, 0xF5, 0x75, 0xD5, 0x55, 0xFF, 0x7F, 0xDF, 0x5F, 0xF7, 0x77, 0xD7, 0x57, |
65 | 0x02, 0x82, 0x22, 0xA2, 0x0A, 0x8A, 0x2A, 0xAA, 0x01 /*0x00*/, 0x80, 0x20, 0xA0, 0x08, 0x88, 0x28, 0xA8, |
66 | 0xC2, 0x42, 0xE2, 0x62, 0xCA, 0x4A, 0xEA, 0x6A, 0xC0, 0x40, 0xE0, 0x60, 0xC8, 0x48, 0xE8, 0x68, |
67 | 0x32, 0xB2, 0x12, 0x92, 0x3A, 0xBA, 0x1A, 0x9A, 0x30, 0xB0, 0x10, 0x90, 0x38, 0xB8, 0x18, 0x98, |
68 | 0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58 |
69 | }; |
70 | |
71 | /* |
72 | Create a 'default' halftone structure |
73 | for the given number of components. |
74 | |
75 | num_comps: The number of components to use. |
76 | |
77 | Returns a simple default halftone. The default halftone uses |
78 | the same halftone tile for each plane, which may not be ideal |
79 | for all purposes. |
80 | */ |
81 | fz_halftone *fz_default_halftone(fz_context *ctx, int num_comps) |
82 | { |
83 | fz_halftone *ht = fz_new_halftone(ctx, num_comps); |
84 | |
85 | fz_try(ctx) |
86 | { |
87 | int i; |
88 | for (i = 0; i < num_comps; i++) |
89 | ht->comp[i] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, NULL, 1, 16, mono_ht); |
90 | } |
91 | fz_catch(ctx) |
92 | { |
93 | fz_drop_halftone(ctx, ht); |
94 | fz_rethrow(ctx); |
95 | } |
96 | |
97 | return ht; |
98 | } |
99 | |
100 | /* Finally, code to actually perform halftoning. */ |
101 | static void make_ht_line(unsigned char *buf, fz_halftone *ht, int x, int y, int w) |
102 | { |
103 | int k, n; |
104 | n = ht->n; |
105 | for (k = 0; k < n; k++) |
106 | { |
107 | fz_pixmap *tile = ht->comp[k]; |
108 | unsigned char *b = buf++; |
109 | unsigned char *t; |
110 | unsigned char *tbase; |
111 | int px = x + tile->x; |
112 | int py = y + tile->y; |
113 | int tw = tile->w; |
114 | int th = tile->h; |
115 | int w2 = w; |
116 | int len; |
117 | px = px % tw; |
118 | if (px < 0) |
119 | px += tw; |
120 | py = py % th; |
121 | if (py < 0) |
122 | py += th; |
123 | |
124 | assert(tile->n == 1); |
125 | |
126 | /* Left hand section; from x to tile width */ |
127 | tbase = tile->samples + (unsigned int)(py * tw); |
128 | t = tbase + px; |
129 | len = tw - px; |
130 | if (len > w2) |
131 | len = w2; |
132 | w2 -= len; |
133 | while (len--) |
134 | { |
135 | *b = *t++; |
136 | b += n; |
137 | } |
138 | |
139 | /* Centre section - complete copies */ |
140 | w2 -= tw; |
141 | while (w2 >= 0) |
142 | { |
143 | len = tw; |
144 | t = tbase; |
145 | while (len--) |
146 | { |
147 | *b = *t++; |
148 | b += n; |
149 | } |
150 | w2 -= tw; |
151 | } |
152 | w2 += tw; |
153 | |
154 | /* Right hand section - stragglers */ |
155 | t = tbase; |
156 | while (w2--) |
157 | { |
158 | *b = *t++; |
159 | b += n; |
160 | } |
161 | } |
162 | } |
163 | |
164 | /* Inner mono thresholding code */ |
165 | typedef void (threshold_fn)(const unsigned char *ht_line, const unsigned char *pixmap, unsigned char *out, int w, int ht_len); |
166 | |
167 | #ifdef ARCH_ARM |
168 | static void |
169 | do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
170 | __attribute__((naked)); |
171 | |
172 | static void |
173 | do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
174 | { |
175 | asm volatile( |
176 | ENTER_ARM |
177 | // Store one more reg that required to keep double stack alignment |
178 | ".syntax unified\n" |
179 | "stmfd r13!,{r4-r7,r9,r14} \n" |
180 | "@ r0 = ht_line \n" |
181 | "@ r1 = pixmap \n" |
182 | "@ r2 = out \n" |
183 | "@ r3 = w \n" |
184 | "@ <> = ht_len \n" |
185 | "ldr r9, [r13,#6*4] @ r9 = ht_len \n" |
186 | "subs r3, r3, #7 @ r3 = w -= 7 \n" |
187 | "ble 2f @ while (w > 0) { \n" |
188 | "mov r12,r9 @ r12= l = ht_len \n" |
189 | "b 1f \n" |
190 | "9: \n" |
191 | "strb r14,[r2], #1 @ *out++ = 0 \n" |
192 | "subs r12,r12,#8 @ r12 = l -= 8 \n" |
193 | "moveq r12,r9 @ if(l==0) l = ht_len \n" |
194 | "subeq r0, r0, r9 @ ht_line -= l \n" |
195 | "subs r3, r3, #8 @ w -= 8 \n" |
196 | "ble 2f @ } \n" |
197 | "1: \n" |
198 | "ldr r14,[r1], #4 @ r14= pixmap[0..3] \n" |
199 | "ldr r5, [r1], #4 @ r5 = pixmap[4..7] \n" |
200 | "ldrb r4, [r0], #8 @ r0 = ht_line += 8 \n" |
201 | "adds r14,r14,#1 @ set eq iff r14=-1 \n" |
202 | "addseq r5, r5, #1 @ set eq iff r14=r5=-1 \n" |
203 | "beq 9b @ white \n" |
204 | "ldrb r5, [r1, #-8] @ r5 = pixmap[0] \n" |
205 | "ldrb r6, [r0, #-7] @ r6 = ht_line[1] \n" |
206 | "ldrb r7, [r1, #-7] @ r7 = pixmap[1] \n" |
207 | "mov r14,#0 @ r14= h = 0 \n" |
208 | "cmp r5, r4 @ if (r5 < r4) \n" |
209 | "orrlt r14,r14,#0x80 @ h |= 0x80 \n" |
210 | "ldrb r4, [r0, #-6] @ r4 = ht_line[2] \n" |
211 | "ldrb r5, [r1, #-6] @ r5 = pixmap[2] \n" |
212 | "cmp r7, r6 @ if (r7 < r6) \n" |
213 | "orrlt r14,r14,#0x40 @ h |= 0x40 \n" |
214 | "ldrb r6, [r0, #-5] @ r6 = ht_line[3] \n" |
215 | "ldrb r7, [r1, #-5] @ r7 = pixmap[3] \n" |
216 | "cmp r5, r4 @ if (r5 < r4) \n" |
217 | "orrlt r14,r14,#0x20 @ h |= 0x20 \n" |
218 | "ldrb r4, [r0, #-4] @ r4 = ht_line[4] \n" |
219 | "ldrb r5, [r1, #-4] @ r5 = pixmap[4] \n" |
220 | "cmp r7, r6 @ if (r7 < r6) \n" |
221 | "orrlt r14,r14,#0x10 @ h |= 0x10 \n" |
222 | "ldrb r6, [r0, #-3] @ r6 = ht_line[5] \n" |
223 | "ldrb r7, [r1, #-3] @ r7 = pixmap[5] \n" |
224 | "cmp r5, r4 @ if (r5 < r4) \n" |
225 | "orrlt r14,r14,#0x08 @ h |= 0x08 \n" |
226 | "ldrb r4, [r0, #-2] @ r4 = ht_line[6] \n" |
227 | "ldrb r5, [r1, #-2] @ r5 = pixmap[6] \n" |
228 | "cmp r7, r6 @ if (r7 < r6) \n" |
229 | "orrlt r14,r14,#0x04 @ h |= 0x04 \n" |
230 | "ldrb r6, [r0, #-1] @ r6 = ht_line[7] \n" |
231 | "ldrb r7, [r1, #-1] @ r7 = pixmap[7] \n" |
232 | "cmp r5, r4 @ if (r5 < r4) \n" |
233 | "orrlt r14,r14,#0x02 @ h |= 0x02 \n" |
234 | "cmp r7, r6 @ if (r7 < r6) \n" |
235 | "orrlt r14,r14,#0x01 @ h |= 0x01 \n" |
236 | "subs r12,r12,#8 @ r12 = l -= 8 \n" |
237 | "strb r14,[r2], #1 @ *out++ = h \n" |
238 | "moveq r12,r9 @ if(l==0) l = ht_len \n" |
239 | "subeq r0, r0, r9 @ ht_line -= l \n" |
240 | "subs r3, r3, #8 @ w -= 8 \n" |
241 | "bgt 1b @ } \n" |
242 | "2: \n" |
243 | "adds r3, r3, #7 @ w += 7 \n" |
244 | "ble 4f @ if (w >= 0) { \n" |
245 | "ldrb r4, [r0], #1 @ r4 = ht_line[0] \n" |
246 | "ldrb r5, [r1], #1 @ r5 = pixmap[0] \n" |
247 | "mov r14, #0 @ r14= h = 0 \n" |
248 | "cmp r5, r4 @ if (r5 < r4) \n" |
249 | "orrlt r14,r14,#0x80 @ h |= 0x80 \n" |
250 | "cmp r3, #1 @ \n" |
251 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[1] \n" |
252 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[1] \n" |
253 | "ble 3f @ \n" |
254 | "cmp r5, r4 @ if (r5 < r4) \n" |
255 | "orrlt r14,r14,#0x40 @ h |= 0x40 \n" |
256 | "cmp r3, #2 @ \n" |
257 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[2] \n" |
258 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[2] \n" |
259 | "ble 3f @ \n" |
260 | "cmp r5, r4 @ if (r5 < r4) \n" |
261 | "orrlt r14,r14,#0x20 @ h |= 0x20 \n" |
262 | "cmp r3, #3 @ \n" |
263 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[3] \n" |
264 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[3] \n" |
265 | "ble 3f @ \n" |
266 | "cmp r5, r4 @ if (r5 < r4) \n" |
267 | "orrlt r14,r14,#0x10 @ h |= 0x10 \n" |
268 | "cmp r3, #4 @ \n" |
269 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[4] \n" |
270 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[4] \n" |
271 | "ble 3f @ \n" |
272 | "cmp r5, r4 @ if (r5 < r4) \n" |
273 | "orrlt r14,r14,#0x08 @ h |= 0x08 \n" |
274 | "cmp r3, #5 @ \n" |
275 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[5] \n" |
276 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[5] \n" |
277 | "ble 3f @ \n" |
278 | "cmp r5, r4 @ if (r5 < r4) \n" |
279 | "orrlt r14,r14,#0x04 @ h |= 0x04 \n" |
280 | "cmp r3, #6 @ \n" |
281 | "ldrbgt r4, [r0], #1 @ r6 = ht_line[6] \n" |
282 | "ldrbgt r5, [r1], #1 @ r7 = pixmap[6] \n" |
283 | "ble 3f @ \n" |
284 | "cmp r5, r4 @ if (r5 < r4) \n" |
285 | "orrlt r14,r14,#0x02 @ h |= 0x02 \n" |
286 | "3: \n" |
287 | "strb r14,[r2] @ *out = h \n" |
288 | "4: \n" |
289 | "ldmfd r13!,{r4-r7,r9,PC} @ pop, return to thumb \n" |
290 | ENTER_THUMB |
291 | ); |
292 | } |
293 | #else |
294 | static void do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
295 | { |
296 | int h; |
297 | int l = ht_len; |
298 | |
299 | w -= 7; |
300 | while (w > 0) |
301 | { |
302 | h = 0; |
303 | if (pixmap[0] < ht_line[0]) |
304 | h |= 0x80; |
305 | if (pixmap[1] < ht_line[1]) |
306 | h |= 0x40; |
307 | if (pixmap[2] < ht_line[2]) |
308 | h |= 0x20; |
309 | if (pixmap[3] < ht_line[3]) |
310 | h |= 0x10; |
311 | if (pixmap[4] < ht_line[4]) |
312 | h |= 0x08; |
313 | if (pixmap[5] < ht_line[5]) |
314 | h |= 0x04; |
315 | if (pixmap[6] < ht_line[6]) |
316 | h |= 0x02; |
317 | if (pixmap[7] < ht_line[7]) |
318 | h |= 0x01; |
319 | pixmap += 8; |
320 | ht_line += 8; |
321 | l -= 8; |
322 | if (l == 0) |
323 | { |
324 | l = ht_len; |
325 | ht_line -= ht_len; |
326 | } |
327 | *out++ = h; |
328 | w -= 8; |
329 | } |
330 | if (w > -7) |
331 | { |
332 | h = 0; |
333 | if (pixmap[0] < ht_line[0]) |
334 | h |= 0x80; |
335 | if (w > -6 && pixmap[1] < ht_line[1]) |
336 | h |= 0x40; |
337 | if (w > -5 && pixmap[2] < ht_line[2]) |
338 | h |= 0x20; |
339 | if (w > -4 && pixmap[3] < ht_line[3]) |
340 | h |= 0x10; |
341 | if (w > -3 && pixmap[4] < ht_line[4]) |
342 | h |= 0x08; |
343 | if (w > -2 && pixmap[5] < ht_line[5]) |
344 | h |= 0x04; |
345 | if (w > -1 && pixmap[6] < ht_line[6]) |
346 | h |= 0x02; |
347 | *out++ = h; |
348 | } |
349 | } |
350 | #endif |
351 | |
352 | /* |
353 | Note that the tests in do_threshold_4 are inverted compared to those |
354 | in do_threshold_1. This is to allow for the fact that the CMYK |
355 | contone renderings have white = 0, whereas rgb, and greyscale have |
356 | white = 0xFF. Reversing these tests enables us to maintain that |
357 | BlackIs1 in bitmaps. |
358 | */ |
359 | #ifdef ARCH_ARM |
360 | static void |
361 | do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
362 | __attribute__((naked)); |
363 | |
364 | static void |
365 | do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
366 | { |
367 | asm volatile( |
368 | ENTER_ARM |
369 | // Store one more reg that required to keep double stack alignment |
370 | "stmfd r13!,{r4-r7,r9,r14} \n" |
371 | "@ r0 = ht_line \n" |
372 | "@ r1 = pixmap \n" |
373 | "@ r2 = out \n" |
374 | "@ r3 = w \n" |
375 | "@ <> = ht_len \n" |
376 | "ldr r9, [r13,#6*4] @ r9 = ht_len \n" |
377 | "subs r3, r3, #1 @ r3 = w -= 1 \n" |
378 | "ble 2f @ while (w > 0) { \n" |
379 | "mov r12,r9 @ r12= l = ht_len \n" |
380 | "b 1f @ \n" |
381 | "9: @ \n" |
382 | "strb r14,[r2], #1 @ *out++ = h \n" |
383 | "subs r12,r12,#2 @ r12 = l -= 2 \n" |
384 | "moveq r12,r9 @ if(l==0) l = ht_len \n" |
385 | "subeq r0, r0, r9, LSL #2 @ ht_line -= l \n" |
386 | "subs r3, r3, #2 @ w -= 2 \n" |
387 | "beq 2f @ } \n" |
388 | "blt 3f @ \n" |
389 | "1: \n" |
390 | "ldr r5, [r1], #4 @ r5 = pixmap[0..3] \n" |
391 | "ldr r7, [r1], #4 @ r7 = pixmap[4..7] \n" |
392 | "add r0, r0, #8 @ r0 = ht_line += 8 \n" |
393 | "mov r14,#0 @ r14= h = 0 \n" |
394 | "orrs r5, r5, r7 @ if (r5 | r7 == 0) \n" |
395 | "beq 9b @ white \n" |
396 | "ldrb r4, [r0, #-8] @ r4 = ht_line[0] \n" |
397 | "ldrb r5, [r1, #-8] @ r5 = pixmap[0] \n" |
398 | "ldrb r6, [r0, #-7] @ r6 = ht_line[1] \n" |
399 | "ldrb r7, [r1, #-7] @ r7 = pixmap[1] \n" |
400 | "cmp r4, r5 @ if (r4 < r5) \n" |
401 | "orrle r14,r14,#0x80 @ h |= 0x80 \n" |
402 | "ldrb r4, [r0, #-6] @ r4 = ht_line[2] \n" |
403 | "ldrb r5, [r1, #-6] @ r5 = pixmap[2] \n" |
404 | "cmp r6, r7 @ if (r6 < r7) \n" |
405 | "orrle r14,r14,#0x40 @ h |= 0x40 \n" |
406 | "ldrb r6, [r0, #-5] @ r6 = ht_line[3] \n" |
407 | "ldrb r7, [r1, #-5] @ r7 = pixmap[3] \n" |
408 | "cmp r4, r5 @ if (r4 < r5) \n" |
409 | "orrle r14,r14,#0x20 @ h |= 0x20 \n" |
410 | "ldrb r4, [r0, #-4] @ r4 = ht_line[4] \n" |
411 | "ldrb r5, [r1, #-4] @ r5 = pixmap[4] \n" |
412 | "cmp r6, r7 @ if (r6 < r7) \n" |
413 | "orrle r14,r14,#0x10 @ h |= 0x10 \n" |
414 | "ldrb r6, [r0, #-3] @ r6 = ht_line[5] \n" |
415 | "ldrb r7, [r1, #-3] @ r7 = pixmap[5] \n" |
416 | "cmp r4, r5 @ if (r4 < r5) \n" |
417 | "orrle r14,r14,#0x08 @ h |= 0x08 \n" |
418 | "ldrb r4, [r0, #-2] @ r4 = ht_line[6] \n" |
419 | "ldrb r5, [r1, #-2] @ r5 = pixmap[6] \n" |
420 | "cmp r6, r7 @ if (r6 < r7) \n" |
421 | "orrle r14,r14,#0x04 @ h |= 0x04 \n" |
422 | "ldrb r6, [r0, #-1] @ r6 = ht_line[7] \n" |
423 | "ldrb r7, [r1, #-1] @ r7 = pixmap[7] \n" |
424 | "cmp r4, r5 @ if (r4 < r5) \n" |
425 | "orrle r14,r14,#0x02 @ h |= 0x02 \n" |
426 | "cmp r6, r7 @ if (r7 < r6) \n" |
427 | "orrle r14,r14,#0x01 @ h |= 0x01 \n" |
428 | "subs r12,r12,#2 @ r12 = l -= 2 \n" |
429 | "strb r14,[r2], #1 @ *out++ = h \n" |
430 | "moveq r12,r9 @ if(l==0) l = ht_len \n" |
431 | "subeq r0, r0, r9, LSL #2 @ ht_line -= l \n" |
432 | "subs r3, r3, #2 @ w -= 2 \n" |
433 | "bgt 1b @ } \n" |
434 | "blt 3f @ \n" |
435 | "2: \n" |
436 | "ldrb r4, [r0], #1 @ r4 = ht_line[0] \n" |
437 | "ldrb r5, [r1], #1 @ r5 = pixmap[0] \n" |
438 | "mov r14, #0 @ r14= h = 0 \n" |
439 | "ldrb r6, [r0], #1 @ r6 = ht_line[1] \n" |
440 | "ldrb r7, [r1], #1 @ r7 = pixmap[1] \n" |
441 | "cmp r4, r5 @ if (r4 < r5) \n" |
442 | "orrle r14,r14,#0x80 @ h |= 0x80 \n" |
443 | "ldrb r4, [r0], #1 @ r6 = ht_line[2] \n" |
444 | "ldrb r5, [r1], #1 @ r7 = pixmap[2] \n" |
445 | "cmp r6, r7 @ if (r6 < r7) \n" |
446 | "orrle r14,r14,#0x40 @ h |= 0x40 \n" |
447 | "ldrb r6, [r0], #1 @ r6 = ht_line[1] \n" |
448 | "ldrb r7, [r1], #1 @ r7 = pixmap[3] \n" |
449 | "cmp r4, r5 @ if (r4 < r5) \n" |
450 | "orrle r14,r14,#0x20 @ h |= 0x20 \n" |
451 | "cmp r6, r7 @ if (r6 < r7) \n" |
452 | "orrle r14,r14,#0x10 @ h |= 0x10 \n" |
453 | "strb r14,[r2] @ *out = h \n" |
454 | "3: \n" |
455 | "ldmfd r13!,{r4-r7,r9,PC} @ pop, return to thumb \n" |
456 | ENTER_THUMB |
457 | ); |
458 | } |
459 | #else |
460 | static void do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len) |
461 | { |
462 | int l = ht_len; |
463 | |
464 | w--; |
465 | while (w > 0) |
466 | { |
467 | int h = 0; |
468 | if (pixmap[0] >= ht_line[0]) |
469 | h |= 0x80; |
470 | if (pixmap[1] >= ht_line[1]) |
471 | h |= 0x40; |
472 | if (pixmap[2] >= ht_line[2]) |
473 | h |= 0x20; |
474 | if (pixmap[3] >= ht_line[3]) |
475 | h |= 0x10; |
476 | if (pixmap[4] >= ht_line[4]) |
477 | h |= 0x08; |
478 | if (pixmap[5] >= ht_line[5]) |
479 | h |= 0x04; |
480 | if (pixmap[6] >= ht_line[6]) |
481 | h |= 0x02; |
482 | if (pixmap[7] >= ht_line[7]) |
483 | h |= 0x01; |
484 | *out++ = h; |
485 | l -= 2; |
486 | if (l == 0) |
487 | { |
488 | l = ht_len; |
489 | ht_line -= ht_len<<2; |
490 | } |
491 | pixmap += 8; |
492 | ht_line += 8; |
493 | w -= 2; |
494 | } |
495 | if (w == 0) |
496 | { |
497 | int h = 0; |
498 | if (pixmap[0] >= ht_line[0]) |
499 | h |= 0x80; |
500 | if (pixmap[1] >= ht_line[1]) |
501 | h |= 0x40; |
502 | if (pixmap[2] >= ht_line[2]) |
503 | h |= 0x20; |
504 | if (pixmap[3] >= ht_line[3]) |
505 | h |= 0x10; |
506 | *out = h; |
507 | } |
508 | } |
509 | #endif |
510 | |
511 | /* |
512 | Make a bitmap from a pixmap and a halftone. |
513 | |
514 | pix: The pixmap to generate from. Currently must be a single color |
515 | component with no alpha. |
516 | |
517 | ht: The halftone to use. NULL implies the default halftone. |
518 | |
519 | Returns the resultant bitmap. Throws exceptions in the case of |
520 | failure to allocate. |
521 | */ |
522 | fz_bitmap *fz_new_bitmap_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht) |
523 | { |
524 | return fz_new_bitmap_from_pixmap_band(ctx, pix, ht, 0); |
525 | } |
526 | |
527 | /* TAOCP, vol 2, p337 */ |
528 | static int gcd(int u, int v) |
529 | { |
530 | int r; |
531 | |
532 | do |
533 | { |
534 | if (v == 0) |
535 | return u; |
536 | r = u % v; |
537 | u = v; |
538 | v = r; |
539 | } |
540 | while (1); |
541 | } |
542 | |
543 | /* |
544 | Make a bitmap from a pixmap and a |
545 | halftone, allowing for the position of the pixmap within an |
546 | overall banded rendering. |
547 | |
548 | pix: The pixmap to generate from. Currently must be a single color |
549 | component with no alpha. |
550 | |
551 | ht: The halftone to use. NULL implies the default halftone. |
552 | |
553 | band_start: Vertical offset within the overall banded rendering |
554 | (in pixels) |
555 | |
556 | Returns the resultant bitmap. Throws exceptions in the case of |
557 | failure to allocate. |
558 | */ |
559 | fz_bitmap *fz_new_bitmap_from_pixmap_band(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht, int band_start) |
560 | { |
561 | fz_bitmap *out = NULL; |
562 | unsigned char *ht_line = NULL; |
563 | unsigned char *o, *p; |
564 | int w, h, x, y, n, pstride, ostride, lcm, i; |
565 | fz_halftone *ht_ = NULL; |
566 | threshold_fn *thresh; |
567 | |
568 | fz_var(ht_line); |
569 | |
570 | if (!pix) |
571 | return NULL; |
572 | |
573 | if (pix->alpha != 0) |
574 | fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap may not have alpha channel to convert to bitmap" ); |
575 | |
576 | n = pix->n; |
577 | |
578 | switch(n) |
579 | { |
580 | case 1: |
581 | thresh = do_threshold_1; |
582 | break; |
583 | case 4: |
584 | thresh = do_threshold_4; |
585 | break; |
586 | default: |
587 | fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or CMYK to convert to bitmap" ); |
588 | return NULL; |
589 | } |
590 | |
591 | if (ht == NULL) |
592 | ht_ = ht = fz_default_halftone(ctx, n); |
593 | |
594 | /* Find the minimum length for the halftone line. This |
595 | * is the LCM of the halftone lengths and 8. (We need a |
596 | * multiple of 8 for the unrolled threshold routines - if |
597 | * we ever use SSE, we may need longer.) We use the fact |
598 | * that LCM(a,b) = a * b / GCD(a,b) and use euclids |
599 | * algorithm. |
600 | */ |
601 | lcm = 8; |
602 | for (i = 0; i < ht->n; i++) |
603 | { |
604 | w = ht->comp[i]->w; |
605 | lcm = lcm / gcd(lcm, w) * w; |
606 | } |
607 | |
608 | fz_try(ctx) |
609 | { |
610 | ht_line = fz_malloc(ctx, lcm * n); |
611 | out = fz_new_bitmap(ctx, pix->w, pix->h, n, pix->xres, pix->yres); |
612 | o = out->samples; |
613 | p = pix->samples; |
614 | |
615 | h = pix->h; |
616 | x = pix->x; |
617 | y = pix->y + band_start; |
618 | w = pix->w; |
619 | ostride = out->stride; |
620 | pstride = pix->stride; |
621 | while (h--) |
622 | { |
623 | make_ht_line(ht_line, ht, x, y++, lcm); |
624 | thresh(ht_line, p, o, w, lcm); |
625 | o += ostride; |
626 | p += pstride; |
627 | } |
628 | } |
629 | fz_always(ctx) |
630 | { |
631 | fz_drop_halftone(ctx, ht_); |
632 | fz_free(ctx, ht_line); |
633 | } |
634 | fz_catch(ctx) |
635 | fz_rethrow(ctx); |
636 | |
637 | return out; |
638 | } |
639 | |