ReactOS 0.4.16-dev-2574-g474348f
tif_fax3.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1990-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25#include "tiffiop.h"
26#ifdef CCITT_SUPPORT
27/*
28 * TIFF Library.
29 *
30 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
31 *
32 * This file contains support for decoding and encoding TIFF
33 * compression algorithms 2, 3, 4, and 32771.
34 *
35 * Decoder support is derived, with permission, from the code
36 * in Frank Cringle's viewfax program;
37 * Copyright (C) 1990, 1995 Frank D. Cringle.
38 */
39#include "tif_fax3.h"
40#define G3CODES
41#include "t4.h"
42#include <stdio.h>
43
44#ifndef EOF_REACHED_COUNT_THRESHOLD
45/* Arbitrary threshold to avoid corrupted single-strip files with extremely
46 * large imageheight to cause apparently endless looping, such as in
47 * https://gitlab.com/libtiff/libtiff/-/issues/583
48 */
49#define EOF_REACHED_COUNT_THRESHOLD 8192
50#endif
51
52/*
53 * Compression+decompression state blocks are
54 * derived from this ``base state'' block.
55 */
56typedef struct
57{
58 int rw_mode; /* O_RDONLY for decode, else encode */
59 int mode; /* operating mode */
60 tmsize_t rowbytes; /* bytes in a decoded scanline */
61 uint32_t rowpixels; /* pixels in a scanline */
62
63 uint16_t cleanfaxdata; /* CleanFaxData tag */
64 uint32_t badfaxrun; /* BadFaxRun tag */
65 uint32_t badfaxlines; /* BadFaxLines tag */
66 uint32_t groupoptions; /* Group 3/4 options tag */
67
68 TIFFVGetMethod vgetparent; /* super-class method */
69 TIFFVSetMethod vsetparent; /* super-class method */
70 TIFFPrintMethod printdir; /* super-class method */
71} Fax3BaseState;
72#define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data)
73
74typedef enum
75{
76 G3_1D,
77 G3_2D
78} Ttag;
79typedef struct
80{
81 Fax3BaseState b;
82
83 /* Decoder state info */
84 const unsigned char *bitmap; /* bit reversal table */
85 uint32_t data; /* current i/o byte/word */
86 int bit; /* current i/o bit in byte */
87 int EOLcnt; /* count of EOL codes recognized */
88 int eofReachedCount; /* number of times decode has been called with
89 EOF already reached */
90 int eolReachedCount; /* number of times decode has been called with
91 EOL already reached */
92 int unexpectedReachedCount; /* number of times decode has been called with
93 "unexpedted" already reached */
94 TIFFFaxFillFunc fill; /* fill routine */
95 uint32_t *runs; /* b&w runs for current/previous row */
96 uint32_t nruns; /* size of the refruns / curruns arrays */
97 uint32_t *refruns; /* runs for reference line */
98 uint32_t *curruns; /* runs for current line */
99
100 /* Encoder state info */
101 Ttag tag; /* encoding state */
102 unsigned char *refline; /* reference line for 2d decoding */
103 int k; /* #rows left that can be 2d encoded */
104 int maxk; /* max #rows that can be 2d encoded */
105
106 int line;
107} Fax3CodecState;
108#define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif))
109#define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif))
110
111#define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
112#define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0)
113
114/*
115 * Group 3 and Group 4 Decoding.
116 */
117
118/*
119 * These macros glue the TIFF library state to
120 * the state expected by Frank's decoder.
121 */
122#define DECLARE_STATE(tif, sp, mod) \
123 static const char module[] = mod; \
124 Fax3CodecState *sp = DecoderState(tif); \
125 int a0; /* reference element */ \
126 int lastx = sp->b.rowpixels; /* last element in row */ \
127 uint32_t BitAcc; /* bit accumulator */ \
128 int BitsAvail; /* # valid bits in BitAcc */ \
129 int RunLength; /* length of current run */ \
130 unsigned char *cp; /* next byte of input data */ \
131 unsigned char *ep; /* end of input data */ \
132 uint32_t *pa; /* place to stuff next run */ \
133 uint32_t *thisrun; /* current row's run array */ \
134 int EOLcnt; /* # EOL codes recognized */ \
135 const unsigned char *bitmap = sp->bitmap; /* input data bit reverser */ \
136 const TIFFFaxTabEnt *TabEnt
137
138#define DECLARE_STATE_2D(tif, sp, mod) \
139 DECLARE_STATE(tif, sp, mod); \
140 int b1; /* next change on prev line */ \
141 uint32_t \
142 *pb /* next run in reference line */ /* \
143 * Load any state that may be \
144 * changed during decoding. \
145 */
146#define CACHE_STATE(tif, sp) \
147 do \
148 { \
149 BitAcc = sp->data; \
150 BitsAvail = sp->bit; \
151 EOLcnt = sp->EOLcnt; \
152 cp = (unsigned char *)tif->tif_rawcp; \
153 ep = cp + tif->tif_rawcc; \
154 } while (0)
155/*
156 * Save state possibly changed during decoding.
157 */
158#define UNCACHE_STATE(tif, sp) \
159 do \
160 { \
161 sp->bit = BitsAvail; \
162 sp->data = BitAcc; \
163 sp->EOLcnt = EOLcnt; \
164 tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp); \
165 tif->tif_rawcp = (uint8_t *)cp; \
166 } while (0)
167
168/*
169 * Setup state for decoding a strip.
170 */
171static int Fax3PreDecode(TIFF *tif, uint16_t s)
172{
173 Fax3CodecState *sp = DecoderState(tif);
174
175 (void)s;
176 assert(sp != NULL);
177 sp->bit = 0; /* force initial read */
178 sp->data = 0;
179 sp->EOLcnt = 0; /* force initial scan for EOL */
180 sp->eofReachedCount = 0;
181 sp->eolReachedCount = 0;
182 sp->unexpectedReachedCount = 0;
183 /*
184 * Decoder assumes lsb-to-msb bit order. Note that we select
185 * this here rather than in Fax3SetupState so that viewers can
186 * hold the image open, fiddle with the FillOrder tag value,
187 * and then re-decode the image. Otherwise they'd need to close
188 * and open the image to get the state reset.
189 */
190 sp->bitmap =
192 sp->curruns = sp->runs;
193 if (sp->refruns)
194 { /* init reference line to white */
195 sp->refruns = sp->runs + sp->nruns;
196 sp->refruns[0] = (uint32_t)sp->b.rowpixels;
197 sp->refruns[1] = 0;
198 }
199 sp->line = 0;
200 return (1);
201}
202
203/*
204 * Routine for handling various errors/conditions.
205 * Note how they are "glued into the decoder" by
206 * overriding the definitions used by the decoder.
207 */
208
209static void Fax3Unexpected(const char *module, TIFF *tif, uint32_t line,
210 uint32_t a0)
211{
213 "Bad code word at line %" PRIu32 " of %s %" PRIu32
214 " (x %" PRIu32 ")",
215 line, isTiled(tif) ? "tile" : "strip",
216 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);
217}
218#define unexpected(table, a0) \
219 do \
220 { \
221 Fax3Unexpected(module, tif, sp->line, a0); \
222 ++sp->unexpectedReachedCount; \
223 } while (0)
224
225static void Fax3Extension(const char *module, TIFF *tif, uint32_t line,
226 uint32_t a0)
227{
229 "Uncompressed data (not supported) at line %" PRIu32
230 " of %s %" PRIu32 " (x %" PRIu32 ")",
231 line, isTiled(tif) ? "tile" : "strip",
232 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);
233}
234#define extension(a0) Fax3Extension(module, tif, sp->line, a0)
235
236static void Fax3BadLength(const char *module, TIFF *tif, uint32_t line,
237 uint32_t a0, uint32_t lastx)
238{
240 "%s at line %" PRIu32 " of %s %" PRIu32 " (got %" PRIu32
241 ", expected %" PRIu32 ")",
242 a0 < lastx ? "Premature EOL" : "Line length mismatch", line,
243 isTiled(tif) ? "tile" : "strip",
244 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0,
245 lastx);
246}
247#define badlength(a0, lastx) \
248 do \
249 { \
250 Fax3BadLength(module, tif, sp->line, a0, lastx); \
251 ++sp->eolReachedCount; \
252 } while (0)
253
254static void Fax3PrematureEOF(const char *module, TIFF *tif, uint32_t line,
255 uint32_t a0)
256{
258 "Premature EOF at line %" PRIu32 " of %s %" PRIu32
259 " (x %" PRIu32 ")",
260 line, isTiled(tif) ? "tile" : "strip",
261 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);
262}
263#define prematureEOF(a0) \
264 do \
265 { \
266 Fax3PrematureEOF(module, tif, sp->line, a0); \
267 ++sp->eofReachedCount; \
268 } while (0)
269
270static void Fax3TryG3WithoutEOL(const char *module, TIFF *tif, uint32_t line,
271 uint32_t a0)
272{
274 tif, module,
275 "Try to decode (read) fax Group 3 data without EOL at line %" PRIu32
276 " of %s %" PRIu32 " (x %" PRIu32 "). Please check result",
277 line, isTiled(tif) ? "tile" : "strip",
278 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);
279}
280#define tryG3WithoutEOL(a0) \
281 do \
282 { \
283 Fax3TryG3WithoutEOL(module, tif, sp->line, a0); \
284 } while (0)
285
286#define Nop
287
288static int CheckReachedCounters(TIFF *tif, const char *module,
289 Fax3CodecState *sp)
290{
291 if (sp->eofReachedCount >= EOF_REACHED_COUNT_THRESHOLD)
292 {
294 "End of file (EOF) has already been reached %d times "
295 "within that %s.",
296 sp->eofReachedCount, isTiled(tif) ? "tile" : "strip");
297 return (-1);
298 }
299 if (sp->eolReachedCount >= EOF_REACHED_COUNT_THRESHOLD)
300 {
302 "Bad line length (EOL) has already been reached %d times "
303 "within that %s",
304 sp->eolReachedCount, isTiled(tif) ? "tile" : "strip");
305 return (-1);
306 }
307 if (sp->unexpectedReachedCount >= EOF_REACHED_COUNT_THRESHOLD)
308 {
310 "Bad code word (unexpected) has already been reached %d "
311 "times within that %s",
312 sp->unexpectedReachedCount,
313 isTiled(tif) ? "tile" : "strip");
314 return (-1);
315 }
316 return (0);
317}
318
326static int Fax3Decode1D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
327{
328 DECLARE_STATE(tif, sp, "Fax3Decode1D");
329 (void)s;
330 if (occ % sp->b.rowbytes)
331 {
332 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
333 return (-1);
334 }
335 if (CheckReachedCounters(tif, module, sp))
336 return (-1);
337RETRY_WITHOUT_EOL_1D:
338 CACHE_STATE(tif, sp);
339 thisrun = sp->curruns;
340 while (occ > 0)
341 {
342 a0 = 0;
343 RunLength = 0;
344 pa = thisrun;
345#ifdef FAX3_DEBUG
346 printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail);
347 printf("-------------------- %" PRIu32 "\n", tif->tif_row);
348 fflush(stdout);
349#endif
350 SYNC_EOL(EOF1D, RETRY_WITHOUT_EOL_1D);
351 EXPAND1D(EOF1Da);
352 (*sp->fill)(buf, thisrun, pa, lastx);
353 buf += sp->b.rowbytes;
354 occ -= sp->b.rowbytes;
355 sp->line++;
356 continue;
357 EOF1D: /* premature EOF */
358 CLEANUP_RUNS();
359 EOF1Da: /* premature EOF */
360 (*sp->fill)(buf, thisrun, pa, lastx);
361 UNCACHE_STATE(tif, sp);
362 return (-1);
363 }
364 UNCACHE_STATE(tif, sp);
365 return (1);
366}
367
368#define SWAP(t, a, b) \
369 { \
370 t x; \
371 x = (a); \
372 (a) = (b); \
373 (b) = x; \
374 }
375/*
376 * Decode the requested amount of G3 2D-encoded data.
377 */
378static int Fax3Decode2D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
379{
380 DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
381 int is1D; /* current line is 1d/2d-encoded */
382 (void)s;
383 if (occ % sp->b.rowbytes)
384 {
385 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
386 return (-1);
387 }
388 if (CheckReachedCounters(tif, module, sp))
389 return (-1);
390RETRY_WITHOUT_EOL_2D:
391 CACHE_STATE(tif, sp);
392 while (occ > 0)
393 {
394 a0 = 0;
395 RunLength = 0;
396 pa = thisrun = sp->curruns;
397#ifdef FAX3_DEBUG
398 printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d EOLcnt = %d", BitAcc,
399 BitsAvail, EOLcnt);
400#endif
401 SYNC_EOL(EOF2D, RETRY_WITHOUT_EOL_2D);
402 NeedBits8(1, EOF2D);
403 is1D = GetBits(1); /* 1D/2D-encoding tag bit */
404 ClrBits(1);
405#ifdef FAX3_DEBUG
406 printf(" %s\n-------------------- %" PRIu32 "\n", is1D ? "1D" : "2D",
407 tif->tif_row);
408 fflush(stdout);
409#endif
410 pb = sp->refruns;
411 b1 = *pb++;
412 if (is1D)
413 EXPAND1D(EOF2Da);
414 else
415 EXPAND2D(EOF2Da);
416 (*sp->fill)(buf, thisrun, pa, lastx);
417 if (pa < thisrun + sp->nruns)
418 {
419 SETVALUE(0); /* imaginary change for reference */
420 }
421 SWAP(uint32_t *, sp->curruns, sp->refruns);
422 buf += sp->b.rowbytes;
423 occ -= sp->b.rowbytes;
424 sp->line++;
425 continue;
426 EOF2D: /* premature EOF */
427 CLEANUP_RUNS();
428 EOF2Da: /* premature EOF */
429 (*sp->fill)(buf, thisrun, pa, lastx);
430 UNCACHE_STATE(tif, sp);
431 return (-1);
432 }
433 UNCACHE_STATE(tif, sp);
434 return (1);
435}
436#undef SWAP
437
438#define FILL(n, cp) \
439 for (int32_t ifill = 0; ifill < (n); ++ifill) \
440 { \
441 (cp)[ifill] = 0xff; \
442 } \
443 (cp) += (n);
444
445#define ZERO(n, cp) \
446 for (int32_t izero = 0; izero < (n); ++izero) \
447 { \
448 (cp)[izero] = 0; \
449 } \
450 (cp) += (n);
451
452/*
453 * Bit-fill a row according to the white/black
454 * runs generated during G3/G4 decoding.
455 */
456void _TIFFFax3fillruns(unsigned char *buf, uint32_t *runs, uint32_t *erun,
457 uint32_t lastx)
458{
459 static const unsigned char _fillmasks[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
460 0xf8, 0xfc, 0xfe, 0xff};
461 unsigned char *cp;
462 uint32_t x, bx, run;
463 int32_t n, nw;
464 int64_t *lp;
465
466 if ((erun - runs) & 1)
467 *erun++ = 0;
468 x = 0;
469 for (; runs < erun; runs += 2)
470 {
471 run = runs[0];
472 if (x + run > lastx || run > lastx)
473 run = runs[0] = (uint32_t)(lastx - x);
474 if (run)
475 {
476 cp = buf + (x >> 3);
477 bx = x & 7;
478 if (run > 8 - bx)
479 {
480 if (bx)
481 { /* align to byte boundary */
482 *cp++ &= 0xff << (8 - bx);
483 run -= 8 - bx;
484 }
485 if ((n = run >> 3) != 0)
486 { /* multiple bytes to fill */
487 if ((n / sizeof(int64_t)) > 1)
488 {
489 /*
490 * Align to int64_tword boundary and fill.
491 */
492 for (; n && !isAligned(cp, int64_t); n--)
493 *cp++ = 0x00;
494 lp = (int64_t *)cp;
495 nw = (int32_t)(n / sizeof(int64_t));
496 n -= nw * sizeof(int64_t);
497 do
498 {
499 *lp++ = 0L;
500 } while (--nw);
501 cp = (unsigned char *)lp;
502 }
503 ZERO(n, cp);
504 run &= 7;
505 }
506 if (run)
507 cp[0] &= 0xff >> run;
508 }
509 else
510 cp[0] &= ~(_fillmasks[run] >> bx);
511 x += runs[0];
512 }
513 run = runs[1];
514 if (x + run > lastx || run > lastx)
515 run = runs[1] = lastx - x;
516 if (run)
517 {
518 cp = buf + (x >> 3);
519 bx = x & 7;
520 if (run > 8 - bx)
521 {
522 if (bx)
523 { /* align to byte boundary */
524 *cp++ |= 0xff >> bx;
525 run -= 8 - bx;
526 }
527 if ((n = run >> 3) != 0)
528 { /* multiple bytes to fill */
529 if ((n / sizeof(int64_t)) > 1)
530 {
531 /*
532 * Align to int64_t boundary and fill.
533 */
534 for (; n && !isAligned(cp, int64_t); n--)
535 *cp++ = 0xff;
536 lp = (int64_t *)cp;
537 nw = (int32_t)(n / sizeof(int64_t));
538 n -= nw * sizeof(int64_t);
539 do
540 {
541 *lp++ = -1L;
542 } while (--nw);
543 cp = (unsigned char *)lp;
544 }
545 FILL(n, cp);
546 run &= 7;
547 }
548 /* Explicit 0xff masking to make icc -check=conversions happy */
549 if (run)
550 cp[0] = (unsigned char)((cp[0] | (0xff00 >> run)) & 0xff);
551 }
552 else
553 cp[0] |= _fillmasks[run] >> bx;
554 x += runs[1];
555 }
556 }
557 assert(x == lastx);
558}
559#undef ZERO
560#undef FILL
561
562static int Fax3FixupTags(TIFF *tif)
563{
564 (void)tif;
565 return (1);
566}
567
568/*
569 * Setup G3/G4-related compression/decompression state
570 * before data is processed. This routine is called once
571 * per image -- it sets up different state based on whether
572 * or not decoding or encoding is being done and whether
573 * 1D- or 2D-encoded data is involved.
574 */
575static int Fax3SetupState(TIFF *tif)
576{
577 static const char module[] = "Fax3SetupState";
578 TIFFDirectory *td = &tif->tif_dir;
579 Fax3BaseState *sp = Fax3State(tif);
580 int needsRefLine;
581 Fax3CodecState *dsp = (Fax3CodecState *)Fax3State(tif);
582 tmsize_t rowbytes;
583 uint32_t rowpixels;
584
585 if (td->td_bitspersample != 1)
586 {
588 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
589 return (0);
590 }
591 if (td->td_samplesperpixel != 1 &&
593 {
595 tif, module,
596 "Samples/pixel shall be 1 for Group 3/4 encoding/decoding, "
597 "or PlanarConfiguration must be set to Separate.");
598 return 0;
599 }
600 /*
601 * Calculate the scanline/tile widths.
602 */
603 if (isTiled(tif))
604 {
605 rowbytes = TIFFTileRowSize(tif);
606 rowpixels = td->td_tilewidth;
607 }
608 else
609 {
610 rowbytes = TIFFScanlineSize(tif);
611 rowpixels = td->td_imagewidth;
612 }
613 if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8)
614 {
616 "Inconsistent number of bytes per row : rowbytes=%" PRId64
617 " rowpixels=%" PRIu32,
618 (int64_t)rowbytes, rowpixels);
619 return (0);
620 }
621 sp->rowbytes = rowbytes;
622 sp->rowpixels = rowpixels;
623 /*
624 * Allocate any additional space required for decoding/encoding.
625 */
626 needsRefLine = ((sp->groupoptions & GROUP3OPT_2DENCODING) ||
628
629 /*
630 Assure that allocation computations do not overflow.
631
632 TIFFroundup and TIFFSafeMultiply return zero on integer overflow
633 */
634 if (dsp->runs != NULL)
635 {
636 _TIFFfreeExt(tif, dsp->runs);
637 dsp->runs = (uint32_t *)NULL;
638 }
639 dsp->nruns = TIFFroundup_32(rowpixels + 1, 32);
640 if (needsRefLine)
641 {
642 dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2);
643 }
644 if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32_t, dsp->nruns, 2) == 0))
645 {
646 TIFFErrorExtR(tif, tif->tif_name,
647 "Row pixels integer overflow (rowpixels %" PRIu32 ")",
648 rowpixels);
649 return (0);
650 }
651 dsp->runs = (uint32_t *)_TIFFCheckMalloc(
652 tif, TIFFSafeMultiply(uint32_t, dsp->nruns, 2), sizeof(uint32_t),
653 "for Group 3/4 run arrays");
654 if (dsp->runs == NULL)
655 return (0);
656 memset(dsp->runs, 0,
657 TIFFSafeMultiply(uint32_t, dsp->nruns, 2) * sizeof(uint32_t));
658 dsp->curruns = dsp->runs;
659 if (needsRefLine)
660 dsp->refruns = dsp->runs + dsp->nruns;
661 else
662 dsp->refruns = NULL;
663 if (td->td_compression == COMPRESSION_CCITTFAX3 && is2DEncoding(dsp))
664 { /* NB: default is 1D routine */
665 tif->tif_decoderow = Fax3Decode2D;
666 tif->tif_decodestrip = Fax3Decode2D;
667 tif->tif_decodetile = Fax3Decode2D;
668 }
669
670 if (needsRefLine)
671 { /* 2d encoding */
672 Fax3CodecState *esp = EncoderState(tif);
673 /*
674 * 2d encoding requires a scanline
675 * buffer for the ``reference line''; the
676 * scanline against which delta encoding
677 * is referenced. The reference line must
678 * be initialized to be ``white'' (done elsewhere).
679 */
680 if (esp->refline != NULL)
681 {
682 _TIFFfreeExt(tif, esp->refline);
683 }
684 esp->refline = (unsigned char *)_TIFFmallocExt(tif, rowbytes);
685 if (esp->refline == NULL)
686 {
687 TIFFErrorExtR(tif, module, "No space for Group 3/4 reference line");
688 return (0);
689 }
690 }
691 else /* 1d encoding */
692 EncoderState(tif)->refline = NULL;
693
694 return (1);
695}
696
697/*
698 * CCITT Group 3 FAX Encoding.
699 */
700
701#define Fax3FlushBits(tif, sp) \
702 { \
703 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
704 { \
705 if (!TIFFFlushData1(tif)) \
706 return 0; \
707 } \
708 *(tif)->tif_rawcp++ = (uint8_t)(sp)->data; \
709 (tif)->tif_rawcc++; \
710 (sp)->data = 0, (sp)->bit = 8; \
711 }
712#define _FlushBits(tif) \
713 { \
714 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
715 { \
716 if (!TIFFFlushData1(tif)) \
717 return 0; \
718 } \
719 *(tif)->tif_rawcp++ = (uint8_t)data; \
720 (tif)->tif_rawcc++; \
721 data = 0, bit = 8; \
722 }
723static const int _msbmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f,
724 0x1f, 0x3f, 0x7f, 0xff};
725#define _PutBits(tif, bits, length) \
726 { \
727 while (length > bit) \
728 { \
729 data |= bits >> (length - bit); \
730 length -= bit; \
731 _FlushBits(tif); \
732 } \
733 assert(length < 9); \
734 data |= (bits & _msbmask[length]) << (bit - length); \
735 bit -= length; \
736 if (bit == 0) \
737 _FlushBits(tif); \
738 }
739
740/*
741 * Write a variable-length bit-value to
742 * the output stream. Values are
743 * assumed to be at most 16 bits.
744 */
745static int Fax3PutBits(TIFF *tif, unsigned int bits, unsigned int length)
746{
747 Fax3CodecState *sp = EncoderState(tif);
748 unsigned int bit = sp->bit;
749 int data = sp->data;
750
751 _PutBits(tif, bits, length);
752
753 sp->data = data;
754 sp->bit = bit;
755 return 1;
756}
757
758/*
759 * Write a code to the output stream.
760 */
761#define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
762
763#ifdef FAX3_DEBUG
764#define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
765#define DEBUG_PRINT(what, len) \
766 { \
767 int t; \
768 printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), \
769 len); \
770 for (t = length - 1; t >= 0; t--) \
771 putchar(code & (1 << t) ? '1' : '0'); \
772 putchar('\n'); \
773 }
774#endif
775
776/*
777 * Write the sequence of codes that describes
778 * the specified span of zero's or one's. The
779 * appropriate table that holds the make-up and
780 * terminating codes is supplied.
781 */
782static int putspan(TIFF *tif, int32_t span, const tableentry *tab)
783{
784 Fax3CodecState *sp = EncoderState(tif);
785 unsigned int bit = sp->bit;
786 int data = sp->data;
787 unsigned int code, length;
788
789 while (span >= 2624)
790 {
791 const tableentry *te = &tab[63 + (2560 >> 6)];
792 code = te->code;
793 length = te->length;
794#ifdef FAX3_DEBUG
795 DEBUG_PRINT("MakeUp", te->runlen);
796#endif
797 _PutBits(tif, code, length);
798 span -= te->runlen;
799 }
800 if (span >= 64)
801 {
802 const tableentry *te = &tab[63 + (span >> 6)];
803 assert(te->runlen == 64 * (span >> 6));
804 code = te->code;
805 length = te->length;
806#ifdef FAX3_DEBUG
807 DEBUG_PRINT("MakeUp", te->runlen);
808#endif
809 _PutBits(tif, code, length);
810 span -= te->runlen;
811 }
812 code = tab[span].code;
813 length = tab[span].length;
814#ifdef FAX3_DEBUG
815 DEBUG_PRINT(" Term", tab[span].runlen);
816#endif
817 _PutBits(tif, code, length);
818
819 sp->data = data;
820 sp->bit = bit;
821
822 return 1;
823}
824
825/*
826 * Write an EOL code to the output stream. The zero-fill
827 * logic for byte-aligning encoded scanlines is handled
828 * here. We also handle writing the tag bit for the next
829 * scanline when doing 2d encoding.
830 */
831static int Fax3PutEOL(TIFF *tif)
832{
833 Fax3CodecState *sp = EncoderState(tif);
834 unsigned int bit = sp->bit;
835 int data = sp->data;
836 unsigned int code, length, tparm;
837
838 if (sp->b.groupoptions & GROUP3OPT_FILLBITS)
839 {
840 /*
841 * Force bit alignment so EOL will terminate on
842 * a byte boundary. That is, force the bit alignment
843 * to 16-12 = 4 before putting out the EOL code.
844 */
845 int align = 8 - 4;
846 if (align != sp->bit)
847 {
848 if (align > sp->bit)
849 align = sp->bit + (8 - align);
850 else
851 align = sp->bit - align;
852 tparm = align;
853 _PutBits(tif, 0, tparm);
854 }
855 }
856 code = EOL;
857 length = 12;
858 if (is2DEncoding(sp))
859 {
860 code = (code << 1) | (sp->tag == G3_1D);
861 length++;
862 }
863 _PutBits(tif, code, length);
864
865 sp->data = data;
866 sp->bit = bit;
867
868 return 1;
869}
870
871/*
872 * Reset encoding state at the start of a strip.
873 */
874static int Fax3PreEncode(TIFF *tif, uint16_t s)
875{
876 Fax3CodecState *sp = EncoderState(tif);
877
878 (void)s;
879 assert(sp != NULL);
880 sp->bit = 8;
881 sp->data = 0;
882 sp->tag = G3_1D;
883 /*
884 * This is necessary for Group 4; otherwise it isn't
885 * needed because the first scanline of each strip ends
886 * up being copied into the refline.
887 */
888 if (sp->refline)
889 _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
890 if (is2DEncoding(sp))
891 {
892 float res = tif->tif_dir.td_yresolution;
893 /*
894 * The CCITT spec says that when doing 2d encoding, you
895 * should only do it on K consecutive scanlines, where K
896 * depends on the resolution of the image being encoded
897 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
898 * code initializes td_yresolution to 0, this code will
899 * select a K of 2 unless the YResolution tag is set
900 * appropriately. (Note also that we fudge a little here
901 * and use 150 lpi to avoid problems with units conversion.)
902 */
904 res *= 2.54f; /* convert to inches */
905 sp->maxk = (res > 150 ? 4 : 2);
906 sp->k = sp->maxk - 1;
907 }
908 else
909 sp->k = sp->maxk = 0;
910 sp->line = 0;
911 return (1);
912}
913
914static const unsigned char zeroruns[256] = {
915 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
916 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
917 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
918 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
919 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
920 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
921 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
922 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
923 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
924 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
925 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
926 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
927 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
928 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
930 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
931};
932static const unsigned char oneruns[256] = {
933 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
934 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
935 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
936 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
937 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
938 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
939 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
940 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
941 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
942 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
943 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
944 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
945 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
946 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
947 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
948 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
949};
950
951/*
952 * Find a span of ones or zeros using the supplied
953 * table. The ``base'' of the bit string is supplied
954 * along with the start+end bit indices.
955 */
956static inline int32_t find0span(unsigned char *bp, int32_t bs, int32_t be)
957{
958 int32_t bits = be - bs;
959 int32_t n, span;
960
961 bp += bs >> 3;
962 /*
963 * Check partial byte on lhs.
964 */
965 if (bits > 0 && (n = (bs & 7)) != 0)
966 {
967 span = zeroruns[(*bp << n) & 0xff];
968 if (span > 8 - n) /* table value too generous */
969 span = 8 - n;
970 if (span > bits) /* constrain span to bit range */
971 span = bits;
972 if (n + span < 8) /* doesn't extend to edge of byte */
973 return (span);
974 bits -= span;
975 bp++;
976 }
977 else
978 span = 0;
979 if (bits >= (int32_t)(2 * 8 * sizeof(int64_t)))
980 {
981 int64_t *lp;
982 /*
983 * Align to int64_t boundary and check int64_t words.
984 */
985 while (!isAligned(bp, int64_t))
986 {
987 if (*bp != 0x00)
988 return (span + zeroruns[*bp]);
989 span += 8;
990 bits -= 8;
991 bp++;
992 }
993 lp = (int64_t *)bp;
994 while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp))
995 {
996 span += 8 * sizeof(int64_t);
997 bits -= 8 * sizeof(int64_t);
998 lp++;
999 }
1000 bp = (unsigned char *)lp;
1001 }
1002 /*
1003 * Scan full bytes for all 0's.
1004 */
1005 while (bits >= 8)
1006 {
1007 if (*bp != 0x00) /* end of run */
1008 return (span + zeroruns[*bp]);
1009 span += 8;
1010 bits -= 8;
1011 bp++;
1012 }
1013 /*
1014 * Check partial byte on rhs.
1015 */
1016 if (bits > 0)
1017 {
1018 n = zeroruns[*bp];
1019 span += (n > bits ? bits : n);
1020 }
1021 return (span);
1022}
1023
1024static inline int32_t find1span(unsigned char *bp, int32_t bs, int32_t be)
1025{
1026 int32_t bits = be - bs;
1027 int32_t n, span;
1028
1029 bp += bs >> 3;
1030 /*
1031 * Check partial byte on lhs.
1032 */
1033 if (bits > 0 && (n = (bs & 7)) != 0)
1034 {
1035 span = oneruns[(*bp << n) & 0xff];
1036 if (span > 8 - n) /* table value too generous */
1037 span = 8 - n;
1038 if (span > bits) /* constrain span to bit range */
1039 span = bits;
1040 if (n + span < 8) /* doesn't extend to edge of byte */
1041 return (span);
1042 bits -= span;
1043 bp++;
1044 }
1045 else
1046 span = 0;
1047 if (bits >= (int32_t)(2 * 8 * sizeof(int64_t)))
1048 {
1049 int64_t *lp;
1050 /*
1051 * Align to int64_t boundary and check int64_t words.
1052 */
1053 while (!isAligned(bp, int64_t))
1054 {
1055 if (*bp != 0xff)
1056 return (span + oneruns[*bp]);
1057 span += 8;
1058 bits -= 8;
1059 bp++;
1060 }
1061 lp = (int64_t *)bp;
1062 while ((bits >= (int32_t)(8 * sizeof(int64_t))) &&
1063 (~((uint64_t)0) == (uint64_t)*lp))
1064 {
1065 span += 8 * sizeof(int64_t);
1066 bits -= 8 * sizeof(int64_t);
1067 lp++;
1068 }
1069 bp = (unsigned char *)lp;
1070 }
1071 /*
1072 * Scan full bytes for all 1's.
1073 */
1074 while (bits >= 8)
1075 {
1076 if (*bp != 0xff) /* end of run */
1077 return (span + oneruns[*bp]);
1078 span += 8;
1079 bits -= 8;
1080 bp++;
1081 }
1082 /*
1083 * Check partial byte on rhs.
1084 */
1085 if (bits > 0)
1086 {
1087 n = oneruns[*bp];
1088 span += (n > bits ? bits : n);
1089 }
1090 return (span);
1091}
1092
1093/*
1094 * Return the offset of the next bit in the range
1095 * [bs..be] that is different from the specified
1096 * color. The end, be, is returned if no such bit
1097 * exists.
1098 */
1099#define finddiff(_cp, _bs, _be, _color) \
1100 (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be)))
1101/*
1102 * Like finddiff, but also check the starting bit
1103 * against the end in case start > end.
1104 */
1105#define finddiff2(_cp, _bs, _be, _color) \
1106 (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be)
1107
1108/*
1109 * 1d-encode a row of pixels. The encoding is
1110 * a sequence of all-white or all-black spans
1111 * of pixels encoded with Huffman codes.
1112 */
1113static int Fax3Encode1DRow(TIFF *tif, unsigned char *bp, uint32_t bits)
1114{
1115 Fax3CodecState *sp = EncoderState(tif);
1116 int32_t span;
1117 uint32_t bs = 0;
1118
1119 for (;;)
1120 {
1121 span = find0span(bp, bs, bits); /* white span */
1122 if (!putspan(tif, span, TIFFFaxWhiteCodes))
1123 return 0;
1124 bs += span;
1125 if (bs >= bits)
1126 break;
1127 span = find1span(bp, bs, bits); /* black span */
1128 if (!putspan(tif, span, TIFFFaxBlackCodes))
1129 return 0;
1130 bs += span;
1131 if (bs >= bits)
1132 break;
1133 }
1134 if (sp->b.mode & (FAXMODE_BYTEALIGN | FAXMODE_WORDALIGN))
1135 {
1136 if (sp->bit != 8) /* byte-align */
1137 Fax3FlushBits(tif, sp);
1138 if ((sp->b.mode & FAXMODE_WORDALIGN) &&
1139 !isAligned(tif->tif_rawcp, uint16_t))
1140 Fax3FlushBits(tif, sp);
1141 }
1142 return (1);
1143}
1144
1145static const tableentry horizcode = {3, 0x1, 0}; /* 001 */
1146static const tableentry passcode = {4, 0x1, 0}; /* 0001 */
1147static const tableentry vcodes[7] = {
1148 {7, 0x03, 0}, /* 0000 011 */
1149 {6, 0x03, 0}, /* 0000 11 */
1150 {3, 0x03, 0}, /* 011 */
1151 {1, 0x1, 0}, /* 1 */
1152 {3, 0x2, 0}, /* 010 */
1153 {6, 0x02, 0}, /* 0000 10 */
1154 {7, 0x02, 0} /* 0000 010 */
1155};
1156
1157/*
1158 * 2d-encode a row of pixels. Consult the CCITT
1159 * documentation for the algorithm.
1160 */
1161static int Fax3Encode2DRow(TIFF *tif, unsigned char *bp, unsigned char *rp,
1162 uint32_t bits)
1163{
1164#define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix)&7))) & 1)
1165 uint32_t a0 = 0;
1166 uint32_t a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
1167 uint32_t b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
1168 uint32_t a2, b2;
1169
1170 for (;;)
1171 {
1172 b2 = finddiff2(rp, b1, bits, PIXEL(rp, b1));
1173 if (b2 >= a1)
1174 {
1175 /* Naive computation triggers
1176 * -fsanitize=undefined,unsigned-integer-overflow */
1177 /* although it is correct unless the difference between both is < 31
1178 * bit */
1179 /* int32_t d = b1 - a1; */
1180 int32_t d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32_t)(b1 - a1)
1181 : (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1)
1182 : 0x7FFFFFFF;
1183 if (!(-3 <= d && d <= 3))
1184 { /* horizontal mode */
1185 a2 = finddiff2(bp, a1, bits, PIXEL(bp, a1));
1186 if (!putcode(tif, &horizcode))
1187 return 0;
1188 if (a0 + a1 == 0 || PIXEL(bp, a0) == 0)
1189 {
1190 if (!putspan(tif, a1 - a0, TIFFFaxWhiteCodes))
1191 return 0;
1192 if (!putspan(tif, a2 - a1, TIFFFaxBlackCodes))
1193 return 0;
1194 }
1195 else
1196 {
1197 if (!putspan(tif, a1 - a0, TIFFFaxBlackCodes))
1198 return 0;
1199 if (!putspan(tif, a2 - a1, TIFFFaxWhiteCodes))
1200 return 0;
1201 }
1202 a0 = a2;
1203 }
1204 else
1205 { /* vertical mode */
1206 if (!putcode(tif, &vcodes[d + 3]))
1207 return 0;
1208 a0 = a1;
1209 }
1210 }
1211 else
1212 { /* pass mode */
1213 if (!putcode(tif, &passcode))
1214 return 0;
1215 a0 = b2;
1216 }
1217 if (a0 >= bits)
1218 break;
1219 a1 = finddiff(bp, a0, bits, PIXEL(bp, a0));
1220 b1 = finddiff(rp, a0, bits, !PIXEL(bp, a0));
1221 b1 = finddiff(rp, b1, bits, PIXEL(bp, a0));
1222 }
1223 return (1);
1224#undef PIXEL
1225}
1226
1227/*
1228 * Encode a buffer of pixels.
1229 */
1230static int Fax3Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1231{
1232 static const char module[] = "Fax3Encode";
1233 Fax3CodecState *sp = EncoderState(tif);
1234 (void)s;
1235 if (cc % sp->b.rowbytes)
1236 {
1237 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written");
1238 return (0);
1239 }
1240 while (cc > 0)
1241 {
1242 if ((sp->b.mode & FAXMODE_NOEOL) == 0)
1243 {
1244 if (!Fax3PutEOL(tif))
1245 return 0;
1246 }
1247 if (is2DEncoding(sp))
1248 {
1249 if (sp->tag == G3_1D)
1250 {
1251 if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1252 return (0);
1253 sp->tag = G3_2D;
1254 }
1255 else
1256 {
1257 if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
1258 return (0);
1259 sp->k--;
1260 }
1261 if (sp->k == 0)
1262 {
1263 sp->tag = G3_1D;
1264 sp->k = sp->maxk - 1;
1265 }
1266 else
1267 _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1268 }
1269 else
1270 {
1271 if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1272 return (0);
1273 }
1274 bp += sp->b.rowbytes;
1275 cc -= sp->b.rowbytes;
1276 }
1277 return (1);
1278}
1279
1280static int Fax3PostEncode(TIFF *tif)
1281{
1282 Fax3CodecState *sp = EncoderState(tif);
1283
1284 if (sp->bit != 8)
1285 Fax3FlushBits(tif, sp);
1286 return (1);
1287}
1288
1289static int _Fax3Close(TIFF *tif)
1290{
1291 if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp)
1292 {
1293 Fax3CodecState *sp = EncoderState(tif);
1294 unsigned int code = EOL;
1295 unsigned int length = 12;
1296 int i;
1297
1298 if (is2DEncoding(sp))
1299 {
1300 code = (code << 1) | (sp->tag == G3_1D);
1301 length++;
1302 }
1303 for (i = 0; i < 6; i++)
1304 Fax3PutBits(tif, code, length);
1305 Fax3FlushBits(tif, sp);
1306 }
1307 return 1;
1308}
1309
1310static void Fax3Close(TIFF *tif) { _Fax3Close(tif); }
1311
1312static void Fax3Cleanup(TIFF *tif)
1313{
1314 Fax3CodecState *sp = DecoderState(tif);
1315
1316 assert(sp != 0);
1317
1318 tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
1319 tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
1320 tif->tif_tagmethods.printdir = sp->b.printdir;
1321
1322 if (sp->runs)
1323 _TIFFfreeExt(tif, sp->runs);
1324 if (sp->refline)
1325 _TIFFfreeExt(tif, sp->refline);
1326
1327 _TIFFfreeExt(tif, tif->tif_data);
1328 tif->tif_data = NULL;
1329
1331}
1332
1333#define FIELD_BADFAXLINES (FIELD_CODEC + 0)
1334#define FIELD_CLEANFAXDATA (FIELD_CODEC + 1)
1335#define FIELD_BADFAXRUN (FIELD_CODEC + 2)
1336
1337#define FIELD_OPTIONS (FIELD_CODEC + 7)
1338
1339static const TIFFField faxFields[] = {
1341 FALSE, "FaxMode", NULL},
1343 FALSE, FALSE, "FaxFillFunc", NULL},
1345 FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL},
1347 FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL},
1349 FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL}};
1350static const TIFFField fax3Fields[] = {
1352 FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL},
1353};
1354static const TIFFField fax4Fields[] = {
1356 FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL},
1357};
1358
1359static int Fax3VSetField(TIFF *tif, uint32_t tag, va_list ap)
1360{
1361 Fax3BaseState *sp = Fax3State(tif);
1362 const TIFFField *fip;
1363
1364 assert(sp != 0);
1365 assert(sp->vsetparent != 0);
1366
1367 switch (tag)
1368 {
1369 case TIFFTAG_FAXMODE:
1370 sp->mode = (int)va_arg(ap, int);
1371 return 1; /* NB: pseudo tag */
1373 DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
1374 return 1; /* NB: pseudo tag */
1376 /* XXX: avoid reading options if compression mismatches. */
1378 sp->groupoptions = (uint32_t)va_arg(ap, uint32_t);
1379 break;
1381 /* XXX: avoid reading options if compression mismatches. */
1383 sp->groupoptions = (uint32_t)va_arg(ap, uint32_t);
1384 break;
1386 sp->badfaxlines = (uint32_t)va_arg(ap, uint32_t);
1387 break;
1389 sp->cleanfaxdata = (uint16_t)va_arg(ap, uint16_vap);
1390 break;
1392 sp->badfaxrun = (uint32_t)va_arg(ap, uint32_t);
1393 break;
1394 default:
1395 return (*sp->vsetparent)(tif, tag, ap);
1396 }
1397
1398 if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
1399 TIFFSetFieldBit(tif, fip->field_bit);
1400 else
1401 return 0;
1402
1404 return 1;
1405}
1406
1407static int Fax3VGetField(TIFF *tif, uint32_t tag, va_list ap)
1408{
1409 Fax3BaseState *sp = Fax3State(tif);
1410
1411 assert(sp != 0);
1412
1413 switch (tag)
1414 {
1415 case TIFFTAG_FAXMODE:
1416 *va_arg(ap, int *) = sp->mode;
1417 break;
1419 *va_arg(ap, TIFFFaxFillFunc *) = DecoderState(tif)->fill;
1420 break;
1423 *va_arg(ap, uint32_t *) = sp->groupoptions;
1424 break;
1426 *va_arg(ap, uint32_t *) = sp->badfaxlines;
1427 break;
1429 *va_arg(ap, uint16_t *) = sp->cleanfaxdata;
1430 break;
1432 *va_arg(ap, uint32_t *) = sp->badfaxrun;
1433 break;
1434 default:
1435 return (*sp->vgetparent)(tif, tag, ap);
1436 }
1437 return (1);
1438}
1439
1440static void Fax3PrintDir(TIFF *tif, FILE *fd, long flags)
1441{
1442 Fax3BaseState *sp = Fax3State(tif);
1443
1444 assert(sp != 0);
1445
1446 (void)flags;
1447 if (TIFFFieldSet(tif, FIELD_OPTIONS))
1448 {
1449 const char *sep = " ";
1451 {
1452 fprintf(fd, " Group 4 Options:");
1453 if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
1454 fprintf(fd, "%suncompressed data", sep);
1455 }
1456 else
1457 {
1458
1459 fprintf(fd, " Group 3 Options:");
1460 if (sp->groupoptions & GROUP3OPT_2DENCODING)
1461 {
1462 fprintf(fd, "%s2-d encoding", sep);
1463 sep = "+";
1464 }
1465 if (sp->groupoptions & GROUP3OPT_FILLBITS)
1466 {
1467 fprintf(fd, "%sEOL padding", sep);
1468 sep = "+";
1469 }
1470 if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
1471 fprintf(fd, "%suncompressed data", sep);
1472 }
1473 fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", sp->groupoptions,
1474 sp->groupoptions);
1475 }
1476 if (TIFFFieldSet(tif, FIELD_CLEANFAXDATA))
1477 {
1478 fprintf(fd, " Fax Data:");
1479 switch (sp->cleanfaxdata)
1480 {
1481 case CLEANFAXDATA_CLEAN:
1482 fprintf(fd, " clean");
1483 break;
1485 fprintf(fd, " receiver regenerated");
1486 break;
1488 fprintf(fd, " uncorrected errors");
1489 break;
1490 }
1491 fprintf(fd, " (%" PRIu16 " = 0x%" PRIx16 ")\n", sp->cleanfaxdata,
1492 sp->cleanfaxdata);
1493 }
1494 if (TIFFFieldSet(tif, FIELD_BADFAXLINES))
1495 fprintf(fd, " Bad Fax Lines: %" PRIu32 "\n", sp->badfaxlines);
1496 if (TIFFFieldSet(tif, FIELD_BADFAXRUN))
1497 fprintf(fd, " Consecutive Bad Fax Lines: %" PRIu32 "\n",
1498 sp->badfaxrun);
1499 if (sp->printdir)
1500 (*sp->printdir)(tif, fd, flags);
1501}
1502
1503static int InitCCITTFax3(TIFF *tif)
1504{
1505 static const char module[] = "InitCCITTFax3";
1506 Fax3BaseState *sp;
1507
1508 /*
1509 * Merge codec-specific tag information.
1510 */
1511 if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields)))
1512 {
1513 TIFFErrorExtR(tif, "InitCCITTFax3",
1514 "Merging common CCITT Fax codec-specific tags failed");
1515 return 0;
1516 }
1517
1518 /*
1519 * Allocate state block so tag methods have storage to record values.
1520 */
1521 tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(Fax3CodecState));
1522
1523 if (tif->tif_data == NULL)
1524 {
1525 TIFFErrorExtR(tif, module, "No space for state block");
1526 return (0);
1527 }
1528 _TIFFmemset(tif->tif_data, 0, sizeof(Fax3CodecState));
1529
1530 sp = Fax3State(tif);
1531 sp->rw_mode = tif->tif_mode;
1532
1533 /*
1534 * Override parent get/set field methods.
1535 */
1536 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1537 tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
1538 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1539 tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
1540 sp->printdir = tif->tif_tagmethods.printdir;
1541 tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */
1542 sp->groupoptions = 0;
1543
1544 if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
1545 tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
1546 DecoderState(tif)->runs = NULL;
1548 EncoderState(tif)->refline = NULL;
1549
1550 /*
1551 * Install codec methods.
1552 */
1553 tif->tif_fixuptags = Fax3FixupTags;
1554 tif->tif_setupdecode = Fax3SetupState;
1555 tif->tif_predecode = Fax3PreDecode;
1556 tif->tif_decoderow = Fax3Decode1D;
1557 tif->tif_decodestrip = Fax3Decode1D;
1558 tif->tif_decodetile = Fax3Decode1D;
1559 tif->tif_setupencode = Fax3SetupState;
1560 tif->tif_preencode = Fax3PreEncode;
1561 tif->tif_postencode = Fax3PostEncode;
1562 tif->tif_encoderow = Fax3Encode;
1563 tif->tif_encodestrip = Fax3Encode;
1564 tif->tif_encodetile = Fax3Encode;
1565 tif->tif_close = Fax3Close;
1566 tif->tif_cleanup = Fax3Cleanup;
1567
1568 return (1);
1569}
1570
1571int TIFFInitCCITTFax3(TIFF *tif, int scheme)
1572{
1573 (void)scheme;
1574 if (InitCCITTFax3(tif))
1575 {
1576 /*
1577 * Merge codec-specific tag information.
1578 */
1579 if (!_TIFFMergeFields(tif, fax3Fields, TIFFArrayCount(fax3Fields)))
1580 {
1581 TIFFErrorExtR(tif, "TIFFInitCCITTFax3",
1582 "Merging CCITT Fax 3 codec-specific tags failed");
1583 return 0;
1584 }
1585
1586 /*
1587 * The default format is Class/F-style w/o RTC.
1588 */
1590 }
1591 else
1592 return 01;
1593}
1594
1595/*
1596 * CCITT Group 4 (T.6) Facsimile-compatible
1597 * Compression Scheme Support.
1598 */
1599
1600#define SWAP(t, a, b) \
1601 { \
1602 t x; \
1603 x = (a); \
1604 (a) = (b); \
1605 (b) = x; \
1606 }
1607/*
1608 * Decode the requested amount of G4-encoded data.
1609 */
1610static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
1611{
1612 DECLARE_STATE_2D(tif, sp, "Fax4Decode");
1613 (void)s;
1614 if (occ % sp->b.rowbytes)
1615 {
1616 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
1617 return (-1);
1618 }
1619 if (CheckReachedCounters(tif, module, sp))
1620 return (-1);
1621 CACHE_STATE(tif, sp);
1622 int start = sp->line;
1623 while (occ > 0)
1624 {
1625 a0 = 0;
1626 RunLength = 0;
1627 pa = thisrun = sp->curruns;
1628 pb = sp->refruns;
1629 b1 = *pb++;
1630#ifdef FAX3_DEBUG
1631 printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail);
1632 printf("-------------------- %d\n", tif->tif_row);
1633 fflush(stdout);
1634#endif
1635 EXPAND2D(EOFG4);
1636 if (EOLcnt)
1637 goto EOFG4;
1638 if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */
1639 {
1640 TIFFErrorExtR(tif, module,
1641 "Buffer overrun detected : %" TIFF_SSIZE_FORMAT
1642 " bytes available, %d bits needed",
1643 occ, lastx);
1644 return -1;
1645 }
1646 (*sp->fill)(buf, thisrun, pa, lastx);
1647 SETVALUE(0); /* imaginary change for reference */
1648 SWAP(uint32_t *, sp->curruns, sp->refruns);
1649 buf += sp->b.rowbytes;
1650 occ -= sp->b.rowbytes;
1651 sp->line++;
1652 continue;
1653 EOFG4:
1654 NeedBits16(13, BADG4);
1655 BADG4:
1656#ifdef FAX3_DEBUG
1657 if (GetBits(13) != 0x1001)
1658 fputs("Bad EOFB\n", stderr);
1659#endif
1660 ClrBits(13);
1661 if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */
1662 {
1663 TIFFErrorExtR(tif, module,
1664 "Buffer overrun detected : %" TIFF_SSIZE_FORMAT
1665 " bytes available, %d bits needed",
1666 occ, lastx);
1667 return -1;
1668 }
1669 (*sp->fill)(buf, thisrun, pa, lastx);
1670 UNCACHE_STATE(tif, sp);
1671 return (sp->line != start
1672 ? 1
1673 : -1); /* don't error on badly-terminated strips */
1674 }
1675 UNCACHE_STATE(tif, sp);
1676 return (1);
1677}
1678#undef SWAP
1679
1680/*
1681 * Encode the requested amount of data.
1682 */
1683static int Fax4Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1684{
1685 static const char module[] = "Fax4Encode";
1686 Fax3CodecState *sp = EncoderState(tif);
1687 (void)s;
1688 if (cc % sp->b.rowbytes)
1689 {
1690 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written");
1691 return (0);
1692 }
1693 while (cc > 0)
1694 {
1695 if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
1696 return (0);
1697 _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1698 bp += sp->b.rowbytes;
1699 cc -= sp->b.rowbytes;
1700 }
1701 return (1);
1702}
1703
1704static int Fax4PostEncode(TIFF *tif)
1705{
1706 Fax3CodecState *sp = EncoderState(tif);
1707
1708 /* terminate strip w/ EOFB */
1709 Fax3PutBits(tif, EOL, 12);
1710 Fax3PutBits(tif, EOL, 12);
1711 if (sp->bit != 8)
1712 Fax3FlushBits(tif, sp);
1713 return (1);
1714}
1715
1716int TIFFInitCCITTFax4(TIFF *tif, int scheme)
1717{
1718 (void)scheme;
1719 if (InitCCITTFax3(tif))
1720 { /* reuse G3 support */
1721 /*
1722 * Merge codec-specific tag information.
1723 */
1724 if (!_TIFFMergeFields(tif, fax4Fields, TIFFArrayCount(fax4Fields)))
1725 {
1726 TIFFErrorExtR(tif, "TIFFInitCCITTFax4",
1727 "Merging CCITT Fax 4 codec-specific tags failed");
1728 return 0;
1729 }
1730
1731 tif->tif_decoderow = Fax4Decode;
1732 tif->tif_decodestrip = Fax4Decode;
1733 tif->tif_decodetile = Fax4Decode;
1734 tif->tif_encoderow = Fax4Encode;
1735 tif->tif_encodestrip = Fax4Encode;
1736 tif->tif_encodetile = Fax4Encode;
1737 tif->tif_postencode = Fax4PostEncode;
1738 /*
1739 * Suppress RTC at the end of each strip.
1740 */
1742 }
1743 else
1744 return (0);
1745}
1746
1747/*
1748 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1749 * (Compression algorithms 2 and 32771)
1750 */
1751
1752/*
1753 * Decode the requested amount of RLE-encoded data.
1754 */
1755static int Fax3DecodeRLE(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
1756{
1757 DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
1758 int mode = sp->b.mode;
1759 (void)s;
1760 if (occ % sp->b.rowbytes)
1761 {
1762 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
1763 return (-1);
1764 }
1765 if (CheckReachedCounters(tif, module, sp))
1766 return (-1);
1767 CACHE_STATE(tif, sp);
1768 thisrun = sp->curruns;
1769 while (occ > 0)
1770 {
1771 a0 = 0;
1772 RunLength = 0;
1773 pa = thisrun;
1774#ifdef FAX3_DEBUG
1775 printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail);
1776 printf("-------------------- %" PRIu32 "\n", tif->tif_row);
1777 fflush(stdout);
1778#endif
1779 EXPAND1D(EOFRLE);
1780 (*sp->fill)(buf, thisrun, pa, lastx);
1781 /*
1782 * Cleanup at the end of the row.
1783 */
1784 if (mode & FAXMODE_BYTEALIGN)
1785 {
1786 int n = BitsAvail - (BitsAvail & ~7);
1787 ClrBits(n);
1788 }
1789 else if (mode & FAXMODE_WORDALIGN)
1790 {
1791 int n = BitsAvail - (BitsAvail & ~15);
1792 ClrBits(n);
1793 if (BitsAvail == 0 && !isAligned(cp, uint16_t))
1794 cp++;
1795 }
1796 buf += sp->b.rowbytes;
1797 occ -= sp->b.rowbytes;
1798 sp->line++;
1799 continue;
1800 EOFRLE: /* premature EOF */
1801 (*sp->fill)(buf, thisrun, pa, lastx);
1802 UNCACHE_STATE(tif, sp);
1803 return (-1);
1804 }
1805 UNCACHE_STATE(tif, sp);
1806 return (1);
1807}
1808
1809int TIFFInitCCITTRLE(TIFF *tif, int scheme)
1810{
1811 (void)scheme;
1812 if (InitCCITTFax3(tif))
1813 { /* reuse G3 support */
1814 tif->tif_decoderow = Fax3DecodeRLE;
1815 tif->tif_decodestrip = Fax3DecodeRLE;
1816 tif->tif_decodetile = Fax3DecodeRLE;
1817 /*
1818 * Suppress RTC+EOLs when encoding and byte-align data.
1819 */
1820 return TIFFSetField(tif, TIFFTAG_FAXMODE,
1822 }
1823 else
1824 return (0);
1825}
1826
1827int TIFFInitCCITTRLEW(TIFF *tif, int scheme)
1828{
1829 (void)scheme;
1830 if (InitCCITTFax3(tif))
1831 { /* reuse G3 support */
1832 tif->tif_decoderow = Fax3DecodeRLE;
1833 tif->tif_decodestrip = Fax3DecodeRLE;
1834 tif->tif_decodetile = Fax3DecodeRLE;
1835 /*
1836 * Suppress RTC+EOLs when encoding and word-align data.
1837 */
1838 return TIFFSetField(tif, TIFFTAG_FAXMODE,
1840 }
1841 else
1842 return (0);
1843}
1844#endif /* CCITT_SUPPORT */
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
#define ZERO
Definition: arc.cc:50
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
INT32 int32_t
Definition: types.h:71
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
INT64 int64_t
Definition: types.h:72
#define SWAP()
Definition: pattern.c:1196
int WINAPIV fprintf(FILE *file, const char *format,...)
Definition: file.c:5549
int CDECL fputs(const char *s, FILE *file)
Definition: file.c:4769
int CDECL fflush(FILE *file)
Definition: file.c:1182
#define assert(_expr)
Definition: assert.h:32
#define stdout
#define stderr
#define O_RDONLY
Definition: fcntl.h:34
#define PRIX32
Definition: inttypes.h:117
#define PRIx16
Definition: inttypes.h:100
#define PRIx32
Definition: inttypes.h:101
#define PRIu16
Definition: inttypes.h:83
#define PRIu32
Definition: inttypes.h:84
#define PRId64
Definition: inttypes.h:25
#define va_arg(v, l)
Definition: stdarg.h:27
unsigned short uint16_t
Definition: stdint.h:35
unsigned char uint8_t
Definition: stdint.h:33
char * va_list
Definition: vadefs.h:50
unsigned char
Definition: typeof.h:29
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define L(x)
Definition: resources.c:13
int align(int length, int align)
Definition: dsound8.c:36
#define EOL
Definition: evtmsggen.c:23
#define printf
Definition: freeldr.h:104
GLuint start
Definition: gl.h:1545
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLdouble s
Definition: gl.h:2039
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble n
Definition: glext.h:7729
GLuint res
Definition: glext.h:9613
GLenum GLenum GLvoid GLvoid GLvoid * span
Definition: glext.h:5664
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
GLbitfield flags
Definition: glext.h:7161
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define bits
Definition: infblock.c:15
uint32_t cc
Definition: isohybrid.c:75
#define d
Definition: ke_i.h:81
#define b
Definition: ke_i.h:79
POINT cp
Definition: magnifier.c:59
static struct msdos_boot_sector bs
Definition: mkdosfs.c:539
static const struct update_accum a1
Definition: msg.c:534
static const struct update_accum a2
Definition: msg.c:542
static CRYPT_DATA_BLOB b2[]
Definition: msg.c:538
static CRYPT_DATA_BLOB b1[]
Definition: msg.c:529
static const WCHAR sp[]
Definition: suminfo.c:287
#define FILL(len)
Definition: rtl.c:306
static int ** pa
Definition: server.c:145
int k
Definition: mpi.c:3369
#define uint32_t
Definition: nsiface.idl:61
#define int32_t
Definition: nsiface.idl:56
#define uint16_t
Definition: nsiface.idl:60
#define int64_t
Definition: nsiface.idl:57
DWORD scheme
static int fd
Definition: io.c:51
#define memset(x, y, z)
Definition: compat.h:39
uint16_t td_fillorder
Definition: tif_dir.h:91
uint32_t td_imagewidth
Definition: tif_dir.h:83
uint16_t td_compression
Definition: tif_dir.h:88
uint32_t td_tilewidth
Definition: tif_dir.h:84
float td_yresolution
Definition: tif_dir.h:98
uint16_t td_bitspersample
Definition: tif_dir.h:86
uint16_t td_resolutionunit
Definition: tif_dir.h:99
uint16_t td_planarconfig
Definition: tif_dir.h:100
uint16_t td_samplesperpixel
Definition: tif_dir.h:93
TIFFVGetMethod vgetfield
Definition: tiffio.h:376
TIFFVSetMethod vsetfield
Definition: tiffio.h:375
TIFFPrintMethod printdir
Definition: tiffio.h:377
unsigned short field_bit
Definition: tif_dir.h:340
Definition: uimain.c:89
Definition: inflate.c:139
Definition: parser.c:49
Definition: t4.h:34
unsigned short code
Definition: t4.h:36
unsigned short length
Definition: t4.h:35
short runlen
Definition: t4.h:37
Definition: ecma_167.h:138
Definition: tiffiop.h:113
uint32_t tif_curtile
Definition: tiffiop.h:194
TIFFCodeMethod tif_encodestrip
Definition: tiffiop.h:208
TIFFCodeMethod tif_encodetile
Definition: tiffiop.h:210
TIFFTagMethods tif_tagmethods
Definition: tiffiop.h:244
TIFFPreMethod tif_preencode
Definition: tiffiop.h:203
TIFFBoolMethod tif_fixuptags
Definition: tiffiop.h:198
TIFFPreMethod tif_predecode
Definition: tiffiop.h:200
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:207
char * tif_name
Definition: tiffiop.h:114
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:205
TIFFBoolMethod tif_setupencode
Definition: tiffiop.h:201
TIFFDirectory tif_dir
Definition: tiffiop.h:157
TIFFBoolMethod tif_postencode
Definition: tiffiop.h:204
TIFFCodeMethod tif_encoderow
Definition: tiffiop.h:206
TIFFVoidMethod tif_close
Definition: tiffiop.h:211
TIFFVoidMethod tif_cleanup
Definition: tiffiop.h:213
uint8_t * tif_data
Definition: tiffiop.h:216
uint8_t * tif_rawcp
Definition: tiffiop.h:224
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:199
uint32_t tif_flags
Definition: tiffiop.h:117
uint32_t tif_row
Definition: tiffiop.h:162
uint32_t tif_curstrip
Definition: tiffiop.h:184
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:209
int tif_mode
Definition: tiffiop.h:116
const tableentry TIFFFaxWhiteCodes[]
const tableentry TIFFFaxBlackCodes[]
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Definition: tif_aux.c:122
#define TIFFInitCCITTRLEW
Definition: tif_codec.c:54
#define TIFFInitCCITTFax4
Definition: tif_codec.c:56
#define TIFFInitCCITTFax3
Definition: tif_codec.c:55
#define TIFFInitCCITTRLE
Definition: tif_codec.c:53
void _TIFFSetDefaultCompressionState(TIFF *tif)
Definition: tif_compress.c:142
#define TIFF_SSIZE_FORMAT
Definition: tif_config.h:176
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Definition: tif_dir.c:1146
#define TIFFFieldSet(tif, field)
Definition: tif_dir.h:236
#define FIELD_PSEUDO
Definition: tif_dir.h:230
@ TIFF_SETGET_INT
Definition: tif_dir.h:258
@ TIFF_SETGET_UINT16
Definition: tif_dir.h:249
@ TIFF_SETGET_OTHER
Definition: tif_dir.h:296
@ TIFF_SETGET_UINT32
Definition: tif_dir.h:251
#define TIFFSetFieldBit(tif, field)
Definition: tif_dir.h:237
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Definition: tif_dirinfo.c:845
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Definition: tif_dirinfo.c:573
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
#define ClrBits(n)
Definition: tif_fax3.h:195
#define EXPAND2D(eoflab)
Definition: tif_fax3.h:460
void(* TIFFFaxFillFunc)(unsigned char *, uint32_t *, uint32_t *, uint32_t)
Definition: tif_fax3.h:53
#define SYNC_EOL(eoflab, retrywithouteol)
Definition: tif_fax3.h:292
#define CLEANUP_RUNS()
Definition: tif_fax3.h:332
#define EXPAND1D(eoflab)
Definition: tif_fax3.h:370
#define NeedBits16(n, eoflab)
Definition: tif_fax3.h:163
#define SETVALUE(x)
Definition: tif_fax3.h:265
#define GetBits(n)
Definition: tif_fax3.h:194
void _TIFFFax3fillruns(unsigned char *, uint32_t *, uint32_t *, uint32_t)
#define NeedBits8(n, eoflab)
Definition: tif_fax3.h:143
void _TIFFfreeExt(TIFF *tif, void *p)
Definition: tif_open.c:275
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Definition: tif_open.c:173
tmsize_t TIFFScanlineSize(TIFF *tif)
Definition: tif_strip.c:343
const unsigned char * TIFFGetBitRevTable(int reversed)
Definition: tif_swab.c:305
tmsize_t TIFFTileRowSize(TIFF *tif)
Definition: tif_tile.c:177
void _TIFFmemset(void *p, int v, tmsize_t c)
Definition: tif_unix.c:353
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_warning.c:80
#define PLANARCONFIG_SEPARATE
Definition: tiff.h:266
int uint16_vap
Definition: tiff.h:100
#define COMPRESSION_CCITTFAX3
Definition: tiff.h:184
#define FAXMODE_NORTC
Definition: tiff.h:699
#define RESUNIT_CENTIMETER
Definition: tiff.h:290
#define GROUP3OPT_2DENCODING
Definition: tiff.h:281
#define TIFFTAG_GROUP3OPTIONS
Definition: tiff.h:279
#define TIFFTAG_FAXFILLFUNC
Definition: tiff.h:713
#define TIFFTAG_GROUP4OPTIONS
Definition: tiff.h:284
#define CLEANFAXDATA_REGENERATED
Definition: tiff.h:318
#define TIFFTAG_CONSECUTIVEBADFAXLINES
Definition: tiff.h:320
#define FAXMODE_BYTEALIGN
Definition: tiff.h:701
#define GROUP3OPT_UNCOMPRESSED
Definition: tiff.h:282
#define FILLORDER_LSB2MSB
Definition: tiff.h:242
#define CLEANFAXDATA_CLEAN
Definition: tiff.h:317
@ TIFF_SHORT
Definition: tiff.h:150
@ TIFF_LONG
Definition: tiff.h:151
#define CLEANFAXDATA_UNCLEAN
Definition: tiff.h:319
#define FAXMODE_NOEOL
Definition: tiff.h:700
#define GROUP3OPT_FILLBITS
Definition: tiff.h:283
#define TIFFTAG_FAXMODE
Definition: tiff.h:697
#define TIFFTAG_CLEANFAXDATA
Definition: tiff.h:316
#define TIFFTAG_BADFAXLINES
Definition: tiff.h:315
#define FAXMODE_CLASSF
Definition: tiff.h:703
#define COMPRESSION_CCITTFAX4
Definition: tiff.h:186
#define GROUP4OPT_UNCOMPRESSED
Definition: tiff.h:286
#define FAXMODE_WORDALIGN
Definition: tiff.h:702
void(* TIFFPrintMethod)(TIFF *, FILE *, long)
Definition: tiffio.h:371
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
int(* TIFFVSetMethod)(TIFF *, uint32_t, va_list)
Definition: tiffio.h:369
int(* TIFFVGetMethod)(TIFF *, uint32_t, va_list)
Definition: tiffio.h:370
#define TIFF_ANY
Definition: tiffio.h:341
#define TIFFSafeMultiply(t, v, m)
Definition: tiffiop.h:325
#define isTiled(tif)
Definition: tiffiop.h:274
#define TIFFroundup_32(x, y)
Definition: tiffiop.h:316
#define TIFF_DIRTYDIRECT
Definition: tiffiop.h:120
#define TIFF_NOBITREV
Definition: tiffiop.h:125
#define TIFFArrayCount(a)
Definition: tiffiop.h:333
static struct wctab tab[]
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36