summaryrefslogtreecommitdiff
path: root/fitz/filt_predict.c
blob: 17242742e5bf50da34972c7f5a2b5b5cf58b1da9 (plain)
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
#include "fitz.h"

/* TODO: check if this works with 16bpp images */

enum { MAXC = 32 };

typedef struct fz_predict_s fz_predict;

struct fz_predict_s
{
	fz_filter super;

	int predictor;
	int columns;
	int colors;
	int bpc;

	int stride;
	int bpp;
	unsigned char *refbuf;
	unsigned char *ref;
};

fz_filter *
fz_newpredictd(fz_obj *params)
{
	fz_obj *obj;

	FZ_NEWFILTER(fz_predict, p, predict);

	p->predictor = 1;
	p->columns = 1;
	p->colors = 1;
	p->bpc = 8;

	obj = fz_dictgets(params, "Predictor");
	if (obj)
		p->predictor = fz_toint(obj);

	if (p->predictor != 1 && p->predictor != 2 &&
		p->predictor != 10 && p->predictor != 11 &&
		p->predictor != 12 && p->predictor != 13 &&
		p->predictor != 14 && p->predictor != 15)
	{
		fz_warn("invalid predictor: %d", p->predictor);
		p->predictor = 1;
	}

	obj = fz_dictgets(params, "Columns");
	if (obj)
		p->columns = fz_toint(obj);

	obj = fz_dictgets(params, "Colors");
	if (obj)
		p->colors = fz_toint(obj);

	obj = fz_dictgets(params, "BitsPerComponent");
	if (obj)
		p->bpc = fz_toint(obj);

	p->stride = (p->bpc * p->colors * p->columns + 7) / 8;
	p->bpp = (p->bpc * p->colors + 7) / 8;

	if (p->predictor >= 10)
	{
		p->refbuf = fz_malloc(p->stride);
		memset(p->refbuf, 0, p->stride);
	}
	else
	{
		p->refbuf = nil;
	}
	p->ref = nil;

	return (fz_filter*)p;
}

void
fz_droppredict(fz_filter *filter)
{
	fz_predict *p = (fz_predict*)filter;
	fz_free(p->refbuf);
}

static inline int
getcomponent(unsigned char *buf, int x, int bpc)
{
	switch (bpc)
	{
	case 1: return buf[x / 8] >> (7 - (x % 8)) & 0x01;
	case 2: return buf[x / 4] >> ((3 - (x % 4)) * 2) & 0x03;
	case 4: return buf[x / 2] >> ((1 - (x % 2)) * 4) & 0x0f;
	case 8: return buf[x];
	}
	return 0;
}

static inline void
putcomponent(unsigned char *buf, int x, int bpc, int value)
{
	switch (bpc)
	{
	case 1: buf[x / 8] |= value << (7 - (x % 8)); break;
	case 2: buf[x / 4] |= value << ((3 - (x % 4)) * 2); break;
	case 4: buf[x / 2] |= value << ((1 - (x % 2)) * 4); break;
	case 8: buf[x] = value; break;
	}
}

static inline int
paeth(int a, int b, int c)
{
	/* The definitions of ac and bc are correct, not a typo. */
	int ac = b - c, bc = a - c, abcc = ac + bc;
	int pa = ABS(ac);
	int pb = ABS(bc);
	int pc = ABS(abcc);
	return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
}

static inline void
fz_predictnone(fz_predict *p, unsigned char *in, unsigned char *out)
{
	memcpy(out, in, p->stride);
}

static void
fz_predicttiff(fz_predict *p, unsigned char *in, unsigned char *out)
{
	int left[MAXC];
	int i, k;

	for (k = 0; k < p->colors; k++)
		left[k] = 0;

	for (i = 0; i < p->columns; i++)
	{
		for (k = 0; k < p->colors; k++)
		{
			int a = getcomponent(in, i * p->colors + k, p->bpc);
			int b = a + left[k];
			int c = b % (1 << p->bpc);
			putcomponent(out, i * p->colors + k, p->bpc, c);
			left[k] = c;
		}
	}
}

static void
fz_predictpng(fz_predict *p, unsigned char *in, unsigned char *out, int predictor)
{
	int bpp = p->bpp;
	int i;
	unsigned char *ref = p->ref;

	switch (predictor)
	{
	case 0:
		memcpy(out, in, p->stride);
		break;
	case 1:
		for (i = bpp; i > 0; i--)
		{
			*out++ = *in++;
		}
		for (i = p->stride - bpp; i > 0; i--)
		{
			*out = *in++ + out[-bpp];
			out++;
		}
		break;
	case 2:
		for (i = bpp; i > 0; i--)
		{
			*out++ = *in++ + *ref++;
		}
		for (i = p->stride - bpp; i > 0; i--)
		{
			*out++ = *in++ + *ref++;
		}
		break;
	case 3:
		for (i = bpp; i > 0; i--)
		{
			*out++ = *in++ + (*ref++) / 2;
		}
		for (i = p->stride - bpp; i > 0; i--)
		{
			*out = *in++ + (out[-bpp] + *ref++)/2;
			out++;
		}
		break;
	case 4:
		for (i = bpp; i > 0; i--)
		{
			*out++ = *in++ + paeth(0, *ref++, 0);
		}
		for (i = p->stride - bpp; i > 0; i --)
		{
			*out = *in++ + paeth(out[-bpp], *ref, ref[-bpp]);
			ref++;
			out++;
		}
		break;
	}
}

fz_error
fz_processpredict(fz_filter *filter, fz_buffer *in, fz_buffer *out)
{
	fz_predict *dec = (fz_predict*)filter;
	int ispng = dec->predictor >= 10;
	int predictor;

	dec->ref = dec->refbuf;
	while (1)
	{
		if (in->rp + dec->stride + ispng > in->wp)
		{
			if (in->eof)
				return fz_iodone;
			if (dec->refbuf != dec->ref)
			{
				memcpy(dec->refbuf, dec->ref, dec->stride);
			}
			return fz_ioneedin;
		}

		if (out->wp + dec->stride > out->ep)
		{
			if (dec->refbuf != dec->ref)
			{
				memcpy(dec->refbuf, dec->ref, dec->stride);
			}
			return fz_ioneedout;
		}

		if (dec->predictor == 1)
		{
			fz_predictnone(dec, in->rp, out->wp);
		}
		else if (dec->predictor == 2)
		{
			if (dec->bpc != 8)
				memset(out->wp, 0, dec->stride);
			fz_predicttiff(dec, in->rp, out->wp);
		}
		else
		{
			predictor = *in->rp++;
			fz_predictpng(dec, in->rp, out->wp, predictor);
		}

		if (dec->refbuf)
		{
			dec->ref = out->wp;
		}

		in->rp += dec->stride;
		out->wp += dec->stride;
	}
}