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
|
/* $XConsortium: dg32d.c,v 1.3 93/11/07 11:52:20 rws Exp $ */
/**** module fax/dg32d.c ****/
/******************************************************************************
NOTICE
This software is being provided by AGE Logic, Inc. and MIT under the
following license. By obtaining, using and/or copying this software,
you agree that you have read, understood, and will comply with these
terms and conditions:
Permission to use, copy, modify, distribute and sell this
software and its documentation for any purpose and without
fee or royalty and to grant others any or all rights granted
herein is hereby granted, provided that you agree to comply
with the following copyright notice and statements, including
the disclaimer, and that the same appears on all copies and
derivative works of the software and documentation you make.
"Copyright 1993, 1994 by AGE Logic, Inc. and the Massachusetts
Institute of Technology"
THIS SOFTWARE IS PROVIDED "AS IS". AGE LOGIC AND MIT MAKE NO
REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way of
example, but not limitation, AGE LOGIC AND MIT MAKE NO
REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
INFRINGE THIRD-PARTY PROPRIETARY RIGHTS. AGE LOGIC AND MIT
SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE. IN NO
EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
BASED ON A WARRANTY, EVEN IF AGE LOGIC OR MIT OR LICENSEES
HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
The names of AGE Logic, Inc. and MIT may not be used in
advertising or publicity pertaining to this software without
specific, written prior permission from AGE Logic and MIT.
Title to this software shall at all times remain with AGE
Logic, Inc.
*****************************************************************************
fax/dg32d.c -- DDXIE G32D fax decode technique/element
Ben Fahy -- AGE Logic, Inc. May, 1993
*****************************************************************************/
#define lenient_decoder
/* the folling define causes extra stuff to be saved in state recorder */
#define _G32D
#include "fax.h"
#include "faxint.h"
#include "bits.h"
#include <servermd.h> /* pick up the BITMAP_BIT_ORDER from Core X*/
/**********************************************************************/
int decode_g32d(state)
FaxState *state;
{
register int bitpos;
register unsigned char *byteptr;
register unsigned char *endptr;
register int a0_color;
register int a0_pos,a1_pos;
register int goal;
register int mode; /* could overload goal, but will resist */
register int length;
register int width;
register int g32d_horiz;
register int rl;
int lines_found=0;
int n_old_trans;
int n_new_trans;
int *old_trans;
int *new_trans;
int a0a1,a1a2;
int length_acc=0;
int last_b1_idx=0;
int b1_pos,b2_pos;
int code,nbits;
if (state == (FaxState *)NULL)
return(-1);
/* set up initial bitstream for the very first strip */
if (!state->bits.started) {
if (state->strip_state != StripStateNew) {
return(-1);
}
state->bits.byteptr = (unsigned char *)state->strip;
state->bits.endptr = state->bits.byteptr + state->strip_size-4;
/* we will panic with four bytes to go */
state->bits.bitpos = 0;
state->bits.started = 1;
}
localize_state(state);
if (state->magic_needs)
finish_magic(state->final);
/* a magic strip was waiting for 1st word of next strip */
/*** Main Decoding Loop ***/
while(1) {
if (endptr < byteptr) {
return(-1);
}
switch(goal) {
case FAX_GOAL_SkipPastAnyToEOL:
code = 0; /* get Gnu cc to shut up. */
while (byteptr < endptr) {
/* look for EOL code */
code = get_wcode(byteptr,bitpos,endptr);
rl = _WhiteFaxTable[code].run_length;
if (rl == EOL_RUN_LENGTH)
break;
#if defined(lenient_decoder)
else
/* move bitstream one bit further and try again */
adjust_1bit(byteptr,bitpos,endptr);
#else
else if (!state->o_line && !lines_found) {
/* we'll be lenient starting out */
adjust_1bit(byteptr,bitpos,endptr);
}
else {
state->decoder_done = FAX_DECODE_DONE_ErrorSkipPast;
save_state_and_return(state);
}
#endif
}
if (byteptr >= endptr) {
state->decoder_done = FAX_DECODE_DONE_ErrorSkipPast;
save_state_and_return(state);
}
nbits = _WhiteFaxTable[code].n_bits;
goal = FAX_GOAL_SeekTagBit;
adjust_bitstream(nbits,byteptr,bitpos,endptr);
break;
case FAX_GOAL_SeekEOLandTag:
goal = FAX_GOAL_SkipPastAnyToEOL;
break;
case FAX_GOAL_SeekTagBit:
if (get_bit(byteptr,bitpos,endptr)) {
g32d_horiz = 1;
}
else {
g32d_horiz = 0;
}
goal = FAX_GOAL_AdjustTag;
/* have to set this goal in case adjusting throws us
back to the caller in order to get more data. */
adjust_1bit(byteptr,bitpos,endptr);
case FAX_GOAL_AdjustTag:
/*** ok, now start a new line ***/
goal = FAX_GOAL_StartNewLine;
break;
case FAX_GOAL_StartNewLine:
/* if we got all the data we want, write it out */
if (lines_found >= state->nl_sought)
save_state_and_return(state);
/* normal line initialization stuff */
a0_pos = -1;
a0_color = WHITE;
if (g32d_horiz)
goal = FAX_GOAL_HandleHoriz;
else
goal = FAX_GOAL_DetermineMode;
reset_transitions();
break;
case FAX_GOAL_DetermineMode:
get_mode_and_length(mode,length,byteptr,bitpos,endptr);
goal = mode;
/* our goal is now to handle whatever mode we're in! */
if (mode == FAX_MODE_Unknown) {
/* hopefully, we just hit the first 0 in an EOL */
goal = FAX_GOAL_SeekEOLandTag;
}
else
adjust_bitstream_8(length,byteptr,bitpos,endptr);
break;
case FAX_GOAL_RecoverZero:
/* have to set new goal in case adjusting throws us */
/* back to the caller (to get more data) */
goal = FAX_GOAL_AdjustedButStillRecovering;
adjust_bitstream(nbits,byteptr,bitpos,endptr);
case FAX_GOAL_AdjustedButStillRecovering:
{
int next_goal = FAX_GOAL_FoundOneEOL;
while (1) {
if (*byteptr) {
register int mask;
/* important assumption: we only get here if we have */
/* found at least twelve bits of zero leading up to */
/* the current bitposition. Therefore, there can't */
/* be any bits in the byte before the one we want */
mask = 1 << (7-bitpos);
while (bitpos < 8) {
if (mask & (*byteptr))
break;
mask >>= 1;
++bitpos;
} /* can't really reach bitpos = 8 above */
goal = next_goal;
adjust_1bit(byteptr,bitpos,endptr);
break;
/* break out of while (1) */
}
else {
bitpos = 0;
if (++byteptr >= endptr)
do_magic(byteptr,bitpos,endptr);
/* loop around to search some more */
}
} /* end of while */
}
break;
case FAX_GOAL_FoundOneEOL:
/* found an EOL. If this is RTC, there will be five more EOL's */
/* one the way. If this is a normal EOL, next code will not */
/* be an EOL, and we should interpret the next bit as tag bit */
/* look for second EOL code */
code = get_wcode(byteptr,bitpos,endptr);
rl = _WhiteFaxTable[code].run_length;
if (rl != EOL_RUN_LENGTH) {
goal = FAX_GOAL_SeekTagBit;
break;
}
/* Got a second EOL code */
return(-1);
break;
case FAX_GOAL_HandleHoriz:
length_acc=0;
goal = FAX_GOAL_AccumulateA0A1;
/* in case I run out of data while getting a0-a1 distance */
case FAX_GOAL_AccumulateA0A1:
get_a0a1(FAX_GOAL_RecordA0A1);
/* If we have to return for data before getting the */
/* whole a0a1 distance, we want to return to the a0a1 */
/* accumulate state when we reenter the decoder. If */
/* adjusting the bitstream forces us to return after */
/* getting the last part of the a0a1 distance, then */
/* we want to jump to state FAX_GOAL_RecordA0A1 when */
/* we reenter the decoder. */
case FAX_GOAL_RecordA0A1:
a0a1 = length_acc;
if (a0_pos < 0) {
/* at start of line, a0a1 is the number of white pixels, */
/* which is also the index on the line where white->black */
new_trans[n_new_trans++] = a1_pos = a0a1;
} else {
/* in middle of the line, a0a1 is run-length, so
* a1_pos = a0_pos + a0a1, a2_pos = a1_pos + a1a2
*
* 0 1 2 3 4 5 6 7 8 9 A B C D
* |w |w |w |b |b |b |b |b |w | | | | |
* a0 a1 a2
*/
new_trans[n_new_trans++] = a1_pos = a0_pos + a0a1;
}
if (a1_pos >= width) {
if (a1_pos > width) {
#if defined(lenient_decoder)
/* we went too far, but we'll be forgiving */
a1_pos = width;
#else /* not so lenient */
state->decoder_done = FAX_DECODE_DONE_ErrorPastWidth;
return(lines_found);
#endif
}
FlushLineData();
goal = FAX_GOAL_SeekEOLandTag;
break;
}
if (goal == FAX_GOAL_RecoverZero) {
/* it's possible we got here by reading a zero in get_a0a1, */
/* in which case 'goal' was set to FAX_GOAL_RecoverZero. We */
/* must assume that the zero is an EOL pre-padded with a */
/* variable number of zeros, which is legal according to */
/* the coding spec. So we will record the increment in */
/* line number and then attempt to recover. */
FlushLineData();
goal = FAX_GOAL_SkipPastAnyToEOL;
break;
/* break out of the switch, loop with the while */
}
if (rl == EOL_RUN_LENGTH) {
FlushLineData();
goal = FAX_GOAL_SeekTagBit;
/* if we got a non-zero length, remember the transition */
/* in case the next line is coded in vertical mode */
if (a0a1)
new_trans[n_new_trans++] = a0a1;
/* notice we don't have to worry about a0_pos -
it will be reset when we start the new line
*/
break;
/* break out of the switch, loop with the while */
}
length_acc=0;
goal = FAX_GOAL_AccumulateA1A2;
/* in case I run out of data while getting a1-a2 distance */
case FAX_GOAL_AccumulateA1A2:
get_a1a2(FAX_GOAL_FinishHoriz);
/* If we have to return for data before getting the */
/* whole a1a2 distance, we want to return to the a1a2 */
/* accumulate state when we reenter the decoder. If */
/* adjusting the bitstream forces us to return after */
/* getting the last part of the a1a2 distance, then */
/* we want to jump to state FAX_GOAL_FinishHoriz when */
/* we reenter the decoder. */
case FAX_GOAL_FinishHoriz:
a1a2 = length_acc;
/* XXX - I may regret not checking for a1a2 > 0 later... */
new_trans[n_new_trans] = a0_pos = new_trans[n_new_trans-1]+a1a2;
n_new_trans++;
/* it's possible we got here by reading a zero in get_a1a2, */
/* in which case 'goal' was set to FAX_GOAL_RecoverZero. We */
/* must assume that the zero is an EOL pre-padded with a */
/* variable number of zeros, which is legal according to */
/* the coding spec. So we will update the line count and */
/* then attempt to recover. */
if (goal == FAX_GOAL_RecoverZero) {
/* assume we have hit the begining of an EOL */
FlushLineData();
goal = FAX_GOAL_SkipPastAnyToEOL;
break;
}
if (rl == EOL_RUN_LENGTH) {
FlushLineData();
goal = FAX_GOAL_SeekTagBit;
break;
}
if (a0_pos >= width) {
if (a0_pos > width) {
#if defined(lenient_decoder)
/* we went too far, but we'll be forgiving */
a0_pos = width;
#else
state->decoder_done = FAX_DECODE_DONE_ErrorPastWidth;
return(lines_found);
#endif
}
FlushLineData();
goal = FAX_GOAL_SeekEOLandTag;
break;
}
else {
if (g32d_horiz)
goal = FAX_GOAL_HandleHoriz;
else
goal = FAX_GOAL_DetermineMode;
}
break;
case FAX_MODE_Pass:
if (!n_old_trans) { /* line above all white */
return(-1);
}
find_b2pos(a0_pos,a0_color,n_old_trans,old_trans);
a0_pos = b2_pos;
if (a0_pos < 0) {
FlushLineData();
goal = FAX_GOAL_SeekEOLandTag;
break;
}
if (a0_pos >= width) {
return(-1);
}
goal = FAX_GOAL_DetermineMode;
break;
case FAX_GOAL_HandleVertL3:
case FAX_GOAL_HandleVertL2:
case FAX_GOAL_HandleVertL1:
case FAX_GOAL_HandleVert0:
case FAX_GOAL_HandleVertR1:
case FAX_GOAL_HandleVertR2:
case FAX_GOAL_HandleVertR3:
if (n_old_trans) { /* line above not all white */
find_b1pos(a0_pos,a0_color,n_old_trans,old_trans);
if (b1_pos < 0) {
if (goal > FAX_GOAL_HandleVert0) {
return(-1); /* error! */
}
b1_pos = width;
}
/* ok, we are guaranteed that 0 <= b1_pos <= width */
/* set a0_pos = a1_pos, which is relative to b1_pos */
a0_pos = b1_pos + (goal-FAX_GOAL_HandleVert0);
a0_color = 1-a0_color;
if (a0_pos >= width) {
FlushLineData();
goal = FAX_GOAL_SeekEOLandTag;
}
else {
/* not at eol yet */
goal = FAX_GOAL_DetermineMode;
new_trans[n_new_trans++] = a0_pos;
}
}
else {
/* line above was all white. Since b1 was first non-white */
/* b1 is imaginary transition off right edge. In other */
/* words, there are no more transitions! Done with line */
FlushLineData();
goal = FAX_GOAL_SeekEOLandTag;
}
break;
default:
return(-1);
break;
} /* end of switch */
}
/*** End, Main Decoding Loop ***/
}
/**** module fax/g32d.c ****/
|