49#ifndef EOF_REACHED_COUNT_THRESHOLD
54#define EOF_REACHED_COUNT_THRESHOLD 8192
96#define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data)
175#define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif))
176#define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif))
178#define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
179#define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0)
189#define DECLARE_STATE(tif, sp, mod) \
190 static const char module[] = mod; \
191 Fax3CodecState *sp = DecoderState(tif); \
193 int lastx = (int)sp->b.rowpixels; \
202 const unsigned char *bitmap = sp->bitmap; \
203 const TIFFFaxTabEnt *TabEnt
205#define DECLARE_STATE_2D(tif, sp, mod) \
206 DECLARE_STATE(tif, sp, mod); \
213#define CACHE_STATE(tif, sp) \
217 BitsAvail = sp->bit; \
218 EOLcnt = sp->EOLcnt; \
219 cp = (unsigned char *)tif->tif_rawcp; \
220 ep = cp + tif->tif_rawcc; \
225#define UNCACHE_STATE(tif, sp) \
228 sp->bit = BitsAvail; \
230 sp->EOLcnt = EOLcnt; \
231 tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp); \
232 tif->tif_rawcp = (uint8_t *)cp; \
313 "Bad code word at line %" PRIu32
" of %s %" PRIu32
" (x %" PRIu32
")",
314 line,
isTiled(tif) ?
"tile" :
"strip",
315 (
isTiled(tif) ? tif->tif_dir.td_curtile : tif->tif_dir.td_curstrip),
318#define unexpected(table, a0) \
321 Fax3Unexpected(module, tif, (uint32_t)sp->line, (uint32_t)(a0)); \
322 ++sp->unexpectedReachedCount; \
345 "Uncompressed data (not supported) at line %" PRIu32
" of %s %" PRIu32
347 line,
isTiled(tif) ?
"tile" :
"strip",
351#define extension(a0) \
352 Fax3Extension(module, tif, (uint32_t)sp->line, (uint32_t)(a0))
371 uint32_t a0, uint32_t lastx)
375 "%s at line %" PRIu32
" of %s %" PRIu32
" (got %" PRIu32
376 ", expected %" PRIu32
")",
377 a0 < lastx ?
"Premature EOL" :
"Line length mismatch", line,
378 isTiled(tif) ?
"tile" :
"strip",
379 (
isTiled(tif) ? tif->tif_dir.td_curtile : tif->tif_dir.td_curstrip), a0,
382#define badlength(a0, lastx) \
385 Fax3BadLength(module, tif, (uint32_t)sp->line, (uint32_t)(a0), \
386 (uint32_t)(lastx)); \
387 ++sp->eolReachedCount; \
410 "Premature EOF at line %" PRIu32
" of %s %" PRIu32
" (x %" PRIu32
")",
411 line,
isTiled(tif) ?
"tile" :
"strip",
415#define prematureEOF(a0) \
418 Fax3PrematureEOF(module, tif, (uint32_t)sp->line, (uint32_t)(a0)); \
419 ++sp->eofReachedCount; \
442 "Try to decode (read) fax Group 3 data without EOL at line %" PRIu32
443 " of %s %" PRIu32
" (x %" PRIu32
"). Please check result",
444 line,
isTiled(tif) ?
"tile" :
"strip",
445 (
isTiled(tif) ? tif->tif_dir.td_curtile : tif->tif_dir.td_curstrip),
448#define tryG3WithoutEOL(a0) \
451 Fax3TryG3WithoutEOL(module, tif, (uint32_t)sp->line, (uint32_t)(a0)); \
474 "End of file (EOF) has already been reached %d times "
482 "Bad line length (EOL) has already been reached %d times "
490 "Bad code word (unexpected) has already been reached %d "
491 "times within that %s",
493 isTiled(tif) ?
"tile" :
"strip");
517 memset(buf, 0, (
size_t)occ);
556 const int has_buf = (buf != NULL);
560 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be read");
577 printf(
"\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc, BitsAvail);
578 printf(
"-------------------- %" PRIu32
"\n", tif->
tif_dir.
td_row);
581 SYNC_EOL(EOF1D, RETRY_WITHOUT_EOL_1D);
583 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
592 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
604#define SWAP(t, a, b) \
651 const int has_buf = (buf != NULL);
655 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be read");
671 printf(
"\nBitAcc=%08" PRIX32
", BitsAvail = %d EOLcnt = %d", BitAcc,
674 SYNC_EOL(EOF2D, RETRY_WITHOUT_EOL_2D);
679 printf(
" %s\n-------------------- %" PRIu32
"\n", is1D ?
"1D" :
"2D",
680 tif->tif_dir.td_row);
689 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
690 if (pa < thisrun + sp->nruns)
703 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
717 for (int32_t ifill = 0; ifill < (n); ++ifill) \
719 (cp)[ifill] = 0xff; \
724 for (int32_t izero = 0; izero < (n); ++izero) \
737 static const unsigned char _fillmasks[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
738 0xf8, 0xfc, 0xfe, 0xff};
748 if ((erun - runs) & 1)
751 for (; runs < erun; runs += 2)
754 if (x + run > lastx || run > lastx)
755 run = runs[0] = (uint32_t)(lastx - x);
764 *cp++ &= (
unsigned char)(0xff << (8 - bx));
767 if ((n = (int32_t)(run >> 3)) != 0)
769 if (((
size_t)n /
sizeof(int64_t)) > 1)
774 for (; n && !
isAligned(cp, int64_t); n--)
777 nw = (int32_t)((
size_t)n /
sizeof(int64_t));
778 n -= (int32_t)((
size_t)nw *
sizeof(int64_t));
783 cp = (
unsigned char *)lp;
789 cp[0] &= (
unsigned char)(0xff >> run);
792 cp[0] &= (
unsigned char)~(_fillmasks[run] >> bx);
796 if (x + run > lastx || run > lastx)
797 run = runs[1] = lastx - x;
806 *cp++ |= (
unsigned char)(0xff >> bx);
809 if ((n = (int32_t)(run >> 3)) != 0)
811 if (((
size_t)n /
sizeof(int64_t)) > 1)
816 for (; n && !
isAligned(cp, int64_t); n--)
819 nw = (int32_t)((
size_t)n /
sizeof(int64_t));
820 n -= (int32_t)((
size_t)nw *
sizeof(int64_t));
825 cp = (
unsigned char *)lp;
832 cp[0] = (
unsigned char)((cp[0] | (0xff00 >> run)) & 0xff);
835 cp[0] |= (
unsigned char)(_fillmasks[run] >> bx);
887 static const char module[] = "Fax3SetupState";
896 if (td->td_bitspersample != 1)
899 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
902 if (td->td_samplesperpixel != 1 &&
907 "Samples/pixel shall be 1 for Group 3/4 encoding/decoding, "
908 "or PlanarConfiguration must be set to Separate.");
917 rowpixels = td->td_tilewidth;
922 rowpixels = td->td_imagewidth;
924 if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8)
927 "Inconsistent number of bytes per row : rowbytes=%" PRId64
928 " rowpixels=%" PRIu32,
929 (int64_t)rowbytes, rowpixels);
948 if (dsp->
runs != NULL)
951 dsp->
runs = (uint32_t *)NULL;
957 "Row pixels integer overflow (rowpixels %" PRIu32
")",
972 "for Group 3/4 run arrays");
973 if (dsp->
runs == NULL)
975 memset(dsp->
runs, 0, totalRuns *
sizeof(uint32_t));
1005 TIFFErrorExtR(tif, module,
"No space for Group 3/4 reference line");
1019#define Fax3FlushBits(tif, sp) \
1021 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
1023 if (!TIFFFlushData1(tif)) \
1026 *(tif)->tif_rawcp++ = (uint8_t)(sp)->data; \
1027 (tif)->tif_rawcc++; \
1028 (sp)->data = 0, (sp)->bit = 8; \
1030#define _FlushBits(tif) \
1032 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
1034 if (!TIFFFlushData1(tif)) \
1037 *(tif)->tif_rawcp++ = (uint8_t)data; \
1038 (tif)->tif_rawcc++; \
1039 data = 0, bit = 8; \
1047static const int _msbmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f,
1048 0x1f, 0x3f, 0x7f, 0xff};
1049#define _PutBits(tif, bits, length) \
1051 while (length > bit) \
1053 data |= (int)((unsigned int)bits >> (length - bit)); \
1057 assert(length < 9); \
1058 data |= (int)(((unsigned int)bits & (unsigned int)_msbmask[length]) \
1059 << (unsigned int)(bit - length)); \
1086static int Fax3PutBits(
TIFF *tif,
unsigned int bits,
unsigned int length)
1089 unsigned int bit = (
unsigned int)sp->
bit;
1090 int data = (int)sp->data;
1094 sp->data = (uint32_t)data;
1102#define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
1116#define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
1128#define DEBUG_PRINT(what, len) \
1131 printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), \
1133 for (t = length - 1; t >= 0; t--) \
1134 putchar(code & (1 << t) ? '1' : '0'); \
1166 unsigned int bit = (
unsigned int)sp->
bit;
1167 int data = (int)sp->
data;
1168 unsigned int code, length;
1170 while (span >= 2624)
1172 const tableentry *te = &tab[63 + (2560 >> 6)];
1176 DEBUG_PRINT(
"MakeUp", te->
runlen);
1183 const tableentry *te = &tab[63 + (span >> 6)];
1184 assert(te->
runlen == 64 * (span >> 6));
1188 DEBUG_PRINT(
"MakeUp", te->
runlen);
1193 code = tab[span].
code;
1194 length = tab[span].
length;
1196 DEBUG_PRINT(
" Term", tab[span].runlen);
1200 sp->
data = (uint32_t)data;
1227 unsigned int bit = (
unsigned int)sp->
bit;
1228 int data = (int)sp->data;
1229 unsigned int code, length, tparm;
1239 if (align != sp->bit)
1241 if (align > sp->bit)
1242 align = sp->bit + (8 - align);
1244 align = sp->bit - align;
1245 tparm = (
unsigned int)align;
1253 code = (code << 1) | (sp->tag ==
G3_1D);
1258 sp->data = (uint32_t)data;
1312 sp->
maxk = (res > 150 ? 4 : 2);
1313 sp->
k = sp->
maxk - 1;
1316 sp->
k = sp->
maxk = 0;
1327static const unsigned char zeroruns[256] = {
1328 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
1329 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1330 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1331 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1332 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1333 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1334 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1335 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1338 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1351static const unsigned char oneruns[256] = {
1352 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1355 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1358 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1359 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1360 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1361 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1362 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1363 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1364 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1365 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1366 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1367 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,
1389static inline int32_t
find0span(
unsigned char *bp, int32_t bs, int32_t be)
1391 int32_t bits = be - bs;
1398 if (bits > 0 && (n = (bs & 7)) != 0)
1400 span =
zeroruns[(*bp << n) & 0xff];
1412 if (bits >= (int32_t)(2 * 8 *
sizeof(int64_t)))
1427 while ((bits >= (int32_t)(size_t)(8 *
sizeof(int64_t))) && (0 == *lp))
1429 span += (int32_t)(
size_t)(8 *
sizeof(int64_t));
1430 bits -= (int32_t)(
size_t)(8 *
sizeof(int64_t));
1433 bp = (
unsigned char *)lp;
1452 span += (n > bits ? bits : n);
1471static inline int32_t
find1span(
unsigned char *bp, int32_t bs, int32_t be)
1473 int32_t bits = be - bs;
1480 if (bits > 0 && (n = (bs & 7)) != 0)
1482 span =
oneruns[(*bp << n) & 0xff];
1494 if (bits >= (int32_t)(2 * 8 *
sizeof(int64_t)))
1509 while ((bits >= (int32_t)(size_t)(8 *
sizeof(int64_t))) &&
1510 (~((uint64_t)0) == (uint64_t)*lp))
1512 span += (int32_t)(
size_t)(8 *
sizeof(int64_t));
1513 bits -= (int32_t)(
size_t)(8 *
sizeof(int64_t));
1516 bp = (
unsigned char *)lp;
1535 span += (n > bits ? bits : n);
1546#define finddiff(_cp, _bs, _be, _color) \
1547 (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be)))
1552#define finddiff2(_cp, _bs, _be, _color) \
1553 (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be)
1585 span =
find0span(bp, (int32_t)bs, (int32_t)bits);
1588 bs += (uint32_t)span;
1591 span =
find1span(bp, (int32_t)bs, (int32_t)bits);
1594 bs += (uint32_t)span;
1666#define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix) & 7))) & 1)
1668 uint32_t a1 = (
PIXEL(bp, 0) != 0
1670 : (uint32_t)
finddiff(bp, (int32_t)0, (int32_t)bits, 0));
1671 uint32_t b1 = (
PIXEL(rp, 0) != 0
1673 : (uint32_t)
finddiff(rp, (int32_t)0, (int32_t)bits, 0));
1678 b2 = (uint32_t)
finddiff2(rp, (int32_t)b1, (int32_t)bits,
1679 (int32_t)
PIXEL(rp, b1));
1687 int32_t d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32_t)(b1 - a1)
1688 : (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1)
1690 if (!(-3 <= d && d <= 3))
1692 a2 = (uint32_t)
finddiff2(bp, (int32_t)a1, (int32_t)bits,
1693 (int32_t)
PIXEL(bp, a1));
1696 if (a0 + a1 == 0 ||
PIXEL(bp, a0) == 0)
1727 a1 = (uint32_t)
finddiff(bp, (int32_t)a0, (int32_t)bits,
1728 (int32_t)
PIXEL(bp, a0));
1729 b1 = (uint32_t)
finddiff(rp, (int32_t)a0, (int32_t)bits,
1730 (int32_t)!
PIXEL(bp, a0));
1731 b1 = (uint32_t)
finddiff(rp, (int32_t)b1, (int32_t)bits,
1732 (int32_t)
PIXEL(bp, a0));
1761 static const char module[] = "Fax3Encode";
1764 if (cc % sp->b.rowbytes)
1766 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be written");
1778 if (sp->tag ==
G3_1D)
1793 sp->k = sp->maxk - 1;
1803 bp += sp->b.rowbytes;
1804 cc -= sp->b.rowbytes;
1848 unsigned int code =
EOL;
1849 unsigned int length = 12;
1854 code = (code << 1) | (sp->
tag ==
G3_1D);
1857 for (i = 0; i < 6; i++)
1910#define FIELD_BADFAXLINES (FIELD_CODEC + 0)
1911#define FIELD_CLEANFAXDATA (FIELD_CODEC + 1)
1912#define FIELD_BADFAXRUN (FIELD_CODEC + 2)
1914#define FIELD_OPTIONS (FIELD_CODEC + 7)
1925 FALSE,
"FaxMode", NULL},
1991 sp->
mode = (int)va_arg(ap,
int);
2013 sp->
badfaxrun = (uint32_t)va_arg(ap, uint32_t);
2053 *va_arg(ap,
int *) = sp->
mode;
2069 *va_arg(ap, uint32_t *) = sp->
badfaxrun;
2099 const char *sep =
" ";
2102 fprintf(fd,
" Group 4 Options:");
2104 fprintf(fd,
"%suncompressed data", sep);
2109 fprintf(fd,
" Group 3 Options:");
2112 fprintf(fd,
"%s2-d encoding", sep);
2117 fprintf(fd,
"%sEOL padding", sep);
2121 fprintf(fd,
"%suncompressed data", sep);
2123 fprintf(fd,
" (%" PRIu32
" = 0x%" PRIx32
")\n", sp->
groupoptions,
2128 fprintf(fd,
" Fax Data:");
2132 fprintf(fd,
" clean");
2135 fprintf(fd,
" receiver regenerated");
2138 fprintf(fd,
" uncorrected errors");
2143 fprintf(fd,
" (%" PRIu16
" = 0x%" PRIx16
")\n", sp->
cleanfaxdata,
2147 fprintf(fd,
" Bad Fax Lines: %" PRIu32
"\n", sp->
badfaxlines);
2149 fprintf(fd,
" Consecutive Bad Fax Lines: %" PRIu32
"\n",
2199 static const char module[] = "InitCCITTFax3";
2208 "Merging common CCITT Fax codec-specific tags failed");
2236 sp->groupoptions = 0;
2238 if (sp->rw_mode == O_RDONLY)
2277 "Merging CCITT Fax 3 codec-specific tags failed");
2295#define SWAP(t, a, b) \
2346 const int has_buf = (buf != NULL);
2347 if (occ % sp->b.rowbytes)
2350 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be read");
2359 int start = sp->line;
2364 pa = thisrun = sp->curruns;
2368 printf(
"\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc, BitsAvail);
2375 if (((lastx + 7) >> 3) > (
int)occ)
2380 " bytes available, %d bits needed",
2384 (*sp->fill)(buf, thisrun, pa, (uint32_t)lastx);
2386 SWAP(uint32_t *, sp->curruns, sp->refruns);
2387 buf += sp->b.rowbytes;
2388 occ -= sp->b.rowbytes;
2396 fputs(
"Bad EOFB\n", stderr);
2399 if (((lastx + 7) >> 3) > (
int)occ)
2404 " bytes available, %d bits needed",
2411 (*sp->fill)(buf, thisrun, pa, (uint32_t)lastx);
2413 return (sp->line != start
2449 static const char module[] = "Fax4Encode";
2452 if (cc % sp->b.rowbytes)
2454 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be written");
2462 bp += sp->b.rowbytes;
2463 cc -= sp->b.rowbytes;
2526 "Merging CCITT Fax 4 codec-specific tags failed");
2589 int mode = sp->
b.
mode;
2593 const int has_buf = (buf != NULL);
2597 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be read");
2613 printf(
"\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc, BitsAvail);
2614 printf(
"-------------------- %" PRIu32
"\n", tif->
tif_dir.
td_row);
2618 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
2624 int n = BitsAvail - (BitsAvail & ~7);
2629 int n = BitsAvail - (BitsAvail & ~15);
2631 if (BitsAvail == 0 && !
isAligned(cp, uint16_t))
2640 (*sp->
fill)(buf, thisrun, pa, (uint32_t)lastx);
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Multiply two unsigned values with an overflow check.
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
void(* TIFFFaxFillFunc)(unsigned char *buf, uint32_t *runs, uint32_t *erun, uint32_t lastx)
Render decoded fax runs through an installed callback.
#define FIELD_BADFAXRUN
Reserve the consecutive-bad-lines codec field bit.
static int putspan(TIFF *tif, int32_t span, const tableentry *tab)
Emit makeup and terminating Huffman codes for one fax run.
static void Fax3Close(TIFF *tif)
Invoke fax return-to-control completion without a status channel.
static int Fax3PreEncode(TIFF *tif, uint16_t s)
Reset fax encoding bits reference line and dimensional schedule.
static int Fax3PreDecode(TIFF *tif, uint16_t s)
Reset fax bit state run pointers and recovery counters.
static int Fax3DecodeRLE(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Decode complete aligned modified-Huffman fax rows.
int TIFFInitCCITTRLE(TIFF *tif, int scheme)
Install CCITTRLE fax callbacks and default mode.
static int Fax4Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Encode complete Group4 rows relative to the preceding row.
static const TIFFField fax3Fields[]
Define Group3 options metadata.
static uint64_t Fax3RLEGetMaxCompressionRatio(TIFF *tif)
Report the fax expansion allocation heuristic.
#define EncoderState(tif)
Borrow the complete fax encoder state.
static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Decode complete Group4 two-dimensional fax rows.
#define DECLARE_STATE_2D(tif, sp, mod)
Declare local two-dimensional decode and reference state.
static int Fax3VGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve fax mode callback or persistent fax values.
#define SWAP(t, a, b)
Exchange current and reference run-array pointers.
static int Fax3PutBits(TIFF *tif, unsigned int bits, unsigned int length)
Append up to sixteen significant fax code bits.
int TIFFInitCCITTRLEW(TIFF *tif, int scheme)
Install CCITTRLEW fax callbacks and default mode.
static void Fax3TryG3WithoutEOL(const char *module, TIFF *tif, uint32_t line, uint32_t a0)
Warn that Group3 recovery will retry without EOL.
static int Fax3Decode1D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Decode complete Group3 one-dimensional fax rows.
#define CACHE_STATE(tif, sp)
Load codec and raw cursor state into decode locals.
static void Fax3PrintDir(TIFF *tif, FILE *fd, long flags)
Print present fax values then chain the saved directory printer.
#define ClrBits(n)
Consume low bits from the local accumulator.
#define EXPAND2D(eoflab)
Expand a two-dimensional row relative to reference runs.
#define EOL
Represent the twelve-bit EOL code value.
static void Fax3BadLength(const char *module, TIFF *tif, uint32_t line, uint32_t a0, uint32_t lastx)
Warn about a fax row-length mismatch.
#define is2DEncoding(sp)
Test Group3 two-dimensional mode.
#define DecoderState(tif)
Borrow the complete fax decoder state.
const tableentry TIFFFaxWhiteCodes[]
Describe white CCITT terminating and makeup run codes.
#define FIELD_OPTIONS
Reserve the Group3/4 options codec field bit.
#define finddiff(_cp, _bs, _be, _color)
Find the next pixel differing from the selected colour.
static int Fax3VSetField(TIFF *tif, uint32_t tag, va_list ap)
Assign fax pseudo-tags or persistent fax metadata.
static const unsigned char zeroruns[256]
Count leading zero bits in each byte.
static int Fax3SetupState(TIFF *tif)
Validate bilevel geometry and allocate fax run/reference storage.
static const int _msbmask[9]
Mask the requested low bit count for encoder code packing.
static void Fax3PrematureEOF(const char *module, TIFF *tif, uint32_t line, uint32_t a0)
Warn about premature fax input exhaustion.
#define FILL(n, cp)
Set complete black output bytes and advance the pointer.
static const TIFFField fax4Fields[]
Define Group4 options metadata.
#define putcode(tif, te)
Emit one Huffman table entry.
#define ZERO(n, cp)
Clear complete white output bytes and advance the pointer.
static void Fax3Extension(const char *module, TIFF *tif, uint32_t line, uint32_t a0)
Diagnose unsupported fax uncompressed extension data.
static int Fax3FixupTags(TIFF *tif)
Accept fax tag fixup without changes.
#define EOF_REACHED_COUNT_THRESHOLD
Limit repeated fax recovery conditions per strile.
#define Fax3FlushBits(tif, sp)
Append a partial code byte and flush raw output when full.
static void Fax3ClearUnwritten(int has_buf, uint8_t *buf, tmsize_t occ)
Zero the part of the caller buffer the decoder will not write.
#define isAligned(p, t)
Test a pointer against a type-sized alignment.
static const TIFFField faxFields[]
Define common fax metadata and pseudo-tags.
static const tableentry passcode
Encode the Group4 pass mode prefix.
static int32_t find1span(unsigned char *bp, int32_t bs, int32_t be)
Measure the leading one bit run in a bounded packed row.
#define SYNC_EOL(eoflab, retrywithouteol)
Synchronise EOL or retry configured no-EOL recovery.
Ttag
Select the next Group3 encoded row dimensionality.
static int Fax3Encode1DRow(TIFF *tif, unsigned char *bp, uint32_t bits)
Encode alternating white and black runs for one bilevel row.
static uint64_t Fax4GetMaxCompressionRatio(TIFF *tif)
Report the fax expansion allocation heuristic.
#define _PutBits(tif, bits, length)
Append local code bits across partial-byte boundaries.
static int _Fax3Close(TIFF *tif)
Append an optional six-EOL fax return-to-control sequence.
#define FIELD_BADFAXLINES
Reserve the BadFaxLines codec field bit.
#define DECLARE_STATE(tif, sp, mod)
Declare cached local one-dimensional decode state.
static int Fax3Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Encode complete Group3 or aligned RLE rows.
#define CLEANUP_RUNS()
Repair the current row run total to the requested width.
#define EXPAND1D(eoflab)
Expand a one-dimensional Huffman row into runs.
void _TIFFFax3fillruns(unsigned char *buf, uint32_t *runs, uint32_t *erun, uint32_t lastx)
Render alternating white/black fax runs into an MSB-first row.
static void Fax3Cleanup(TIFF *tif)
Restore fax tag/print methods and release run/reference state.
static const tableentry vcodes[7]
Encode Group4 vertical offsets minus three through three.
#define NeedBits16(n, eoflab)
Load up to sixteen bits or branch to EOF.
static void Fax3Unexpected(const char *module, TIFF *tif, uint32_t line, uint32_t a0)
Diagnose an invalid fax code word.
static int Fax3PutEOL(TIFF *tif)
Append Group3 EOL padding and optional dimensional tag.
static uint64_t Fax3GetMaxCompressionRatio(TIFF *tif)
Report the fax expansion allocation heuristic.
#define SETVALUE(x)
Append a checked decoded run and reset its local length.
static int Fax3PostEncode(TIFF *tif)
Pad and buffer the final partial fax code byte.
static const tableentry horizcode
Encode the Group4 horizontal mode prefix.
#define UNCACHE_STATE(tif, sp)
Commit local bit state and consumed raw bytes.
static int Fax3Encode2DRow(TIFF *tif, unsigned char *bp, unsigned char *rp, uint32_t bits)
Encode one bilevel row relative to its reference row.
static int InitCCITTFax3(TIFF *tif)
Allocate shared fax state and install tag and transfer methods.
static int CheckReachedCounters(TIFF *tif, const char *module, Fax3CodecState *sp)
Stop fax recovery after repeated EOF EOL or bad-code conditions.
#define GetBits(n)
Inspect low bits of the local accumulator.
#define PIXEL(buf, ix)
Read one MSB-first packed pixel bit.
#define Fax3State(tif)
Borrow the shared fax state prefix.
#define NeedBits8(n, eoflab)
Load bits or branch to the caller EOF label.
const tableentry TIFFFaxBlackCodes[]
Describe black CCITT terminating and makeup run codes.
#define FIELD_CLEANFAXDATA
Reserve the CleanFaxData codec field bit.
static const unsigned char oneruns[256]
Count leading one bits in each byte.
int TIFFInitCCITTFax4(TIFF *tif, int scheme)
Install CCITTFax4 fax callbacks and default mode.
static int Fax3Decode2D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Decode complete Group3 mixed-dimensional fax rows.
static int32_t find0span(unsigned char *bp, int32_t bs, int32_t be)
Measure the leading zero bit run in a bounded packed row.
static int Fax4PostEncode(TIFF *tif)
Append Group4 EOFB and align the final byte.
#define finddiff2(_cp, _bs, _be, _color)
Find a change while bounding an already exhausted start.
int TIFFInitCCITTFax3(TIFF *tif, int scheme)
Install CCITTFax3 fax callbacks and default mode.
@ G3_2D
Encode the next row relative to its predecessor.
@ G3_1D
Encode the next row independently.
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
@ TIFF_SETGET_INT
Promoted int scalar representation.
@ TIFF_SETGET_UINT16
Unsigned 16-bit scalar storage.
@ TIFF_SETGET_OTHER
Tag-specific calling convention outside the regular encodings.
@ TIFF_SETGET_UINT32
Unsigned 32-bit scalar storage.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
#define isTiled(tif)
Test whether a handle uses tiled storage.
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
#define TRUE
Represent the legacy internal true value.
#define FALSE
Represent the legacy internal false value.
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
#define TIFF_NOBITREV
Suppress automatic bit reversal.
#define TIFFArrayCount(a)
Compute the element count of an actual array.
const unsigned char * TIFFGetBitRevTable(int reversed)
Select the immutable byte reversal or identity lookup table.
#define TIFF_ANY
Match any data type when searching for a field descriptor.
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Find a registered field by tag number.
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
tmsize_t TIFFTileRowSize(TIFF *tif)
Compute the packed byte count of one tile row.
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
int(* TIFFVGetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag getter.
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
void(* TIFFPrintMethod)(TIFF *tif, FILE *fd, long flags)
Append extension-specific directory information to a stream.
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
int uint16_vap
Represent a uint16_t value after default variadic argument promotion.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
@ TIFF_SHORT
Unsigned 16-bit integer.
@ TIFF_LONG
Unsigned 32-bit integer.
Prefix fax state with geometry options and saved method chains.
uint16_t cleanfaxdata
Cached CleanFaxData tag value.
uint32_t badfaxrun
Cached consecutive bad fax line count.
int mode
FAXMODE control bits.
TIFFPrintMethod printdir
Borrowed optional parent printer restored during cleanup.
uint32_t rowpixels
Pixels per scanline/tile row, within signed decoder limits.
uint32_t badfaxlines
Cached total bad fax line count.
TIFFVSetMethod vsetparent
Borrowed parent tag setter restored during cleanup.
TIFFVGetMethod vgetparent
Borrowed parent tag getter restored during cleanup.
int rw_mode
Open mode saved during codec initialisation.
uint32_t groupoptions
Cached Group3/4 options for the selected scheme.
tmsize_t rowbytes
Positive decoded scanline/tile-row byte extent after setup.
Own fax run/reference buffers and bit-stream progress.
uint32_t data
Cached decoder bit accumulator or encoder partial byte.
int line
Current line count within the strile.
int bit
Valid input bits or remaining output bit positions.
Fax3BaseState b
Shared base prefix, first in codec state.
int unexpectedReachedCount
Repeated invalid-code recovery count in the current strile.
int maxk
Resolution-derived maximum dimensional schedule interval.
TIFFFaxFillFunc fill
Borrowed synchronous run renderer; application code/context must remain live.
int eofReachedCount
Repeated EOF recovery count in the current strile.
Ttag tag
Next Group3 row dimensionality.
int EOLcnt
Recognised end-of-line count/state.
int k
Remaining two-dimensional rows before another one-dimensional row.
unsigned char * refline
Owned Ext packed reference row used by 2D encoding.
uint32_t * runs
Owned Ext allocation holding current and optional reference run arrays.
const unsigned char * bitmap
Borrowed immutable byte reversal table selected for current FillOrder.
uint32_t * refruns
Borrowed reference partition within runs, or NULL for one-dimensional state.
uint32_t * curruns
Borrowed current partition within runs; swapped with reference in 2D decoding.
int eolReachedCount
Repeated bad-length recovery count in the current strile.
uint32_t nruns
Capacity of each run-array partition in uint32_t entries.
Store the current directory values and image traversal state.
uint16_t td_fillorder
FILLORDER_* bit order within file bytes.
uint32_t td_imagewidth
Image width in pixels.
uint16_t td_compression
COMPRESSION_* codec identifier.
uint32_t td_tilewidth
Tile width in pixels.
float td_yresolution
Vertical pixels per resolution unit.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
uint16_t td_resolutionunit
RESUNIT_* unit for resolution and position values.
uint32_t td_curtile
Current tile index, or NOTILE.
uint32_t td_curstrip
Current strip index, or NOSTRIP.
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
TIFFPrintMethod printdir
Optional codec/extension directory-print callback.
Describe a field's file type and internal argument contract.
unsigned short field_bit
FIELD_* presence index; FIELD_CUSTOM shares storage through the custom list.
Describe one CCITT Huffman run-length code.
unsigned short code
Right-adjusted code bits.
unsigned short length
Code width in bits.
short runlen
Run length in pixels or negative G3CODE sentinel.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
TIFFVoidMethod tif_close
Codec close callback; distinct from the client I/O close callback.
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
int tif_mode
Current access mode using the O_* constants.
Define CCITT Huffman code entries tables and run sentinels.
Define fax run callbacks generated transitions and decoder state macros.
Declare private library state, callbacks, and implementation helpers.