summaryrefslogtreecommitdiff
path: root/fitz/filt_faxe.c
blob: 3aa4f09ab9da5be29708bfcee5ced0470d36a337 (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
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
#include "fitz_base.h"
#include "fitz_stream.h"

#include "filt_faxe.h"
#include "filt_faxc.h"

/* TODO: honor Rows param */

typedef struct fz_faxe_s fz_faxe;

struct fz_faxe_s
{
	fz_filter super;

	int k;
	int endofline;
	int encodedbytealign;
	int columns;
	int endofblock;
	int blackis1;

	int stride;
	int ridx;		/* how many rows in total */
	int bidx;		/* how many bits are already used in out->wp */
	unsigned char bsave;	/* partial byte saved between process() calls */

	int stage;
	int a0, c;		/* mid-line coding state */

	unsigned char *ref;
	unsigned char *src;
};

fz_error 
fz_newfaxe(fz_filter **fp, fz_obj *params)
{
	fz_obj *obj;

	FZ_NEWFILTER(fz_faxe, fax, faxe);

	fax->ref = nil;
	fax->src = nil;

	fax->k = 0;
	fax->endofline = 0;
	fax->encodedbytealign = 0;
	fax->columns = 1728;
	fax->endofblock = 1;
	fax->blackis1 = 0;

	obj = fz_dictgets(params, "K");
	if (obj) fax->k = fz_toint(obj);

	obj = fz_dictgets(params, "EndOfLine");
	if (obj) fax->endofline = fz_tobool(obj);

	obj = fz_dictgets(params, "EncodedByteAlign");
	if (obj) fax->encodedbytealign = fz_tobool(obj);

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

	obj = fz_dictgets(params, "EndOfBlock");
	if (obj) fax->endofblock = fz_tobool(obj);

	obj = fz_dictgets(params, "BlackIs1");
	if (obj) fax->blackis1 = fz_tobool(obj);

	fax->stride = ((fax->columns - 1) >> 3) + 1;
	fax->bidx = 0;
	fax->ridx = 0;

	fax->stage = 0;
	fax->a0 = -1;
	fax->c = 0;

	fax->ref = fz_malloc(fax->stride);
	if (!fax->ref)
	{
	    fz_free(fax);
	    return fz_throw("outofmemory: scanline buffer one");
	}

	fax->src = fz_malloc(fax->stride);
	if (!fax->src)
	{
	    fz_free(fax);
	    fz_free(fax->ref);
	    return fz_throw("outofmemory: scanline buffer two");
	}

	memset(fax->ref, 0, fax->stride);
	memset(fax->src, 0, fax->stride);

	return fz_okay;
}

void
fz_dropfaxe(fz_filter *p)
{
	fz_faxe *fax = (fz_faxe*) p;
	fz_free(fax->src);
	fz_free(fax->ref);
}

enum { codebytes = 2 };

static inline int runbytes(int run)
{
	int m = (run / 64) / 40 + 1;	/* number of makeup codes */
	return codebytes * (m + 1); /* bytes for makeup + term codes */
}

static void
putbits(fz_faxe *fax, fz_buffer *out, int code, int nbits)
{
	while (nbits > 0)
	{
		if (fax->bidx == 0)
			*out->wp = 0;

		/* code does not fit: shift right */
		if (nbits > (8 - fax->bidx))
		{
			*out->wp |= code >> (nbits - (8 - fax->bidx));
			nbits = nbits - (8 - fax->bidx);
			fax->bidx = 0;
			out->wp ++;
		}

		/* shift left */
		else
		{
			*out->wp |= code << ((8 - fax->bidx) - nbits);
			fax->bidx += nbits;
			if (fax->bidx == 8)
			{
				fax->bidx = 0;
				out->wp ++;
			}
			nbits = 0;
		}
	}
}

static inline void
putcode(fz_faxe *fax, fz_buffer *out, const cfe_code *run)
{
	putbits(fax, out, run->code, run->nbits);
}

static void
putrun(fz_faxe *fax, fz_buffer *out, int run, int c)
{
	int m;

	const cf_runs *codetable = c ? &cf_black_runs : &cf_white_runs;

	if (run > 63)
	{
		m = run / 64;
		while (m > 40)
		{
			putcode(fax, out, &codetable->makeup[40]);
			m -= 40;
		}
		if (m > 0)
		{
			putcode(fax, out, &codetable->makeup[m]);
		}
		putcode(fax, out, &codetable->termination[run % 64]);
	}
	else
	{
		putcode(fax, out, &codetable->termination[run]);
	}
}

static fz_error 
enc1d(fz_faxe *fax, unsigned char *line, fz_buffer *out)
{
	int run;

	if (fax->a0 < 0)
		fax->a0 = 0;

	while (fax->a0 < fax->columns)
	{
		run = getrun(line, fax->a0, fax->columns, fax->c);

		if (out->wp + 1 + runbytes(run) > out->ep)
			return fz_ioneedout;

		putrun(fax, out, run, fax->c);

		fax->a0 += run;
		fax->c = !fax->c;
	}

	return fz_okay;
}

static fz_error 
enc2d(fz_faxe *fax, unsigned char *ref, unsigned char *src, fz_buffer *out)
{
	int a1, a2, b1, b2;
	int run1, run2, n;

	while (fax->a0 < fax->columns)
	{
		a1 = findchanging(src, fax->a0, fax->columns);
		b1 = findchangingcolor(ref, fax->a0, fax->columns, !fax->c);
		b2 = findchanging(ref, b1, fax->columns);

		/* pass */
		if (b2 < a1)
		{
			if (out->wp + 1 + codebytes > out->ep)
				return fz_ioneedout;

			putcode(fax, out, &cf2_run_pass);

			fax->a0 = b2;
		}

		/* vertical */
		else if (ABS(b1 - a1) <= 3)
		{
			if (out->wp + 1 + codebytes > out->ep)
				return fz_ioneedout;

			putcode(fax, out, &cf2_run_vertical[b1 - a1 + 3]);

			fax->a0 = a1;
			fax->c = !fax->c;
		}

		/* horizontal */
		else
		{
			a2 = findchanging(src, a1, fax->columns);
			run1 = a1 - fax->a0;
			run2 = a2 - a1;
			n = codebytes + runbytes(run1) + runbytes(run2);

			if (out->wp + 1 + n > out->ep)
				return fz_ioneedout;

			putcode(fax, out, &cf2_run_horizontal);
			putrun(fax, out, run1, fax->c);
			putrun(fax, out, run2, !fax->c);

			fax->a0 = a2;
		}
	}

	return fz_okay;
}

static fz_error 
process(fz_faxe *fax, fz_buffer *in, fz_buffer *out)
{
	fz_error error;
	int i, n;

	while (1)
	{
		if (in->rp == in->wp && in->eof)
			goto rtc;

		switch (fax->stage)
		{
		case 0:
			if (fax->encodedbytealign)
			{
				if (fax->endofline)
				{
					if (out->wp + 1 + 1 > out->ep)
						return fz_ioneedout;

					/* make sure that EOL ends on a byte border */
					putbits(fax, out, 0, (12 - fax->bidx) & 7);
				}
				else
				{
					if (fax->bidx)
					{
						if (out->wp + 1 > out->ep)
							return fz_ioneedout;
						fax->bidx = 0;
						out->wp ++;
					}
				}
			}

			fax->stage ++;

		case 1:
			if (fax->endofline)
			{
				if (out->wp + 1 + codebytes + 1 > out->ep)
					return fz_ioneedout;

				if (fax->k > 0)
				{
					if (fax->ridx % fax->k == 0)
						putcode(fax, out, &cf2_run_eol_1d);
					else
						putcode(fax, out, &cf2_run_eol_2d);
				}
				else
				{
					putcode(fax, out, &cf_run_eol);
				}
			}

			fax->stage ++;

		case 2:
			if (in->rp + fax->stride > in->wp)
			{
				if (in->eof) /* XXX barf here? */
					goto rtc;
				return fz_ioneedin;
			}

			memcpy(fax->ref, fax->src, fax->stride);
			memcpy(fax->src, in->rp, fax->stride);

			if (!fax->blackis1)
			{
				for (i = 0; i < fax->stride; i++)
					fax->src[i] = ~fax->src[i];
			}

			in->rp += fax->stride;

			fax->c = 0;
			fax->a0 = -1;

			fax->stage ++;

		case 3:
			error = 0; /* to silence compiler */

			if (fax->k < 0)
				error = enc2d(fax, fax->ref, fax->src, out);

			else if (fax->k == 0)
				error = enc1d(fax, fax->src, out);

			else if (fax->k > 0)
			{
				if (fax->ridx % fax->k == 0)
					error = enc1d(fax, fax->src, out);
				else
					error = enc2d(fax, fax->ref, fax->src, out);
			}

			if (error)
				return error; /* one of fz_io* */

			fax->ridx ++;

			fax->stage = 0;
		}
	}

rtc:
	if (fax->endofblock)
	{
		n = fax->k < 0 ? 2 : 6;

		if (out->wp + 1 + codebytes * n > out->ep)
			return fz_ioneedout;

		for (i = 0; i < n; i++)
		{
			putcode(fax, out, &cf_run_eol);
			if (fax->k > 0)
				putbits(fax, out, 1, 1);
		}
	}

	if (fax->bidx)
		out->wp ++;

	return fz_iodone;
}

fz_error 
fz_processfaxe(fz_filter *p, fz_buffer *in, fz_buffer *out)
{
	fz_faxe *fax = (fz_faxe*) p;
	fz_error error;

	/* restore partial bits */
	*out->wp = fax->bsave;

	error = process(fax, in, out);

	/* save partial bits */
	fax->bsave = *out->wp;

	return error;
}