tif_fax3.c 47 KB

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