tif_fax3.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* $Id: tif_fax3.h,v 1.9 2011-03-10 20:23:07 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1990-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #ifndef _FAX3_
  26. #define _FAX3_
  27. /*
  28. * TIFF Library.
  29. *
  30. * CCITT Group 3 (T.4) and Group 4 (T.6) Decompression Support.
  31. *
  32. * Decoder support is derived, with permission, from the code
  33. * in Frank Cringle's viewfax program;
  34. * Copyright (C) 1990, 1995 Frank D. Cringle.
  35. */
  36. #include "tiff.h"
  37. /*
  38. * To override the default routine used to image decoded
  39. * spans one can use the pseduo tag TIFFTAG_FAXFILLFUNC.
  40. * The routine must have the type signature given below;
  41. * for example:
  42. *
  43. * fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
  44. *
  45. * where buf is place to set the bits, runs is the array of b&w run
  46. * lengths (white then black), erun is the last run in the array, and
  47. * lastx is the width of the row in pixels. Fill routines can assume
  48. * the run array has room for at least lastx runs and can overwrite
  49. * data in the run array as needed (e.g. to append zero runs to bring
  50. * the count up to a nice multiple).
  51. */
  52. typedef void (*TIFFFaxFillFunc)(unsigned char*, uint32*, uint32*, uint32);
  53. /*
  54. * The default run filler; made external for other decoders.
  55. */
  56. #if defined(__cplusplus)
  57. extern "C" {
  58. #endif
  59. extern void _TIFFFax3fillruns(unsigned char*, uint32*, uint32*, uint32);
  60. #if defined(__cplusplus)
  61. }
  62. #endif
  63. /* finite state machine codes */
  64. #define S_Null 0
  65. #define S_Pass 1
  66. #define S_Horiz 2
  67. #define S_V0 3
  68. #define S_VR 4
  69. #define S_VL 5
  70. #define S_Ext 6
  71. #define S_TermW 7
  72. #define S_TermB 8
  73. #define S_MakeUpW 9
  74. #define S_MakeUpB 10
  75. #define S_MakeUp 11
  76. #define S_EOL 12
  77. typedef struct { /* state table entry */
  78. unsigned char State; /* see above */
  79. unsigned char Width; /* width of code in bits */
  80. uint32 Param; /* unsigned 32-bit run length in bits */
  81. } TIFFFaxTabEnt;
  82. extern const TIFFFaxTabEnt TIFFFaxMainTable[];
  83. extern const TIFFFaxTabEnt TIFFFaxWhiteTable[];
  84. extern const TIFFFaxTabEnt TIFFFaxBlackTable[];
  85. /*
  86. * The following macros define the majority of the G3/G4 decoder
  87. * algorithm using the state tables defined elsewhere. To build
  88. * a decoder you need some setup code and some glue code. Note
  89. * that you may also need/want to change the way the NeedBits*
  90. * macros get input data if, for example, you know the data to be
  91. * decoded is properly aligned and oriented (doing so before running
  92. * the decoder can be a big performance win).
  93. *
  94. * Consult the decoder in the TIFF library for an idea of what you
  95. * need to define and setup to make use of these definitions.
  96. *
  97. * NB: to enable a debugging version of these macros define FAX3_DEBUG
  98. * before including this file. Trace output goes to stdout.
  99. */
  100. #ifndef EndOfData
  101. #define EndOfData() (cp >= ep)
  102. #endif
  103. /*
  104. * Need <=8 or <=16 bits of input data. Unlike viewfax we
  105. * cannot use/assume a word-aligned, properly bit swizzled
  106. * input data set because data may come from an arbitrarily
  107. * aligned, read-only source such as a memory-mapped file.
  108. * Note also that the viewfax decoder does not check for
  109. * running off the end of the input data buffer. This is
  110. * possible for G3-encoded data because it prescans the input
  111. * data to count EOL markers, but can cause problems for G4
  112. * data. In any event, we don't prescan and must watch for
  113. * running out of data since we can't permit the library to
  114. * scan past the end of the input data buffer.
  115. *
  116. * Finally, note that we must handle remaindered data at the end
  117. * of a strip specially. The coder asks for a fixed number of
  118. * bits when scanning for the next code. This may be more bits
  119. * than are actually present in the data stream. If we appear
  120. * to run out of data but still have some number of valid bits
  121. * remaining then we makeup the requested amount with zeros and
  122. * return successfully. If the returned data is incorrect then
  123. * we should be called again and get a premature EOF error;
  124. * otherwise we should get the right answer.
  125. */
  126. #ifndef NeedBits8
  127. #define NeedBits8(n,eoflab) do { \
  128. if (BitsAvail < (n)) { \
  129. if (EndOfData()) { \
  130. if (BitsAvail == 0) /* no valid bits */ \
  131. goto eoflab; \
  132. BitsAvail = (n); /* pad with zeros */ \
  133. } else { \
  134. BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
  135. BitsAvail += 8; \
  136. } \
  137. } \
  138. } while (0)
  139. #endif
  140. #ifndef NeedBits16
  141. #define NeedBits16(n,eoflab) do { \
  142. if (BitsAvail < (n)) { \
  143. if (EndOfData()) { \
  144. if (BitsAvail == 0) /* no valid bits */ \
  145. goto eoflab; \
  146. BitsAvail = (n); /* pad with zeros */ \
  147. } else { \
  148. BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
  149. if ((BitsAvail += 8) < (n)) { \
  150. if (EndOfData()) { \
  151. /* NB: we know BitsAvail is non-zero here */ \
  152. BitsAvail = (n); /* pad with zeros */ \
  153. } else { \
  154. BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
  155. BitsAvail += 8; \
  156. } \
  157. } \
  158. } \
  159. } \
  160. } while (0)
  161. #endif
  162. #define GetBits(n) (BitAcc & ((1<<(n))-1))
  163. #define ClrBits(n) do { \
  164. BitsAvail -= (n); \
  165. BitAcc >>= (n); \
  166. } while (0)
  167. #ifdef FAX3_DEBUG
  168. static const char* StateNames[] = {
  169. "Null ",
  170. "Pass ",
  171. "Horiz ",
  172. "V0 ",
  173. "VR ",
  174. "VL ",
  175. "Ext ",
  176. "TermW ",
  177. "TermB ",
  178. "MakeUpW",
  179. "MakeUpB",
  180. "MakeUp ",
  181. "EOL ",
  182. };
  183. #define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0')
  184. #define LOOKUP8(wid,tab,eoflab) do { \
  185. int t; \
  186. NeedBits8(wid,eoflab); \
  187. TabEnt = tab + GetBits(wid); \
  188. printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
  189. StateNames[TabEnt->State], TabEnt->Param); \
  190. for (t = 0; t < TabEnt->Width; t++) \
  191. DEBUG_SHOW; \
  192. putchar('\n'); \
  193. fflush(stdout); \
  194. ClrBits(TabEnt->Width); \
  195. } while (0)
  196. #define LOOKUP16(wid,tab,eoflab) do { \
  197. int t; \
  198. NeedBits16(wid,eoflab); \
  199. TabEnt = tab + GetBits(wid); \
  200. printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
  201. StateNames[TabEnt->State], TabEnt->Param); \
  202. for (t = 0; t < TabEnt->Width; t++) \
  203. DEBUG_SHOW; \
  204. putchar('\n'); \
  205. fflush(stdout); \
  206. ClrBits(TabEnt->Width); \
  207. } while (0)
  208. #define SETVALUE(x) do { \
  209. *pa++ = RunLength + (x); \
  210. printf("SETVALUE: %d\t%d\n", RunLength + (x), a0); \
  211. a0 += x; \
  212. RunLength = 0; \
  213. } while (0)
  214. #else
  215. #define LOOKUP8(wid,tab,eoflab) do { \
  216. NeedBits8(wid,eoflab); \
  217. TabEnt = tab + GetBits(wid); \
  218. ClrBits(TabEnt->Width); \
  219. } while (0)
  220. #define LOOKUP16(wid,tab,eoflab) do { \
  221. NeedBits16(wid,eoflab); \
  222. TabEnt = tab + GetBits(wid); \
  223. ClrBits(TabEnt->Width); \
  224. } while (0)
  225. /*
  226. * Append a run to the run length array for the
  227. * current row and reset decoding state.
  228. */
  229. #define SETVALUE(x) do { \
  230. *pa++ = RunLength + (x); \
  231. a0 += (x); \
  232. RunLength = 0; \
  233. } while (0)
  234. #endif
  235. /*
  236. * Synchronize input decoding at the start of each
  237. * row by scanning for an EOL (if appropriate) and
  238. * skipping any trash data that might be present
  239. * after a decoding error. Note that the decoding
  240. * done elsewhere that recognizes an EOL only consumes
  241. * 11 consecutive zero bits. This means that if EOLcnt
  242. * is non-zero then we still need to scan for the final flag
  243. * bit that is part of the EOL code.
  244. */
  245. #define SYNC_EOL(eoflab) do { \
  246. if (EOLcnt == 0) { \
  247. for (;;) { \
  248. NeedBits16(11,eoflab); \
  249. if (GetBits(11) == 0) \
  250. break; \
  251. ClrBits(1); \
  252. } \
  253. } \
  254. for (;;) { \
  255. NeedBits8(8,eoflab); \
  256. if (GetBits(8)) \
  257. break; \
  258. ClrBits(8); \
  259. } \
  260. while (GetBits(1) == 0) \
  261. ClrBits(1); \
  262. ClrBits(1); /* EOL bit */ \
  263. EOLcnt = 0; /* reset EOL counter/flag */ \
  264. } while (0)
  265. /*
  266. * Cleanup the array of runs after decoding a row.
  267. * We adjust final runs to insure the user buffer is not
  268. * overwritten and/or undecoded area is white filled.
  269. */
  270. #define CLEANUP_RUNS() do { \
  271. if (RunLength) \
  272. SETVALUE(0); \
  273. if (a0 != lastx) { \
  274. badlength(a0, lastx); \
  275. while (a0 > lastx && pa > thisrun) \
  276. a0 -= *--pa; \
  277. if (a0 < lastx) { \
  278. if (a0 < 0) \
  279. a0 = 0; \
  280. if ((pa-thisrun)&1) \
  281. SETVALUE(0); \
  282. SETVALUE(lastx - a0); \
  283. } else if (a0 > lastx) { \
  284. SETVALUE(lastx); \
  285. SETVALUE(0); \
  286. } \
  287. } \
  288. } while (0)
  289. /*
  290. * Decode a line of 1D-encoded data.
  291. *
  292. * The line expanders are written as macros so that they can be reused
  293. * but still have direct access to the local variables of the "calling"
  294. * function.
  295. *
  296. * Note that unlike the original version we have to explicitly test for
  297. * a0 >= lastx after each black/white run is decoded. This is because
  298. * the original code depended on the input data being zero-padded to
  299. * insure the decoder recognized an EOL before running out of data.
  300. */
  301. #define EXPAND1D(eoflab) do { \
  302. for (;;) { \
  303. for (;;) { \
  304. LOOKUP16(12, TIFFFaxWhiteTable, eof1d); \
  305. switch (TabEnt->State) { \
  306. case S_EOL: \
  307. EOLcnt = 1; \
  308. goto done1d; \
  309. case S_TermW: \
  310. SETVALUE(TabEnt->Param); \
  311. goto doneWhite1d; \
  312. case S_MakeUpW: \
  313. case S_MakeUp: \
  314. a0 += TabEnt->Param; \
  315. RunLength += TabEnt->Param; \
  316. break; \
  317. default: \
  318. unexpected("WhiteTable", a0); \
  319. goto done1d; \
  320. } \
  321. } \
  322. doneWhite1d: \
  323. if (a0 >= lastx) \
  324. goto done1d; \
  325. for (;;) { \
  326. LOOKUP16(13, TIFFFaxBlackTable, eof1d); \
  327. switch (TabEnt->State) { \
  328. case S_EOL: \
  329. EOLcnt = 1; \
  330. goto done1d; \
  331. case S_TermB: \
  332. SETVALUE(TabEnt->Param); \
  333. goto doneBlack1d; \
  334. case S_MakeUpB: \
  335. case S_MakeUp: \
  336. a0 += TabEnt->Param; \
  337. RunLength += TabEnt->Param; \
  338. break; \
  339. default: \
  340. unexpected("BlackTable", a0); \
  341. goto done1d; \
  342. } \
  343. } \
  344. doneBlack1d: \
  345. if (a0 >= lastx) \
  346. goto done1d; \
  347. if( *(pa-1) == 0 && *(pa-2) == 0 ) \
  348. pa -= 2; \
  349. } \
  350. eof1d: \
  351. prematureEOF(a0); \
  352. CLEANUP_RUNS(); \
  353. goto eoflab; \
  354. done1d: \
  355. CLEANUP_RUNS(); \
  356. } while (0)
  357. /*
  358. * Update the value of b1 using the array
  359. * of runs for the reference line.
  360. */
  361. #define CHECK_b1 do { \
  362. if (pa != thisrun) while (b1 <= a0 && b1 < lastx) { \
  363. b1 += pb[0] + pb[1]; \
  364. pb += 2; \
  365. } \
  366. } while (0)
  367. /*
  368. * Expand a row of 2D-encoded data.
  369. */
  370. #define EXPAND2D(eoflab) do { \
  371. while (a0 < lastx) { \
  372. LOOKUP8(7, TIFFFaxMainTable, eof2d); \
  373. switch (TabEnt->State) { \
  374. case S_Pass: \
  375. CHECK_b1; \
  376. b1 += *pb++; \
  377. RunLength += b1 - a0; \
  378. a0 = b1; \
  379. b1 += *pb++; \
  380. break; \
  381. case S_Horiz: \
  382. if ((pa-thisrun)&1) { \
  383. for (;;) { /* black first */ \
  384. LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
  385. switch (TabEnt->State) { \
  386. case S_TermB: \
  387. SETVALUE(TabEnt->Param); \
  388. goto doneWhite2da; \
  389. case S_MakeUpB: \
  390. case S_MakeUp: \
  391. a0 += TabEnt->Param; \
  392. RunLength += TabEnt->Param; \
  393. break; \
  394. default: \
  395. goto badBlack2d; \
  396. } \
  397. } \
  398. doneWhite2da:; \
  399. for (;;) { /* then white */ \
  400. LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
  401. switch (TabEnt->State) { \
  402. case S_TermW: \
  403. SETVALUE(TabEnt->Param); \
  404. goto doneBlack2da; \
  405. case S_MakeUpW: \
  406. case S_MakeUp: \
  407. a0 += TabEnt->Param; \
  408. RunLength += TabEnt->Param; \
  409. break; \
  410. default: \
  411. goto badWhite2d; \
  412. } \
  413. } \
  414. doneBlack2da:; \
  415. } else { \
  416. for (;;) { /* white first */ \
  417. LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
  418. switch (TabEnt->State) { \
  419. case S_TermW: \
  420. SETVALUE(TabEnt->Param); \
  421. goto doneWhite2db; \
  422. case S_MakeUpW: \
  423. case S_MakeUp: \
  424. a0 += TabEnt->Param; \
  425. RunLength += TabEnt->Param; \
  426. break; \
  427. default: \
  428. goto badWhite2d; \
  429. } \
  430. } \
  431. doneWhite2db:; \
  432. for (;;) { /* then black */ \
  433. LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
  434. switch (TabEnt->State) { \
  435. case S_TermB: \
  436. SETVALUE(TabEnt->Param); \
  437. goto doneBlack2db; \
  438. case S_MakeUpB: \
  439. case S_MakeUp: \
  440. a0 += TabEnt->Param; \
  441. RunLength += TabEnt->Param; \
  442. break; \
  443. default: \
  444. goto badBlack2d; \
  445. } \
  446. } \
  447. doneBlack2db:; \
  448. } \
  449. CHECK_b1; \
  450. break; \
  451. case S_V0: \
  452. CHECK_b1; \
  453. SETVALUE(b1 - a0); \
  454. b1 += *pb++; \
  455. break; \
  456. case S_VR: \
  457. CHECK_b1; \
  458. SETVALUE(b1 - a0 + TabEnt->Param); \
  459. b1 += *pb++; \
  460. break; \
  461. case S_VL: \
  462. CHECK_b1; \
  463. if (b1 <= (int) (a0 + TabEnt->Param)) { \
  464. if (b1 < (int) (a0 + TabEnt->Param) || pa != thisrun) { \
  465. unexpected("VL", a0); \
  466. goto eol2d; \
  467. } \
  468. } \
  469. SETVALUE(b1 - a0 - TabEnt->Param); \
  470. b1 -= *--pb; \
  471. break; \
  472. case S_Ext: \
  473. *pa++ = lastx - a0; \
  474. extension(a0); \
  475. goto eol2d; \
  476. case S_EOL: \
  477. *pa++ = lastx - a0; \
  478. NeedBits8(4,eof2d); \
  479. if (GetBits(4)) \
  480. unexpected("EOL", a0); \
  481. ClrBits(4); \
  482. EOLcnt = 1; \
  483. goto eol2d; \
  484. default: \
  485. badMain2d: \
  486. unexpected("MainTable", a0); \
  487. goto eol2d; \
  488. badBlack2d: \
  489. unexpected("BlackTable", a0); \
  490. goto eol2d; \
  491. badWhite2d: \
  492. unexpected("WhiteTable", a0); \
  493. goto eol2d; \
  494. eof2d: \
  495. prematureEOF(a0); \
  496. CLEANUP_RUNS(); \
  497. goto eoflab; \
  498. } \
  499. } \
  500. if (RunLength) { \
  501. if (RunLength + a0 < lastx) { \
  502. /* expect a final V0 */ \
  503. NeedBits8(1,eof2d); \
  504. if (!GetBits(1)) \
  505. goto badMain2d; \
  506. ClrBits(1); \
  507. } \
  508. SETVALUE(0); \
  509. } \
  510. eol2d: \
  511. CLEANUP_RUNS(); \
  512. } while (0)
  513. #endif /* _FAX3_ */
  514. /*
  515. * Local Variables:
  516. * mode: c
  517. * c-basic-offset: 8
  518. * fill-column: 78
  519. * End:
  520. */