File: draw-unpack.c

package info (click to toggle)
mupdf 1.25.1%2Bds1-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 21,620 kB
  • sloc: ansic: 270,929; python: 20,709; java: 6,916; javascript: 1,865; makefile: 1,130; xml: 550; sh: 430; cpp: 325; cs: 313; awk: 10; sed: 7; lisp: 3
file content (490 lines) | stat: -rw-r--r-- 11,764 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2004-2021 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.

#include "mupdf/fitz.h"
#include "draw-imp.h"

#include <string.h>

/* Unpack image samples and optionally pad pixels with opaque alpha */

#define get1(buf,x) ((buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
#define get2(buf,x) ((buf[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3 )
#define get4(buf,x) ((buf[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15 )
#define get8(buf,x) (buf[x])
#define get16(buf,x) (buf[x << 1])
#define get24(buf,x) (buf[(x << 1) + x])
#define get32(buf,x) (buf[x << 2])

static unsigned char get1_tab_1[256][8];
static unsigned char get1_tab_1p[256][16];
static unsigned char get1_tab_255[256][8];
static unsigned char get1_tab_255p[256][16];

/*
	Bug 697012 shows that the unpacking code can confuse valgrind due
	to the use of undefined bits in the padding at the end of lines.
	We unpack from bits to bytes by copying from a lookup table.
	Valgrind is not capable of understanding that it doesn't matter
	what the undefined bits are, as the bytes we copy that correspond
	to the defined bits will always agree regardless of these
	undefined bits by construction of the table.

	We therefore have a VGMASK macro that explicitly masks off these
	bits in PACIFY_VALGRIND builds.
*/
#ifdef PACIFY_VALGRIND
static const unsigned char mask[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
#define VGMASK(v,m) (v & mask[(m)])
#else
#define VGMASK(v,m) (v)
#endif

static void
init_get1_tables(void)
{
	static int once = 0;
	unsigned char bits[1];
	int i, k, x;

	/* TODO: mutex lock here */

	if (once)
		return;

	for (i = 0; i < 256; i++)
	{
		bits[0] = i;
		for (k = 0; k < 8; k++)
		{
			x = get1(bits, k);

			get1_tab_1[i][k] = x;
			get1_tab_1p[i][k * 2] = x;
			get1_tab_1p[i][k * 2 + 1] = 255;

			get1_tab_255[i][k] = x * 255;
			get1_tab_255p[i][k * 2] = x * 255;
			get1_tab_255p[i][k * 2 + 1] = 255;
		}
	}

	once = 1;
}

static void
fz_unpack_mono_line_unscaled(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int w3 = w >> 3;
	int x;

	for (x = 0; x < w3; x++)
	{
		memcpy(dp, get1_tab_1[*sp++], 8);
		dp += 8;
	}
	x = x << 3;
	if (x < w)
		memcpy(dp, get1_tab_1[VGMASK(*sp, w - x)], w - x);
}

static void
fz_unpack_mono_line_scaled(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int w3 = w >> 3;
	int x;

	for (x = 0; x < w3; x++)
	{
		memcpy(dp, get1_tab_255[*sp++], 8);
		dp += 8;
	}
	x = x << 3;
	if (x < w)
		memcpy(dp, get1_tab_255[VGMASK(*sp, w - x)], w - x);
}

static void
fz_unpack_mono_line_unscaled_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int w3 = w >> 3;
	int x;

	for (x = 0; x < w3; x++)
	{
		memcpy(dp, get1_tab_1p[*sp++], 16);
		dp += 16;
	}
	x = x << 3;
	if (x < w)
		memcpy(dp, get1_tab_1p[VGMASK(*sp, w - x)], (w - x) << 1);
}

static void
fz_unpack_mono_line_scaled_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int w3 = w >> 3;
	int x;

	for (x = 0; x < w3; x++)
	{
		memcpy(dp, get1_tab_255p[*sp++], 16);
		dp += 16;
	}
	x = x << 3;
	if (x < w)
		memcpy(dp, get1_tab_255p[VGMASK(*sp, w - x)], (w - x) << 1);
}

static void
fz_unpack_line(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int len = w * n;
	while (len--)
		*dp++ = *sp++;
}

static void
fz_unpack_line_with_padding(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	int x, k;

	for (x = 0; x < w; x++)
	{
		for (k = 0; k < n; k++)
			*dp++ = *sp++;
		*dp++ = 255;
	}
}

static void
fz_unpack_any_l2depth(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip)
{
	unsigned char *p = dp;
	int b = 0;
	int x, k;

	for (x = 0; x < w; x++)
	{
		for (k = 0; k < n; k++)
		{
			switch (depth)
			{
			case 1: *p++ = get1(sp, b) * scale; break;
			case 2: *p++ = get2(sp, b) * scale; break;
			case 4: *p++ = get4(sp, b) * scale; break;
			case 8: *p++ = get8(sp, b); break;
			case 16: *p++ = get16(sp, b); break;
			case 24: *p++ = get24(sp, b); break;
			case 32: *p++ = get32(sp, b); break;
			}
			b++;
		}
		b += skip;
		if (pad)
			*p++ = 255;
	}
}

typedef void (*fz_unpack_line_fn)(unsigned char *dp, unsigned char *sp, int w, int n, int depth, int scale, int pad, int skip);

void
fz_unpack_tile(fz_context *ctx, fz_pixmap *dst, unsigned char *src, int n, int depth, size_t stride, int scale)
{
	unsigned char *sp = src;
	unsigned char *dp = dst->samples;
	fz_unpack_line_fn unpack_line = NULL;
	int pad, y, skip;
	int w = dst->w;
	int h = dst->h;

	pad = 0;
	skip = 0;
	if (dst->n > n)
		pad = 255;
	if (dst->n < n)
	{
		skip = n - dst->n;
		n = dst->n;
	}

	if (depth == 1)
		init_get1_tables();

	if (scale == 0)
	{
		switch (depth)
		{
		case 1: scale = 255; break;
		case 2: scale = 85; break;
		case 4: scale = 17; break;
		}
	}

	if (n == 1 && depth == 1 && scale == 1 && !pad && !skip)
		unpack_line = fz_unpack_mono_line_unscaled;
	else if (n == 1 && depth == 1 && scale == 255 && !pad && !skip)
		unpack_line = fz_unpack_mono_line_scaled;
	else if (n == 1 && depth == 1 && scale == 1 && pad && !skip)
		unpack_line = fz_unpack_mono_line_unscaled_with_padding;
	else if (n == 1 && depth == 1 && scale == 255 && pad && !skip)
		unpack_line = fz_unpack_mono_line_scaled_with_padding;
	else if (depth == 8 && !pad && !skip)
		unpack_line = fz_unpack_line;
	else if (depth == 8 && pad && !skip)
		unpack_line = fz_unpack_line_with_padding;
	else if (depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth  == 16 || depth == 24 || depth == 32)
		unpack_line = fz_unpack_any_l2depth;

	if (unpack_line)
	{
		for (y = 0; y < h; y++, sp += stride, dp += dst->stride)
			unpack_line(dp, sp, w, n, depth, scale, pad, skip);
	}
	else if (depth > 0 && depth <= 8 * (int)sizeof(int))
	{
		fz_stream *stm;
		int x, k;
		size_t skipbits = 8 * stride - (size_t)w * n * depth;

		if (skipbits > 32)
			fz_throw(ctx, FZ_ERROR_ARGUMENT, "Inappropriate stride!");

		stm = fz_open_memory(ctx, sp, h * stride);
		fz_try(ctx)
		{
			for (y = 0; y < h; y++)
			{
				for (x = 0; x < w; x++)
				{
					for (k = 0; k < n; k++)
					{
						if (depth <= 8)
							*dp++ = fz_read_bits(ctx, stm, depth) << (8 - depth);
						else
							*dp++ = fz_read_bits(ctx, stm, depth) >> (depth - 8);
					}
					if (pad)
						*dp++ = 255;
				}

				dp += dst->stride - (size_t)w * (n + (pad > 0));
				(void) fz_read_bits(ctx, stm, (int)skipbits);
			}
		}
		fz_always(ctx)
			fz_drop_stream(ctx, stm);
		fz_catch(ctx)
			fz_rethrow(ctx);
	}
	else
		fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot unpack tile with %d bits per component", depth);
}

/* Apply decode array */

void
fz_decode_indexed_tile(fz_context *ctx, fz_pixmap *pix, const float *decode, int maxval)
{
	int add[FZ_MAX_COLORS];
	int mul[FZ_MAX_COLORS];
	unsigned char *p = pix->samples;
	size_t stride = pix->stride - pix->w * (size_t)pix->n;
	int len;
	int pn = pix->n;
	int n = pn - pix->alpha;
	int needed;
	int k;
	int h;

	needed = 0;
	for (k = 0; k < n; k++)
	{
		int min = decode[k * 2] * 256;
		int max = decode[k * 2 + 1] * 256;
		add[k] = min;
		mul[k] = (max - min) / maxval;
		needed |= min != 0 || max != maxval * 256;
	}

	if (!needed)
		return;

	h = pix->h;
	while (h--)
	{
		len = pix->w;
		while (len--)
		{
			for (k = 0; k < n; k++)
			{
				int value = (add[k] + (((p[k] << 8) * mul[k]) >> 8)) >> 8;
				p[k] = fz_clampi(value, 0, 255);
			}
			p += pn;
		}
		p += stride;
	}
}

void
fz_decode_tile(fz_context *ctx, fz_pixmap *pix, const float *decode)
{
	int add[FZ_MAX_COLORS];
	int mul[FZ_MAX_COLORS];
	unsigned char *p = pix->samples;
	size_t stride = pix->stride - pix->w * (size_t)pix->n;
	int len;
	int n = fz_maxi(1, pix->n - pix->alpha);
	int k;
	int h;

	for (k = 0; k < n; k++)
	{
		int min = decode[k * 2] * 255;
		int max = decode[k * 2 + 1] * 255;
		add[k] = min;
		mul[k] = max - min;
	}

	h = pix->h;
	while (h--)
	{
		len = pix->w;
		while (len--)
		{
			for (k = 0; k < n; k++)
			{
				int value = add[k] + fz_mul255(p[k], mul[k]);
				p[k] = fz_clampi(value, 0, 255);
			}
			p += pix->n;
		}
		p += stride;
	}
}

typedef struct
{
	fz_stream *src;
	int depth;
	int w;
	int h;
	int n;
	int skip;
	int pad;
	int scale;
	int src_stride;
	int dst_stride;
	fz_unpack_line_fn unpack;
	unsigned char buf[1];
} unpack_state;

static int
unpack_next(fz_context *ctx, fz_stream *stm, size_t max)
{
	unpack_state *state = (unpack_state *)stm->state;
	size_t n = state->src_stride;

	stm->rp = state->buf;
	do
	{
		size_t a = fz_available(ctx, state->src, n);
		if (a == 0)
			return EOF;
		if (a > n)
			a = n;
		memcpy(stm->rp, state->src->rp, a);
		stm->rp += a;
		state->src->rp += a;
		n -= a;
	}
	while (n);

	state->h--;
	stm->pos += state->dst_stride;
	stm->wp = stm->rp + state->dst_stride;
	state->unpack(stm->rp, state->buf, state->w, state->n, state->depth, state->scale, state->pad, state->skip);

	return *stm->rp++;
}

static void
unpack_drop(fz_context *ctx, void *state)
{
	fz_free(ctx, state);
}

fz_stream *
fz_unpack_stream(fz_context *ctx, fz_stream *src, int depth, int w, int h, int n, int indexed, int pad, int skip)
{
	int src_stride = (w*depth*n+7)>>3;
	int dst_stride;
	unpack_state *state;
	fz_unpack_line_fn unpack_line = NULL;
	int scale = 1;

	if (depth == 1)
		init_get1_tables();

	if (!indexed)
		switch (depth)
		{
		case 1: scale = 255; break;
		case 2: scale = 85; break;
		case 4: scale = 17; break;
		}

	dst_stride = w * (n + !!pad);

	if (n == 1 && depth == 1 && scale == 1 && !pad && !skip)
		unpack_line = fz_unpack_mono_line_unscaled;
	else if (n == 1 && depth == 1 && scale == 255 && !pad && !skip)
		unpack_line = fz_unpack_mono_line_scaled;
	else if (n == 1 && depth == 1 && scale == 1 && pad && !skip)
		unpack_line = fz_unpack_mono_line_unscaled_with_padding;
	else if (n == 1 && depth == 1 && scale == 255 && pad && !skip)
		unpack_line = fz_unpack_mono_line_scaled_with_padding;
	else if (depth == 8 && !pad && !skip)
		unpack_line = fz_unpack_line;
	else if (depth == 8 && pad && !skip)
		unpack_line = fz_unpack_line_with_padding;
	else if (depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth  == 16 || depth == 24 || depth == 32)
		unpack_line = fz_unpack_any_l2depth;
	else
		fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported combination in fz_unpack_stream");

	state = fz_malloc(ctx, sizeof(unpack_state) + dst_stride + src_stride);
	state->src = src;
	state->depth = depth;
	state->w = w;
	state->h = h;
	state->n = n;
	state->skip = skip;
	state->pad = pad;
	state->scale = scale;
	state->unpack = unpack_line;
	state->src_stride = src_stride;
	state->dst_stride = dst_stride;

	return fz_new_stream(ctx, state, unpack_next, unpack_drop);
}