Coverage Report

Created: 2025-12-03 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/source/fitz/halftone.c
Line
Count
Source
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
0
{
37
0
  fz_halftone *ht;
38
0
  int i;
39
40
0
  ht = Memento_label(fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)), "fz_halftone");
41
0
  ht->refs = 1;
42
0
  ht->n = comps;
43
0
  for (i = 0; i < comps; i++)
44
0
    ht->comp[i] = NULL;
45
46
0
  return ht;
47
0
}
48
49
fz_halftone *
50
fz_keep_halftone(fz_context *ctx, fz_halftone *ht)
51
0
{
52
0
  return fz_keep_imp(ctx, ht, &ht->refs);
53
0
}
54
55
void
56
fz_drop_halftone(fz_context *ctx, fz_halftone *ht)
57
0
{
58
0
  int i;
59
0
  if (fz_drop_imp(ctx, ht, &ht->refs))
60
0
  {
61
0
    for (i = 0; i < ht->n; i++)
62
0
      fz_drop_pixmap(ctx, ht->comp[i]);
63
0
    fz_free(ctx, ht);
64
0
  }
65
0
}
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
0
{
94
0
  fz_halftone *ht = fz_new_halftone(ctx, num_comps);
95
96
0
  fz_try(ctx)
97
0
  {
98
0
    int i;
99
0
    for (i = 0; i < num_comps; i++)
100
0
      ht->comp[i] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, NULL, 1, 16, mono_ht);
101
0
  }
102
0
  fz_catch(ctx)
103
0
  {
104
0
    fz_drop_halftone(ctx, ht);
105
0
    fz_rethrow(ctx);
106
0
  }
107
108
0
  return ht;
109
0
}
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
0
{
114
0
  int k, n;
115
0
  n = ht->n;
116
0
  for (k = 0; k < n; k++)
117
0
  {
118
0
    fz_pixmap *tile = ht->comp[k];
119
0
    unsigned char *b = buf++;
120
0
    unsigned char *t;
121
0
    unsigned char *tbase;
122
0
    int px = x + tile->x;
123
0
    int py = y + tile->y;
124
0
    int tw = tile->w;
125
0
    int th = tile->h;
126
0
    int w2 = w;
127
0
    int len;
128
0
    px = px % tw;
129
0
    if (px < 0)
130
0
      px += tw;
131
0
    py = py % th;
132
0
    if (py < 0)
133
0
      py += th;
134
135
0
    assert(tile->n == 1);
136
137
    /* Left hand section; from x to tile width */
138
0
    tbase = tile->samples + (unsigned int)(py * tw);
139
0
    t = tbase + px;
140
0
    len = tw - px;
141
0
    if (len > w2)
142
0
      len = w2;
143
0
    w2 -= len;
144
0
    while (len--)
145
0
    {
146
0
      *b = *t++;
147
0
      b += n;
148
0
    }
149
150
    /* Centre section - complete copies */
151
0
    w2 -= tw;
152
0
    while (w2 >= 0)
153
0
    {
154
0
      len = tw;
155
0
      t = tbase;
156
0
      while (len--)
157
0
      {
158
0
        *b = *t++;
159
0
        b += n;
160
0
      }
161
0
      w2 -= tw;
162
0
    }
163
0
    w2 += tw;
164
165
    /* Right hand section - stragglers */
166
0
    t = tbase;
167
0
    while (w2--)
168
0
    {
169
0
      *b = *t++;
170
0
      b += n;
171
0
    }
172
0
  }
173
0
}
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
0
{
307
0
  int h;
308
0
  int l = ht_len;
309
310
0
  w -= 7;
311
0
  while (w > 0)
312
0
  {
313
0
    h = 0;
314
0
    if (pixmap[0] < ht_line[0])
315
0
      h |= 0x80;
316
0
    if (pixmap[1] < ht_line[1])
317
0
      h |= 0x40;
318
0
    if (pixmap[2] < ht_line[2])
319
0
      h |= 0x20;
320
0
    if (pixmap[3] < ht_line[3])
321
0
      h |= 0x10;
322
0
    if (pixmap[4] < ht_line[4])
323
0
      h |= 0x08;
324
0
    if (pixmap[5] < ht_line[5])
325
0
      h |= 0x04;
326
0
    if (pixmap[6] < ht_line[6])
327
0
      h |= 0x02;
328
0
    if (pixmap[7] < ht_line[7])
329
0
      h |= 0x01;
330
0
    pixmap += 8;
331
0
    ht_line += 8;
332
0
    l -= 8;
333
0
    if (l == 0)
334
0
    {
335
0
      l = ht_len;
336
0
      ht_line -= ht_len;
337
0
    }
338
0
    *out++ = h;
339
0
    w -= 8;
340
0
  }
341
0
  if (w > -7)
342
0
  {
343
0
    h = 0;
344
0
    if (pixmap[0] < ht_line[0])
345
0
      h |= 0x80;
346
0
    if (w > -6 && pixmap[1] < ht_line[1])
347
0
      h |= 0x40;
348
0
    if (w > -5 && pixmap[2] < ht_line[2])
349
0
      h |= 0x20;
350
0
    if (w > -4 && pixmap[3] < ht_line[3])
351
0
      h |= 0x10;
352
0
    if (w > -3 && pixmap[4] < ht_line[4])
353
0
      h |= 0x08;
354
0
    if (w > -2 && pixmap[5] < ht_line[5])
355
0
      h |= 0x04;
356
0
    if (w > -1 && pixmap[6] < ht_line[6])
357
0
      h |= 0x02;
358
0
    *out++ = h;
359
0
  }
360
0
}
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
0
{
473
0
  int l = ht_len;
474
475
0
  w--;
476
0
  while (w > 0)
477
0
  {
478
0
    int h = 0;
479
0
    if (pixmap[0] >= ht_line[0])
480
0
      h |= 0x80;
481
0
    if (pixmap[1] >= ht_line[1])
482
0
      h |= 0x40;
483
0
    if (pixmap[2] >= ht_line[2])
484
0
      h |= 0x20;
485
0
    if (pixmap[3] >= ht_line[3])
486
0
      h |= 0x10;
487
0
    if (pixmap[4] >= ht_line[4])
488
0
      h |= 0x08;
489
0
    if (pixmap[5] >= ht_line[5])
490
0
      h |= 0x04;
491
0
    if (pixmap[6] >= ht_line[6])
492
0
      h |= 0x02;
493
0
    if (pixmap[7] >= ht_line[7])
494
0
      h |= 0x01;
495
0
    *out++ = h;
496
0
    l -= 2;
497
0
    if (l == 0)
498
0
    {
499
0
      l = ht_len;
500
0
      ht_line -= ht_len<<2;
501
0
    }
502
0
    pixmap += 8;
503
0
    ht_line += 8;
504
0
    w -= 2;
505
0
  }
506
0
  if (w == 0)
507
0
  {
508
0
    int h = 0;
509
0
    if (pixmap[0] >= ht_line[0])
510
0
      h |= 0x80;
511
0
    if (pixmap[1] >= ht_line[1])
512
0
      h |= 0x40;
513
0
    if (pixmap[2] >= ht_line[2])
514
0
      h |= 0x20;
515
0
    if (pixmap[3] >= ht_line[3])
516
0
      h |= 0x10;
517
0
    *out = h;
518
0
  }
519
0
}
520
#endif
521
522
fz_bitmap *fz_new_bitmap_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht)
523
0
{
524
0
  return fz_new_bitmap_from_pixmap_band(ctx, pix, ht, 0);
525
0
}
526
527
fz_bitmap *fz_new_bitmap_from_image(fz_context *ctx, fz_image *img, fz_halftone *ht)
528
0
{
529
0
  fz_pixmap *pix = fz_get_pixmap_from_image(ctx, img, NULL, NULL, NULL, NULL);
530
0
  fz_bitmap *bitmap;
531
0
  fz_try(ctx)
532
0
    bitmap = fz_new_bitmap_from_pixmap_band(ctx, pix, ht, 0);
533
0
  fz_always(ctx)
534
0
    fz_drop_pixmap(ctx, pix);
535
0
  fz_catch(ctx)
536
0
    fz_rethrow(ctx);
537
0
  return bitmap;
538
0
}
539
540
541
void fz_invert_bitmap(fz_context *ctx, fz_bitmap *bmp)
542
0
{
543
0
  unsigned char *s = bmp->samples;
544
0
  int w, h, w2 = (bmp->w+7)>>3;
545
546
0
  for (h = bmp->h; h > 0; h--)
547
0
  {
548
0
    unsigned char *t = s;
549
0
    for (w = w2; w > 0; w--)
550
0
      *t++ ^= 255;
551
0
    s += bmp->stride;
552
0
  }
553
0
}
554
555
/* TAOCP, vol 2, p337 */
556
static int gcd(int u, int v)
557
0
{
558
0
  int r;
559
560
0
  do
561
0
  {
562
0
    if (v == 0)
563
0
      return u;
564
0
    r = u % v;
565
0
    u = v;
566
0
    v = r;
567
0
  }
568
0
  while (1);
569
0
}
570
571
fz_bitmap *fz_new_bitmap_from_pixmap_band(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht, int band_start)
572
0
{
573
0
  fz_bitmap *out = NULL;
574
0
  unsigned char *ht_line = NULL;
575
0
  unsigned char *o, *p;
576
0
  int w, h, x, y, n, pstride, ostride, lcm, i, alpha;
577
0
  fz_halftone *ht_ = NULL;
578
0
  threshold_fn *thresh;
579
580
0
  fz_var(ht_line);
581
582
0
  if (!pix)
583
0
    return NULL;
584
585
0
  n = pix->n;
586
0
  alpha = pix->alpha;
587
588
  /* Treat alpha only as greyscale */
589
0
  if (n == 1 && alpha)
590
0
    alpha = 0;
591
0
  n -= alpha;
592
593
0
  if (alpha != 0)
594
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have alpha channel to convert to bitmap");
595
596
0
  switch(n)
597
0
  {
598
0
  case 1:
599
0
    thresh = do_threshold_1;
600
0
    break;
601
0
  case 4:
602
0
    thresh = do_threshold_4;
603
0
    break;
604
0
  default:
605
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be grayscale or CMYK to convert to bitmap");
606
0
  }
607
608
0
  if (ht == NULL)
609
0
    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
0
  lcm = 8;
619
0
  for (i = 0; i < ht->n; i++)
620
0
  {
621
0
    w = ht->comp[i]->w;
622
0
    lcm = lcm / gcd(lcm, w) * w;
623
0
  }
624
625
0
  fz_try(ctx)
626
0
  {
627
0
    ht_line = fz_malloc(ctx, lcm * (size_t)n);
628
0
    out = fz_new_bitmap(ctx, pix->w, pix->h, n, pix->xres, pix->yres);
629
0
    o = out->samples;
630
0
    p = pix->samples;
631
632
0
    h = pix->h;
633
0
    x = pix->x;
634
0
    y = pix->y + band_start;
635
0
    w = pix->w;
636
0
    ostride = out->stride;
637
0
    pstride = pix->stride;
638
0
    while (h--)
639
0
    {
640
0
      make_ht_line(ht_line, ht, x, y++, lcm);
641
0
      thresh(ht_line, p, o, w, lcm);
642
0
      o += ostride;
643
0
      p += pstride;
644
0
    }
645
0
  }
646
0
  fz_always(ctx)
647
0
  {
648
0
    fz_drop_halftone(ctx, ht_);
649
0
    fz_free(ctx, ht_line);
650
0
  }
651
0
  fz_catch(ctx)
652
0
    fz_rethrow(ctx);
653
654
0
  return out;
655
0
}