summaryrefslogtreecommitdiff
path: root/filter/faxd.c
blob: 81e04b816e535e036c41c7c409f5e241d15d5c27 (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
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
#include <fitz.h>

#include "faxd.h"
#include "faxc.h"

enum
{
	SNORMAL,	/* neutral state, waiting for any code */
	SMAKEUP,	/* got a 1d makeup code, waiting for terminating code */
	SEOL,		/* at eol, needs output buffer space */
	SH1, SH2	/* in H part 1 and 2 (both makeup and terminating codes) */
};

#define DEBUG 1

#ifdef noDEBUG
#define DPRINT(...) fprintf(stderr, __VA_ARGS__)
#else
#define DPRINT(...)
#endif

/* TODO: uncompressed */

typedef struct fz_faxd_s fz_faxd;

struct fz_faxd_s
{
	fz_filter super;
	
	int k;
	int endofline;
	int encodedbytealign;
	int columns;
	int rows;
	int endofblock;
	int blackis1;

	int stride;
	int ridx;

	int bidx;
	unsigned int word;

	int stage, a, c, dim, eolc;
	unsigned char *ref;
	unsigned char *dst;
};

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

	FZ_NEWFILTER(fz_faxd, fax, faxd);

	fax->ref = nil;
	fax->dst = nil;

	fax->k = 0;
	fax->endofline = 0;
	fax->encodedbytealign = 0;
	fax->columns = 1728;
	fax->rows = 0;
	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, "Rows");
	if (obj) fax->rows = 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->ridx = 0;
	fax->bidx = 32;
	fax->word = 0;

	fax->stage = SNORMAL;
	fax->a = -1;
	fax->c = 0;
	fax->dim = fax->k < 0 ? 2 : 1;
	fax->eolc = 0;

	fax->ref = fz_malloc(fax->stride);
	if (!fax->ref) { fz_free(fax); return fz_outofmem; }

	fax->dst = fz_malloc(fax->stride);
	if (!fax->dst) { fz_free(fax); fz_free(fax->ref); return fz_outofmem; }

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

	DPRINT("FAXD k=%d eol=%d eba=%d cols=%d rows=%d eob=%d black1=%d stride=%d\n",
		fax->k, fax->endofline, fax->encodedbytealign,
		fax->columns, fax->rows, fax->endofblock, fax->blackis1,
		fax->stride);

	return nil;
}

void
fz_dropfaxd(fz_filter *p)
{
	fz_faxd *fax = (fz_faxd*) p;
	fz_free(fax->ref);
	fz_free(fax->dst);
}

static inline void eatbits(fz_faxd *fax, int nbits)
{
	fax->word <<= nbits;
	fax->bidx += nbits;
}

static inline fz_error * fillbits(fz_faxd *fax, fz_buffer *in)
{
	while (fax->bidx >= 8)
	{
		if (in->rp + 1 > in->wp)
			return fz_ioneedin;
		fax->bidx -= 8;
		fax->word |= *in->rp << fax->bidx;
		in->rp ++;
	}
	return nil;
}

static int
getcode(fz_faxd *fax, const cfd_node *table, int initialbits)
{
	unsigned int word = fax->word;
	int tidx = word >> (32 - initialbits);
	int val = table[tidx].val;
	int nbits = table[tidx].nbits;

	if (nbits > initialbits)
	{
		int mask = (1 << (32 - initialbits)) - 1;
		tidx = val + ((word & mask) >> (32 - nbits));
		val = table[tidx].val;
		nbits = initialbits + table[tidx].nbits;
	}

	eatbits(fax, nbits);

	return val;
}

/* decode one 1d code */
static fz_error *
dec1d(fz_faxd *fax)
{
	int code;

	if (fax->a == -1)
		fax->a = 0;

	if (fax->c)
		code = getcode(fax, cf_black_decode, cfd_black_initial_bits);
	else
		code = getcode(fax, cf_white_decode, cfd_white_initial_bits);

	DPRINT("%c %d\n", fax->c?'b':'w', code);

	if (code == UNCOMPRESSED)
		return fz_throw("ioerror: uncompressed data in faxd");

	if (code < 0)
		return fz_throw("ioerror: invalid 1d code in faxd");

	if (fax->a + code > fax->columns)
		return fz_throw("ioerror: invalid 1d data in faxd");

	if (fax->c)
		setbits(fax->dst, fax->a, fax->a + code);

	fax->a += code;

	if (code < 64)
	{
		fax->c = !fax->c;
		fax->stage = SNORMAL;
	}
	else
		fax->stage = SMAKEUP;

	return nil;
}

/* decode one 2d code */
static fz_error *
dec2d(fz_faxd *fax)
{
	int code, b1, b2;

	if (fax->stage == SH1 || fax->stage == SH2)
	{
		if (fax->a == -1)
			fax->a = 0;

		if (fax->c)
			code = getcode(fax, cf_black_decode, cfd_black_initial_bits);
		else
			code = getcode(fax, cf_white_decode, cfd_white_initial_bits);

		DPRINT("%c %d\n", fax->c ? 'b' : 'w', code);

		if (code == UNCOMPRESSED)
			return fz_throw("ioerror: uncompressed data in faxd");

		if (code < 0)
			return fz_throw("ioerror: invalid 2d code in faxd");

		if (fax->a + code > fax->columns)
			return fz_throw("ioerror: invalid 2d data in faxd");

		if (fax->c)
			setbits(fax->dst, fax->a, fax->a + code);

		fax->a += code;

		if (code < 64)
		{
			fax->c = !fax->c;
			if (fax->stage == SH1)
				fax->stage = SH2;
			else if (fax->stage == SH2)
				fax->stage = SNORMAL;
		}

		return nil;
	}

	code = getcode(fax, cf_2d_decode, cfd_2d_initial_bits);

	switch (code)
	{
		case H:
			fax->stage = SH1;
			DPRINT("H\n");
			break;

		case P:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			b2 = findchanging(fax->ref, b1, fax->columns);
			DPRINT("P\n");
			if (fax->c) setbits(fax->dst, fax->a, b2);
			fax->a = b2;
			break;

		case V0:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("V0\n");
			if (fax->c) setbits(fax->dst, fax->a, b1);
			fax->a = b1;
			fax->c = !fax->c;
			break;

		case VR1:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VR1\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 + 1);
			fax->a = b1 + 1;
			fax->c = !fax->c;
			break;

		case VR2:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VR2\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 + 2);
			fax->a = b1 + 2;
			fax->c = !fax->c;
			break;

		case VR3:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VR3\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 + 3);
			fax->a = b1 + 3;
			fax->c = !fax->c;
			break;

		case VL1:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VL1\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 - 1);
			fax->a = b1 - 1;
			fax->c = !fax->c;
			break;

		case VL2:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VL2\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 - 2);
			fax->a = b1 - 2;
			fax->c = !fax->c;
			break;

		case VL3:
			b1 = findchangingcolor(fax->ref, fax->a, fax->columns, !fax->c);
			DPRINT("VL3\n");
			if (fax->c) setbits(fax->dst, fax->a, b1 - 3);
			fax->a = b1 - 3;
			fax->c = !fax->c;
			break;

		case UNCOMPRESSED:
			return fz_throw("ioerror: uncompressed data in faxd");

		case ERROR:
			return fz_throw("ioerror: invalid 2d code in faxd");

		default:
			return fz_throw("ioerror: invalid 2d code in faxd (%d)", code);
	}

	return 0;
}

fz_error *
fz_processfaxd(fz_filter *f, fz_buffer *in, fz_buffer *out)
{
	fz_faxd *fax = (fz_faxd*)f;
	fz_error * err;
	int i;

	if (fax->stage == SEOL)
		goto eol;

loop:

	if (fillbits(fax, in))
	{
		if (in->eof) {
			if (fax->bidx > 31) {
				if (fax->a > 0)
					goto eol;
				goto rtc;
			}
		}
		else {
			return fz_ioneedin;
		}
	}

	if ((fax->word >> (32 - 12)) == 0)
	{
		eatbits(fax, 1);
		goto loop;
	}

	if ((fax->word >> (32 - 12)) == 1)
	{
		DPRINT("EOL\n");
		eatbits(fax, 12);
		fax->eolc ++;

		if (fax->k > 0) {
			if ((fax->word >> (32 - 1)) == 1)
				fax->dim = 1;
			else
				fax->dim = 2;
			eatbits(fax, 1);
			DPRINT("DIM %d\n", fax->dim);
		}
	}
	else if (fax->dim == 1)
	{
		fax->eolc = 0;
		err = dec1d(fax);
		if (err) return err;

	}
	else if (fax->dim == 2)
	{
		fax->eolc = 0;
		err = dec2d(fax);
		if (err) return err;
	}

	/* no eol check after makeup codes nor in the middle of an H code */
	if (fax->stage == SMAKEUP || fax->stage == SH1 || fax->stage == SH2)
		goto loop;

	/* check for eol conditions */
	if (fax->eolc || fax->a >= fax->columns)
	{
		if (fax->a > 0)
			goto eol;
		if (fax->eolc == (fax->k < 0 ? 2 : 6))
			goto rtc;
	}

	goto loop;

eol:
	fax->stage = SEOL;
	if (out->wp + fax->stride > out->ep)
		return fz_ioneedout;

	if (fax->blackis1)
		memcpy(out->wp, fax->dst, fax->stride);
	else
		for (i = 0; i < fax->stride; i++)
			out->wp[i] = ~fax->dst[i];

	memcpy(fax->ref, fax->dst, fax->stride);
	memset(fax->dst, 0, fax->stride);
	out->wp += fax->stride;

	fax->stage = SNORMAL;
	fax->c = 0;
	fax->a = -1;
	fax->ridx ++;

	if (!fax->endofblock && fax->rows) {
		if (fax->ridx >= fax->rows)
			goto rtc;
	}

	/* we have not read dim from eol, make a guess */
	if (fax->k > 0 && !fax->eolc)
	{
		if (fax->ridx % fax->k == 0)
			fax->dim = 1;
		else
			fax->dim = 2;
	}

	DPRINT("%dd scanline %d\n", fax->dim, fax->ridx + 1);

	/* if endofline & encodedbytealign, EOLs are *not* optional */
	if (fax->encodedbytealign) {
		if (fax->endofline)
			eatbits(fax, (12 - fax->bidx) & 7);
		else
			eatbits(fax, (8 - fax->bidx) & 7);
	}

	goto loop;

rtc:
	DPRINT("RTC\n");
	out->eof = 1;
	return fz_iodone;
}