tif_fax3.c 44 KB

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