Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#include "mupdf/fitz.h"
2
3
/* Fax G3/G4 decoder */
4
5
/* TODO: uncompressed */
6
7
/*
8
<raph> the first 2^(initialbits) entries map bit patterns to decodes
9
<raph> let's say initial_bits is 8 for the sake of example
10
<raph> and that the code is 1001
11
<raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
12
<raph> because those are all the bytes that start with the code
13
<raph> and the 4 is the length of the code
14
... if (n_bits > initial_bits) ...
15
<raph> anyway, in that case, it basically points to a mini table
16
<raph> the n_bits is the maximum length of all codes beginning with that byte
17
<raph> so 2^(n_bits - initial_bits) is the size of the mini-table
18
<raph> peter came up with this, and it makes sense
19
*/
20
21
typedef struct cfd_node_s cfd_node;
22
23
struct cfd_node_s
24
{
25
short val;
26
short nbits;
27
};
28
29
enum
30
{
31
cfd_white_initial_bits = 8,
32
cfd_black_initial_bits = 7,
33
cfd_2d_initial_bits = 7,
34
cfd_uncompressed_initial_bits = 6 /* must be 6 */
35
};
36
37
/* non-run codes in tables */
38
enum
39
{
40
ERROR = -1,
41
ZEROS = -2, /* EOL follows, possibly with more padding first */
42
UNCOMPRESSED = -3
43
};
44
45
/* semantic codes for cf_2d_decode */
46
enum
47
{
48
P = -4,
49
H = -5,
50
VR3 = 0,
51
VR2 = 1,
52
VR1 = 2,
53
V0 = 3,
54
VL1 = 4,
55
VL2 = 5,
56
VL3 = 6
57
};
58
59
/* White decoding table. */
60
static const cfd_node cf_white_decode[] = {
61
{256,12},{272,12},{29,8},{30,8},{45,8},{46,8},{22,7},{22,7},
62
{23,7},{23,7},{47,8},{48,8},{13,6},{13,6},{13,6},{13,6},{20,7},
63
{20,7},{33,8},{34,8},{35,8},{36,8},{37,8},{38,8},{19,7},{19,7},
64
{31,8},{32,8},{1,6},{1,6},{1,6},{1,6},{12,6},{12,6},{12,6},{12,6},
65
{53,8},{54,8},{26,7},{26,7},{39,8},{40,8},{41,8},{42,8},{43,8},
66
{44,8},{21,7},{21,7},{28,7},{28,7},{61,8},{62,8},{63,8},{0,8},
67
{320,8},{384,8},{10,5},{10,5},{10,5},{10,5},{10,5},{10,5},{10,5},
68
{10,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},
69
{27,7},{27,7},{59,8},{60,8},{288,9},{290,9},{18,7},{18,7},{24,7},
70
{24,7},{49,8},{50,8},{51,8},{52,8},{25,7},{25,7},{55,8},{56,8},
71
{57,8},{58,8},{192,6},{192,6},{192,6},{192,6},{1664,6},{1664,6},
72
{1664,6},{1664,6},{448,8},{512,8},{292,9},{640,8},{576,8},{294,9},
73
{296,9},{298,9},{300,9},{302,9},{256,7},{256,7},{2,4},{2,4},{2,4},
74
{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},
75
{2,4},{2,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},
76
{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{128,5},{128,5},{128,5},
77
{128,5},{128,5},{128,5},{128,5},{128,5},{8,5},{8,5},{8,5},{8,5},
78
{8,5},{8,5},{8,5},{8,5},{9,5},{9,5},{9,5},{9,5},{9,5},{9,5},{9,5},
79
{9,5},{16,6},{16,6},{16,6},{16,6},{17,6},{17,6},{17,6},{17,6},
80
{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},
81
{4,4},{4,4},{4,4},{4,4},{4,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
82
{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
83
{14,6},{14,6},{14,6},{14,6},{15,6},{15,6},{15,6},{15,6},{64,5},
84
{64,5},{64,5},{64,5},{64,5},{64,5},{64,5},{64,5},{6,4},{6,4},
85
{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},
86
{6,4},{6,4},{6,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},
87
{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{-2,3},{-2,3},
88
{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
89
{-1,0},{-1,0},{-1,0},{-1,0},{-3,4},{1792,3},{1792,3},{1984,4},
90
{2048,4},{2112,4},{2176,4},{2240,4},{2304,4},{1856,3},{1856,3},
91
{1920,3},{1920,3},{2368,4},{2432,4},{2496,4},{2560,4},{1472,1},
92
{1536,1},{1600,1},{1728,1},{704,1},{768,1},{832,1},{896,1},
93
{960,1},{1024,1},{1088,1},{1152,1},{1216,1},{1280,1},{1344,1},
94
{1408,1}
95
};
96
97
/* Black decoding table. */
98
static const cfd_node cf_black_decode[] = {
99
{128,12},{160,13},{224,12},{256,12},{10,7},{11,7},{288,12},{12,7},
100
{9,6},{9,6},{8,6},{8,6},{7,5},{7,5},{7,5},{7,5},{6,4},{6,4},{6,4},
101
{6,4},{6,4},{6,4},{6,4},{6,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
102
{5,4},{5,4},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},
103
{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{4,3},{4,3},{4,3},{4,3},
104
{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},
105
{4,3},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
106
{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
107
{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
108
{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
109
{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
110
{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
111
{-2,4},{-2,4},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
112
{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-3,5},{1792,4},
113
{1792,4},{1984,5},{2048,5},{2112,5},{2176,5},{2240,5},{2304,5},
114
{1856,4},{1856,4},{1920,4},{1920,4},{2368,5},{2432,5},{2496,5},
115
{2560,5},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},
116
{52,5},{52,5},{640,6},{704,6},{768,6},{832,6},{55,5},{55,5},
117
{56,5},{56,5},{1280,6},{1344,6},{1408,6},{1472,6},{59,5},{59,5},
118
{60,5},{60,5},{1536,6},{1600,6},{24,4},{24,4},{24,4},{24,4},
119
{25,4},{25,4},{25,4},{25,4},{1664,6},{1728,6},{320,5},{320,5},
120
{384,5},{384,5},{448,5},{448,5},{512,6},{576,6},{53,5},{53,5},
121
{54,5},{54,5},{896,6},{960,6},{1024,6},{1088,6},{1152,6},{1216,6},
122
{64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{13,1},
123
{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},
124
{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{23,4},{23,4},{50,5},
125
{51,5},{44,5},{45,5},{46,5},{47,5},{57,5},{58,5},{61,5},{256,5},
126
{16,3},{16,3},{16,3},{16,3},{17,3},{17,3},{17,3},{17,3},{48,5},
127
{49,5},{62,5},{63,5},{30,5},{31,5},{32,5},{33,5},{40,5},{41,5},
128
{22,4},{22,4},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},
129
{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},
130
{15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{128,5},
131
{192,5},{26,5},{27,5},{28,5},{29,5},{19,4},{19,4},{20,4},{20,4},
132
{34,5},{35,5},{36,5},{37,5},{38,5},{39,5},{21,4},{21,4},{42,5},
133
{43,5},{0,3},{0,3},{0,3},{0,3}
134
};
135
136
/* 2-D decoding table. */
137
static const cfd_node cf_2d_decode[] = {
138
{128,11},{144,10},{6,7},{0,7},{5,6},{5,6},{1,6},{1,6},{-4,4},
139
{-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-5,3},{-5,3},
140
{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},
141
{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{4,3},{4,3},{4,3},{4,3},{4,3},
142
{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},
143
{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},
144
{2,3},{2,3},{2,3},{2,3},{2,3},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
145
{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
146
{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
147
{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
148
{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
149
{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
150
{3,1},{3,1},{3,1},{-2,4},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
151
{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
152
{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-3,3}
153
};
154
155
/* bit magic */
156
157
static inline int getbit(const unsigned char *buf, int x)
158
{
159
return ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1;
160
}
161
162
static const unsigned char mask[8] = {
163
0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0
164
};
165
166
static const unsigned char clz[256] = {
167
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
168
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
169
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
170
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
171
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
172
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
173
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
183
};
184
185
static inline int
186
find_changing(const unsigned char *line, int x, int w)
187
{
188
int a, b, m, W;
189
190
if (!line)
191
return w;
192
193
/* We assume w > 0, -1 <= x < w */
194
if (x < 0)
195
{
196
x = 0;
197
m = 0xFF;
198
}
199
else
200
{
201
/* Mask out the bits we've already used (including the one
202
* we started from) */
203
m = mask[x & 7];
204
}
205
/* We have 'w' pixels (bits) in line. The last pixel that can be
206
* safely accessed is the (w-1)th bit of line.
207
* By taking W = w>>3, we know that the first W bytes of line are
208
* full, with w&7 stray bits following. */
209
W = w>>3;
210
x >>= 3;
211
a = line[x]; /* Safe as x < w => x <= w-1 => x>>3 <= (w-1)>>3 */
212
b = a ^ (a>>1);
213
b &= m;
214
if (x >= W)
215
{
216
/* Within the last byte already */
217
x = (x<<3) + clz[b];
218
if (x > w)
219
x = w;
220
return x;
221
}
222
while (b == 0)
223
{
224
if (++x >= W)
225
goto nearend;
226
b = a & 1;
227
a = line[x];
228
b = (b<<7) ^ a ^ (a>>1);
229
}
230
return (x<<3) + clz[b];
231
nearend:
232
/* We have less than a byte to go. If no stray bits, exit now. */
233
if ((x<<3) == w)
234
return w;
235
b = a&1;
236
a = line[x];
237
b = (b<<7) ^ a ^ (a>>1);
238
x = (x<<3) + clz[b];
239
if (x > w)
240
x = w;
241
return x;
242
}
243
244
static inline int
245
find_changing_color(const unsigned char *line, int x, int w, int color)
246
{
247
if (!line || x >= w)
248
return w;
249
250
x = find_changing(line, (x > 0 || !color) ? x : -1, w);
251
252
if (x < w && getbit(line, x) != color)
253
x = find_changing(line, x, w);
254
255
return x;
256
}
257
258
static const unsigned char lm[8] = {
259
0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01
260
};
261
262
static const unsigned char rm[8] = {
263
0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE
264
};
265
266
static inline void setbits(unsigned char *line, int x0, int x1)
267
{
268
int a0, a1, b0, b1, a;
269
270
if (x1 <= x0)
271
return;
272
273
a0 = x0 >> 3;
274
a1 = x1 >> 3;
275
276
b0 = x0 & 7;
277
b1 = x1 & 7;
278
279
if (a0 == a1)
280
{
281
if (b1)
282
line[a0] |= lm[b0] & rm[b1];
283
}
284
else
285
{
286
line[a0] |= lm[b0];
287
for (a = a0 + 1; a < a1; a++)
288
line[a] = 0xFF;
289
if (b1)
290
line[a1] |= rm[b1];
291
}
292
}
293
294
typedef struct fz_faxd_s fz_faxd;
295
296
enum
297
{
298
STATE_INIT, /* initial state, optionally waiting for EOL */
299
STATE_NORMAL, /* neutral state, waiting for any code */
300
STATE_MAKEUP, /* got a 1d makeup code, waiting for terminating code */
301
STATE_EOL, /* at eol, needs output buffer space */
302
STATE_H1, STATE_H2, /* in H part 1 and 2 (both makeup and terminating codes) */
303
STATE_DONE /* all done */
304
};
305
306
struct fz_faxd_s
307
{
308
fz_stream *chain;
309
310
int k;
311
int end_of_line;
312
int encoded_byte_align;
313
int columns;
314
int rows;
315
int end_of_block;
316
int black_is_1;
317
318
int stride;
319
int ridx;
320
321
int bidx;
322
unsigned int word;
323
324
int stage;
325
326
int a, c, dim, eolc;
327
unsigned char *ref;
328
unsigned char *dst;
329
unsigned char *rp, *wp;
330
331
unsigned char buffer[4096];
332
};
333
334
static inline void eat_bits(fz_faxd *fax, int nbits)
335
{
336
fax->word <<= nbits;
337
fax->bidx += nbits;
338
}
339
340
static int
341
fill_bits(fz_context *ctx, fz_faxd *fax)
342
{
343
/* The longest length of bits we'll ever need is 13. Never read more
344
* than we need to avoid unnecessary overreading of the end of the
345
* stream. */
346
while (fax->bidx > (32-13))
347
{
348
int c = fz_read_byte(ctx, fax->chain);
349
if (c == EOF)
350
return EOF;
351
fax->bidx -= 8;
352
fax->word |= c << fax->bidx;
353
}
354
return 0;
355
}
356
357
static int
358
get_code(fz_context *ctx, fz_faxd *fax, const cfd_node *table, int initialbits)
359
{
360
unsigned int word = fax->word;
361
int tidx = word >> (32 - initialbits);
362
int val = table[tidx].val;
363
int nbits = table[tidx].nbits;
364
365
if (nbits > initialbits)
366
{
367
int mask = (1 << (32 - initialbits)) - 1;
368
tidx = val + ((word & mask) >> (32 - nbits));
369
val = table[tidx].val;
370
nbits = initialbits + table[tidx].nbits;
371
}
372
373
eat_bits(fax, nbits);
374
375
return val;
376
}
377
378
/* decode one 1d code */
379
static void
380
dec1d(fz_context *ctx, fz_faxd *fax)
381
{
382
int code;
383
384
if (fax->a == -1)
385
fax->a = 0;
386
387
if (fax->c)
388
code = get_code(ctx, fax, cf_black_decode, cfd_black_initial_bits);
389
else
390
code = get_code(ctx, fax, cf_white_decode, cfd_white_initial_bits);
391
392
if (code == UNCOMPRESSED)
393
fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
394
395
if (code < 0)
396
fz_throw(ctx, FZ_ERROR_GENERIC, "negative code in 1d faxd");
397
398
if (fax->a + code > fax->columns)
399
fz_throw(ctx, FZ_ERROR_GENERIC, "overflow in 1d faxd");
400
401
if (fax->c)
402
setbits(fax->dst, fax->a, fax->a + code);
403
404
fax->a += code;
405
406
if (code < 64)
407
{
408
fax->c = !fax->c;
409
fax->stage = STATE_NORMAL;
410
}
411
else
412
fax->stage = STATE_MAKEUP;
413
}
414
415
/* decode one 2d code */
416
static void
417
dec2d(fz_context *ctx, fz_faxd *fax)
418
{
419
int code, b1, b2;
420
421
if (fax->stage == STATE_H1 || fax->stage == STATE_H2)
422
{
423
if (fax->a == -1)
424
fax->a = 0;
425
426
if (fax->c)
427
code = get_code(ctx, fax, cf_black_decode, cfd_black_initial_bits);
428
else
429
code = get_code(ctx, fax, cf_white_decode, cfd_white_initial_bits);
430
431
if (code == UNCOMPRESSED)
432
fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
433
434
if (code < 0)
435
fz_throw(ctx, FZ_ERROR_GENERIC, "negative code in 2d faxd");
436
437
if (fax->a + code > fax->columns)
438
fz_throw(ctx, FZ_ERROR_GENERIC, "overflow in 2d faxd");
439
440
if (fax->c)
441
setbits(fax->dst, fax->a, fax->a + code);
442
443
fax->a += code;
444
445
if (code < 64)
446
{
447
fax->c = !fax->c;
448
if (fax->stage == STATE_H1)
449
fax->stage = STATE_H2;
450
else if (fax->stage == STATE_H2)
451
fax->stage = STATE_NORMAL;
452
}
453
454
return;
455
}
456
457
code = get_code(ctx, fax, cf_2d_decode, cfd_2d_initial_bits);
458
459
switch (code)
460
{
461
case H:
462
fax->stage = STATE_H1;
463
break;
464
465
case P:
466
b1 = find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
467
if (b1 >= fax->columns)
468
b2 = fax->columns;
469
else
470
b2 = find_changing(fax->ref, b1, fax->columns);
471
if (fax->c) setbits(fax->dst, fax->a, b2);
472
fax->a = b2;
473
break;
474
475
case V0:
476
b1 = find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
477
if (fax->c) setbits(fax->dst, fax->a, b1);
478
fax->a = b1;
479
fax->c = !fax->c;
480
break;
481
482
case VR1:
483
b1 = 1 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
484
if (b1 >= fax->columns) b1 = fax->columns;
485
if (fax->c) setbits(fax->dst, fax->a, b1);
486
fax->a = b1;
487
fax->c = !fax->c;
488
break;
489
490
case VR2:
491
b1 = 2 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
492
if (b1 >= fax->columns) b1 = fax->columns;
493
if (fax->c) setbits(fax->dst, fax->a, b1);
494
fax->a = b1;
495
fax->c = !fax->c;
496
break;
497
498
case VR3:
499
b1 = 3 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
500
if (b1 >= fax->columns) b1 = fax->columns;
501
if (fax->c) setbits(fax->dst, fax->a, b1);
502
fax->a = b1;
503
fax->c = !fax->c;
504
break;
505
506
case VL1:
507
b1 = -1 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
508
if (b1 < 0) b1 = 0;
509
if (fax->c) setbits(fax->dst, fax->a, b1);
510
fax->a = b1;
511
fax->c = !fax->c;
512
break;
513
514
case VL2:
515
b1 = -2 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
516
if (b1 < 0) b1 = 0;
517
if (fax->c) setbits(fax->dst, fax->a, b1);
518
fax->a = b1;
519
fax->c = !fax->c;
520
break;
521
522
case VL3:
523
b1 = -3 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
524
if (b1 < 0) b1 = 0;
525
if (fax->c) setbits(fax->dst, fax->a, b1);
526
fax->a = b1;
527
fax->c = !fax->c;
528
break;
529
530
case UNCOMPRESSED:
531
fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
532
533
case ERROR:
534
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid code in 2d faxd");
535
536
default:
537
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid code in 2d faxd (%d)", code);
538
}
539
}
540
541
static int
542
next_faxd(fz_context *ctx, fz_stream *stm, int max)
543
{
544
fz_faxd *fax = stm->state;
545
unsigned char *p = fax->buffer;
546
unsigned char *ep;
547
unsigned char *tmp;
548
549
if (max > sizeof(fax->buffer))
550
max = sizeof(fax->buffer);
551
ep = p + max;
552
if (fax->stage == STATE_INIT && fax->end_of_line)
553
{
554
fill_bits(ctx, fax);
555
if ((fax->word >> (32 - 12)) != 1)
556
{
557
fz_warn(ctx, "faxd stream doesn't start with EOL");
558
while (!fill_bits(ctx, fax) && (fax->word >> (32 - 12)) != 1)
559
eat_bits(fax, 1);
560
}
561
if ((fax->word >> (32 - 12)) != 1)
562
fz_throw(ctx, FZ_ERROR_GENERIC, "initial EOL not found");
563
}
564
565
if (fax->stage == STATE_INIT)
566
fax->stage = STATE_NORMAL;
567
568
if (fax->stage == STATE_DONE)
569
return EOF;
570
571
if (fax->stage == STATE_EOL)
572
goto eol;
573
574
loop:
575
576
if (fill_bits(ctx, fax))
577
{
578
if (fax->bidx > 31)
579
{
580
if (fax->a > 0)
581
goto eol;
582
goto rtc;
583
}
584
}
585
586
if ((fax->word >> (32 - 12)) == 0)
587
{
588
eat_bits(fax, 1);
589
goto loop;
590
}
591
592
if ((fax->word >> (32 - 12)) == 1)
593
{
594
eat_bits(fax, 12);
595
fax->eolc ++;
596
597
if (fax->k > 0)
598
{
599
if (fax->a == -1)
600
fax->a = 0;
601
if ((fax->word >> (32 - 1)) == 1)
602
fax->dim = 1;
603
else
604
fax->dim = 2;
605
eat_bits(fax, 1);
606
}
607
}
608
else if (fax->k > 0 && fax->a == -1)
609
{
610
fax->a = 0;
611
if ((fax->word >> (32 - 1)) == 1)
612
fax->dim = 1;
613
else
614
fax->dim = 2;
615
eat_bits(fax, 1);
616
}
617
else if (fax->dim == 1)
618
{
619
fax->eolc = 0;
620
fz_try(ctx)
621
{
622
dec1d(ctx, fax);
623
}
624
fz_catch(ctx)
625
{
626
goto error;
627
}
628
}
629
else if (fax->dim == 2)
630
{
631
fax->eolc = 0;
632
fz_try(ctx)
633
{
634
dec2d(ctx, fax);
635
}
636
fz_catch(ctx)
637
{
638
goto error;
639
}
640
}
641
642
/* no eol check after makeup codes nor in the middle of an H code */
643
if (fax->stage == STATE_MAKEUP || fax->stage == STATE_H1 || fax->stage == STATE_H2)
644
goto loop;
645
646
/* check for eol conditions */
647
if (fax->eolc || fax->a >= fax->columns)
648
{
649
if (fax->a > 0)
650
goto eol;
651
if (fax->eolc == (fax->k < 0 ? 2 : 6))
652
goto rtc;
653
}
654
655
goto loop;
656
657
eol:
658
fax->stage = STATE_EOL;
659
660
if (fax->black_is_1)
661
{
662
while (fax->rp < fax->wp && p < ep)
663
*p++ = *fax->rp++;
664
}
665
else
666
{
667
while (fax->rp < fax->wp && p < ep)
668
*p++ = *fax->rp++ ^ 0xff;
669
}
670
671
if (fax->rp < fax->wp)
672
{
673
stm->rp = fax->buffer;
674
stm->wp = p;
675
stm->pos += (p - fax->buffer);
676
if (p == fax->buffer)
677
return EOF;
678
return *stm->rp++;
679
}
680
681
tmp = fax->ref;
682
fax->ref = fax->dst;
683
fax->dst = tmp;
684
memset(fax->dst, 0, fax->stride);
685
686
fax->rp = fax->dst;
687
fax->wp = fax->dst + fax->stride;
688
689
fax->stage = STATE_NORMAL;
690
fax->c = 0;
691
fax->a = -1;
692
fax->ridx ++;
693
694
if (!fax->end_of_block && fax->rows && fax->ridx >= fax->rows)
695
goto rtc;
696
697
/* we have not read dim from eol, make a guess */
698
if (fax->k > 0 && !fax->eolc && fax->a == -1)
699
{
700
if (fax->ridx % fax->k == 0)
701
fax->dim = 1;
702
else
703
fax->dim = 2;
704
}
705
706
/* if end_of_line & encoded_byte_align, EOLs are *not* optional */
707
if (fax->encoded_byte_align)
708
{
709
if (fax->end_of_line)
710
eat_bits(fax, (12 - fax->bidx) & 7);
711
else
712
eat_bits(fax, (8 - fax->bidx) & 7);
713
}
714
715
/* no more space in output, don't decode the next row yet */
716
if (p == fax->buffer + max)
717
{
718
stm->rp = fax->buffer;
719
stm->wp = p;
720
stm->pos += (p - fax->buffer);
721
if (p == fax->buffer)
722
return EOF;
723
return *stm->rp++;
724
}
725
726
goto loop;
727
728
error:
729
/* decode the remaining pixels up to where the error occurred */
730
if (fax->black_is_1)
731
{
732
while (fax->rp < fax->wp && p < ep)
733
*p++ = *fax->rp++;
734
}
735
else
736
{
737
while (fax->rp < fax->wp && p < ep)
738
*p++ = *fax->rp++ ^ 0xff;
739
}
740
/* fallthrough */
741
742
rtc:
743
fax->stage = STATE_DONE;
744
stm->rp = fax->buffer;
745
stm->wp = p;
746
stm->pos += (p - fax->buffer);
747
if (p == fax->buffer)
748
return EOF;
749
return *stm->rp++;
750
}
751
752
static void
753
close_faxd(fz_context *ctx, void *state_)
754
{
755
fz_faxd *fax = (fz_faxd *)state_;
756
int i;
757
758
/* if we read any extra bytes, try to put them back */
759
i = (32 - fax->bidx) / 8;
760
while (i--)
761
fz_unread_byte(ctx, fax->chain);
762
763
fz_drop_stream(ctx, fax->chain);
764
fz_free(ctx, fax->ref);
765
fz_free(ctx, fax->dst);
766
fz_free(ctx, fax);
767
}
768
769
/* Default: columns = 1728, end_of_block = 1, the rest = 0 */
770
fz_stream *
771
fz_open_faxd(fz_context *ctx, fz_stream *chain,
772
int k, int end_of_line, int encoded_byte_align,
773
int columns, int rows, int end_of_block, int black_is_1)
774
{
775
fz_faxd *fax = NULL;
776
777
fz_var(fax);
778
779
fz_try(ctx)
780
{
781
if (columns < 0 || columns >= INT_MAX - 7)
782
fz_throw(ctx, FZ_ERROR_GENERIC, "too many columns lead to an integer overflow (%d)", columns);
783
784
fax = fz_malloc_struct(ctx, fz_faxd);
785
fax->chain = chain;
786
787
fax->ref = NULL;
788
fax->dst = NULL;
789
790
fax->k = k;
791
fax->end_of_line = end_of_line;
792
fax->encoded_byte_align = encoded_byte_align;
793
fax->columns = columns;
794
fax->rows = rows;
795
fax->end_of_block = end_of_block;
796
fax->black_is_1 = black_is_1;
797
798
fax->stride = ((fax->columns - 1) >> 3) + 1;
799
fax->ridx = 0;
800
fax->bidx = 32;
801
fax->word = 0;
802
803
fax->stage = STATE_INIT;
804
fax->a = -1;
805
fax->c = 0;
806
fax->dim = fax->k < 0 ? 2 : 1;
807
fax->eolc = 0;
808
809
fax->ref = fz_malloc(ctx, fax->stride);
810
fax->dst = fz_malloc(ctx, fax->stride);
811
fax->rp = fax->dst;
812
fax->wp = fax->dst + fax->stride;
813
814
memset(fax->ref, 0, fax->stride);
815
memset(fax->dst, 0, fax->stride);
816
}
817
fz_catch(ctx)
818
{
819
if (fax)
820
{
821
fz_free(ctx, fax->dst);
822
fz_free(ctx, fax->ref);
823
}
824
fz_free(ctx, fax);
825
fz_drop_stream(ctx, chain);
826
fz_rethrow(ctx);
827
}
828
829
return fz_new_stream(ctx, fax, next_faxd, close_faxd);
830
}
831
832