comparison mupdf-source/source/fitz/halftone.c @ 2:b50eed0cc0ef upstream

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