tif_fax3.c 58 KB

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