tif_fax3.c 57 KB

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