~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to FreeImage/Source/LibTIFF/tif_fax3.c

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-05-15 03:18:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080515031800-rhaod8dwr4trqewm
Tags: 3.10.0-1
* New upstream release. Closes: #471242
* Added extra freeimage documentation in orig tarball.
* Added get-orig-source target.
* Added Homepage field in control file.
* Removing some unnecessary stuff from rules file.
* Adding some necessary build dependencies.
* Adding some modifications to allow for configuring various compiler flags.
* Fix FTBFS on amd64.
* Adding debug package.
* Added DM-Upload-Allowed: yes field.
* Added Vcs entries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tif_fax3.c,v 1.16 2006/10/28 19:36:43 drolon Exp $ */
2
 
 
3
 
/*
4
 
 * Copyright (c) 1990-1997 Sam Leffler
5
 
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6
 
 *
7
 
 * Permission to use, copy, modify, distribute, and sell this software and 
8
 
 * its documentation for any purpose is hereby granted without fee, provided
9
 
 * that (i) the above copyright notices and this permission notice appear in
10
 
 * all copies of the software and related documentation, and (ii) the names of
11
 
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12
 
 * publicity relating to the software without the specific, prior written
13
 
 * permission of Sam Leffler and Silicon Graphics.
14
 
 * 
15
 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
16
 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
17
 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
18
 
 * 
19
 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20
 
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21
 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22
 
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
23
 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
24
 
 * OF THIS SOFTWARE.
25
 
 */
26
 
 
27
 
#include "tiffiop.h"
28
 
#ifdef CCITT_SUPPORT
29
 
/*
30
 
 * TIFF Library.
31
 
 *
32
 
 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
33
 
 *
34
 
 * This file contains support for decoding and encoding TIFF
35
 
 * compression algorithms 2, 3, 4, and 32771.
36
 
 *
37
 
 * Decoder support is derived, with permission, from the code
38
 
 * in Frank Cringle's viewfax program;
39
 
 *      Copyright (C) 1990, 1995  Frank D. Cringle.
40
 
 */
41
 
#include "tif_fax3.h"
42
 
#define G3CODES
43
 
#include "t4.h"
44
 
#include <stdio.h>
45
 
 
46
 
/*
47
 
 * Compression+decompression state blocks are
48
 
 * derived from this ``base state'' block.
49
 
 */
50
 
typedef struct {
51
 
        int     rw_mode;                /* O_RDONLY for decode, else encode */
52
 
        int     mode;                   /* operating mode */
53
 
        uint32  rowbytes;               /* bytes in a decoded scanline */
54
 
        uint32  rowpixels;              /* pixels in a scanline */
55
 
 
56
 
        uint16  cleanfaxdata;           /* CleanFaxData tag */
57
 
        uint32  badfaxrun;              /* BadFaxRun tag */
58
 
        uint32  badfaxlines;            /* BadFaxLines tag */
59
 
        uint32  groupoptions;           /* Group 3/4 options tag */
60
 
        uint32  recvparams;             /* encoded Class 2 session params */
61
 
        char*   subaddress;             /* subaddress string */
62
 
        uint32  recvtime;               /* time spent receiving (secs) */
63
 
        char*   faxdcs;                 /* Table 2/T.30 encoded session params */
64
 
        TIFFVGetMethod vgetparent;      /* super-class method */
65
 
        TIFFVSetMethod vsetparent;      /* super-class method */
66
 
        TIFFPrintMethod printdir;       /* super-class method */
67
 
} Fax3BaseState;
68
 
#define Fax3State(tif)          ((Fax3BaseState*) (tif)->tif_data)
69
 
 
70
 
typedef enum { G3_1D, G3_2D } Ttag;
71
 
typedef struct {
72
 
        Fax3BaseState b;
73
 
 
74
 
        /* Decoder state info */
75
 
        const unsigned char* bitmap;    /* bit reversal table */
76
 
        uint32  data;                   /* current i/o byte/word */
77
 
        int     bit;                    /* current i/o bit in byte */
78
 
        int     EOLcnt;                 /* count of EOL codes recognized */
79
 
        TIFFFaxFillFunc fill;           /* fill routine */
80
 
        uint32* runs;                   /* b&w runs for current/previous row */
81
 
        uint32* refruns;                /* runs for reference line */
82
 
        uint32* curruns;                /* runs for current line */
83
 
 
84
 
        /* Encoder state info */
85
 
        Ttag    tag;                    /* encoding state */
86
 
        unsigned char*  refline;        /* reference line for 2d decoding */
87
 
        int     k;                      /* #rows left that can be 2d encoded */
88
 
        int     maxk;                   /* max #rows that can be 2d encoded */
89
 
 
90
 
        int line;
91
 
} Fax3CodecState;
92
 
#define DecoderState(tif)       ((Fax3CodecState*) Fax3State(tif))
93
 
#define EncoderState(tif)       ((Fax3CodecState*) Fax3State(tif))
94
 
 
95
 
#define is2DEncoding(sp) \
96
 
        (sp->b.groupoptions & GROUP3OPT_2DENCODING)
97
 
#define isAligned(p,t)  ((((unsigned long)(p)) & (sizeof (t)-1)) == 0)
98
 
 
99
 
/*
100
 
 * Group 3 and Group 4 Decoding.
101
 
 */
102
 
 
103
 
/*
104
 
 * These macros glue the TIFF library state to
105
 
 * the state expected by Frank's decoder.
106
 
 */
107
 
#define DECLARE_STATE(tif, sp, mod)                                     \
108
 
    static const char module[] = mod;                                   \
109
 
    Fax3CodecState* sp = DecoderState(tif);                             \
110
 
    int a0;                             /* reference element */         \
111
 
    int lastx = sp->b.rowpixels;        /* last element in row */       \
112
 
    uint32 BitAcc;                      /* bit accumulator */           \
113
 
    int BitsAvail;                      /* # valid bits in BitAcc */    \
114
 
    int RunLength;                      /* length of current run */     \
115
 
    unsigned char* cp;                  /* next byte of input data */   \
116
 
    unsigned char* ep;                  /* end of input data */         \
117
 
    uint32* pa;                         /* place to stuff next run */   \
118
 
    uint32* thisrun;                    /* current row's run array */   \
119
 
    int EOLcnt;                         /* # EOL codes recognized */    \
120
 
    const unsigned char* bitmap = sp->bitmap;   /* input data bit reverser */   \
121
 
    const TIFFFaxTabEnt* TabEnt
122
 
#define DECLARE_STATE_2D(tif, sp, mod)                                  \
123
 
    DECLARE_STATE(tif, sp, mod);                                        \
124
 
    int b1;                             /* next change on prev line */  \
125
 
    uint32* pb                          /* next run in reference line */\
126
 
/*
127
 
 * Load any state that may be changed during decoding.
128
 
 */
129
 
#define CACHE_STATE(tif, sp) do {                                       \
130
 
    BitAcc = sp->data;                                                  \
131
 
    BitsAvail = sp->bit;                                                \
132
 
    EOLcnt = sp->EOLcnt;                                                \
133
 
    cp = (unsigned char*) tif->tif_rawcp;                               \
134
 
    ep = cp + tif->tif_rawcc;                                           \
135
 
} while (0)
136
 
/*
137
 
 * Save state possibly changed during decoding.
138
 
 */
139
 
#define UNCACHE_STATE(tif, sp) do {                                     \
140
 
    sp->bit = BitsAvail;                                                \
141
 
    sp->data = BitAcc;                                                  \
142
 
    sp->EOLcnt = EOLcnt;                                                \
143
 
    tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp;                   \
144
 
    tif->tif_rawcp = (tidata_t) cp;                                     \
145
 
} while (0)
146
 
 
147
 
/*
148
 
 * Setup state for decoding a strip.
149
 
 */
150
 
static int
151
 
Fax3PreDecode(TIFF* tif, tsample_t s)
152
 
{
153
 
        Fax3CodecState* sp = DecoderState(tif);
154
 
 
155
 
        (void) s;
156
 
        assert(sp != NULL);
157
 
        sp->bit = 0;                    /* force initial read */
158
 
        sp->data = 0;
159
 
        sp->EOLcnt = 0;                 /* force initial scan for EOL */
160
 
        /*
161
 
         * Decoder assumes lsb-to-msb bit order.  Note that we select
162
 
         * this here rather than in Fax3SetupState so that viewers can
163
 
         * hold the image open, fiddle with the FillOrder tag value,
164
 
         * and then re-decode the image.  Otherwise they'd need to close
165
 
         * and open the image to get the state reset.
166
 
         */
167
 
        sp->bitmap =
168
 
            TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);
169
 
        if (sp->refruns) {              /* init reference line to white */
170
 
                sp->refruns[0] = (uint32) sp->b.rowpixels;
171
 
                sp->refruns[1] = 0;
172
 
        }
173
 
        sp->line = 0;
174
 
        return (1);
175
 
}
176
 
 
177
 
/*
178
 
 * Routine for handling various errors/conditions.
179
 
 * Note how they are "glued into the decoder" by
180
 
 * overriding the definitions used by the decoder.
181
 
 */
182
 
 
183
 
static void
184
 
Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0)
185
 
{
186
 
        TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %lu of %s %lu (x %lu)",
187
 
                tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip",
188
 
           (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
189
 
           (unsigned long) a0);
190
 
}
191
 
#define unexpected(table, a0)   Fax3Unexpected(module, tif, sp->line, a0)
192
 
 
193
 
static void
194
 
Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0)
195
 
{
196
 
        TIFFErrorExt(tif->tif_clientdata, module,
197
 
            "%s: Uncompressed data (not supported) at line %lu of %s %lu (x %lu)",
198
 
            tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip",
199
 
       (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
200
 
       (unsigned long) a0);
201
 
}
202
 
#define extension(a0)   Fax3Extension(module, tif, sp->line, a0)
203
 
 
204
 
static void
205
 
Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx)
206
 
{
207
 
        TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %lu of %s %lu (got %lu, expected %lu)",
208
 
            tif->tif_name,
209
 
            a0 < lastx ? "Premature EOL" : "Line length mismatch",
210
 
            (unsigned long) line, isTiled(tif) ? "tile" : "strip",
211
 
        (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
212
 
        (unsigned long) a0, lastx);
213
 
}
214
 
#define badlength(a0,lastx)     Fax3BadLength(module, tif, sp->line, a0, lastx)
215
 
 
216
 
static void
217
 
Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0)
218
 
{
219
 
        TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %lu of %s %lu (x %lu)",
220
 
            tif->tif_name,
221
 
            (unsigned long) line, isTiled(tif) ? "tile" : "strip",
222
 
        (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
223
 
        (unsigned long) a0);
224
 
}
225
 
#define prematureEOF(a0)        Fax3PrematureEOF(module, tif, sp->line, a0)
226
 
 
227
 
#define Nop
228
 
 
229
 
/*
230
 
 * Decode the requested amount of G3 1D-encoded data.
231
 
 */
232
 
static int
233
 
Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
234
 
{
235
 
        DECLARE_STATE(tif, sp, "Fax3Decode1D");
236
 
 
237
 
        (void) s;
238
 
        CACHE_STATE(tif, sp);
239
 
        thisrun = sp->curruns;
240
 
        while ((long)occ > 0) {
241
 
                a0 = 0;
242
 
                RunLength = 0;
243
 
                pa = thisrun;
244
 
#ifdef FAX3_DEBUG
245
 
                printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
246
 
                printf("-------------------- %d\n", tif->tif_row);
247
 
                fflush(stdout);
248
 
#endif
249
 
                SYNC_EOL(EOF1D);
250
 
                EXPAND1D(EOF1Da);
251
 
                (*sp->fill)(buf, thisrun, pa, lastx);
252
 
                buf += sp->b.rowbytes;
253
 
                occ -= sp->b.rowbytes;
254
 
                sp->line++;
255
 
                continue;
256
 
        EOF1D:                          /* premature EOF */
257
 
                CLEANUP_RUNS();
258
 
        EOF1Da:                         /* premature EOF */
259
 
                (*sp->fill)(buf, thisrun, pa, lastx);
260
 
                UNCACHE_STATE(tif, sp);
261
 
                return (-1);
262
 
        }
263
 
        UNCACHE_STATE(tif, sp);
264
 
        return (1);
265
 
}
266
 
 
267
 
#define SWAP(t,a,b)     { t x; x = (a); (a) = (b); (b) = x; }
268
 
/*
269
 
 * Decode the requested amount of G3 2D-encoded data.
270
 
 */
271
 
static int
272
 
Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
273
 
{
274
 
        DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
275
 
        int is1D;                       /* current line is 1d/2d-encoded */
276
 
 
277
 
        (void) s;
278
 
        CACHE_STATE(tif, sp);
279
 
        while ((long)occ > 0) {
280
 
                a0 = 0;
281
 
                RunLength = 0;
282
 
                pa = thisrun = sp->curruns;
283
 
#ifdef FAX3_DEBUG
284
 
                printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
285
 
                    BitAcc, BitsAvail, EOLcnt);
286
 
#endif
287
 
                SYNC_EOL(EOF2D);
288
 
                NeedBits8(1, EOF2D);
289
 
                is1D = GetBits(1);      /* 1D/2D-encoding tag bit */
290
 
                ClrBits(1);
291
 
#ifdef FAX3_DEBUG
292
 
                printf(" %s\n-------------------- %d\n",
293
 
                    is1D ? "1D" : "2D", tif->tif_row);
294
 
                fflush(stdout);
295
 
#endif
296
 
                pb = sp->refruns;
297
 
                b1 = *pb++;
298
 
                if (is1D)
299
 
                        EXPAND1D(EOF2Da);
300
 
                else
301
 
                        EXPAND2D(EOF2Da);
302
 
                (*sp->fill)(buf, thisrun, pa, lastx);
303
 
                SETVALUE(0);            /* imaginary change for reference */
304
 
                SWAP(uint32*, sp->curruns, sp->refruns);
305
 
                buf += sp->b.rowbytes;
306
 
                occ -= sp->b.rowbytes;
307
 
                sp->line++;
308
 
                continue;
309
 
        EOF2D:                          /* premature EOF */
310
 
                CLEANUP_RUNS();
311
 
        EOF2Da:                         /* premature EOF */
312
 
                (*sp->fill)(buf, thisrun, pa, lastx);
313
 
                UNCACHE_STATE(tif, sp);
314
 
                return (-1);
315
 
        }
316
 
        UNCACHE_STATE(tif, sp);
317
 
        return (1);
318
 
}
319
 
#undef SWAP
320
 
 
321
 
/*
322
 
 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
323
 
 * For machines with 64-bit longs this is <16 bytes; otherwise
324
 
 * this is <8 bytes.  We optimize the code here to reflect the
325
 
 * machine characteristics.
326
 
 */
327
 
#if SIZEOF_LONG == 8
328
 
# define FILL(n, cp)                                                        \
329
 
    switch (n) {                                                            \
330
 
    case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
331
 
    case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
332
 
    case  9: (cp)[8] = 0xff; case  8: (cp)[7] = 0xff; case  7: (cp)[6] = 0xff;\
333
 
    case  6: (cp)[5] = 0xff; case  5: (cp)[4] = 0xff; case  4: (cp)[3] = 0xff;\
334
 
    case  3: (cp)[2] = 0xff; case  2: (cp)[1] = 0xff;                         \
335
 
    case  1: (cp)[0] = 0xff; (cp) += (n); case 0:  ;                          \
336
 
    }
337
 
# define ZERO(n, cp)                                                    \
338
 
    switch (n) {                                                        \
339
 
    case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0;  \
340
 
    case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0;   \
341
 
    case  9: (cp)[8] = 0; case  8: (cp)[7] = 0; case  7: (cp)[6] = 0;   \
342
 
    case  6: (cp)[5] = 0; case  5: (cp)[4] = 0; case  4: (cp)[3] = 0;   \
343
 
    case  3: (cp)[2] = 0; case  2: (cp)[1] = 0;                         \
344
 
    case  1: (cp)[0] = 0; (cp) += (n); case 0:  ;                       \
345
 
    }
346
 
#else
347
 
# define FILL(n, cp)                                                        \
348
 
    switch (n) {                                                            \
349
 
    case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
350
 
    case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
351
 
    case 1: (cp)[0] = 0xff; (cp) += (n); case 0:  ;                         \
352
 
    }
353
 
# define ZERO(n, cp)                                                    \
354
 
    switch (n) {                                                        \
355
 
    case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0;      \
356
 
    case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0;      \
357
 
    case 1: (cp)[0] = 0; (cp) += (n); case 0:  ;                        \
358
 
    }
359
 
#endif
360
 
 
361
 
/*
362
 
 * Bit-fill a row according to the white/black
363
 
 * runs generated during G3/G4 decoding.
364
 
 */
365
 
void
366
 
_TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
367
 
{
368
 
        static const unsigned char _fillmasks[] =
369
 
            { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
370
 
        unsigned char* cp;
371
 
        uint32 x, bx, run;
372
 
        int32 n, nw;
373
 
        long* lp;
374
 
 
375
 
        if ((erun-runs)&1)
376
 
            *erun++ = 0;
377
 
        x = 0;
378
 
        for (; runs < erun; runs += 2) {
379
 
            run = runs[0];
380
 
            if (x+run > lastx || run > lastx )
381
 
                run = runs[0] = (uint32) (lastx - x);
382
 
            if (run) {
383
 
                cp = buf + (x>>3);
384
 
                bx = x&7;
385
 
                if (run > 8-bx) {
386
 
                    if (bx) {                   /* align to byte boundary */
387
 
                        *cp++ &= 0xff << (8-bx);
388
 
                        run -= 8-bx;
389
 
                    }
390
 
                    if( (n = run >> 3) != 0 ) { /* multiple bytes to fill */
391
 
                        if ((n/sizeof (long)) > 1) {
392
 
                            /*
393
 
                             * Align to longword boundary and fill.
394
 
                             */
395
 
                            for (; n && !isAligned(cp, long); n--)
396
 
                                    *cp++ = 0x00;
397
 
                            lp = (long*) cp;
398
 
                            nw = (int32)(n / sizeof (long));
399
 
                            n -= nw * sizeof (long);
400
 
                            do {
401
 
                                    *lp++ = 0L;
402
 
                            } while (--nw);
403
 
                            cp = (unsigned char*) lp;
404
 
                        }
405
 
                        ZERO(n, cp);
406
 
                        run &= 7;
407
 
                    }
408
 
                    if (run)
409
 
                        cp[0] &= 0xff >> run;
410
 
                } else
411
 
                    cp[0] &= ~(_fillmasks[run]>>bx);
412
 
                x += runs[0];
413
 
            }
414
 
            run = runs[1];
415
 
            if (x+run > lastx || run > lastx )
416
 
                run = runs[1] = lastx - x;
417
 
            if (run) {
418
 
                cp = buf + (x>>3);
419
 
                bx = x&7;
420
 
                if (run > 8-bx) {
421
 
                    if (bx) {                   /* align to byte boundary */
422
 
                        *cp++ |= 0xff >> bx;
423
 
                        run -= 8-bx;
424
 
                    }
425
 
                    if( (n = run>>3) != 0 ) {   /* multiple bytes to fill */
426
 
                        if ((n/sizeof (long)) > 1) {
427
 
                            /*
428
 
                             * Align to longword boundary and fill.
429
 
                             */
430
 
                            for (; n && !isAligned(cp, long); n--)
431
 
                                *cp++ = 0xff;
432
 
                            lp = (long*) cp;
433
 
                            nw = (int32)(n / sizeof (long));
434
 
                            n -= nw * sizeof (long);
435
 
                            do {
436
 
                                *lp++ = -1L;
437
 
                            } while (--nw);
438
 
                            cp = (unsigned char*) lp;
439
 
                        }
440
 
                        FILL(n, cp);
441
 
                        run &= 7;
442
 
                    }
443
 
                    if (run)
444
 
                        cp[0] |= 0xff00 >> run;
445
 
                } else
446
 
                    cp[0] |= _fillmasks[run]>>bx;
447
 
                x += runs[1];
448
 
            }
449
 
        }
450
 
        assert(x == lastx);
451
 
}
452
 
#undef  ZERO
453
 
#undef  FILL
454
 
 
455
 
/*
456
 
 * Setup G3/G4-related compression/decompression state
457
 
 * before data is processed.  This routine is called once
458
 
 * per image -- it sets up different state based on whether
459
 
 * or not decoding or encoding is being done and whether
460
 
 * 1D- or 2D-encoded data is involved.
461
 
 */
462
 
static int
463
 
Fax3SetupState(TIFF* tif)
464
 
{
465
 
        TIFFDirectory* td = &tif->tif_dir;
466
 
        Fax3BaseState* sp = Fax3State(tif);
467
 
        int needsRefLine;
468
 
        Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
469
 
        uint32 rowbytes, rowpixels, nruns;
470
 
 
471
 
        if (td->td_bitspersample != 1) {
472
 
                TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
473
 
                    "Bits/sample must be 1 for Group 3/4 encoding/decoding");
474
 
                return (0);
475
 
        }
476
 
        /*
477
 
         * Calculate the scanline/tile widths.
478
 
         */
479
 
        if (isTiled(tif)) {
480
 
                rowbytes = TIFFTileRowSize(tif);
481
 
                rowpixels = td->td_tilewidth;
482
 
        } else {
483
 
                rowbytes = TIFFScanlineSize(tif);
484
 
                rowpixels = td->td_imagewidth;
485
 
        }
486
 
        sp->rowbytes = (uint32) rowbytes;
487
 
        sp->rowpixels = (uint32) rowpixels;
488
 
        /*
489
 
         * Allocate any additional space required for decoding/encoding.
490
 
         */
491
 
        needsRefLine = (
492
 
            (sp->groupoptions & GROUP3OPT_2DENCODING) ||
493
 
            td->td_compression == COMPRESSION_CCITTFAX4
494
 
        );
495
 
 
496
 
        nruns = needsRefLine ? 2*TIFFroundup(rowpixels,32) : rowpixels;
497
 
        nruns += 3;
498
 
        dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 2*nruns, sizeof (uint32),
499
 
                                          "for Group 3/4 run arrays");
500
 
        if (dsp->runs == NULL)
501
 
                return (0);
502
 
        dsp->curruns = dsp->runs;
503
 
        if (needsRefLine)
504
 
                dsp->refruns = dsp->runs + nruns;
505
 
        else
506
 
                dsp->refruns = NULL;
507
 
        if (td->td_compression == COMPRESSION_CCITTFAX3
508
 
            && is2DEncoding(dsp)) {     /* NB: default is 1D routine */
509
 
                tif->tif_decoderow = Fax3Decode2D;
510
 
                tif->tif_decodestrip = Fax3Decode2D;
511
 
                tif->tif_decodetile = Fax3Decode2D;
512
 
        }
513
 
 
514
 
        if (needsRefLine) {             /* 2d encoding */
515
 
                Fax3CodecState* esp = EncoderState(tif);
516
 
                /*
517
 
                 * 2d encoding requires a scanline
518
 
                 * buffer for the ``reference line''; the
519
 
                 * scanline against which delta encoding
520
 
                 * is referenced.  The reference line must
521
 
                 * be initialized to be ``white'' (done elsewhere).
522
 
                 */
523
 
                esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
524
 
                if (esp->refline == NULL) {
525
 
                        TIFFErrorExt(tif->tif_clientdata, "Fax3SetupState",
526
 
                            "%s: No space for Group 3/4 reference line",
527
 
                            tif->tif_name);
528
 
                        return (0);
529
 
                }
530
 
        } else                                  /* 1d encoding */
531
 
                EncoderState(tif)->refline = NULL;
532
 
 
533
 
        return (1);
534
 
}
535
 
 
536
 
/*
537
 
 * CCITT Group 3 FAX Encoding.
538
 
 */
539
 
 
540
 
#define Fax3FlushBits(tif, sp) {                                \
541
 
        if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)         \
542
 
                (void) TIFFFlushData1(tif);                     \
543
 
        *(tif)->tif_rawcp++ = (tidataval_t) (sp)->data;         \
544
 
        (tif)->tif_rawcc++;                                     \
545
 
        (sp)->data = 0, (sp)->bit = 8;                          \
546
 
}
547
 
#define _FlushBits(tif) {                                       \
548
 
        if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)         \
549
 
                (void) TIFFFlushData1(tif);                     \
550
 
        *(tif)->tif_rawcp++ = (tidataval_t) data;               \
551
 
        (tif)->tif_rawcc++;                                     \
552
 
        data = 0, bit = 8;                                      \
553
 
}
554
 
static const int _msbmask[9] =
555
 
    { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
556
 
#define _PutBits(tif, bits, length) {                           \
557
 
        while (length > bit) {                                  \
558
 
                data |= bits >> (length - bit);                 \
559
 
                length -= bit;                                  \
560
 
                _FlushBits(tif);                                \
561
 
        }                                                       \
562
 
        data |= (bits & _msbmask[length]) << (bit - length);    \
563
 
        bit -= length;                                          \
564
 
        if (bit == 0)                                           \
565
 
                _FlushBits(tif);                                \
566
 
}
567
 
        
568
 
/*
569
 
 * Write a variable-length bit-value to
570
 
 * the output stream.  Values are
571
 
 * assumed to be at most 16 bits.
572
 
 */
573
 
static void
574
 
Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
575
 
{
576
 
        Fax3CodecState* sp = EncoderState(tif);
577
 
        unsigned int bit = sp->bit;
578
 
        int data = sp->data;
579
 
 
580
 
        _PutBits(tif, bits, length);
581
 
 
582
 
        sp->data = data;
583
 
        sp->bit = bit;
584
 
}
585
 
 
586
 
/*
587
 
 * Write a code to the output stream.
588
 
 */
589
 
#define putcode(tif, te)        Fax3PutBits(tif, (te)->code, (te)->length)
590
 
 
591
 
#ifdef FAX3_DEBUG
592
 
#define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
593
 
#define DEBUG_PRINT(what,len) {                                         \
594
 
    int t;                                                              \
595
 
    printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len);    \
596
 
    for (t = length-1; t >= 0; t--)                                     \
597
 
        putchar(code & (1<<t) ? '1' : '0');                             \
598
 
    putchar('\n');                                                      \
599
 
}
600
 
#endif
601
 
 
602
 
/*
603
 
 * Write the sequence of codes that describes
604
 
 * the specified span of zero's or one's.  The
605
 
 * appropriate table that holds the make-up and
606
 
 * terminating codes is supplied.
607
 
 */
608
 
static void
609
 
putspan(TIFF* tif, int32 span, const tableentry* tab)
610
 
{
611
 
        Fax3CodecState* sp = EncoderState(tif);
612
 
        unsigned int bit = sp->bit;
613
 
        int data = sp->data;
614
 
        unsigned int code, length;
615
 
 
616
 
        while (span >= 2624) {
617
 
                const tableentry* te = &tab[63 + (2560>>6)];
618
 
                code = te->code, length = te->length;
619
 
#ifdef FAX3_DEBUG
620
 
                DEBUG_PRINT("MakeUp", te->runlen);
621
 
#endif
622
 
                _PutBits(tif, code, length);
623
 
                span -= te->runlen;
624
 
        }
625
 
        if (span >= 64) {
626
 
                const tableentry* te = &tab[63 + (span>>6)];
627
 
                assert(te->runlen == 64*(span>>6));
628
 
                code = te->code, length = te->length;
629
 
#ifdef FAX3_DEBUG
630
 
                DEBUG_PRINT("MakeUp", te->runlen);
631
 
#endif
632
 
                _PutBits(tif, code, length);
633
 
                span -= te->runlen;
634
 
        }
635
 
        code = tab[span].code, length = tab[span].length;
636
 
#ifdef FAX3_DEBUG
637
 
        DEBUG_PRINT("  Term", tab[span].runlen);
638
 
#endif
639
 
        _PutBits(tif, code, length);
640
 
 
641
 
        sp->data = data;
642
 
        sp->bit = bit;
643
 
}
644
 
 
645
 
/*
646
 
 * Write an EOL code to the output stream.  The zero-fill
647
 
 * logic for byte-aligning encoded scanlines is handled
648
 
 * here.  We also handle writing the tag bit for the next
649
 
 * scanline when doing 2d encoding.
650
 
 */
651
 
static void
652
 
Fax3PutEOL(TIFF* tif)
653
 
{
654
 
        Fax3CodecState* sp = EncoderState(tif);
655
 
        unsigned int bit = sp->bit;
656
 
        int data = sp->data;
657
 
        unsigned int code, length, tparm;
658
 
 
659
 
        if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
660
 
                /*
661
 
                 * Force bit alignment so EOL will terminate on
662
 
                 * a byte boundary.  That is, force the bit alignment
663
 
                 * to 16-12 = 4 before putting out the EOL code.
664
 
                 */
665
 
                int align = 8 - 4;
666
 
                if (align != sp->bit) {
667
 
                        if (align > sp->bit)
668
 
                                align = sp->bit + (8 - align);
669
 
                        else
670
 
                                align = sp->bit - align;
671
 
                        code = 0;
672
 
                        tparm=align; 
673
 
                        _PutBits(tif, 0, tparm);
674
 
                }
675
 
        }
676
 
        code = EOL, length = 12;
677
 
        if (is2DEncoding(sp))
678
 
                code = (code<<1) | (sp->tag == G3_1D), length++;
679
 
        _PutBits(tif, code, length);
680
 
 
681
 
        sp->data = data;
682
 
        sp->bit = bit;
683
 
}
684
 
 
685
 
/*
686
 
 * Reset encoding state at the start of a strip.
687
 
 */
688
 
static int
689
 
Fax3PreEncode(TIFF* tif, tsample_t s)
690
 
{
691
 
        Fax3CodecState* sp = EncoderState(tif);
692
 
 
693
 
        (void) s;
694
 
        assert(sp != NULL);
695
 
        sp->bit = 8;
696
 
        sp->data = 0;
697
 
        sp->tag = G3_1D;
698
 
        /*
699
 
         * This is necessary for Group 4; otherwise it isn't
700
 
         * needed because the first scanline of each strip ends
701
 
         * up being copied into the refline.
702
 
         */
703
 
        if (sp->refline)
704
 
                _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
705
 
        if (is2DEncoding(sp)) {
706
 
                float res = tif->tif_dir.td_yresolution;
707
 
                /*
708
 
                 * The CCITT spec says that when doing 2d encoding, you
709
 
                 * should only do it on K consecutive scanlines, where K
710
 
                 * depends on the resolution of the image being encoded
711
 
                 * (2 for <= 200 lpi, 4 for > 200 lpi).  Since the directory
712
 
                 * code initializes td_yresolution to 0, this code will
713
 
                 * select a K of 2 unless the YResolution tag is set
714
 
                 * appropriately.  (Note also that we fudge a little here
715
 
                 * and use 150 lpi to avoid problems with units conversion.)
716
 
                 */
717
 
                if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
718
 
                        res *= 2.54f;           /* convert to inches */
719
 
                sp->maxk = (res > 150 ? 4 : 2);
720
 
                sp->k = sp->maxk-1;
721
 
        } else
722
 
                sp->k = sp->maxk = 0;
723
 
        sp->line = 0;
724
 
        return (1);
725
 
}
726
 
 
727
 
static const unsigned char zeroruns[256] = {
728
 
    8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,     /* 0x00 - 0x0f */
729
 
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,     /* 0x10 - 0x1f */
730
 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,     /* 0x20 - 0x2f */
731
 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,     /* 0x30 - 0x3f */
732
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x40 - 0x4f */
733
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x50 - 0x5f */
734
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x60 - 0x6f */
735
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x70 - 0x7f */
736
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x80 - 0x8f */
737
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x90 - 0x9f */
738
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xa0 - 0xaf */
739
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xb0 - 0xbf */
740
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xc0 - 0xcf */
741
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xd0 - 0xdf */
742
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xe0 - 0xef */
743
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0xf0 - 0xff */
744
 
};
745
 
static const unsigned char oneruns[256] = {
746
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x00 - 0x0f */
747
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x10 - 0x1f */
748
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x20 - 0x2f */
749
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x30 - 0x3f */
750
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x40 - 0x4f */
751
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x50 - 0x5f */
752
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x60 - 0x6f */
753
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /* 0x70 - 0x7f */
754
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x80 - 0x8f */
755
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0x90 - 0x9f */
756
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0xa0 - 0xaf */
757
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     /* 0xb0 - 0xbf */
758
 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,     /* 0xc0 - 0xcf */
759
 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,     /* 0xd0 - 0xdf */
760
 
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,     /* 0xe0 - 0xef */
761
 
    4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,     /* 0xf0 - 0xff */
762
 
};
763
 
 
764
 
/*
765
 
 * On certain systems it pays to inline
766
 
 * the routines that find pixel spans.
767
 
 */
768
 
#ifdef VAXC
769
 
static  int32 find0span(unsigned char*, int32, int32);
770
 
static  int32 find1span(unsigned char*, int32, int32);
771
 
#pragma inline(find0span,find1span)
772
 
#endif
773
 
 
774
 
/*
775
 
 * Find a span of ones or zeros using the supplied
776
 
 * table.  The ``base'' of the bit string is supplied
777
 
 * along with the start+end bit indices.
778
 
 */
779
 
inline static int32
780
 
find0span(unsigned char* bp, int32 bs, int32 be)
781
 
{
782
 
        int32 bits = be - bs;
783
 
        int32 n, span;
784
 
 
785
 
        bp += bs>>3;
786
 
        /*
787
 
         * Check partial byte on lhs.
788
 
         */
789
 
        if (bits > 0 && (n = (bs & 7))) {
790
 
                span = zeroruns[(*bp << n) & 0xff];
791
 
                if (span > 8-n)         /* table value too generous */
792
 
                        span = 8-n;
793
 
                if (span > bits)        /* constrain span to bit range */
794
 
                        span = bits;
795
 
                if (n+span < 8)         /* doesn't extend to edge of byte */
796
 
                        return (span);
797
 
                bits -= span;
798
 
                bp++;
799
 
        } else
800
 
                span = 0;
801
 
        if (bits >= (int32)(2 * 8 * sizeof(long))) {
802
 
                long* lp;
803
 
                /*
804
 
                 * Align to longword boundary and check longwords.
805
 
                 */
806
 
                while (!isAligned(bp, long)) {
807
 
                        if (*bp != 0x00)
808
 
                                return (span + zeroruns[*bp]);
809
 
                        span += 8, bits -= 8;
810
 
                        bp++;
811
 
                }
812
 
                lp = (long*) bp;
813
 
                while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) {
814
 
                        span += 8*sizeof (long), bits -= 8*sizeof (long);
815
 
                        lp++;
816
 
                }
817
 
                bp = (unsigned char*) lp;
818
 
        }
819
 
        /*
820
 
         * Scan full bytes for all 0's.
821
 
         */
822
 
        while (bits >= 8) {
823
 
                if (*bp != 0x00)        /* end of run */
824
 
                        return (span + zeroruns[*bp]);
825
 
                span += 8, bits -= 8;
826
 
                bp++;
827
 
        }
828
 
        /*
829
 
         * Check partial byte on rhs.
830
 
         */
831
 
        if (bits > 0) {
832
 
                n = zeroruns[*bp];
833
 
                span += (n > bits ? bits : n);
834
 
        }
835
 
        return (span);
836
 
}
837
 
 
838
 
inline static int32
839
 
find1span(unsigned char* bp, int32 bs, int32 be)
840
 
{
841
 
        int32 bits = be - bs;
842
 
        int32 n, span;
843
 
 
844
 
        bp += bs>>3;
845
 
        /*
846
 
         * Check partial byte on lhs.
847
 
         */
848
 
        if (bits > 0 && (n = (bs & 7))) {
849
 
                span = oneruns[(*bp << n) & 0xff];
850
 
                if (span > 8-n)         /* table value too generous */
851
 
                        span = 8-n;
852
 
                if (span > bits)        /* constrain span to bit range */
853
 
                        span = bits;
854
 
                if (n+span < 8)         /* doesn't extend to edge of byte */
855
 
                        return (span);
856
 
                bits -= span;
857
 
                bp++;
858
 
        } else
859
 
                span = 0;
860
 
        if (bits >= (int32)(2 * 8 * sizeof(long))) {
861
 
                long* lp;
862
 
                /*
863
 
                 * Align to longword boundary and check longwords.
864
 
                 */
865
 
                while (!isAligned(bp, long)) {
866
 
                        if (*bp != 0xff)
867
 
                                return (span + oneruns[*bp]);
868
 
                        span += 8, bits -= 8;
869
 
                        bp++;
870
 
                }
871
 
                lp = (long*) bp;
872
 
                while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) {
873
 
                        span += 8*sizeof (long), bits -= 8*sizeof (long);
874
 
                        lp++;
875
 
                }
876
 
                bp = (unsigned char*) lp;
877
 
        }
878
 
        /*
879
 
         * Scan full bytes for all 1's.
880
 
         */
881
 
        while (bits >= 8) {
882
 
                if (*bp != 0xff)        /* end of run */
883
 
                        return (span + oneruns[*bp]);
884
 
                span += 8, bits -= 8;
885
 
                bp++;
886
 
        }
887
 
        /*
888
 
         * Check partial byte on rhs.
889
 
         */
890
 
        if (bits > 0) {
891
 
                n = oneruns[*bp];
892
 
                span += (n > bits ? bits : n);
893
 
        }
894
 
        return (span);
895
 
}
896
 
 
897
 
/*
898
 
 * Return the offset of the next bit in the range
899
 
 * [bs..be] that is different from the specified
900
 
 * color.  The end, be, is returned if no such bit
901
 
 * exists.
902
 
 */
903
 
#define finddiff(_cp, _bs, _be, _color) \
904
 
        (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
905
 
/*
906
 
 * Like finddiff, but also check the starting bit
907
 
 * against the end in case start > end.
908
 
 */
909
 
#define finddiff2(_cp, _bs, _be, _color) \
910
 
        (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
911
 
 
912
 
/*
913
 
 * 1d-encode a row of pixels.  The encoding is
914
 
 * a sequence of all-white or all-black spans
915
 
 * of pixels encoded with Huffman codes.
916
 
 */
917
 
static int
918
 
Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
919
 
{
920
 
        Fax3CodecState* sp = EncoderState(tif);
921
 
        int32 span;
922
 
        uint32 bs = 0;
923
 
 
924
 
        for (;;) {
925
 
                span = find0span(bp, bs, bits);         /* white span */
926
 
                putspan(tif, span, TIFFFaxWhiteCodes);
927
 
                bs += span;
928
 
                if (bs >= bits)
929
 
                        break;
930
 
                span = find1span(bp, bs, bits);         /* black span */
931
 
                putspan(tif, span, TIFFFaxBlackCodes);
932
 
                bs += span;
933
 
                if (bs >= bits)
934
 
                        break;
935
 
        }
936
 
        if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
937
 
                if (sp->bit != 8)                       /* byte-align */
938
 
                        Fax3FlushBits(tif, sp);
939
 
                if ((sp->b.mode&FAXMODE_WORDALIGN) &&
940
 
                    !isAligned(tif->tif_rawcp, uint16))
941
 
                        Fax3FlushBits(tif, sp);
942
 
        }
943
 
        return (1);
944
 
}
945
 
 
946
 
static const tableentry horizcode =
947
 
    { 3, 0x1, 0 };      /* 001 */
948
 
static const tableentry passcode =
949
 
    { 4, 0x1, 0 };      /* 0001 */
950
 
static const tableentry vcodes[7] = {
951
 
    { 7, 0x03, 0 },     /* 0000 011 */
952
 
    { 6, 0x03, 0 },     /* 0000 11 */
953
 
    { 3, 0x03, 0 },     /* 011 */
954
 
    { 1, 0x1, 0 },      /* 1 */
955
 
    { 3, 0x2, 0 },      /* 010 */
956
 
    { 6, 0x02, 0 },     /* 0000 10 */
957
 
    { 7, 0x02, 0 }      /* 0000 010 */
958
 
};
959
 
 
960
 
/*
961
 
 * 2d-encode a row of pixels.  Consult the CCITT
962
 
 * documentation for the algorithm.
963
 
 */
964
 
static int
965
 
Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits)
966
 
{
967
 
#define PIXEL(buf,ix)   ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
968
 
        uint32 a0 = 0;
969
 
        uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
970
 
        uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
971
 
        uint32 a2, b2;
972
 
 
973
 
        for (;;) {
974
 
                b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
975
 
                if (b2 >= a1) {
976
 
                        int32 d = b1 - a1;
977
 
                        if (!(-3 <= d && d <= 3)) {     /* horizontal mode */
978
 
                                a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
979
 
                                putcode(tif, &horizcode);
980
 
                                if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
981
 
                                        putspan(tif, a1-a0, TIFFFaxWhiteCodes);
982
 
                                        putspan(tif, a2-a1, TIFFFaxBlackCodes);
983
 
                                } else {
984
 
                                        putspan(tif, a1-a0, TIFFFaxBlackCodes);
985
 
                                        putspan(tif, a2-a1, TIFFFaxWhiteCodes);
986
 
                                }
987
 
                                a0 = a2;
988
 
                        } else {                        /* vertical mode */
989
 
                                putcode(tif, &vcodes[d+3]);
990
 
                                a0 = a1;
991
 
                        }
992
 
                } else {                                /* pass mode */
993
 
                        putcode(tif, &passcode);
994
 
                        a0 = b2;
995
 
                }
996
 
                if (a0 >= bits)
997
 
                        break;
998
 
                a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
999
 
                b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
1000
 
                b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
1001
 
        }
1002
 
        return (1);
1003
 
#undef PIXEL
1004
 
}
1005
 
 
1006
 
/*
1007
 
 * Encode a buffer of pixels.
1008
 
 */
1009
 
static int
1010
 
Fax3Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
1011
 
{
1012
 
        Fax3CodecState* sp = EncoderState(tif);
1013
 
 
1014
 
        (void) s;
1015
 
        while ((long)cc > 0) {
1016
 
                if ((sp->b.mode & FAXMODE_NOEOL) == 0)
1017
 
                        Fax3PutEOL(tif);
1018
 
                if (is2DEncoding(sp)) {
1019
 
                        if (sp->tag == G3_1D) {
1020
 
                                if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1021
 
                                        return (0);
1022
 
                                sp->tag = G3_2D;
1023
 
                        } else {
1024
 
                                if (!Fax3Encode2DRow(tif, bp, sp->refline,
1025
 
                                                     sp->b.rowpixels))
1026
 
                                        return (0);
1027
 
                                sp->k--;
1028
 
                        }
1029
 
                        if (sp->k == 0) {
1030
 
                                sp->tag = G3_1D;
1031
 
                                sp->k = sp->maxk-1;
1032
 
                        } else
1033
 
                                _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1034
 
                } else {
1035
 
                        if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1036
 
                                return (0);
1037
 
                }
1038
 
                bp += sp->b.rowbytes;
1039
 
                cc -= sp->b.rowbytes;
1040
 
        }
1041
 
        return (1);
1042
 
}
1043
 
 
1044
 
static int
1045
 
Fax3PostEncode(TIFF* tif)
1046
 
{
1047
 
        Fax3CodecState* sp = EncoderState(tif);
1048
 
 
1049
 
        if (sp->bit != 8)
1050
 
                Fax3FlushBits(tif, sp);
1051
 
        return (1);
1052
 
}
1053
 
 
1054
 
static void
1055
 
Fax3Close(TIFF* tif)
1056
 
{
1057
 
        if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0) {
1058
 
                Fax3CodecState* sp = EncoderState(tif);
1059
 
                unsigned int code = EOL;
1060
 
                unsigned int length = 12;
1061
 
                int i;
1062
 
 
1063
 
                if (is2DEncoding(sp))
1064
 
                        code = (code<<1) | (sp->tag == G3_1D), length++;
1065
 
                for (i = 0; i < 6; i++)
1066
 
                        Fax3PutBits(tif, code, length);
1067
 
                Fax3FlushBits(tif, sp);
1068
 
        }
1069
 
}
1070
 
 
1071
 
static void
1072
 
Fax3Cleanup(TIFF* tif)
1073
 
{
1074
 
        Fax3CodecState* sp = DecoderState(tif);
1075
 
        
1076
 
        assert(sp != 0);
1077
 
 
1078
 
        tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
1079
 
        tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
1080
 
        tif->tif_tagmethods.printdir = sp->b.printdir;
1081
 
 
1082
 
        if (sp->runs)
1083
 
                _TIFFfree(sp->runs);
1084
 
        if (sp->refline)
1085
 
                _TIFFfree(sp->refline);
1086
 
 
1087
 
        if (Fax3State(tif)->subaddress)
1088
 
                _TIFFfree(Fax3State(tif)->subaddress);
1089
 
        _TIFFfree(tif->tif_data);
1090
 
        tif->tif_data = NULL;
1091
 
 
1092
 
        _TIFFSetDefaultCompressionState(tif);
1093
 
}
1094
 
 
1095
 
#define FIELD_BADFAXLINES       (FIELD_CODEC+0)
1096
 
#define FIELD_CLEANFAXDATA      (FIELD_CODEC+1)
1097
 
#define FIELD_BADFAXRUN         (FIELD_CODEC+2)
1098
 
#define FIELD_RECVPARAMS        (FIELD_CODEC+3)
1099
 
#define FIELD_SUBADDRESS        (FIELD_CODEC+4)
1100
 
#define FIELD_RECVTIME          (FIELD_CODEC+5)
1101
 
#define FIELD_FAXDCS            (FIELD_CODEC+6)
1102
 
 
1103
 
#define FIELD_OPTIONS           (FIELD_CODEC+7)
1104
 
 
1105
 
static const TIFFFieldInfo faxFieldInfo[] = {
1106
 
    { TIFFTAG_FAXMODE,           0, 0,  TIFF_ANY,       FIELD_PSEUDO,
1107
 
      FALSE,    FALSE,  "FaxMode" },
1108
 
    { TIFFTAG_FAXFILLFUNC,       0, 0,  TIFF_ANY,       FIELD_PSEUDO,
1109
 
      FALSE,    FALSE,  "FaxFillFunc" },
1110
 
    { TIFFTAG_BADFAXLINES,       1, 1,  TIFF_LONG,      FIELD_BADFAXLINES,
1111
 
      TRUE,     FALSE,  "BadFaxLines" },
1112
 
    { TIFFTAG_BADFAXLINES,       1, 1,  TIFF_SHORT,     FIELD_BADFAXLINES,
1113
 
      TRUE,     FALSE,  "BadFaxLines" },
1114
 
    { TIFFTAG_CLEANFAXDATA,      1, 1,  TIFF_SHORT,     FIELD_CLEANFAXDATA,
1115
 
      TRUE,     FALSE,  "CleanFaxData" },
1116
 
    { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_LONG,    FIELD_BADFAXRUN,
1117
 
      TRUE,     FALSE,  "ConsecutiveBadFaxLines" },
1118
 
    { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_SHORT,   FIELD_BADFAXRUN,
1119
 
      TRUE,     FALSE,  "ConsecutiveBadFaxLines" },
1120
 
    { TIFFTAG_FAXRECVPARAMS,     1, 1, TIFF_LONG,       FIELD_RECVPARAMS,
1121
 
      TRUE,     FALSE,  "FaxRecvParams" },
1122
 
    { TIFFTAG_FAXSUBADDRESS,    -1,-1, TIFF_ASCII,      FIELD_SUBADDRESS,
1123
 
      TRUE,     FALSE,  "FaxSubAddress" },
1124
 
    { TIFFTAG_FAXRECVTIME,       1, 1, TIFF_LONG,       FIELD_RECVTIME,
1125
 
      TRUE,     FALSE,  "FaxRecvTime" },
1126
 
    { TIFFTAG_FAXDCS,           -1,-1, TIFF_ASCII,      FIELD_FAXDCS,
1127
 
      TRUE,     FALSE,  "FaxDcs" },
1128
 
};
1129
 
static const TIFFFieldInfo fax3FieldInfo[] = {
1130
 
    { TIFFTAG_GROUP3OPTIONS,     1, 1,  TIFF_LONG,      FIELD_OPTIONS,
1131
 
      FALSE,    FALSE,  "Group3Options" },
1132
 
};
1133
 
static const TIFFFieldInfo fax4FieldInfo[] = {
1134
 
    { TIFFTAG_GROUP4OPTIONS,     1, 1,  TIFF_LONG,      FIELD_OPTIONS,
1135
 
      FALSE,    FALSE,  "Group4Options" },
1136
 
};
1137
 
#define N(a)    (sizeof (a) / sizeof (a[0]))
1138
 
 
1139
 
static int
1140
 
Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
1141
 
{
1142
 
        Fax3BaseState* sp = Fax3State(tif);
1143
 
        const TIFFFieldInfo* fip;
1144
 
 
1145
 
        assert(sp != 0);
1146
 
        assert(sp->vsetparent != 0);
1147
 
 
1148
 
        switch (tag) {
1149
 
        case TIFFTAG_FAXMODE:
1150
 
                sp->mode = va_arg(ap, int);
1151
 
                return 1;                       /* NB: pseudo tag */
1152
 
        case TIFFTAG_FAXFILLFUNC:
1153
 
                DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
1154
 
                return 1;                       /* NB: pseudo tag */
1155
 
        case TIFFTAG_GROUP3OPTIONS:
1156
 
                /* XXX: avoid reading options if compression mismatches. */
1157
 
                if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
1158
 
                        sp->groupoptions = va_arg(ap, uint32);
1159
 
                break;
1160
 
        case TIFFTAG_GROUP4OPTIONS:
1161
 
                /* XXX: avoid reading options if compression mismatches. */
1162
 
                if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
1163
 
                        sp->groupoptions = va_arg(ap, uint32);
1164
 
                break;
1165
 
        case TIFFTAG_BADFAXLINES:
1166
 
                sp->badfaxlines = va_arg(ap, uint32);
1167
 
                break;
1168
 
        case TIFFTAG_CLEANFAXDATA:
1169
 
                sp->cleanfaxdata = (uint16) va_arg(ap, int);
1170
 
                break;
1171
 
        case TIFFTAG_CONSECUTIVEBADFAXLINES:
1172
 
                sp->badfaxrun = va_arg(ap, uint32);
1173
 
                break;
1174
 
        case TIFFTAG_FAXRECVPARAMS:
1175
 
                sp->recvparams = va_arg(ap, uint32);
1176
 
                break;
1177
 
        case TIFFTAG_FAXSUBADDRESS:
1178
 
                _TIFFsetString(&sp->subaddress, va_arg(ap, char*));
1179
 
                break;
1180
 
        case TIFFTAG_FAXRECVTIME:
1181
 
                sp->recvtime = va_arg(ap, uint32);
1182
 
                break;
1183
 
        case TIFFTAG_FAXDCS:
1184
 
                _TIFFsetString(&sp->faxdcs, va_arg(ap, char*));
1185
 
                break;
1186
 
        default:
1187
 
                return (*sp->vsetparent)(tif, tag, ap);
1188
 
        }
1189
 
        
1190
 
        if ((fip = _TIFFFieldWithTag(tif, tag)))
1191
 
                TIFFSetFieldBit(tif, fip->field_bit);
1192
 
        else
1193
 
                return 0;
1194
 
 
1195
 
        tif->tif_flags |= TIFF_DIRTYDIRECT;
1196
 
        return 1;
1197
 
}
1198
 
 
1199
 
static int
1200
 
Fax3VGetField(TIFF* tif, ttag_t tag, va_list ap)
1201
 
{
1202
 
        Fax3BaseState* sp = Fax3State(tif);
1203
 
 
1204
 
        assert(sp != 0);
1205
 
 
1206
 
        switch (tag) {
1207
 
        case TIFFTAG_FAXMODE:
1208
 
                *va_arg(ap, int*) = sp->mode;
1209
 
                break;
1210
 
        case TIFFTAG_FAXFILLFUNC:
1211
 
                *va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
1212
 
                break;
1213
 
        case TIFFTAG_GROUP3OPTIONS:
1214
 
        case TIFFTAG_GROUP4OPTIONS:
1215
 
                *va_arg(ap, uint32*) = sp->groupoptions;
1216
 
                break;
1217
 
        case TIFFTAG_BADFAXLINES:
1218
 
                *va_arg(ap, uint32*) = sp->badfaxlines;
1219
 
                break;
1220
 
        case TIFFTAG_CLEANFAXDATA:
1221
 
                *va_arg(ap, uint16*) = sp->cleanfaxdata;
1222
 
                break;
1223
 
        case TIFFTAG_CONSECUTIVEBADFAXLINES:
1224
 
                *va_arg(ap, uint32*) = sp->badfaxrun;
1225
 
                break;
1226
 
        case TIFFTAG_FAXRECVPARAMS:
1227
 
                *va_arg(ap, uint32*) = sp->recvparams;
1228
 
                break;
1229
 
        case TIFFTAG_FAXSUBADDRESS:
1230
 
                *va_arg(ap, char**) = sp->subaddress;
1231
 
                break;
1232
 
        case TIFFTAG_FAXRECVTIME:
1233
 
                *va_arg(ap, uint32*) = sp->recvtime;
1234
 
                break;
1235
 
        case TIFFTAG_FAXDCS:
1236
 
                *va_arg(ap, char**) = sp->faxdcs;
1237
 
                break;
1238
 
        default:
1239
 
                return (*sp->vgetparent)(tif, tag, ap);
1240
 
        }
1241
 
        return (1);
1242
 
}
1243
 
 
1244
 
static void
1245
 
Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
1246
 
{
1247
 
        Fax3BaseState* sp = Fax3State(tif);
1248
 
 
1249
 
        assert(sp != 0);
1250
 
 
1251
 
        (void) flags;
1252
 
        if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
1253
 
                const char* sep = " ";
1254
 
                if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
1255
 
                        fprintf(fd, "  Group 4 Options:");
1256
 
                        if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
1257
 
                                fprintf(fd, "%suncompressed data", sep);
1258
 
                } else {
1259
 
 
1260
 
                        fprintf(fd, "  Group 3 Options:");
1261
 
                        if (sp->groupoptions & GROUP3OPT_2DENCODING)
1262
 
                                fprintf(fd, "%s2-d encoding", sep), sep = "+";
1263
 
                        if (sp->groupoptions & GROUP3OPT_FILLBITS)
1264
 
                                fprintf(fd, "%sEOL padding", sep), sep = "+";
1265
 
                        if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
1266
 
                                fprintf(fd, "%suncompressed data", sep);
1267
 
                }
1268
 
                fprintf(fd, " (%lu = 0x%lx)\n",
1269
 
                        (unsigned long) sp->groupoptions,
1270
 
                        (unsigned long) sp->groupoptions);
1271
 
        }
1272
 
        if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
1273
 
                fprintf(fd, "  Fax Data:");
1274
 
                switch (sp->cleanfaxdata) {
1275
 
                case CLEANFAXDATA_CLEAN:
1276
 
                        fprintf(fd, " clean");
1277
 
                        break;
1278
 
                case CLEANFAXDATA_REGENERATED:
1279
 
                        fprintf(fd, " receiver regenerated");
1280
 
                        break;
1281
 
                case CLEANFAXDATA_UNCLEAN:
1282
 
                        fprintf(fd, " uncorrected errors");
1283
 
                        break;
1284
 
                }
1285
 
                fprintf(fd, " (%u = 0x%x)\n",
1286
 
                    sp->cleanfaxdata, sp->cleanfaxdata);
1287
 
        }
1288
 
        if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
1289
 
                fprintf(fd, "  Bad Fax Lines: %lu\n",
1290
 
                        (unsigned long) sp->badfaxlines);
1291
 
        if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
1292
 
                fprintf(fd, "  Consecutive Bad Fax Lines: %lu\n",
1293
 
                    (unsigned long) sp->badfaxrun);
1294
 
        if (TIFFFieldSet(tif,FIELD_RECVPARAMS))
1295
 
                fprintf(fd, "  Fax Receive Parameters: %08lx\n",
1296
 
                   (unsigned long) sp->recvparams);
1297
 
        if (TIFFFieldSet(tif,FIELD_SUBADDRESS))
1298
 
                fprintf(fd, "  Fax SubAddress: %s\n", sp->subaddress);
1299
 
        if (TIFFFieldSet(tif,FIELD_RECVTIME))
1300
 
                fprintf(fd, "  Fax Receive Time: %lu secs\n",
1301
 
                    (unsigned long) sp->recvtime);
1302
 
        if (TIFFFieldSet(tif,FIELD_FAXDCS))
1303
 
                fprintf(fd, "  Fax DCS: %s\n", sp->faxdcs);
1304
 
}
1305
 
 
1306
 
static int
1307
 
InitCCITTFax3(TIFF* tif)
1308
 
{
1309
 
        Fax3BaseState* sp;
1310
 
 
1311
 
        /*
1312
 
         * Allocate state block so tag methods have storage to record values.
1313
 
         */
1314
 
        tif->tif_data = (tidata_t)
1315
 
                _TIFFmalloc(sizeof (Fax3CodecState));
1316
 
 
1317
 
        if (tif->tif_data == NULL) {
1318
 
                TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
1319
 
                    "%s: No space for state block", tif->tif_name);
1320
 
                return (0);
1321
 
        }
1322
 
 
1323
 
        sp = Fax3State(tif);
1324
 
        sp->rw_mode = tif->tif_mode;
1325
 
 
1326
 
        /*
1327
 
         * Merge codec-specific tag information and
1328
 
         * override parent get/set field methods.
1329
 
         */
1330
 
        _TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo));
1331
 
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
1332
 
        tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
1333
 
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
1334
 
        tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
1335
 
        sp->printdir = tif->tif_tagmethods.printdir;
1336
 
        tif->tif_tagmethods.printdir = Fax3PrintDir;   /* hook for codec tags */
1337
 
        sp->groupoptions = 0;   
1338
 
        sp->recvparams = 0;
1339
 
        sp->subaddress = NULL;
1340
 
        sp->faxdcs = NULL;
1341
 
 
1342
 
        if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
1343
 
                tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
1344
 
        DecoderState(tif)->runs = NULL;
1345
 
        TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
1346
 
        EncoderState(tif)->refline = NULL;
1347
 
 
1348
 
        /*
1349
 
         * Install codec methods.
1350
 
         */
1351
 
        tif->tif_setupdecode = Fax3SetupState;
1352
 
        tif->tif_predecode = Fax3PreDecode;
1353
 
        tif->tif_decoderow = Fax3Decode1D;
1354
 
        tif->tif_decodestrip = Fax3Decode1D;
1355
 
        tif->tif_decodetile = Fax3Decode1D;
1356
 
        tif->tif_setupencode = Fax3SetupState;
1357
 
        tif->tif_preencode = Fax3PreEncode;
1358
 
        tif->tif_postencode = Fax3PostEncode;
1359
 
        tif->tif_encoderow = Fax3Encode;
1360
 
        tif->tif_encodestrip = Fax3Encode;
1361
 
        tif->tif_encodetile = Fax3Encode;
1362
 
        tif->tif_close = Fax3Close;
1363
 
        tif->tif_cleanup = Fax3Cleanup;
1364
 
 
1365
 
        return (1);
1366
 
}
1367
 
 
1368
 
int
1369
 
TIFFInitCCITTFax3(TIFF* tif, int scheme)
1370
 
{
1371
 
        (void) scheme;
1372
 
        if (InitCCITTFax3(tif)) {
1373
 
                _TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo));
1374
 
 
1375
 
                /*
1376
 
                 * The default format is Class/F-style w/o RTC.
1377
 
                 */
1378
 
                return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
1379
 
        } else
1380
 
                return (0);
1381
 
}
1382
 
 
1383
 
/*
1384
 
 * CCITT Group 4 (T.6) Facsimile-compatible
1385
 
 * Compression Scheme Support.
1386
 
 */
1387
 
 
1388
 
#define SWAP(t,a,b)     { t x; x = (a); (a) = (b); (b) = x; }
1389
 
/*
1390
 
 * Decode the requested amount of G4-encoded data.
1391
 
 */
1392
 
static int
1393
 
Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
1394
 
{
1395
 
        DECLARE_STATE_2D(tif, sp, "Fax4Decode");
1396
 
 
1397
 
        (void) s;
1398
 
        CACHE_STATE(tif, sp);
1399
 
        while ((long)occ > 0) {
1400
 
                a0 = 0;
1401
 
                RunLength = 0;
1402
 
                pa = thisrun = sp->curruns;
1403
 
                pb = sp->refruns;
1404
 
                b1 = *pb++;
1405
 
#ifdef FAX3_DEBUG
1406
 
                printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
1407
 
                printf("-------------------- %d\n", tif->tif_row);
1408
 
                fflush(stdout);
1409
 
#endif
1410
 
                EXPAND2D(EOFG4);
1411
 
                if (EOLcnt)
1412
 
                    goto EOFG4;
1413
 
                (*sp->fill)(buf, thisrun, pa, lastx);
1414
 
                SETVALUE(0);            /* imaginary change for reference */
1415
 
                SWAP(uint32*, sp->curruns, sp->refruns);
1416
 
                buf += sp->b.rowbytes;
1417
 
                occ -= sp->b.rowbytes;
1418
 
                sp->line++;
1419
 
                continue;
1420
 
        EOFG4:
1421
 
                NeedBits16( 13, BADG4 );
1422
 
        BADG4:
1423
 
#ifdef FAX3_DEBUG
1424
 
                if( GetBits(13) != 0x1001 )
1425
 
                    fputs( "Bad RTC\n", stderr );
1426
 
#endif                
1427
 
                ClrBits( 13 );
1428
 
                (*sp->fill)(buf, thisrun, pa, lastx);
1429
 
                UNCACHE_STATE(tif, sp);
1430
 
                return (-1);
1431
 
        }
1432
 
        UNCACHE_STATE(tif, sp);
1433
 
        return (1);
1434
 
}
1435
 
#undef  SWAP
1436
 
 
1437
 
/*
1438
 
 * Encode the requested amount of data.
1439
 
 */
1440
 
static int
1441
 
Fax4Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
1442
 
{
1443
 
        Fax3CodecState *sp = EncoderState(tif);
1444
 
 
1445
 
        (void) s;
1446
 
        while ((long)cc > 0) {
1447
 
                if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
1448
 
                        return (0);
1449
 
                _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1450
 
                bp += sp->b.rowbytes;
1451
 
                cc -= sp->b.rowbytes;
1452
 
        }
1453
 
        return (1);
1454
 
}
1455
 
 
1456
 
static int
1457
 
Fax4PostEncode(TIFF* tif)
1458
 
{
1459
 
        Fax3CodecState *sp = EncoderState(tif);
1460
 
 
1461
 
        /* terminate strip w/ EOFB */
1462
 
        Fax3PutBits(tif, EOL, 12);
1463
 
        Fax3PutBits(tif, EOL, 12);
1464
 
        if (sp->bit != 8)
1465
 
                Fax3FlushBits(tif, sp);
1466
 
        return (1);
1467
 
}
1468
 
 
1469
 
int
1470
 
TIFFInitCCITTFax4(TIFF* tif, int scheme)
1471
 
{
1472
 
        (void) scheme;
1473
 
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
1474
 
                _TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo));
1475
 
 
1476
 
                tif->tif_decoderow = Fax4Decode;
1477
 
                tif->tif_decodestrip = Fax4Decode;
1478
 
                tif->tif_decodetile = Fax4Decode;
1479
 
                tif->tif_encoderow = Fax4Encode;
1480
 
                tif->tif_encodestrip = Fax4Encode;
1481
 
                tif->tif_encodetile = Fax4Encode;
1482
 
                tif->tif_postencode = Fax4PostEncode;
1483
 
                /*
1484
 
                 * Suppress RTC at the end of each strip.
1485
 
                 */
1486
 
                return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
1487
 
        } else
1488
 
                return (0);
1489
 
}
1490
 
 
1491
 
/*
1492
 
 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1493
 
 * (Compression algorithms 2 and 32771)
1494
 
 */
1495
 
 
1496
 
/*
1497
 
 * Decode the requested amount of RLE-encoded data.
1498
 
 */
1499
 
static int
1500
 
Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
1501
 
{
1502
 
        DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
1503
 
        int mode = sp->b.mode;
1504
 
 
1505
 
        (void) s;
1506
 
        CACHE_STATE(tif, sp);
1507
 
        thisrun = sp->curruns;
1508
 
        while ((long)occ > 0) {
1509
 
                a0 = 0;
1510
 
                RunLength = 0;
1511
 
                pa = thisrun;
1512
 
#ifdef FAX3_DEBUG
1513
 
                printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
1514
 
                printf("-------------------- %d\n", tif->tif_row);
1515
 
                fflush(stdout);
1516
 
#endif
1517
 
                EXPAND1D(EOFRLE);
1518
 
                (*sp->fill)(buf, thisrun, pa, lastx);
1519
 
                /*
1520
 
                 * Cleanup at the end of the row.
1521
 
                 */
1522
 
                if (mode & FAXMODE_BYTEALIGN) {
1523
 
                        int n = BitsAvail - (BitsAvail &~ 7);
1524
 
                        ClrBits(n);
1525
 
                } else if (mode & FAXMODE_WORDALIGN) {
1526
 
                        int n = BitsAvail - (BitsAvail &~ 15);
1527
 
                        ClrBits(n);
1528
 
                        if (BitsAvail == 0 && !isAligned(cp, uint16))
1529
 
                            cp++;
1530
 
                }
1531
 
                buf += sp->b.rowbytes;
1532
 
                occ -= sp->b.rowbytes;
1533
 
                sp->line++;
1534
 
                continue;
1535
 
        EOFRLE:                         /* premature EOF */
1536
 
                (*sp->fill)(buf, thisrun, pa, lastx);
1537
 
                UNCACHE_STATE(tif, sp);
1538
 
                return (-1);
1539
 
        }
1540
 
        UNCACHE_STATE(tif, sp);
1541
 
        return (1);
1542
 
}
1543
 
 
1544
 
int
1545
 
TIFFInitCCITTRLE(TIFF* tif, int scheme)
1546
 
{
1547
 
        (void) scheme;
1548
 
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
1549
 
                tif->tif_decoderow = Fax3DecodeRLE;
1550
 
                tif->tif_decodestrip = Fax3DecodeRLE;
1551
 
                tif->tif_decodetile = Fax3DecodeRLE;
1552
 
                /*
1553
 
                 * Suppress RTC+EOLs when encoding and byte-align data.
1554
 
                 */
1555
 
                return TIFFSetField(tif, TIFFTAG_FAXMODE,
1556
 
                    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
1557
 
        } else
1558
 
                return (0);
1559
 
}
1560
 
 
1561
 
int
1562
 
TIFFInitCCITTRLEW(TIFF* tif, int scheme)
1563
 
{
1564
 
        (void) scheme;
1565
 
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
1566
 
                tif->tif_decoderow = Fax3DecodeRLE;
1567
 
                tif->tif_decodestrip = Fax3DecodeRLE;
1568
 
                tif->tif_decodetile = Fax3DecodeRLE;
1569
 
                /*
1570
 
                 * Suppress RTC+EOLs when encoding and word-align data.
1571
 
                 */
1572
 
                return TIFFSetField(tif, TIFFTAG_FAXMODE,
1573
 
                    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
1574
 
        } else
1575
 
                return (0);
1576
 
}
1577
 
#endif /* CCITT_SUPPORT */
1578
 
 
1579
 
/* vim: set ts=8 sts=8 sw=8 noet: */