tif_fax3.c 45 KB

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