tif_fax3.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /* $Id: tif_fax3.c,v 1.73 2012-06-13 00:27:20 fwarmerdam 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. if (run)
  421. cp[0] |= 0xff00 >> run;
  422. } else
  423. cp[0] |= _fillmasks[run]>>bx;
  424. x += runs[1];
  425. }
  426. }
  427. assert(x == lastx);
  428. }
  429. #undef ZERO
  430. #undef FILL
  431. static int
  432. Fax3FixupTags(TIFF* tif)
  433. {
  434. (void) tif;
  435. return (1);
  436. }
  437. /*
  438. * Setup G3/G4-related compression/decompression state
  439. * before data is processed. This routine is called once
  440. * per image -- it sets up different state based on whether
  441. * or not decoding or encoding is being done and whether
  442. * 1D- or 2D-encoded data is involved.
  443. */
  444. static int
  445. Fax3SetupState(TIFF* tif)
  446. {
  447. static const char module[] = "Fax3SetupState";
  448. TIFFDirectory* td = &tif->tif_dir;
  449. Fax3BaseState* sp = Fax3State(tif);
  450. int needsRefLine;
  451. Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
  452. tmsize_t rowbytes;
  453. uint32 rowpixels, nruns;
  454. if (td->td_bitspersample != 1) {
  455. TIFFErrorExt(tif->tif_clientdata, module,
  456. "Bits/sample must be 1 for Group 3/4 encoding/decoding");
  457. return (0);
  458. }
  459. /*
  460. * Calculate the scanline/tile widths.
  461. */
  462. if (isTiled(tif)) {
  463. rowbytes = TIFFTileRowSize(tif);
  464. rowpixels = td->td_tilewidth;
  465. } else {
  466. rowbytes = TIFFScanlineSize(tif);
  467. rowpixels = td->td_imagewidth;
  468. }
  469. sp->rowbytes = rowbytes;
  470. sp->rowpixels = rowpixels;
  471. /*
  472. * Allocate any additional space required for decoding/encoding.
  473. */
  474. needsRefLine = (
  475. (sp->groupoptions & GROUP3OPT_2DENCODING) ||
  476. td->td_compression == COMPRESSION_CCITTFAX4
  477. );
  478. /*
  479. Assure that allocation computations do not overflow.
  480. TIFFroundup and TIFFSafeMultiply return zero on integer overflow
  481. */
  482. dsp->runs=(uint32*) NULL;
  483. nruns = TIFFroundup_32(rowpixels,32);
  484. if (needsRefLine) {
  485. nruns = TIFFSafeMultiply(uint32,nruns,2);
  486. }
  487. if ((nruns == 0) || (TIFFSafeMultiply(uint32,nruns,2) == 0)) {
  488. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  489. "Row pixels integer overflow (rowpixels %u)",
  490. rowpixels);
  491. return (0);
  492. }
  493. dsp->runs = (uint32*) _TIFFCheckMalloc(tif,
  494. TIFFSafeMultiply(uint32,nruns,2),
  495. sizeof (uint32),
  496. "for Group 3/4 run arrays");
  497. if (dsp->runs == NULL)
  498. return (0);
  499. memset( dsp->runs, 0, TIFFSafeMultiply(uint32,nruns,2));
  500. dsp->curruns = dsp->runs;
  501. if (needsRefLine)
  502. dsp->refruns = dsp->runs + nruns;
  503. else
  504. dsp->refruns = NULL;
  505. if (td->td_compression == COMPRESSION_CCITTFAX3
  506. && is2DEncoding(dsp)) { /* NB: default is 1D routine */
  507. tif->tif_decoderow = Fax3Decode2D;
  508. tif->tif_decodestrip = Fax3Decode2D;
  509. tif->tif_decodetile = Fax3Decode2D;
  510. }
  511. if (needsRefLine) { /* 2d encoding */
  512. Fax3CodecState* esp = EncoderState(tif);
  513. /*
  514. * 2d encoding requires a scanline
  515. * buffer for the ``reference line''; the
  516. * scanline against which delta encoding
  517. * is referenced. The reference line must
  518. * be initialized to be ``white'' (done elsewhere).
  519. */
  520. esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
  521. if (esp->refline == NULL) {
  522. TIFFErrorExt(tif->tif_clientdata, module,
  523. "No space for Group 3/4 reference line");
  524. return (0);
  525. }
  526. } else /* 1d encoding */
  527. EncoderState(tif)->refline = NULL;
  528. return (1);
  529. }
  530. /*
  531. * CCITT Group 3 FAX Encoding.
  532. */
  533. #define Fax3FlushBits(tif, sp) { \
  534. if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
  535. (void) TIFFFlushData1(tif); \
  536. *(tif)->tif_rawcp++ = (uint8) (sp)->data; \
  537. (tif)->tif_rawcc++; \
  538. (sp)->data = 0, (sp)->bit = 8; \
  539. }
  540. #define _FlushBits(tif) { \
  541. if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
  542. (void) TIFFFlushData1(tif); \
  543. *(tif)->tif_rawcp++ = (uint8) data; \
  544. (tif)->tif_rawcc++; \
  545. data = 0, bit = 8; \
  546. }
  547. static const int _msbmask[9] =
  548. { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
  549. #define _PutBits(tif, bits, length) { \
  550. while (length > bit) { \
  551. data |= bits >> (length - bit); \
  552. length -= bit; \
  553. _FlushBits(tif); \
  554. } \
  555. assert( length < 9 ); \
  556. data |= (bits & _msbmask[length]) << (bit - length); \
  557. bit -= length; \
  558. if (bit == 0) \
  559. _FlushBits(tif); \
  560. }
  561. /*
  562. * Write a variable-length bit-value to
  563. * the output stream. Values are
  564. * assumed to be at most 16 bits.
  565. */
  566. static void
  567. Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
  568. {
  569. Fax3CodecState* sp = EncoderState(tif);
  570. unsigned int bit = sp->bit;
  571. int data = sp->data;
  572. _PutBits(tif, bits, length);
  573. sp->data = data;
  574. sp->bit = bit;
  575. }
  576. /*
  577. * Write a code to the output stream.
  578. */
  579. #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
  580. #ifdef FAX3_DEBUG
  581. #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
  582. #define DEBUG_PRINT(what,len) { \
  583. int t; \
  584. printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
  585. for (t = length-1; t >= 0; t--) \
  586. putchar(code & (1<<t) ? '1' : '0'); \
  587. putchar('\n'); \
  588. }
  589. #endif
  590. /*
  591. * Write the sequence of codes that describes
  592. * the specified span of zero's or one's. The
  593. * appropriate table that holds the make-up and
  594. * terminating codes is supplied.
  595. */
  596. static void
  597. putspan(TIFF* tif, int32 span, const tableentry* tab)
  598. {
  599. Fax3CodecState* sp = EncoderState(tif);
  600. unsigned int bit = sp->bit;
  601. int data = sp->data;
  602. unsigned int code, length;
  603. while (span >= 2624) {
  604. const tableentry* te = &tab[63 + (2560>>6)];
  605. code = te->code, length = te->length;
  606. #ifdef FAX3_DEBUG
  607. DEBUG_PRINT("MakeUp", te->runlen);
  608. #endif
  609. _PutBits(tif, code, length);
  610. span -= te->runlen;
  611. }
  612. if (span >= 64) {
  613. const tableentry* te = &tab[63 + (span>>6)];
  614. assert(te->runlen == 64*(span>>6));
  615. code = te->code, length = te->length;
  616. #ifdef FAX3_DEBUG
  617. DEBUG_PRINT("MakeUp", te->runlen);
  618. #endif
  619. _PutBits(tif, code, length);
  620. span -= te->runlen;
  621. }
  622. code = tab[span].code, length = tab[span].length;
  623. #ifdef FAX3_DEBUG
  624. DEBUG_PRINT(" Term", tab[span].runlen);
  625. #endif
  626. _PutBits(tif, code, length);
  627. sp->data = data;
  628. sp->bit = bit;
  629. }
  630. /*
  631. * Write an EOL code to the output stream. The zero-fill
  632. * logic for byte-aligning encoded scanlines is handled
  633. * here. We also handle writing the tag bit for the next
  634. * scanline when doing 2d encoding.
  635. */
  636. static void
  637. Fax3PutEOL(TIFF* tif)
  638. {
  639. Fax3CodecState* sp = EncoderState(tif);
  640. unsigned int bit = sp->bit;
  641. int data = sp->data;
  642. unsigned int code, length, tparm;
  643. if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
  644. /*
  645. * Force bit alignment so EOL will terminate on
  646. * a byte boundary. That is, force the bit alignment
  647. * to 16-12 = 4 before putting out the EOL code.
  648. */
  649. int align = 8 - 4;
  650. if (align != sp->bit) {
  651. if (align > sp->bit)
  652. align = sp->bit + (8 - align);
  653. else
  654. align = sp->bit - align;
  655. code = 0;
  656. tparm=align;
  657. _PutBits(tif, 0, tparm);
  658. }
  659. }
  660. code = EOL, length = 12;
  661. if (is2DEncoding(sp))
  662. code = (code<<1) | (sp->tag == G3_1D), length++;
  663. _PutBits(tif, code, length);
  664. sp->data = data;
  665. sp->bit = bit;
  666. }
  667. /*
  668. * Reset encoding state at the start of a strip.
  669. */
  670. static int
  671. Fax3PreEncode(TIFF* tif, uint16 s)
  672. {
  673. Fax3CodecState* sp = EncoderState(tif);
  674. (void) s;
  675. assert(sp != NULL);
  676. sp->bit = 8;
  677. sp->data = 0;
  678. sp->tag = G3_1D;
  679. /*
  680. * This is necessary for Group 4; otherwise it isn't
  681. * needed because the first scanline of each strip ends
  682. * up being copied into the refline.
  683. */
  684. if (sp->refline)
  685. _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
  686. if (is2DEncoding(sp)) {
  687. float res = tif->tif_dir.td_yresolution;
  688. /*
  689. * The CCITT spec says that when doing 2d encoding, you
  690. * should only do it on K consecutive scanlines, where K
  691. * depends on the resolution of the image being encoded
  692. * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
  693. * code initializes td_yresolution to 0, this code will
  694. * select a K of 2 unless the YResolution tag is set
  695. * appropriately. (Note also that we fudge a little here
  696. * and use 150 lpi to avoid problems with units conversion.)
  697. */
  698. if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
  699. res *= 2.54f; /* convert to inches */
  700. sp->maxk = (res > 150 ? 4 : 2);
  701. sp->k = sp->maxk-1;
  702. } else
  703. sp->k = sp->maxk = 0;
  704. sp->line = 0;
  705. return (1);
  706. }
  707. static const unsigned char zeroruns[256] = {
  708. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
  709. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
  710. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
  711. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
  712. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
  713. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
  714. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
  715. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
  716. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
  717. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
  718. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
  719. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
  720. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
  721. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
  722. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
  723. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
  724. };
  725. static const unsigned char oneruns[256] = {
  726. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
  727. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
  728. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
  729. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
  730. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
  731. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
  732. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
  733. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
  734. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
  735. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
  736. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
  737. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
  738. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
  739. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
  740. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
  741. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
  742. };
  743. /*
  744. * On certain systems it pays to inline
  745. * the routines that find pixel spans.
  746. */
  747. #ifdef VAXC
  748. static int32 find0span(unsigned char*, int32, int32);
  749. static int32 find1span(unsigned char*, int32, int32);
  750. #pragma inline(find0span,find1span)
  751. #endif
  752. /*
  753. * Find a span of ones or zeros using the supplied
  754. * table. The ``base'' of the bit string is supplied
  755. * along with the start+end bit indices.
  756. */
  757. inline static int32
  758. find0span(unsigned char* bp, int32 bs, int32 be)
  759. {
  760. int32 bits = be - bs;
  761. int32 n, span;
  762. bp += bs>>3;
  763. /*
  764. * Check partial byte on lhs.
  765. */
  766. if (bits > 0 && (n = (bs & 7))) {
  767. span = zeroruns[(*bp << n) & 0xff];
  768. if (span > 8-n) /* table value too generous */
  769. span = 8-n;
  770. if (span > bits) /* constrain span to bit range */
  771. span = bits;
  772. if (n+span < 8) /* doesn't extend to edge of byte */
  773. return (span);
  774. bits -= span;
  775. bp++;
  776. } else
  777. span = 0;
  778. if (bits >= (int32)(2 * 8 * sizeof(long))) {
  779. long* lp;
  780. /*
  781. * Align to longword boundary and check longwords.
  782. */
  783. while (!isAligned(bp, long)) {
  784. if (*bp != 0x00)
  785. return (span + zeroruns[*bp]);
  786. span += 8, bits -= 8;
  787. bp++;
  788. }
  789. lp = (long*) bp;
  790. while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) {
  791. span += 8*sizeof (long), bits -= 8*sizeof (long);
  792. lp++;
  793. }
  794. bp = (unsigned char*) lp;
  795. }
  796. /*
  797. * Scan full bytes for all 0's.
  798. */
  799. while (bits >= 8) {
  800. if (*bp != 0x00) /* end of run */
  801. return (span + zeroruns[*bp]);
  802. span += 8, bits -= 8;
  803. bp++;
  804. }
  805. /*
  806. * Check partial byte on rhs.
  807. */
  808. if (bits > 0) {
  809. n = zeroruns[*bp];
  810. span += (n > bits ? bits : n);
  811. }
  812. return (span);
  813. }
  814. inline static int32
  815. find1span(unsigned char* bp, int32 bs, int32 be)
  816. {
  817. int32 bits = be - bs;
  818. int32 n, span;
  819. bp += bs>>3;
  820. /*
  821. * Check partial byte on lhs.
  822. */
  823. if (bits > 0 && (n = (bs & 7))) {
  824. span = oneruns[(*bp << n) & 0xff];
  825. if (span > 8-n) /* table value too generous */
  826. span = 8-n;
  827. if (span > bits) /* constrain span to bit range */
  828. span = bits;
  829. if (n+span < 8) /* doesn't extend to edge of byte */
  830. return (span);
  831. bits -= span;
  832. bp++;
  833. } else
  834. span = 0;
  835. if (bits >= (int32)(2 * 8 * sizeof(long))) {
  836. long* lp;
  837. /*
  838. * Align to longword boundary and check longwords.
  839. */
  840. while (!isAligned(bp, long)) {
  841. if (*bp != 0xff)
  842. return (span + oneruns[*bp]);
  843. span += 8, bits -= 8;
  844. bp++;
  845. }
  846. lp = (long*) bp;
  847. while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) {
  848. span += 8*sizeof (long), bits -= 8*sizeof (long);
  849. lp++;
  850. }
  851. bp = (unsigned char*) lp;
  852. }
  853. /*
  854. * Scan full bytes for all 1's.
  855. */
  856. while (bits >= 8) {
  857. if (*bp != 0xff) /* end of run */
  858. return (span + oneruns[*bp]);
  859. span += 8, bits -= 8;
  860. bp++;
  861. }
  862. /*
  863. * Check partial byte on rhs.
  864. */
  865. if (bits > 0) {
  866. n = oneruns[*bp];
  867. span += (n > bits ? bits : n);
  868. }
  869. return (span);
  870. }
  871. /*
  872. * Return the offset of the next bit in the range
  873. * [bs..be] that is different from the specified
  874. * color. The end, be, is returned if no such bit
  875. * exists.
  876. */
  877. #define finddiff(_cp, _bs, _be, _color) \
  878. (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
  879. /*
  880. * Like finddiff, but also check the starting bit
  881. * against the end in case start > end.
  882. */
  883. #define finddiff2(_cp, _bs, _be, _color) \
  884. (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
  885. /*
  886. * 1d-encode a row of pixels. The encoding is
  887. * a sequence of all-white or all-black spans
  888. * of pixels encoded with Huffman codes.
  889. */
  890. static int
  891. Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
  892. {
  893. Fax3CodecState* sp = EncoderState(tif);
  894. int32 span;
  895. uint32 bs = 0;
  896. for (;;) {
  897. span = find0span(bp, bs, bits); /* white span */
  898. putspan(tif, span, TIFFFaxWhiteCodes);
  899. bs += span;
  900. if (bs >= bits)
  901. break;
  902. span = find1span(bp, bs, bits); /* black span */
  903. putspan(tif, span, TIFFFaxBlackCodes);
  904. bs += span;
  905. if (bs >= bits)
  906. break;
  907. }
  908. if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
  909. if (sp->bit != 8) /* byte-align */
  910. Fax3FlushBits(tif, sp);
  911. if ((sp->b.mode&FAXMODE_WORDALIGN) &&
  912. !isAligned(tif->tif_rawcp, uint16))
  913. Fax3FlushBits(tif, sp);
  914. }
  915. return (1);
  916. }
  917. static const tableentry horizcode =
  918. { 3, 0x1, 0 }; /* 001 */
  919. static const tableentry passcode =
  920. { 4, 0x1, 0 }; /* 0001 */
  921. static const tableentry vcodes[7] = {
  922. { 7, 0x03, 0 }, /* 0000 011 */
  923. { 6, 0x03, 0 }, /* 0000 11 */
  924. { 3, 0x03, 0 }, /* 011 */
  925. { 1, 0x1, 0 }, /* 1 */
  926. { 3, 0x2, 0 }, /* 010 */
  927. { 6, 0x02, 0 }, /* 0000 10 */
  928. { 7, 0x02, 0 } /* 0000 010 */
  929. };
  930. /*
  931. * 2d-encode a row of pixels. Consult the CCITT
  932. * documentation for the algorithm.
  933. */
  934. static int
  935. Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits)
  936. {
  937. #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
  938. uint32 a0 = 0;
  939. uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
  940. uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
  941. uint32 a2, b2;
  942. for (;;) {
  943. b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
  944. if (b2 >= a1) {
  945. int32 d = b1 - a1;
  946. if (!(-3 <= d && d <= 3)) { /* horizontal mode */
  947. a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
  948. putcode(tif, &horizcode);
  949. if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
  950. putspan(tif, a1-a0, TIFFFaxWhiteCodes);
  951. putspan(tif, a2-a1, TIFFFaxBlackCodes);
  952. } else {
  953. putspan(tif, a1-a0, TIFFFaxBlackCodes);
  954. putspan(tif, a2-a1, TIFFFaxWhiteCodes);
  955. }
  956. a0 = a2;
  957. } else { /* vertical mode */
  958. putcode(tif, &vcodes[d+3]);
  959. a0 = a1;
  960. }
  961. } else { /* pass mode */
  962. putcode(tif, &passcode);
  963. a0 = b2;
  964. }
  965. if (a0 >= bits)
  966. break;
  967. a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
  968. b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
  969. b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
  970. }
  971. return (1);
  972. #undef PIXEL
  973. }
  974. /*
  975. * Encode a buffer of pixels.
  976. */
  977. static int
  978. Fax3Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  979. {
  980. static const char module[] = "Fax3Encode";
  981. Fax3CodecState* sp = EncoderState(tif);
  982. (void) s;
  983. if (cc % sp->b.rowbytes)
  984. {
  985. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
  986. return (0);
  987. }
  988. while (cc > 0) {
  989. if ((sp->b.mode & FAXMODE_NOEOL) == 0)
  990. Fax3PutEOL(tif);
  991. if (is2DEncoding(sp)) {
  992. if (sp->tag == G3_1D) {
  993. if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
  994. return (0);
  995. sp->tag = G3_2D;
  996. } else {
  997. if (!Fax3Encode2DRow(tif, bp, sp->refline,
  998. sp->b.rowpixels))
  999. return (0);
  1000. sp->k--;
  1001. }
  1002. if (sp->k == 0) {
  1003. sp->tag = G3_1D;
  1004. sp->k = sp->maxk-1;
  1005. } else
  1006. _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
  1007. } else {
  1008. if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
  1009. return (0);
  1010. }
  1011. bp += sp->b.rowbytes;
  1012. cc -= sp->b.rowbytes;
  1013. }
  1014. return (1);
  1015. }
  1016. static int
  1017. Fax3PostEncode(TIFF* tif)
  1018. {
  1019. Fax3CodecState* sp = EncoderState(tif);
  1020. if (sp->bit != 8)
  1021. Fax3FlushBits(tif, sp);
  1022. return (1);
  1023. }
  1024. static void
  1025. Fax3Close(TIFF* tif)
  1026. {
  1027. if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0) {
  1028. Fax3CodecState* sp = EncoderState(tif);
  1029. unsigned int code = EOL;
  1030. unsigned int length = 12;
  1031. int i;
  1032. if (is2DEncoding(sp))
  1033. code = (code<<1) | (sp->tag == G3_1D), length++;
  1034. for (i = 0; i < 6; i++)
  1035. Fax3PutBits(tif, code, length);
  1036. Fax3FlushBits(tif, sp);
  1037. }
  1038. }
  1039. static void
  1040. Fax3Cleanup(TIFF* tif)
  1041. {
  1042. Fax3CodecState* sp = DecoderState(tif);
  1043. assert(sp != 0);
  1044. tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
  1045. tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
  1046. tif->tif_tagmethods.printdir = sp->b.printdir;
  1047. if (sp->runs)
  1048. _TIFFfree(sp->runs);
  1049. if (sp->refline)
  1050. _TIFFfree(sp->refline);
  1051. _TIFFfree(tif->tif_data);
  1052. tif->tif_data = NULL;
  1053. _TIFFSetDefaultCompressionState(tif);
  1054. }
  1055. #define FIELD_BADFAXLINES (FIELD_CODEC+0)
  1056. #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
  1057. #define FIELD_BADFAXRUN (FIELD_CODEC+2)
  1058. #define FIELD_OPTIONS (FIELD_CODEC+7)
  1059. static const TIFFField faxFields[] = {
  1060. { TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL },
  1061. { TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL },
  1062. { TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL },
  1063. { TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL },
  1064. { TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL }};
  1065. static const TIFFField fax3Fields[] = {
  1066. { TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL },
  1067. };
  1068. static const TIFFField fax4Fields[] = {
  1069. { TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL },
  1070. };
  1071. static int
  1072. Fax3VSetField(TIFF* tif, uint32 tag, va_list ap)
  1073. {
  1074. Fax3BaseState* sp = Fax3State(tif);
  1075. const TIFFField* fip;
  1076. assert(sp != 0);
  1077. assert(sp->vsetparent != 0);
  1078. switch (tag) {
  1079. case TIFFTAG_FAXMODE:
  1080. sp->mode = (int) va_arg(ap, int);
  1081. return 1; /* NB: pseudo tag */
  1082. case TIFFTAG_FAXFILLFUNC:
  1083. DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
  1084. return 1; /* NB: pseudo tag */
  1085. case TIFFTAG_GROUP3OPTIONS:
  1086. /* XXX: avoid reading options if compression mismatches. */
  1087. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
  1088. sp->groupoptions = (uint32) va_arg(ap, uint32);
  1089. break;
  1090. case TIFFTAG_GROUP4OPTIONS:
  1091. /* XXX: avoid reading options if compression mismatches. */
  1092. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
  1093. sp->groupoptions = (uint32) va_arg(ap, uint32);
  1094. break;
  1095. case TIFFTAG_BADFAXLINES:
  1096. sp->badfaxlines = (uint32) va_arg(ap, uint32);
  1097. break;
  1098. case TIFFTAG_CLEANFAXDATA:
  1099. sp->cleanfaxdata = (uint16) va_arg(ap, uint16_vap);
  1100. break;
  1101. case TIFFTAG_CONSECUTIVEBADFAXLINES:
  1102. sp->badfaxrun = (uint32) va_arg(ap, uint32);
  1103. break;
  1104. default:
  1105. return (*sp->vsetparent)(tif, tag, ap);
  1106. }
  1107. if ((fip = TIFFFieldWithTag(tif, tag)))
  1108. TIFFSetFieldBit(tif, fip->field_bit);
  1109. else
  1110. return 0;
  1111. tif->tif_flags |= TIFF_DIRTYDIRECT;
  1112. return 1;
  1113. }
  1114. static int
  1115. Fax3VGetField(TIFF* tif, uint32 tag, va_list ap)
  1116. {
  1117. Fax3BaseState* sp = Fax3State(tif);
  1118. assert(sp != 0);
  1119. switch (tag) {
  1120. case TIFFTAG_FAXMODE:
  1121. *va_arg(ap, int*) = sp->mode;
  1122. break;
  1123. case TIFFTAG_FAXFILLFUNC:
  1124. *va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
  1125. break;
  1126. case TIFFTAG_GROUP3OPTIONS:
  1127. case TIFFTAG_GROUP4OPTIONS:
  1128. *va_arg(ap, uint32*) = sp->groupoptions;
  1129. break;
  1130. case TIFFTAG_BADFAXLINES:
  1131. *va_arg(ap, uint32*) = sp->badfaxlines;
  1132. break;
  1133. case TIFFTAG_CLEANFAXDATA:
  1134. *va_arg(ap, uint16*) = sp->cleanfaxdata;
  1135. break;
  1136. case TIFFTAG_CONSECUTIVEBADFAXLINES:
  1137. *va_arg(ap, uint32*) = sp->badfaxrun;
  1138. break;
  1139. default:
  1140. return (*sp->vgetparent)(tif, tag, ap);
  1141. }
  1142. return (1);
  1143. }
  1144. static void
  1145. Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
  1146. {
  1147. Fax3BaseState* sp = Fax3State(tif);
  1148. assert(sp != 0);
  1149. (void) flags;
  1150. if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
  1151. const char* sep = " ";
  1152. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
  1153. fprintf(fd, " Group 4 Options:");
  1154. if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
  1155. fprintf(fd, "%suncompressed data", sep);
  1156. } else {
  1157. fprintf(fd, " Group 3 Options:");
  1158. if (sp->groupoptions & GROUP3OPT_2DENCODING)
  1159. fprintf(fd, "%s2-d encoding", sep), sep = "+";
  1160. if (sp->groupoptions & GROUP3OPT_FILLBITS)
  1161. fprintf(fd, "%sEOL padding", sep), sep = "+";
  1162. if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
  1163. fprintf(fd, "%suncompressed data", sep);
  1164. }
  1165. fprintf(fd, " (%lu = 0x%lx)\n",
  1166. (unsigned long) sp->groupoptions,
  1167. (unsigned long) sp->groupoptions);
  1168. }
  1169. if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
  1170. fprintf(fd, " Fax Data:");
  1171. switch (sp->cleanfaxdata) {
  1172. case CLEANFAXDATA_CLEAN:
  1173. fprintf(fd, " clean");
  1174. break;
  1175. case CLEANFAXDATA_REGENERATED:
  1176. fprintf(fd, " receiver regenerated");
  1177. break;
  1178. case CLEANFAXDATA_UNCLEAN:
  1179. fprintf(fd, " uncorrected errors");
  1180. break;
  1181. }
  1182. fprintf(fd, " (%u = 0x%x)\n",
  1183. sp->cleanfaxdata, sp->cleanfaxdata);
  1184. }
  1185. if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
  1186. fprintf(fd, " Bad Fax Lines: %lu\n",
  1187. (unsigned long) sp->badfaxlines);
  1188. if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
  1189. fprintf(fd, " Consecutive Bad Fax Lines: %lu\n",
  1190. (unsigned long) sp->badfaxrun);
  1191. if (sp->printdir)
  1192. (*sp->printdir)(tif, fd, flags);
  1193. }
  1194. static int
  1195. InitCCITTFax3(TIFF* tif)
  1196. {
  1197. static const char module[] = "InitCCITTFax3";
  1198. Fax3BaseState* sp;
  1199. /*
  1200. * Merge codec-specific tag information.
  1201. */
  1202. if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) {
  1203. TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
  1204. "Merging common CCITT Fax codec-specific tags failed");
  1205. return 0;
  1206. }
  1207. /*
  1208. * Allocate state block so tag methods have storage to record values.
  1209. */
  1210. tif->tif_data = (uint8*)
  1211. _TIFFmalloc(sizeof (Fax3CodecState));
  1212. if (tif->tif_data == NULL) {
  1213. TIFFErrorExt(tif->tif_clientdata, module,
  1214. "No space for state block");
  1215. return (0);
  1216. }
  1217. sp = Fax3State(tif);
  1218. sp->rw_mode = tif->tif_mode;
  1219. /*
  1220. * Override parent get/set field methods.
  1221. */
  1222. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1223. tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
  1224. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1225. tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
  1226. sp->printdir = tif->tif_tagmethods.printdir;
  1227. tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */
  1228. sp->groupoptions = 0;
  1229. if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
  1230. tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
  1231. DecoderState(tif)->runs = NULL;
  1232. TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
  1233. EncoderState(tif)->refline = NULL;
  1234. /*
  1235. * Install codec methods.
  1236. */
  1237. tif->tif_fixuptags = Fax3FixupTags;
  1238. tif->tif_setupdecode = Fax3SetupState;
  1239. tif->tif_predecode = Fax3PreDecode;
  1240. tif->tif_decoderow = Fax3Decode1D;
  1241. tif->tif_decodestrip = Fax3Decode1D;
  1242. tif->tif_decodetile = Fax3Decode1D;
  1243. tif->tif_setupencode = Fax3SetupState;
  1244. tif->tif_preencode = Fax3PreEncode;
  1245. tif->tif_postencode = Fax3PostEncode;
  1246. tif->tif_encoderow = Fax3Encode;
  1247. tif->tif_encodestrip = Fax3Encode;
  1248. tif->tif_encodetile = Fax3Encode;
  1249. tif->tif_close = Fax3Close;
  1250. tif->tif_cleanup = Fax3Cleanup;
  1251. return (1);
  1252. }
  1253. int
  1254. TIFFInitCCITTFax3(TIFF* tif, int scheme)
  1255. {
  1256. (void) scheme;
  1257. if (InitCCITTFax3(tif)) {
  1258. /*
  1259. * Merge codec-specific tag information.
  1260. */
  1261. if (!_TIFFMergeFields(tif, fax3Fields,
  1262. TIFFArrayCount(fax3Fields))) {
  1263. TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
  1264. "Merging CCITT Fax 3 codec-specific tags failed");
  1265. return 0;
  1266. }
  1267. /*
  1268. * The default format is Class/F-style w/o RTC.
  1269. */
  1270. return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
  1271. } else
  1272. return 01;
  1273. }
  1274. /*
  1275. * CCITT Group 4 (T.6) Facsimile-compatible
  1276. * Compression Scheme Support.
  1277. */
  1278. #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
  1279. /*
  1280. * Decode the requested amount of G4-encoded data.
  1281. */
  1282. static int
  1283. Fax4Decode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
  1284. {
  1285. DECLARE_STATE_2D(tif, sp, "Fax4Decode");
  1286. (void) s;
  1287. if (occ % sp->b.rowbytes)
  1288. {
  1289. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  1290. return (-1);
  1291. }
  1292. CACHE_STATE(tif, sp);
  1293. while (occ > 0) {
  1294. a0 = 0;
  1295. RunLength = 0;
  1296. pa = thisrun = sp->curruns;
  1297. pb = sp->refruns;
  1298. b1 = *pb++;
  1299. #ifdef FAX3_DEBUG
  1300. printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
  1301. printf("-------------------- %d\n", tif->tif_row);
  1302. fflush(stdout);
  1303. #endif
  1304. EXPAND2D(EOFG4);
  1305. if (EOLcnt)
  1306. goto EOFG4;
  1307. (*sp->fill)(buf, thisrun, pa, lastx);
  1308. SETVALUE(0); /* imaginary change for reference */
  1309. SWAP(uint32*, sp->curruns, sp->refruns);
  1310. buf += sp->b.rowbytes;
  1311. occ -= sp->b.rowbytes;
  1312. sp->line++;
  1313. continue;
  1314. EOFG4:
  1315. NeedBits16( 13, BADG4 );
  1316. BADG4:
  1317. #ifdef FAX3_DEBUG
  1318. if( GetBits(13) != 0x1001 )
  1319. fputs( "Bad EOFB\n", stderr );
  1320. #endif
  1321. ClrBits( 13 );
  1322. (*sp->fill)(buf, thisrun, pa, lastx);
  1323. UNCACHE_STATE(tif, sp);
  1324. return ( sp->line ? 1 : -1); /* don't error on badly-terminated strips */
  1325. }
  1326. UNCACHE_STATE(tif, sp);
  1327. return (1);
  1328. }
  1329. #undef SWAP
  1330. /*
  1331. * Encode the requested amount of data.
  1332. */
  1333. static int
  1334. Fax4Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  1335. {
  1336. static const char module[] = "Fax4Encode";
  1337. Fax3CodecState *sp = EncoderState(tif);
  1338. (void) s;
  1339. if (cc % sp->b.rowbytes)
  1340. {
  1341. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
  1342. return (0);
  1343. }
  1344. while (cc > 0) {
  1345. if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
  1346. return (0);
  1347. _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
  1348. bp += sp->b.rowbytes;
  1349. cc -= sp->b.rowbytes;
  1350. }
  1351. return (1);
  1352. }
  1353. static int
  1354. Fax4PostEncode(TIFF* tif)
  1355. {
  1356. Fax3CodecState *sp = EncoderState(tif);
  1357. /* terminate strip w/ EOFB */
  1358. Fax3PutBits(tif, EOL, 12);
  1359. Fax3PutBits(tif, EOL, 12);
  1360. if (sp->bit != 8)
  1361. Fax3FlushBits(tif, sp);
  1362. return (1);
  1363. }
  1364. int
  1365. TIFFInitCCITTFax4(TIFF* tif, int scheme)
  1366. {
  1367. (void) scheme;
  1368. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1369. /*
  1370. * Merge codec-specific tag information.
  1371. */
  1372. if (!_TIFFMergeFields(tif, fax4Fields,
  1373. TIFFArrayCount(fax4Fields))) {
  1374. TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
  1375. "Merging CCITT Fax 4 codec-specific tags failed");
  1376. return 0;
  1377. }
  1378. tif->tif_decoderow = Fax4Decode;
  1379. tif->tif_decodestrip = Fax4Decode;
  1380. tif->tif_decodetile = Fax4Decode;
  1381. tif->tif_encoderow = Fax4Encode;
  1382. tif->tif_encodestrip = Fax4Encode;
  1383. tif->tif_encodetile = Fax4Encode;
  1384. tif->tif_postencode = Fax4PostEncode;
  1385. /*
  1386. * Suppress RTC at the end of each strip.
  1387. */
  1388. return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
  1389. } else
  1390. return (0);
  1391. }
  1392. /*
  1393. * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
  1394. * (Compression algorithms 2 and 32771)
  1395. */
  1396. /*
  1397. * Decode the requested amount of RLE-encoded data.
  1398. */
  1399. static int
  1400. Fax3DecodeRLE(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
  1401. {
  1402. DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
  1403. int mode = sp->b.mode;
  1404. (void) s;
  1405. if (occ % sp->b.rowbytes)
  1406. {
  1407. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  1408. return (-1);
  1409. }
  1410. CACHE_STATE(tif, sp);
  1411. thisrun = sp->curruns;
  1412. while (occ > 0) {
  1413. a0 = 0;
  1414. RunLength = 0;
  1415. pa = thisrun;
  1416. #ifdef FAX3_DEBUG
  1417. printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
  1418. printf("-------------------- %d\n", tif->tif_row);
  1419. fflush(stdout);
  1420. #endif
  1421. EXPAND1D(EOFRLE);
  1422. (*sp->fill)(buf, thisrun, pa, lastx);
  1423. /*
  1424. * Cleanup at the end of the row.
  1425. */
  1426. if (mode & FAXMODE_BYTEALIGN) {
  1427. int n = BitsAvail - (BitsAvail &~ 7);
  1428. ClrBits(n);
  1429. } else if (mode & FAXMODE_WORDALIGN) {
  1430. int n = BitsAvail - (BitsAvail &~ 15);
  1431. ClrBits(n);
  1432. if (BitsAvail == 0 && !isAligned(cp, uint16))
  1433. cp++;
  1434. }
  1435. buf += sp->b.rowbytes;
  1436. occ -= sp->b.rowbytes;
  1437. sp->line++;
  1438. continue;
  1439. EOFRLE: /* premature EOF */
  1440. (*sp->fill)(buf, thisrun, pa, lastx);
  1441. UNCACHE_STATE(tif, sp);
  1442. return (-1);
  1443. }
  1444. UNCACHE_STATE(tif, sp);
  1445. return (1);
  1446. }
  1447. int
  1448. TIFFInitCCITTRLE(TIFF* tif, int scheme)
  1449. {
  1450. (void) scheme;
  1451. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1452. tif->tif_decoderow = Fax3DecodeRLE;
  1453. tif->tif_decodestrip = Fax3DecodeRLE;
  1454. tif->tif_decodetile = Fax3DecodeRLE;
  1455. /*
  1456. * Suppress RTC+EOLs when encoding and byte-align data.
  1457. */
  1458. return TIFFSetField(tif, TIFFTAG_FAXMODE,
  1459. FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
  1460. } else
  1461. return (0);
  1462. }
  1463. int
  1464. TIFFInitCCITTRLEW(TIFF* tif, int scheme)
  1465. {
  1466. (void) scheme;
  1467. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1468. tif->tif_decoderow = Fax3DecodeRLE;
  1469. tif->tif_decodestrip = Fax3DecodeRLE;
  1470. tif->tif_decodetile = Fax3DecodeRLE;
  1471. /*
  1472. * Suppress RTC+EOLs when encoding and word-align data.
  1473. */
  1474. return TIFFSetField(tif, TIFFTAG_FAXMODE,
  1475. FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
  1476. } else
  1477. return (0);
  1478. }
  1479. #endif /* CCITT_SUPPORT */
  1480. /* vim: set ts=8 sts=8 sw=8 noet: */
  1481. /*
  1482. * Local Variables:
  1483. * mode: c
  1484. * c-basic-offset: 8
  1485. * fill-column: 78
  1486. * End:
  1487. */