1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
#include <mupdf/fitz.h>
typedef struct fz_test_device_s
{
fz_device super;
int *is_color;
float threshold;
int options;
fz_device *passthrough;
int resolved;
} fz_test_device;
static int
is_rgb_color(float threshold, float r, float g, float b)
{
float rg_diff = fz_abs(r - g);
float rb_diff = fz_abs(r - b);
float gb_diff = fz_abs(g - b);
return rg_diff > threshold || rb_diff > threshold || gb_diff > threshold;
}
static int
is_rgb_color_u8(int threshold_u8, int r, int g, int b)
{
int rg_diff = fz_absi(r - g);
int rb_diff = fz_absi(r - b);
int gb_diff = fz_absi(g - b);
return rg_diff > threshold_u8 || rb_diff > threshold_u8 || gb_diff > threshold_u8;
}
static void
fz_test_color(fz_context *ctx, fz_test_device *t, fz_colorspace *colorspace, const float *color)
{
if (!*t->is_color && colorspace && colorspace != fz_device_gray(ctx))
{
if (colorspace == fz_device_rgb(ctx))
{
if (is_rgb_color(t->threshold, color[0], color[1], color[2]))
{
*t->is_color = 2;
t->resolved = 1;
if (t->passthrough == NULL)
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
}
else
{
float rgb[3];
fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, color);
if (is_rgb_color(t->threshold, rgb[0], rgb[1], rgb[2]))
{
*t->is_color = 2;
t->resolved = 1;
if (t->passthrough == NULL)
{
t->super.hints |= FZ_IGNORE_IMAGE;
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
}
}
}
}
static void
fz_test_fill_path(fz_context *ctx, fz_device *dev_, const fz_path *path, int even_odd, const fz_matrix *ctm,
fz_colorspace *colorspace, const float *color, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
if (alpha != 0.0f)
fz_test_color(ctx, dev, colorspace, color);
}
if (dev->passthrough)
fz_fill_path(ctx, dev->passthrough, path, even_odd, ctm, colorspace, color, alpha);
}
static void
fz_test_stroke_path(fz_context *ctx, fz_device *dev_, const fz_path *path, const fz_stroke_state *stroke,
const fz_matrix *ctm, fz_colorspace *colorspace, const float *color, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
if (alpha != 0.0f)
fz_test_color(ctx, dev, colorspace, color);
}
if (dev->passthrough)
fz_stroke_path(ctx, dev->passthrough, path, stroke, ctm, colorspace, color, alpha);
}
static void
fz_test_fill_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_matrix *ctm,
fz_colorspace *colorspace, const float *color, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
if (alpha != 0.0f)
fz_test_color(ctx, dev, colorspace, color);
}
if (dev->passthrough)
fz_fill_text(ctx, dev->passthrough, text, ctm, colorspace, color, alpha);
}
static void
fz_test_stroke_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_stroke_state *stroke,
const fz_matrix *ctm, fz_colorspace *colorspace, const float *color, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
if (alpha != 0.0f)
fz_test_color(ctx, dev, colorspace, color);
}
if (dev->passthrough)
fz_stroke_text(ctx, dev->passthrough, text, stroke, ctm, colorspace, color, alpha);
}
struct shadearg
{
fz_test_device *dev;
fz_shade *shade;
};
static void
prepare_vertex(fz_context *ctx, void *arg_, fz_vertex *v, const float *color)
{
struct shadearg *arg = arg_;
fz_test_device *dev = arg->dev;
fz_shade *shade = arg->shade;
if (!shade->use_function)
fz_test_color(ctx, dev, shade->colorspace, color);
}
static void
fz_test_fill_shade(fz_context *ctx, fz_device *dev_, fz_shade *shade, const fz_matrix *ctm, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
if ((dev->options & FZ_TEST_OPT_SHADINGS) == 0)
{
if (shade->colorspace != fz_device_gray(ctx))
{
/* Don't test every pixel. Upgrade us from "black and white" to "probably color" */
if (*dev->is_color == 0)
*dev->is_color = 1;
dev->resolved = 1;
if (dev->passthrough == NULL)
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
}
else
{
if (shade->use_function)
{
int i;
for (i = 0; i < 256; i++)
fz_test_color(ctx, dev, shade->colorspace, shade->function[i]);
}
else
{
struct shadearg arg;
arg.dev = dev;
arg.shade = shade;
fz_process_mesh(ctx, shade, ctm, prepare_vertex, NULL, &arg);
}
}
}
if (dev->passthrough)
fz_fill_shade(ctx, dev->passthrough, shade, ctm, alpha);
}
static void
fz_test_fill_image(fz_context *ctx, fz_device *dev_, fz_image *image, const fz_matrix *ctm, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
while (dev->resolved == 0) /* So we can break out */
{
fz_pixmap *pix;
unsigned int count, i, k, h, sa, ss;
unsigned char *s;
fz_compressed_buffer *buffer;
if (*dev->is_color || !image->colorspace || image->colorspace == fz_device_gray(ctx))
break;
if ((dev->options & FZ_TEST_OPT_IMAGES) == 0)
{
/* Don't test every pixel. Upgrade us from "black and white" to "probably color" */
if (*dev->is_color == 0)
*dev->is_color = 1;
dev->resolved = 1;
if (dev->passthrough == NULL)
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
break;
}
buffer = fz_compressed_image_buffer(ctx, image);
if (buffer && image->bpc == 8)
{
fz_stream *stream = fz_open_compressed_buffer(ctx, buffer);
count = (unsigned int)image->w * (unsigned int)image->h;
if (image->colorspace == fz_device_rgb(ctx))
{
int threshold_u8 = dev->threshold * 255;
for (i = 0; i < count; i++)
{
int r = fz_read_byte(ctx, stream);
int g = fz_read_byte(ctx, stream);
int b = fz_read_byte(ctx, stream);
if (is_rgb_color_u8(threshold_u8, r, g, b))
{
*dev->is_color = 1;
dev->resolved = 1;
fz_drop_stream(ctx, stream);
if (dev->passthrough == NULL)
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
break;
}
}
}
else
{
fz_color_converter cc;
unsigned int n = (unsigned int)image->n;
fz_init_cached_color_converter(ctx, &cc, fz_device_rgb(ctx), image->colorspace);
for (i = 0; i < count; i++)
{
float cs[FZ_MAX_COLORS];
float ds[FZ_MAX_COLORS];
for (k = 0; k < n; k++)
cs[k] = fz_read_byte(ctx, stream) / 255.0f;
cc.convert(ctx, &cc, ds, cs);
if (is_rgb_color(dev->threshold, ds[0], ds[1], ds[2]))
{
*dev->is_color = 1;
dev->resolved = 1;
if (dev->passthrough == NULL)
{
fz_drop_stream(ctx, stream);
fz_fin_cached_color_converter(ctx, &cc);
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
break;
}
}
fz_fin_cached_color_converter(ctx, &cc);
}
fz_drop_stream(ctx, stream);
break;
}
pix = fz_get_pixmap_from_image(ctx, image, NULL, NULL, 0, 0);
if (pix == NULL) /* Should never happen really, but... */
break;
count = pix->w;
h = pix->h;
s = pix->samples;
sa = pix->alpha;
ss = pix->stride - pix->w * pix->n;
if (pix->colorspace == fz_device_rgb(ctx))
{
int threshold_u8 = dev->threshold * 255;
while (h--)
{
for (i = 0; i < count; i++)
{
if ((!sa || s[3] != 0) && is_rgb_color_u8(threshold_u8, s[0], s[1], s[2]))
{
*dev->is_color = 1;
dev->resolved = 1;
if (dev->passthrough == NULL)
{
fz_drop_pixmap(ctx, pix);
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
break;
}
s += 3 + sa;
}
s += ss;
}
}
else
{
fz_color_converter cc;
unsigned int n = (unsigned int)pix->n-1;
fz_init_cached_color_converter(ctx, &cc, fz_device_rgb(ctx), pix->colorspace);
while (h--)
{
for (i = 0; i < count; i++)
{
float cs[FZ_MAX_COLORS];
float ds[FZ_MAX_COLORS];
for (k = 0; k < n; k++)
cs[k] = (*s++) / 255.0f;
if (sa && *s++ == 0)
continue;
cc.convert(ctx, &cc, ds, cs);
if (is_rgb_color(dev->threshold, ds[0], ds[1], ds[2]))
{
*dev->is_color = 1;
dev->resolved = 1;
if (dev->passthrough == NULL)
{
fz_fin_cached_color_converter(ctx, &cc);
fz_drop_pixmap(ctx, pix);
fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation");
}
break;
}
}
s += ss;
}
fz_fin_cached_color_converter(ctx, &cc);
}
fz_drop_pixmap(ctx, pix);
break;
}
if (dev->passthrough)
fz_fill_image(ctx, dev->passthrough, image, ctm, alpha);
}
static void
fz_test_fill_image_mask(fz_context *ctx, fz_device *dev_, fz_image *image, const fz_matrix *ctm,
fz_colorspace *colorspace, const float *color, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
if (dev->resolved == 0)
{
/* We assume that at least some of the image pixels are non-zero */
fz_test_color(ctx, dev, colorspace, color);
}
if (dev->passthrough)
fz_fill_image_mask(ctx, dev->passthrough, image, ctm, colorspace, color, alpha);
}
static void
fz_test_clip_path(fz_context *ctx, fz_device *dev_, const fz_path *path, int even_odd, const fz_matrix *ctm, const fz_rect *scissor)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_clip_path(ctx, dev->passthrough, path, even_odd, ctm, scissor);
}
static void
fz_test_clip_stroke_path(fz_context *ctx, fz_device *dev_, const fz_path *path, const fz_stroke_state *stroke, const fz_matrix *ctm, const fz_rect *scissor)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_clip_stroke_path(ctx, dev->passthrough, path, stroke, ctm, scissor);
}
static void
fz_test_clip_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_matrix *ctm, const fz_rect *scissor)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_clip_text(ctx, dev->passthrough, text, ctm, scissor);
}
static void
fz_test_clip_stroke_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_stroke_state *stroke, const fz_matrix *ctm, const fz_rect *scissor)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_clip_stroke_text(ctx, dev->passthrough, text, stroke, ctm, scissor);
}
static void
fz_test_ignore_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_matrix *ctm)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_ignore_text(ctx, dev->passthrough, text, ctm);
}
static void
fz_test_clip_image_mask(fz_context *ctx, fz_device *dev_, fz_image *img, const fz_matrix *ctm, const fz_rect *scissor)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_clip_image_mask(ctx, dev->passthrough, img, ctm, scissor);
}
static void
fz_test_pop_clip(fz_context *ctx, fz_device *dev_)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_pop_clip(ctx, dev->passthrough);
}
static void
fz_test_begin_mask(fz_context *ctx, fz_device *dev_, const fz_rect *rect, int luminosity, fz_colorspace *cs, const float *bc)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_begin_mask(ctx, dev->passthrough, rect, luminosity, cs, bc);
}
static void
fz_test_end_mask(fz_context *ctx, fz_device *dev_)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_end_mask(ctx, dev->passthrough);
}
static void
fz_test_begin_group(fz_context *ctx, fz_device *dev_, const fz_rect *rect, int isolated, int knockout, int blendmode, float alpha)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_begin_group(ctx, dev->passthrough, rect, isolated, knockout, blendmode, alpha);
}
static void
fz_test_end_group(fz_context *ctx, fz_device *dev_)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_end_group(ctx, dev->passthrough);
}
static int
fz_test_begin_tile(fz_context *ctx, fz_device *dev_, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
{
fz_test_device *dev = (fz_test_device*)dev_;
return fz_begin_tile_id(ctx, dev->passthrough, area, view, xstep, ystep, ctm, id);
}
static void
fz_test_end_tile(fz_context *ctx, fz_device *dev_)
{
fz_test_device *dev = (fz_test_device*)dev_;
fz_end_tile(ctx, dev->passthrough);
}
fz_device *
fz_new_test_device(fz_context *ctx, int *is_color, float threshold, int options, fz_device *passthrough)
{
fz_test_device *dev = fz_new_device(ctx, sizeof *dev);
dev->super.fill_path = fz_test_fill_path;
dev->super.stroke_path = fz_test_stroke_path;
dev->super.fill_text = fz_test_fill_text;
dev->super.stroke_text = fz_test_stroke_text;
dev->super.fill_shade = fz_test_fill_shade;
dev->super.fill_image = fz_test_fill_image;
dev->super.fill_image_mask = fz_test_fill_image_mask;
if (passthrough)
{
dev->super.clip_path = fz_test_clip_path;
dev->super.clip_stroke_path = fz_test_clip_stroke_path;
dev->super.clip_text = fz_test_clip_text;
dev->super.clip_stroke_text = fz_test_clip_stroke_text;
dev->super.ignore_text = fz_test_ignore_text;
dev->super.clip_image_mask = fz_test_clip_image_mask;
dev->super.pop_clip = fz_test_pop_clip;
dev->super.begin_mask = fz_test_begin_mask;
dev->super.end_mask = fz_test_end_mask;
dev->super.begin_group = fz_test_begin_group;
dev->super.end_group = fz_test_end_group;
dev->super.begin_tile = fz_test_begin_tile;
dev->super.end_tile = fz_test_end_tile;
}
dev->is_color = is_color;
dev->options = options;
dev->threshold = threshold;
dev->passthrough = passthrough;
dev->resolved = 0;
*dev->is_color = 0;
return (fz_device*)dev;
}
|