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
|
/* $Xorg: bits.h,v 1.3 2000/08/17 19:47:36 cpqbld Exp $ */
/* AGE Logic - Oct 15 1995 - Larry Hare */
/**** module fax/bits.h ****/
/******************************************************************************
Copyright 1993, 1994, 1998 The Open Group
All Rights Reserved.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
NOTICE
This software is being provided by AGE Logic, Inc. 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."
THIS SOFTWARE IS PROVIDED "AS IS". AGE LOGIC MAKES NO
REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way of
example, but not limitation, AGE LOGIC 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
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 LICENSEES
HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
The name of AGE Logic, Inc. may not be used in
advertising or publicity pertaining to this software without
specific, written prior permission from AGE Logic.
Title to this software shall at all times remain with AGE
Logic, Inc.
*****************************************************************************
fax/bits.h -- DDXIE G4 fax bitstream management macros
Ben Fahy -- AGE Logic, Inc. May, 1993
*****************************************************************************/
#include "gbits.h"
/* ------------------------------------------------------------------- */
/*
* get_white_run_length(next_goal)
* get_black_run_length(next_goal)
*
* These macros try to determine run lengths in horizonal mode.
* All possible bit combinations have been previously coded in
* a lookup table, so it's not a very big deal. The only real
* complication arises from the fact I may run out of strip data
* before finishing. In this case I want to save my length
* accumulator and read it back in again. If I have finished
* with this length, I want to move on to the next goal specified
* by 'next_goal'. Otherwise my goal remains to accumulate the
* current desired run length.
*/
#define get_white_run_length(next_goal) \
{ \
register int code=0,nbits,makeup; \
rl = 0; \
while (1) { \
code = get_wcode(byteptr,bitpos,endptr); \
rl = _WhiteFaxTable[code].run_length; \
nbits = _WhiteFaxTable[code].n_bits; \
makeup = _WhiteFaxTable[code].makeup; \
if (rl == BAD_RUN_LENGTH) { \
length_acc = 0; \
if (!code) { \
/* could just be fill bits, try to recover */ \
goal = FAX_GOAL_RecoverZero; \
break; \
} \
else { \
goal = FAX_GOAL_FallOnSword; \
break; \
} \
} \
if (rl != EOL_RUN_LENGTH) \
length_acc += rl; \
if (!makeup) \
goal = next_goal; \
/* just in case adjust_bitstream returns for more data */ \
adjust_bitstream(nbits,byteptr,bitpos,endptr); \
if (!makeup) \
break; \
} \
}
#define get_black_run_length(next_goal) \
{ \
register int code=0,nbits,makeup; \
rl = 0; \
while (1) { \
code = get_bcode(byteptr,bitpos,endptr); \
rl = _BlackFaxTable[code].run_length; \
nbits = _BlackFaxTable[code].n_bits; \
makeup = _BlackFaxTable[code].makeup; \
if (rl == BAD_RUN_LENGTH) { \
length_acc = 0; \
if (!code) { \
/* could just be fill bits, will try to recover */ \
goal = FAX_GOAL_RecoverZero; \
break; \
} \
else { \
goal = FAX_GOAL_FallOnSword; \
break; \
} \
} \
if (rl != EOL_RUN_LENGTH) \
length_acc += rl; \
/* if this is an EOL of terminating code, go to next goal */\
if (!makeup) \
goal = next_goal; \
/* (set in case adjust_bitstream returns for more data) */\
adjust_bitstream(nbits,byteptr,bitpos,endptr); \
if (!makeup) \
break; \
} \
}
/* ------------------------------------------------------------------- */
/*
* get_a0a1(next_goal)
* get_a1a2(next_goal)
*
* These macros try to determine run lengths in horizonal mode.
* They just leverage off of lower-level macros, so they're easy
*/
#define get_a0a1(next_goal) \
{ \
if (a0_color == WHITE) \
get_white_run_length(next_goal) /* ; */ \
else \
get_black_run_length(next_goal); \
}
#define get_a1a2(next_goal) \
{ \
if (a0_color == WHITE) \
get_black_run_length(next_goal) /* ; */ \
else \
get_white_run_length(next_goal); \
}
/* ------------------------------------------------------------------- */
/*
* save_state_and_return(state);
* localize_state(state);
*
* These macros transfer state variables back and forth between
* the reentrant data structure 'state' and register variables.
* Hopefully neither will be called often, so we don't have to
* spend energy analyzing what is 'dirty' versus what hasn't been
* touched since the last save, etc.
*
* Strategy:
* brute force.
*
*/
#if defined(_G32D)
#define save_state_and_return(state) \
{ \
state->a0_color = a0_color; \
state->a0_pos = a0_pos; \
state->a0a1 = a0a1; \
state->bits.bitpos = bitpos; \
state->bits.byteptr = byteptr; \
state->bits.endptr = endptr; \
state->goal = goal; \
state->n_old_trans = n_old_trans; \
state->old_trans = old_trans; \
state->n_new_trans = n_new_trans; \
state->new_trans = new_trans; \
state->length_acc = length_acc; \
state->last_b1_idx = last_b1_idx; \
state->width = width; \
state->rl = rl; \
state->g32d_horiz = g32d_horiz; \
return(lines_found); \
}
#define localize_state(state) \
a0_color = state->a0_color; \
a0_pos = state->a0_pos; \
a0a1 = state->a0a1; \
bitpos = state->bits.bitpos; \
byteptr = state->bits.byteptr; \
endptr = state->bits.endptr; \
goal = state->goal; \
n_old_trans = state->n_old_trans; \
old_trans = state->old_trans; \
n_new_trans = state->n_new_trans; \
new_trans = state->new_trans; \
length_acc = state->length_acc; \
last_b1_idx = state->last_b1_idx; \
width = state->width; \
rl = state->rl; \
g32d_horiz = state->g32d_horiz;
#endif /* if defined(_G32D) */
#if defined(_G31D) || defined(_G4)
#define save_state_and_return(state) \
{ \
state->a0_color = a0_color; \
state->a0_pos = a0_pos; \
state->a0a1 = a0a1; \
state->bits.bitpos = bitpos; \
state->bits.byteptr = byteptr; \
state->bits.endptr = endptr; \
state->goal = goal; \
state->n_old_trans = n_old_trans; \
state->old_trans = old_trans; \
state->n_new_trans = n_new_trans; \
state->new_trans = new_trans; \
state->length_acc = length_acc; \
state->last_b1_idx = last_b1_idx; \
state->width = width; \
state->rl = rl; \
return(lines_found); \
}
#define localize_state(state) \
a0_color = state->a0_color; \
a0_pos = state->a0_pos; \
a0a1 = state->a0a1; \
bitpos = state->bits.bitpos; \
byteptr = state->bits.byteptr; \
endptr = state->bits.endptr; \
goal = state->goal; \
n_old_trans = state->n_old_trans; \
old_trans = state->old_trans; \
n_new_trans = state->n_new_trans; \
new_trans = state->new_trans; \
length_acc = state->length_acc; \
last_b1_idx = state->last_b1_idx; \
width = state->width; \
rl = state->rl;
#endif /* defined(_G31D) */
#if defined(_PBits)
#define save_state_and_return(state) \
{ \
state->a0_color = a0_color; \
state->a0_pos = a0_pos; \
state->bits.byteptr = byteptr; \
state->bits.endptr = endptr; \
state->goal = goal; \
state->length_acc = length_acc; \
state->width = width; \
state->rl = rl; \
return(lines_found); \
}
#define localize_state(state) \
a0_color = state->a0_color; \
a0_pos = state->a0_pos; \
byteptr = state->bits.byteptr; \
endptr = state->bits.endptr; \
goal = state->goal; \
length_acc = state->length_acc; \
width = state->width; \
rl = state->rl;
#endif /* defined(_Pbits) */
/* ------------------------------------------------------------------- */
/*
* reset_transitions();
*
* Macro to trade old and new transition buffers
*
* Strategy:
* Do the obvious
*/
#define reset_transitions() \
{ \
register int *tmp = old_trans; \
old_trans = new_trans; \
new_trans = tmp; \
n_old_trans = n_new_trans; \
n_new_trans = 0; \
last_b1_idx = 0; \
}
/* ------------------------------------------------------------------- */
/*
* get_mode_and_length(mode,length,byteptr,bitpos,endptr);
*
* Macro to get next coding mode (vertical or horizontal or ...)
*
* Strategy: the modes are encoded with 8 bits or less except
* for EOL, which is 001 (12 bits). However, none of
* the other modes use 00, so there's no conflict.
*
* We have precomputed a lookup table that tells us for
* any sequence of 8 bits, what mode and length goes
* along with that sequence. So all we have to do is
* get the 8 bits and run them through the lookup table
* and we're done.
*
* Note: I'm trying to write this so a decent compiler will only
* do one load to get both mode and length. Hopefully, your
* compiler will fetch '*entry' all at once, then shift and
* mask to get components.
*
* Very old compilers may have troubles with the struct =
* construction. You'll have to change entry to *entry
* and the entry.mode to entry->mode, etc.
*/
#define get_mode_and_length(mode,length,byteptr,bitpos,endptr) \
{ register unsigned char bits=get_byte(byteptr,bitpos,endptr); \
register TwoDTable entry; \
entry = _TwoDFaxTable[bits]; \
mode = entry.mode; \
length = entry.n_bits; \
}
/* ------------------------------------------------------------------- */
/*
* find_b1pos(a0_pos,a0_color,n_old_trans,old_trans);
*
* Macro to find pixel of opposite color of a0, to right of a0,
* on the previous line.
*
* Strategy: b1 is more or less monotonically increasing. The
* only possible exception is during vertical coding when
* a1 is left of b1 by 2 or 3:
*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
* b1' b1
* B B B B W W B B B B
* B B W B W B W
* a0 a1
*
* In the diagram above, b1 is the first changing element to the
* right of a0 which goes from white to black. a1 is three to the
* left of b1.
*
* When we set a0'=a1 and proceed decoding, since the new a0' is
* black, we want the first changing element to the right of a0'
* which goes from black to white. This is b1', as pictured. Note
* that b1' < b1! So the b1 sequence is not monotically increasing,
* though it is close.
*
* At most we have to back up one transition to get the right
* changing element for a0'. This is because b1 is W->B, while
* the transition before is B-W, the one before that W->B, and
* so on. The transition before b1 is a candidate for b1', but
* not the one before that (it's the wrong color). The third
* transition before b1 must be at a position <= a0'. But the
* definition of b1' requires that b1' be to the right of a0'.
* Therefore, only the transition immediately before b1 could
* possibly server as b1'.
*
* Moral: If the last b1 we found was index last_b1_idx, then
* we can start searching for b1' at last_b1_idx-1, with complete
* assurance that we'll find it. (if it exists :)
*
* One more thing: WHITE is defined as 0. BLACK is 1. The 1st
* transition (last_b1_idx=0) is W->B, the 2nd (last_b1_idx=1)
* is B->W, etc. So an even transition is black, and odd is white.
*
* BLACK WHITE
* | 0 | 1 | bit1 of transition idx
* ----------|-------|-------|
* a0 WHITE(0) | ok | bad |
* color BLACK(1) | bad | ok |
* ----------|-------|-------|
*
* We need a transition which is opposite in color to a0_color.
* From the table above, if a0_color ^ (idx & 1), the colors
* match and we need to choose the next transition to the right.
*/
#define find_b1pos(a0_pos,a0_color,n_old,old) \
{ \
if (last_b1_idx > 0) \
--last_b1_idx; /* avoid gotcha from above evil case */ \
\
/* search for first changing element to right of a0_pos */ \
while (old[last_b1_idx] <= a0_pos) \
if (++last_b1_idx >= n_old) \
break; /* if out of transitions, give up */ \
\
/* check out color - if matching, use next transition */ \
if (a0_color ^ (last_b1_idx & 1)) \
++last_b1_idx; \
\
if (last_b1_idx < n_old) \
b1_pos = old[last_b1_idx]; \
else { \
last_b1_idx = 0; \
b1_pos = -1; \
} \
}
/* ------------------------------------------------------------------- */
/*
* find_b2pos(a0_pos,a0_color,n_old_trans,old_trans);
*
* Macro to find the end of a chunk of pixels in the previous line,
* which we passed by because they are uncorrelated with this line.
*
* Strategy: teeny tiny modification to find_b1pos.
*
*/
#define find_b2pos(a0_pos,a0_color,n_old,old) \
{ \
if (last_b1_idx > 0) \
--last_b1_idx; /* avoid gotcha from above evil case */ \
\
/* search for first changing element to right of a0_pos */ \
while (old[last_b1_idx] <= a0_pos) \
if (++last_b1_idx >= (n_old-1)) /* CHANGE */ \
break; /* if out of transitions, give up */ \
\
/* check out color - if matching, use next transition */ \
if (a0_color ^ (last_b1_idx & 1)) \
++last_b1_idx; \
\
if (last_b1_idx < n_old-1) /* CHANGE */ \
b2_pos = old[last_b1_idx+1]; /* CHANGE */ \
else { \
last_b1_idx = 0; \
b2_pos = -1; \
} \
}
/* ------------------------------------------------------------------- */
/**** module fax/bits.h ****/
|