iljpgdecodejif.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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: iljpgdecodejif.c /main/3 1995/10/23 15:55:08 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 <stdlib.h>
  40. #include "iljpgdecodeint.h"
  41. /* -------------------- iljpgFreeData -------------------------- */
  42. /* Free the given data block and associated tables, for example as
  43. created by iljpgDecodeJIF().
  44. */
  45. ILJPG_PUBLIC_EXTERN
  46. iljpgError iljpgFreeData (
  47. iljpgDataPtr pData
  48. )
  49. {
  50. int i;
  51. if (!pData) /* null data ptr; exit */
  52. return 0;
  53. for (i = 0; i < 4; i++) {
  54. if (pData->QTables[i])
  55. ILJPG_FREE (pData->QTables[i]);
  56. if (pData->DCTables[i])
  57. ILJPG_FREE (pData->DCTables[i]);
  58. if (pData->ACTables[i])
  59. ILJPG_FREE (pData->ACTables[i]);
  60. }
  61. ILJPG_FREE (pData);
  62. return 0;
  63. }
  64. /* -------------------- iljpgGet2Bytes -------------------------- */
  65. /* Get 2 bytes from the given stream and return (short) value or error.
  66. */
  67. static iljpgError iljpgGet2Bytes (
  68. ILJPG_DECODE_STREAM stream,
  69. int *pValue /* RETURNED */
  70. )
  71. {
  72. int byte0, byte1;
  73. iljpgError error;
  74. if (!ILJPG_DECODE_GET_BYTE (stream, byte0, error))
  75. return error;
  76. if (!ILJPG_DECODE_GET_BYTE (stream, byte1, error))
  77. return error;
  78. *pValue = (byte0 << 8) + byte1;
  79. return 0;
  80. }
  81. /* -------------------- iljpgSkipBytes -------------------------- */
  82. /* Skip given # of bytes in JPEG stream
  83. */
  84. static iljpgError iljpgSkipBytes (
  85. ILJPG_DECODE_STREAM stream,
  86. int nBytes
  87. )
  88. {
  89. int byte;
  90. iljpgError error;
  91. while (nBytes-- > 0) {
  92. if (!ILJPG_DECODE_GET_BYTE (stream, byte, error))
  93. return error;
  94. }
  95. return 0;
  96. }
  97. /* -------------------- iljpgDecodeJIF -------------------------- */
  98. /* Parse the given stream which must point to the beginning of a
  99. JIF (JPEG Interchange Format) stream. Return a ptr to a created
  100. iljpgData block, which can be freed by iljpgFreeData(), or null
  101. if an error occurs (non-zero error code returned).
  102. */
  103. ILJPG_PUBLIC_EXTERN
  104. iljpgError iljpgDecodeJIF (
  105. ILJPG_DECODE_STREAM stream,
  106. iljpgDataPtr *ppData /* RETURNED */
  107. )
  108. {
  109. iljpgDataPtr pData;
  110. iljpgBool firstMarker, SOSFound, SOF0Found;
  111. iljpgError error;
  112. int value, nFrameComps = 0, length;
  113. iljpgPtr *ppTable;
  114. iljpgCompDataPtr pComp;
  115. int i, index;
  116. iljpgPtr pTable;
  117. struct {
  118. int id, horiFactor, vertFactor, QTableIndex;
  119. } frameComp[ILJPG_MAX_COMPS], *pFrame;
  120. pData = (iljpgDataPtr)ILJPG_MALLOC_ZERO (sizeof (iljpgDataRec));
  121. if (!pData)
  122. return ILJPG_ERROR_DECODE_MALLOC;
  123. firstMarker = TRUE;
  124. SOSFound = FALSE;
  125. SOF0Found = FALSE;
  126. /* Parse input stream until SOS marker found.
  127. Note that the "length" value following most markers includes the length
  128. of length itself, and thus must be made -2 to get length of rest of data.
  129. */
  130. while (!SOSFound) {
  131. do { /* get a marker */
  132. do {
  133. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  134. goto JIFError;
  135. } while (value != ILJPGM_FIRST_BYTE);
  136. do {
  137. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  138. goto JIFError;
  139. } while (value == ILJPGM_FIRST_BYTE);
  140. } while (!value); /* skip "0" after ff */
  141. /* First marker must be SOI or error; else parse marker */
  142. if (firstMarker) {
  143. if (value != ILJPGM_SOI)
  144. goto BadJIFData;
  145. firstMarker = FALSE;
  146. }
  147. else switch (value) {
  148. case ILJPGM_TEM:
  149. break; /* legal (?) "stand-alone" markers - no data to skip */
  150. case ILJPGM_SOI:
  151. case ILJPGM_RST0: case ILJPGM_RST1: case ILJPGM_RST2: case ILJPGM_RST3:
  152. case ILJPGM_RST4: case ILJPGM_RST5: case ILJPGM_RST6: case ILJPGM_RST7:
  153. case ILJPGM_EOI:
  154. goto BadJIFData;
  155. break; /* illegal markers - should not see these before SOS */
  156. /* DRI: get value and save into restartInterval */
  157. case ILJPGM_DRI:
  158. if ((error = iljpgGet2Bytes (stream, &value))) /* length */
  159. goto JIFError;
  160. if ((error = iljpgGet2Bytes (stream, &value))) /* restart interval */
  161. goto JIFError;
  162. pData->restartInterval = value;
  163. break;
  164. /* Start Of Frame 0 (SOF0): save Q table indices; note marker seen. */
  165. case ILJPGM_SOF0:
  166. SOF0Found = TRUE;
  167. if ((error = iljpgGet2Bytes (stream, &value))) /* length */
  168. goto JIFError;
  169. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  170. goto JIFError;
  171. if (value != 8) /* precision != 8; not supported */
  172. goto BadJIFData;
  173. if ((error = iljpgGet2Bytes (stream, &pData->height)))
  174. goto JIFError;
  175. if ((error = iljpgGet2Bytes (stream, &pData->width)))
  176. goto JIFError;
  177. if (!ILJPG_DECODE_GET_BYTE (stream, nFrameComps, error))
  178. goto JIFError;
  179. if (nFrameComps > ILJPG_MAX_COMPS)
  180. goto BadJIFData;
  181. /* Get and store info for each frame component (Ci, Hi, Vi, Tqi) */
  182. for (i = 0, pFrame = frameComp; i < nFrameComps; i++, pFrame++) {
  183. if (!ILJPG_DECODE_GET_BYTE (stream, pFrame->id, error))
  184. goto JIFError;
  185. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  186. goto JIFError;
  187. pFrame->horiFactor = value >> 4; /* Hi in upper nibble */
  188. pFrame->vertFactor = value & 0xf; /* Vi in lower nibble */
  189. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  190. goto JIFError;
  191. if (value > 3) /* Tqi must be 0..3 */
  192. goto BadJIFData;
  193. pFrame->QTableIndex = value;
  194. }
  195. break;
  196. /* Define Quantization Table (DQT): malloc memory for table and set into
  197. pData->QTables for this index, freeing table if already there.
  198. Multiple tables ("n") can be defined: n = (length-2)/65;
  199. */
  200. case ILJPGM_DQT:
  201. if ((error = iljpgGet2Bytes (stream, &length))) /* length */
  202. goto JIFError;
  203. length -= 2; /* includes itself */
  204. while (length > 0) {
  205. if ((length -= 65) < 0) /* at least 65 bytes */
  206. goto JIFError;
  207. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Pq (0), Tq */
  208. goto JIFError;
  209. value &= 0xf; /* table index (0..3) */
  210. if (value > (ILJPG_MAX_COMPS-1))
  211. goto BadJIFData;
  212. /* Read bytes into malloc'd space, free old if non-null and store */
  213. if (!(pTable = (iljpgPtr)ILJPG_MALLOC (64))) {
  214. error = ILJPG_ERROR_DECODE_MALLOC;
  215. goto JIFError;
  216. }
  217. if (pData->QTables[value])
  218. ILJPG_FREE (pData->QTables[value]);
  219. pData->QTables[value] = pTable;
  220. for (i = 0; i < 64; i++) {
  221. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  222. goto JIFError;
  223. *pTable++ = value;
  224. }
  225. }
  226. break;
  227. /* Define Huffman Table (DHT): see comment for DQT.
  228. Multiple tables can be defined in this marker; the "length" value must
  229. be used; = 2 * n * (17 + m), where "n" is the number of tables, and "m"
  230. is the sum of 16 length bytes (bytes 1..16, after Th).
  231. The table index (Th) is 0,1 for DC tables 0,1 or 16, 17 for AC tables 0,1.
  232. 4 AC or DC tables are allowed,used by "extended" DCT mode.
  233. */
  234. case ILJPGM_DHT:
  235. if ((error = iljpgGet2Bytes (stream, &length)))
  236. goto JIFError;
  237. length -= 2; /* length includes itself */
  238. while (length > 0) {
  239. iljpgByte L[16];
  240. int sumL;
  241. if ((length -= 17) < 0) /* sub Th, 16 bytes of Li */
  242. goto JIFError;
  243. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Th */
  244. goto JIFError;
  245. i = value & 0xf;
  246. if (i > (ILJPG_MAX_COMPS-1))
  247. goto BadJIFData;
  248. if (value >= 16)
  249. ppTable = &pData->ACTables[i];
  250. else ppTable = &pData->DCTables[i];
  251. /* Read 16 bytes of Li; set sumL to their sum */
  252. for (i = 0, sumL = 0; i < 16; i++) {
  253. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Th */
  254. goto JIFError;
  255. sumL += value;
  256. L[i] = value;
  257. }
  258. /* Malloc space for table; copy 1st 16 bytes; read in rest */
  259. if ((length -= sumL) < 0) /* sub # bytes of huff codes */
  260. goto JIFError;
  261. if (!(pTable = (iljpgPtr)ILJPG_MALLOC (16 + sumL))) {
  262. error = ILJPG_ERROR_DECODE_MALLOC;
  263. goto JIFError;
  264. }
  265. if (*ppTable)
  266. ILJPG_FREE (*ppTable);
  267. *ppTable = pTable;
  268. for (i = 0; i < 16; i++)
  269. *pTable++ = L[i];
  270. for (i = 0; i < sumL; i++) {
  271. if (!ILJPG_DECODE_GET_BYTE (stream, value, error))
  272. goto JIFError;
  273. *pTable++ = value;
  274. }
  275. } /* END while length > 0 */
  276. break;
  277. /* Start Of Scan (SOS): read the scan header and leave stream positioned
  278. right after header, i.e. pointing to the image data.
  279. */
  280. case ILJPGM_SOS:
  281. SOSFound = TRUE; /* terminate while loop */
  282. if (!SOF0Found) /* SOS before SOF0 is an error */
  283. goto BadJIFData;
  284. if ((error = iljpgGet2Bytes (stream, &value))) /* length */
  285. goto JIFError;
  286. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Ns (0..3) */
  287. goto JIFError;
  288. if (value > (ILJPG_MAX_COMPS-1))
  289. goto BadJIFData;
  290. pData->nComps = value;
  291. /* For each scan component: get "id" and find component with same id
  292. in frameComponents or error. Copy component data from frame to
  293. pData->component, and get DC/AC table indices from stream.
  294. Set pData->maxHori/VertFactor to largest component factor found.
  295. */
  296. for (i = 0, pComp = pData->comp; i < pData->nComps; i++, pComp++) {
  297. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Csj (id) */
  298. goto JIFError;
  299. pFrame = &frameComp[nFrameComps - 1];
  300. while (TRUE) {
  301. if (pFrame->id == value)
  302. break;
  303. if (pFrame == frameComp)
  304. goto BadJIFData; /* frames searched; id not found */
  305. pFrame--; /* seach back one */
  306. }
  307. /* pFrame points to frame with given id; copy data to *pComp */
  308. pComp->horiFactor = pFrame->horiFactor;
  309. if (pComp->horiFactor > pData->maxHoriFactor)
  310. pData->maxHoriFactor = pComp->horiFactor;
  311. pComp->vertFactor = pFrame->vertFactor;
  312. if (pComp->vertFactor > pData->maxVertFactor)
  313. pData->maxVertFactor = pComp->vertFactor;
  314. /* Get DC/AC table index; error if that or Q table not defd yet. */
  315. if (!ILJPG_DECODE_GET_BYTE (stream, value, error)) /* Tdj/Taj */
  316. goto JIFError;
  317. index = (value >> 4) & 3; /* DC table index, 0..3 */
  318. if (!pData->DCTables[index])
  319. goto BadJIFData;
  320. pComp->DCTableIndex = index;
  321. index = value & 3; /* AC table index, 0..3 */
  322. if (!pData->ACTables[index])
  323. goto BadJIFData;
  324. pComp->ACTableIndex = index;
  325. pComp->QTableIndex = pFrame->QTableIndex;
  326. if (!pData->QTables[pComp->QTableIndex])
  327. goto BadJIFData;
  328. } /* END each scan header component */
  329. if ((error = iljpgSkipBytes (stream, 3))) /* skip Ss, Se, Ah/Al */
  330. goto JIFError;
  331. break;
  332. /* All other markers have lengths: get length and skip length-2 bytes */
  333. default:
  334. if ((error = iljpgGet2Bytes (stream, &value)))
  335. goto JIFError;
  336. if ((error = iljpgSkipBytes (stream, value - 2))) /* "length" already skipped */
  337. goto JIFError;
  338. break;
  339. } /* END switch marker */
  340. } /* END while (!SOSFound) */
  341. *ppData = pData; /* return filled in data block */
  342. return 0;
  343. /* Branch point if bad JPEG data */
  344. BadJIFData:
  345. iljpgFreeData (pData);
  346. return ILJPG_ERROR_DECODE_JIF;
  347. /* Branch point if other error; return "error" */
  348. JIFError:
  349. iljpgFreeData (pData);
  350. return error;
  351. }