iljpgenhuff.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: iljpgenhuff.c /main/3 1995/10/23 15:57:13 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1992 Hewlett-Packard Co.
  27. ***
  28. *** RESTRICTED RIGHTS LEGEND
  29. *** Use, duplication, or disclosure by the U.S. Government is subject to
  30. *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
  31. *** Technical Data and Computer Software clause in DFARS 252.227-7013.
  32. *** Hewlett-Packard Company
  33. *** 3000 Hanover Street
  34. *** Palo Alto, CA 94304 U.S.A.
  35. *** Rights for non-DOD U.S. Government Departments and Agencies are as set
  36. *** forth in FAR 52.227-19(c)(1,2).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. #include "iljpgencodeint.h"
  40. /* Indexed by "i" = 0..255, table of # of bits required to store "i". */
  41. static iljpgByte iljpgBitsNeeded[256] = {
  42. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
  43. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  44. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  45. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  46. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  47. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  48. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  49. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  50. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  51. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  52. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  53. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  54. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  55. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  56. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  57. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};
  58. /* Internal representation of a Huffman table. "size" and "code" are
  59. each indexed by the value to be encoded (0..255). "size" is the
  60. # of used (low-order) bits in "code", and "code" contains the actual
  61. Huffman code.
  62. */
  63. typedef struct {
  64. int size[257]; /* called "EHUFSI" in JPEG spec */
  65. int code[257]; /* called "EHUFCO" in JPEG spec */
  66. } iljpgEnhuffTableRec, *iljpgEnhuffTablePtr;
  67. /* Data private to this file. Pointed to by iljpgEncodePrivRec.pHuffPriv.
  68. Each entry in DC/ACTables[] corresponds to the same table entry in
  69. iljpgDataRec (public view), but has been converted for faster access.
  70. The entry in compDC/ACTables[] are the table for that component.
  71. */
  72. typedef struct {
  73. iljpgEnhuffTablePtr DCTables[4];
  74. iljpgEnhuffTablePtr ACTables[4];
  75. iljpgEnhuffTablePtr compDCTables[ILJPG_MAX_COMPS];
  76. iljpgEnhuffTablePtr compACTables[ILJPG_MAX_COMPS];
  77. int huffBits; /* unflushed Huffman encoded bits */
  78. int nHuffBits; /* # of bits in huffBits */
  79. } iljpgEnhuffRec, *iljpgEnhuffPtr;
  80. /* -------------------- _iljpgBuildEnhuffTable -------------------------- */
  81. /* Build and return a local optimized version of the given standard JPEG
  82. Huffman DC/AC table.
  83. */
  84. ILJPG_PRIVATE
  85. iljpgError _iljpgBuildEnhuffTable (
  86. iljpgPtr pTableIn,
  87. iljpgEnhuffTablePtr *ppTableOut /* RETURNED */
  88. )
  89. {
  90. iljpgEnhuffTablePtr pTable;
  91. int i, j, nValues, value;
  92. int huffSize[257]; /* called "HUFFSIZE" in JPEG spec */
  93. int huffCode[257]; /* called "HUFFCODE" in JPEG spec */
  94. /* Malloc space for internal table, return ptr to it */
  95. pTable = (iljpgEnhuffTablePtr)ILJPG_MALLOC_ZERO (sizeof (iljpgEnhuffTableRec));
  96. if (!pTable)
  97. return ILJPG_ERROR_ENCODE_MALLOC;
  98. *ppTableOut = pTable;
  99. /* Fill in internal table, based on method described in C.2 (pg C-2)
  100. of the JPEG spec. pTableIn points to the standard JPEG table:
  101. 16 bytes of the # of codes of each size, followed by "n" bytes of
  102. the value to be associated with the codes, where "n" = sum of the
  103. first 16 bytes.
  104. First generate local size table as in C.2 of spec, leaving
  105. "pTableIn" pointing past first 16 bytes, to "HUFFVAL" in JPEG spec.
  106. */
  107. for (i = 1, nValues = 0; i <= 16; i++) {
  108. value = *pTableIn++;
  109. if ((value + nValues) > 256) /* sum of first 16 bytes too large */
  110. return ILJPG_ERROR_ENCODE_DCAC_TABLE;
  111. while (value-- > 0)
  112. huffSize[nValues++] = i;
  113. }
  114. huffSize[nValues] = 0; /* "nValues" now "LASTK" in spec */
  115. /* Generate HUFFCODE as in fig C.2 (pg C-3) of spec */
  116. i = j = 0; /* i = "K"; j = "CODE" in spec */
  117. value = huffSize[0]; /* "SI" in spec */
  118. while (TRUE) {
  119. do {
  120. huffCode[i] = j;
  121. i++;
  122. j++;
  123. } while (huffSize[i] == value);
  124. if (huffSize[i] == 0) /* terminating 0 seen; done */
  125. break;
  126. do {
  127. j <<= 1;
  128. value++;
  129. } while (huffSize[i] != value);
  130. }
  131. /* Reorder values created above into final tables; fig C.3 (C-4) in spec */
  132. for (j = 0; j < nValues; j++) { /* j = "K" in spec */
  133. i = pTableIn[j];
  134. pTable->code[i] = huffCode[j];
  135. pTable->size[i] = huffSize[j];
  136. }
  137. return 0;
  138. }
  139. /* -------------------- _iljpgEnhuffInit -------------------------- */
  140. /* Called by iljpgEncodeInit() to init for Huffman encoding.
  141. */
  142. ILJPG_PRIVATE
  143. iljpgError _iljpgEnhuffInit (
  144. iljpgEncodePrivPtr pPriv
  145. )
  146. {
  147. iljpgEnhuffPtr pHuffPriv;
  148. iljpgDataPtr pData;
  149. iljpgError error;
  150. int i;
  151. /* Allocate Huffman private area and point to it in encode private */
  152. pData = pPriv->pData;
  153. pHuffPriv = (iljpgEnhuffPtr)ILJPG_MALLOC_ZERO (sizeof (iljpgEnhuffRec));
  154. if (!pHuffPriv)
  155. return ILJPG_ERROR_ENCODE_MALLOC;
  156. pPriv->pHuffPriv = (iljpgPtr)pHuffPriv;
  157. /* For each of caller's DC/AC tables, build a local one for more
  158. efficient access and point to it in private.
  159. */
  160. for (i = 0; i < 4; i++) {
  161. if (pData->DCTables[i]) {
  162. if (error = _iljpgBuildEnhuffTable (pData->DCTables[i],
  163. &pHuffPriv->DCTables[i]))
  164. return error;
  165. }
  166. if (pData->ACTables[i]) {
  167. if (error = _iljpgBuildEnhuffTable (pData->ACTables[i],
  168. &pHuffPriv->ACTables[i]))
  169. return error;
  170. }
  171. }
  172. /* For each component, point to DC/AC tables for that component */
  173. for (i = 0; i < pData->nComps; i++) {
  174. pHuffPriv->compDCTables[i] = pHuffPriv->DCTables[pData->comp[i].DCTableIndex];
  175. pHuffPriv->compACTables[i] = pHuffPriv->ACTables[pData->comp[i].ACTableIndex];
  176. }
  177. return 0;
  178. }
  179. /* -------------------- _iljpgEnhuffCleanup -------------------------- */
  180. /* Called by iljpgEncodeCleanup() to cleanup after Huffman encoding.
  181. */
  182. ILJPG_PRIVATE
  183. iljpgError _iljpgEnhuffCleanup (
  184. iljpgEncodePrivPtr pPriv
  185. )
  186. {
  187. iljpgEnhuffPtr pHuffPriv;
  188. int i;
  189. /* Free the Huffman encode private data including lookup tables */
  190. pHuffPriv = (iljpgEnhuffPtr)pPriv->pHuffPriv;
  191. if (pHuffPriv) {
  192. for (i = 0; i < 4; i++) {
  193. if (pHuffPriv->DCTables[i])
  194. ILJPG_FREE (pHuffPriv->DCTables[i]);
  195. if (pHuffPriv->ACTables[i])
  196. ILJPG_FREE (pHuffPriv->ACTables[i]);
  197. }
  198. ILJPG_FREE (pHuffPriv);
  199. }
  200. return 0;
  201. }
  202. /* -------------------- _iljpgPackHuffman -------------------------- */
  203. /* Pack the list of (size, value) pairs (pointed to by "pHuff", with
  204. "nHuff" pairs in it), with output to "stream".
  205. */
  206. static iljpgError _iljpgPackHuffman (
  207. iljpgEnhuffPtr pHuffPriv,
  208. int *pHuff,
  209. int nHuff,
  210. ILJPG_ENCODE_STREAM stream
  211. )
  212. {
  213. int size, value, huffBits, nHuffBits;
  214. iljpgError error;
  215. static int sizeMasks[9] = /* masks for lower "i" bits, indexed by i */
  216. {0x0, 0x1, 0x3, 0x7, 0x0f, 0x1f, 0x3f, 0x7f, 0x0ff};
  217. /* For each (size, value) pair, pack the lower "size" bits from "value" and
  218. write to the output stream. Bits are packed with MSB first packing.
  219. "nHuffBits" (0..7!) bits are left from before in "huffBits", in the
  220. low-order bits.
  221. After any 0xff, write a 0, as per JPEG spec F.1.2.3 (pg F-7).
  222. */
  223. huffBits = pHuffPriv->huffBits;
  224. nHuffBits = pHuffPriv->nHuffBits;
  225. while (nHuff-- > 0) {
  226. size = *pHuff++;
  227. value = *pHuff++;
  228. while ((nHuffBits + size) >= 8) { /* while a byte or more worth */
  229. nHuffBits = 8 - nHuffBits; /* now # bits that fit in this byte */
  230. huffBits <<= nHuffBits; /* move existing bits up to make room */
  231. size -= nHuffBits; /* # bits from value -= # bits written */
  232. huffBits |= (value >> size) & /* or in upper unwritten bits */
  233. sizeMasks[nHuffBits];
  234. if (!ILJPG_ENCODE_PUT_BYTE (stream, huffBits, error))
  235. return error; /* write a byte */
  236. if (((iljpgByte)huffBits) == ((iljpgByte)0xff))
  237. if (!ILJPG_ENCODE_PUT_BYTE (stream, 0, error))
  238. return error; /* write a 0 after any 0xff */
  239. nHuffBits = 0; /* all Huff bits written out */
  240. }
  241. if (size > 0) { /* bits (if any) fit in less than byte */
  242. huffBits <<= size; /* make room for bits from value */
  243. huffBits |= value & sizeMasks[size]; /* add lower "size" bits from value */
  244. nHuffBits += size; /* "size" more bits now written */
  245. }
  246. } /* END while (size, value) pairs */
  247. /* Save unwritten bits and count of same back into private */
  248. pHuffPriv->huffBits = huffBits;
  249. pHuffPriv->nHuffBits = nHuffBits;
  250. return 0;
  251. }
  252. /* -------------------- _iljpgEnhuffExecute -------------------------- */
  253. /* Huffman encode an 8x8 block (already zig-zagged) from "pSrc" (array of
  254. 64 ints) out to "stream", for component index "comp". The DC component
  255. (pSrc[0]) must have already had the lastDC value subtracted from it.
  256. */
  257. ILJPG_PRIVATE
  258. iljpgError _iljpgEnhuffExecute (
  259. iljpgEncodePrivPtr pPriv,
  260. int comp,
  261. int *pSrc,
  262. ILJPG_ENCODE_STREAM stream
  263. )
  264. {
  265. iljpgEnhuffPtr pHuffPriv;
  266. iljpgEnhuffTablePtr pTable;
  267. int huff[4 * 64 + 10]; /* room for 64 * 2 pairs plus some slop */
  268. int *pHuff;
  269. int size, value, nACLeft, nZeros;
  270. # define ENHUFF(_value) { \
  271. *pHuff++ = pTable->size[_value]; \
  272. *pHuff++ = pTable->code[_value]; \
  273. }
  274. /* Build a list of (size, value) pairs to Huffman encode in "huff", two
  275. per DC or AC coefficient, possibly less for AC due to zero run-lengths.
  276. Use pHuff to index thru it. When done, go back thru list and encode it.
  277. This is done so that encoding can be done without a function call per encode,
  278. and without having to replicate long code all over the place.
  279. */
  280. pHuffPriv = (iljpgEnhuffPtr)pPriv->pHuffPriv;
  281. pHuff = huff;
  282. /* Encode the first (DC) coefficient, which must already be the difference
  283. from the previous DC. See section F.1.2.1 (pg F-3) of the JPEG spec.
  284. Write Huff code for size of DC, then Huff for DC value, -1 if value < 0.
  285. */
  286. pTable = pHuffPriv->compDCTables[comp]; /* use DC table for this component */
  287. value = *pSrc++;
  288. if (value < 0) {
  289. size = -value;
  290. value -= 1;
  291. }
  292. else size = value;
  293. if (size < 256)
  294. size = iljpgBitsNeeded[size];
  295. else size = iljpgBitsNeeded[size>>8] + 8;
  296. ENHUFF (size) /* write huff code for size of DC */
  297. *pHuff++ = size; /* write "size" bits of DC value */
  298. *pHuff++ = value;
  299. /* Encode 63 ACs. See section F.1.2.2 (pg F-4) of JPEG spec.
  300. Each AC is represented by 8 bits: RRRRSSSS, where RRRR is the offset
  301. from the previous non-zero AC - i.e. the # of zero ACs before this one.
  302. 0xf0 = 16 zero ACs; 0 = end of block (EOB), meaning rest of ACs are zero.
  303. SSSS is the "size" of the AC. For each AC (excluding zeros), Huff encode
  304. the 8 bit representation, then write "size" bits as in DC encoding.
  305. */
  306. pTable = pHuffPriv->compACTables[comp]; /* use AC table for all other components */
  307. nACLeft = 63;
  308. nZeros = 0;
  309. while (nACLeft-- > 0) {
  310. value = *pSrc++;
  311. if (value == 0)
  312. nZeros++; /* AC == 0: count it and continue */
  313. else {
  314. while (nZeros >= 16) { /* AC != 0: flush out runs of 16 or more */
  315. nZeros -= 16;
  316. ENHUFF (0xf0)
  317. }
  318. if (value < 0) /* get SSSS = size of "value" */
  319. size = -value;
  320. else size = value;
  321. if (size < 256)
  322. size = iljpgBitsNeeded[size];
  323. else size = iljpgBitsNeeded[size>>8] + 8;
  324. nZeros = (nZeros << 4) + size; /* now have RRRRSSSS in "nZeros" */
  325. ENHUFF (nZeros) /* write huff code for RRRRSSSS */
  326. nZeros = 0; /* reset count of zeros */
  327. *pHuff++ = size; /* write "size" bits of AC value */
  328. if (value < 0) value--; /* -1 if < 0 */
  329. *pHuff++ = value;
  330. } /* END AC != 0 */
  331. } /* END one AC */
  332. /* If any zeros were at end of block, write special EOB value (0) */
  333. if (nZeros > 0) {
  334. ENHUFF (0)
  335. }
  336. /* Now bit encode all (size, value) pairs in "huff" array */
  337. return _iljpgPackHuffman (pHuffPriv, huff, (pHuff - huff) / 2, stream);
  338. }
  339. /* -------------------- _iljpgEnhuffFlush -------------------------- */
  340. /* Flush out any bits left over from Huffman encoding.
  341. */
  342. ILJPG_PRIVATE
  343. iljpgError _iljpgEnhuffFlush (
  344. iljpgEncodePrivPtr pPriv,
  345. ILJPG_ENCODE_STREAM stream
  346. )
  347. {
  348. iljpgEnhuffPtr pHuffPriv;
  349. int nHuffBits, huffBits;
  350. iljpgError error;
  351. /* If any bits left, flush them out. Pad with binary 1's, and
  352. stuff a 0 after any 0xff, as per JPEG spec F.1.2.3 (pg F-7).
  353. */
  354. pHuffPriv = (iljpgEnhuffPtr)pPriv->pHuffPriv;
  355. nHuffBits = pHuffPriv->nHuffBits;
  356. if (nHuffBits > 0) {
  357. huffBits = pHuffPriv->huffBits;
  358. huffBits <<= (8 - nHuffBits); /* move bits to upper bits of byte */
  359. huffBits |= (1 << (8 - nHuffBits)) - 1; /* or in all 1's in unused bits */
  360. pHuffPriv->nHuffBits = 0; /* now no bits left to output */
  361. if (!ILJPG_ENCODE_PUT_BYTE (stream, huffBits, error))
  362. return error; /* write a byte */
  363. if (((iljpgByte)huffBits) == ((iljpgByte)0xff))
  364. if (!ILJPG_ENCODE_PUT_BYTE (stream, 0, error))
  365. return error; /* write a 0 after any 0xff */
  366. }
  367. return 0;
  368. }