tjpgd.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*----------------------------------------------------------------------------/
  2. / TJpgDec - Tiny JPEG Decompressor R0.01a (C)ChaN, 2012
  3. /-----------------------------------------------------------------------------/
  4. / The TJpgDec is a generic JPEG decompressor module for tiny embedded systems.
  5. / This is a free software that opened for education, research and commercial
  6. / developments under license policy of following terms.
  7. /
  8. / Copyright (C) 2012, ChaN, all right reserved.
  9. /
  10. / * The TJpgDec module is a free software and there is NO WARRANTY.
  11. / * No restriction on use. You can use, modify and redistribute it for
  12. / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /-----------------------------------------------------------------------------/
  16. / Oct 04,'11 R0.01 First release.
  17. / Jan 25,'12 Modified by onelife
  18. / Feb 19,'12 R0.01a Fixed decompression fails when scan starts with an escape seq.
  19. /----------------------------------------------------------------------------*/
  20. #include "tjpgd.h"
  21. /*-----------------------------------------------*/
  22. /* Zigzag-order to raster-order conversion table */
  23. /*-----------------------------------------------*/
  24. #define ZIG(n) Zig[n]
  25. static
  26. const BYTE Zig[64] = { /* Zigzag-order to raster-order conversion table */
  27. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  28. 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  29. 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  30. 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  31. };
  32. /*-------------------------------------------------*/
  33. /* Input scale factor of Arai algorithm */
  34. /* (scaled up 16 bits for fixed point operations) */
  35. /*-------------------------------------------------*/
  36. #define IPSF(n) Ipsf[n]
  37. static
  38. const WORD Ipsf[64] = { /* See also aa_idct.png */
  39. (WORD)(1.00000*8192), (WORD)(1.38704*8192), (WORD)(1.30656*8192), (WORD)(1.17588*8192), (WORD)(1.00000*8192), (WORD)(0.78570*8192), (WORD)(0.54120*8192), (WORD)(0.27590*8192),
  40. (WORD)(1.38704*8192), (WORD)(1.92388*8192), (WORD)(1.81226*8192), (WORD)(1.63099*8192), (WORD)(1.38704*8192), (WORD)(1.08979*8192), (WORD)(0.75066*8192), (WORD)(0.38268*8192),
  41. (WORD)(1.30656*8192), (WORD)(1.81226*8192), (WORD)(1.70711*8192), (WORD)(1.53636*8192), (WORD)(1.30656*8192), (WORD)(1.02656*8192), (WORD)(0.70711*8192), (WORD)(0.36048*8192),
  42. (WORD)(1.17588*8192), (WORD)(1.63099*8192), (WORD)(1.53636*8192), (WORD)(1.38268*8192), (WORD)(1.17588*8192), (WORD)(0.92388*8192), (WORD)(0.63638*8192), (WORD)(0.32442*8192),
  43. (WORD)(1.00000*8192), (WORD)(1.38704*8192), (WORD)(1.30656*8192), (WORD)(1.17588*8192), (WORD)(1.00000*8192), (WORD)(0.78570*8192), (WORD)(0.54120*8192), (WORD)(0.27590*8192),
  44. (WORD)(0.78570*8192), (WORD)(1.08979*8192), (WORD)(1.02656*8192), (WORD)(0.92388*8192), (WORD)(0.78570*8192), (WORD)(0.61732*8192), (WORD)(0.42522*8192), (WORD)(0.21677*8192),
  45. (WORD)(0.54120*8192), (WORD)(0.75066*8192), (WORD)(0.70711*8192), (WORD)(0.63638*8192), (WORD)(0.54120*8192), (WORD)(0.42522*8192), (WORD)(0.29290*8192), (WORD)(0.14932*8192),
  46. (WORD)(0.27590*8192), (WORD)(0.38268*8192), (WORD)(0.36048*8192), (WORD)(0.32442*8192), (WORD)(0.27590*8192), (WORD)(0.21678*8192), (WORD)(0.14932*8192), (WORD)(0.07612*8192)
  47. };
  48. /*---------------------------------------------*/
  49. /* Conversion table for fast clipping process */
  50. /*---------------------------------------------*/
  51. #define BYTECLIP(v) Clip8[(UINT)(v) & 0x3FF]
  52. static
  53. const BYTE Clip8[1024] = {
  54. /* 0..255 */
  55. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  56. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  57. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  58. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  59. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  60. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  61. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  62. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
  63. /* 256..511 */
  64. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  65. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  66. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  67. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  68. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  69. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  70. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  71. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  72. /* -512..-257 */
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81. /* -256..-1 */
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  90. };
  91. /*-----------------------------------------------------------------------*/
  92. /* Allocate a memory block from memory pool */
  93. /*-----------------------------------------------------------------------*/
  94. static
  95. void* alloc_pool ( /* Pointer to allocated memory block (NULL:no memory available) */
  96. JDEC* jd, /* Pointer to the decompressor object */
  97. UINT nd /* Number of bytes to allocate */
  98. )
  99. {
  100. char *rp = 0;
  101. nd = (nd + 3) & ~3; /* Align block size to the word boundary */
  102. if (jd->sz_pool >= nd) {
  103. jd->sz_pool -= nd;
  104. rp = (char*)jd->pool; /* Get start of available memory pool */
  105. jd->pool = (void*)(rp + nd); /* Allocate requierd bytes */
  106. }
  107. return (void*)rp; /* Return allocated memory block (NULL:no memory to allocate) */
  108. }
  109. /*-----------------------------------------------------------------------*/
  110. /* Create de-quantization and prescaling tables with a DQT segment */
  111. /*-----------------------------------------------------------------------*/
  112. static
  113. UINT create_qt_tbl ( /* 0:OK, !0:Failed */
  114. JDEC* jd, /* Pointer to the decompressor object */
  115. const BYTE* data, /* Pointer to the quantizer tables */
  116. UINT ndata /* Size of input data */
  117. )
  118. {
  119. UINT i;
  120. BYTE d, z;
  121. LONG *pb;
  122. while (ndata) { /* Process all tables in the segment */
  123. if (ndata < 65) return JDR_FMT1; /* Err: table size is unaligned */
  124. ndata -= 65;
  125. d = *data++; /* Get table property */
  126. if (d & 0xF0) return JDR_FMT1; /* Err: not 8-bit resolution */
  127. i = d & 3; /* Get table ID */
  128. pb = alloc_pool(jd, 64 * sizeof (LONG));/* Allocate a memory block for the table */
  129. if (!pb) return JDR_MEM1; /* Err: not enough memory */
  130. jd->qttbl[i] = pb; /* Register the table */
  131. for (i = 0; i < 64; i++) { /* Load the table */
  132. z = ZIG(i); /* Zigzag-order to raster-order conversion */
  133. pb[z] = (LONG)((DWORD)*data++ * IPSF(z)); /* Apply scale factor of Arai algorithm to the de-quantizers */
  134. }
  135. }
  136. return JDR_OK;
  137. }
  138. /*-----------------------------------------------------------------------*/
  139. /* Create huffman code tables with a DHT segment */
  140. /*-----------------------------------------------------------------------*/
  141. static
  142. UINT create_huffman_tbl ( /* 0:OK, !0:Failed */
  143. JDEC* jd, /* Pointer to the decompressor object */
  144. const BYTE* data, /* Pointer to the packed huffman tables */
  145. UINT ndata /* Size of input data */
  146. )
  147. {
  148. UINT i, j, b, np, cls, num;
  149. BYTE d, *pb, *pd;
  150. WORD hc, *ph;
  151. while (ndata) { /* Process all tables in the segment */
  152. if (ndata < 17) return JDR_FMT1; /* Err: wrong data size */
  153. ndata -= 17;
  154. d = *data++; /* Get table number and class */
  155. cls = (d >> 4); num = d & 0x0F; /* class = dc(0)/ac(1), table number = 0/1 */
  156. if (d & 0xEE) return JDR_FMT1; /* Err: invalid class/number */
  157. pb = alloc_pool(jd, 16); /* Allocate a memory block for the bit distribution table */
  158. if (!pb) return JDR_MEM1; /* Err: not enough memory */
  159. jd->huffbits[num][cls] = pb;
  160. for (np = i = 0; i < 16; i++) { /* Load number of patterns for 1 to 16-bit code */
  161. pb[i] = b = *data++;
  162. np += b; /* Get sum of code words for each code */
  163. }
  164. ph = alloc_pool(jd, np * sizeof (WORD));/* Allocate a memory block for the code word table */
  165. if (!ph) return JDR_MEM1; /* Err: not enough memory */
  166. jd->huffcode[num][cls] = ph;
  167. hc = 0;
  168. for (j = i = 0; i < 16; i++) { /* Re-build huffman code word table */
  169. b = pb[i];
  170. while (b--) ph[j++] = hc++;
  171. hc <<= 1;
  172. }
  173. if (ndata < np) return JDR_FMT1; /* Err: wrong data size */
  174. ndata -= np;
  175. pd = alloc_pool(jd, np); /* Allocate a memory block for the decoded data */
  176. if (!pd) return JDR_MEM1; /* Err: not enough memory */
  177. jd->huffdata[num][cls] = pd;
  178. for (i = 0; i < np; i++) { /* Load decoded data corresponds to each code ward */
  179. d = *data++;
  180. if (!cls && d > 11) return JDR_FMT1;
  181. *pd++ = d;
  182. }
  183. }
  184. return JDR_OK;
  185. }
  186. /*-----------------------------------------------------------------------*/
  187. /* Extract N bits from input stream */
  188. /*-----------------------------------------------------------------------*/
  189. static
  190. INT bitext ( /* >=0: extracted data, <0: error code */
  191. JDEC* jd, /* Pointer to the decompressor object */
  192. UINT nbit /* Number of bits to extract (1 to 11) */
  193. )
  194. {
  195. BYTE msk, s, *dp;
  196. UINT dc, v, f;
  197. msk = jd->dmsk; dc = jd->dctr; dp = jd->dptr; /* Bit mask, number of data available, read ptr */
  198. s = *dp; v = f = 0;
  199. do {
  200. if (!msk) { /* Next byte? */
  201. if (!dc) { /* No input data is available, re-fill input buffer */
  202. dp = jd->inbuf; /* Top of input buffer */
  203. dc = jd->infunc(jd, dp, JD_SZBUF);
  204. if (!dc) return 0 - JDR_INP; /* Err: read error or wrong stream termination */
  205. } else {
  206. dp++; /* Next data ptr */
  207. }
  208. dc--; /* Decrement number of available bytes */
  209. if (f) { /* In flag sequence? */
  210. f = 0; /* Exit flag sequence */
  211. if (*dp != 0) return 0 - JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
  212. *dp = s = 0xFF; /* The flag is a data 0xFF */
  213. } else {
  214. s = *dp; /* Get next data byte */
  215. if (s == 0xFF) { /* Is start of flag sequence? */
  216. f = 1; continue; /* Enter flag sequence */
  217. }
  218. }
  219. msk = 0x80; /* Read from MSB */
  220. }
  221. v <<= 1; /* Get a bit */
  222. if (s & msk) v++;
  223. msk >>= 1;
  224. nbit--;
  225. } while (nbit);
  226. jd->dmsk = msk; jd->dctr = dc; jd->dptr = dp;
  227. return (INT)v;
  228. }
  229. /*-----------------------------------------------------------------------*/
  230. /* Extract a huffman decoded data from input stream */
  231. /*-----------------------------------------------------------------------*/
  232. static
  233. INT huffext ( /* >=0: decoded data, <0: error code */
  234. JDEC* jd, /* Pointer to the decompressor object */
  235. const BYTE* hbits, /* Pointer to the bit distribution table */
  236. const WORD* hcode, /* Pointer to the code word table */
  237. const BYTE* hdata /* Pointer to the data table */
  238. )
  239. {
  240. BYTE msk, s, *dp;
  241. UINT dc, v, f, bl, nd;
  242. msk = jd->dmsk; dc = jd->dctr; dp = jd->dptr; /* Bit mask, number of data available, read ptr */
  243. s = *dp; v = f = 0;
  244. bl = 16; /* Max code length */
  245. do {
  246. if (!msk) { /* Next byte? */
  247. if (!dc) { /* No input data is available, re-fill input buffer */
  248. dp = jd->inbuf; /* Top of input buffer */
  249. dc = jd->infunc(jd, dp, JD_SZBUF);
  250. if (!dc) return 0 - JDR_INP; /* Err: read error or wrong stream termination */
  251. } else {
  252. dp++; /* Next data ptr */
  253. }
  254. dc--; /* Decrement number of available bytes */
  255. if (f) { /* In flag sequence? */
  256. f = 0; /* Exit flag sequence */
  257. if (*dp != 0)
  258. return 0 - JDR_FMT1;/* Err: unexpected flag is detected (may be collapted data) */
  259. *dp = s = 0xFF; /* The flag is a data 0xFF */
  260. } else {
  261. s = *dp; /* Get next data byte */
  262. if (s == 0xFF) { /* Is start of flag sequence? */
  263. f = 1; continue; /* Enter flag sequence, get trailing byte */
  264. }
  265. }
  266. msk = 0x80; /* Read from MSB */
  267. }
  268. v <<= 1; /* Get a bit */
  269. if (s & msk) v++;
  270. msk >>= 1;
  271. for (nd = *hbits++; nd; nd--) { /* Search the code word in this bit length */
  272. if (v == *hcode++) { /* Matched? */
  273. jd->dmsk = msk; jd->dctr = dc; jd->dptr = dp;
  274. return *hdata; /* Return the decoded data */
  275. }
  276. hdata++;
  277. }
  278. bl--;
  279. } while (bl);
  280. return 0 - JDR_FMT1; /* Err: code not found (may be collapted data) */
  281. }
  282. /*-----------------------------------------------------------------------*/
  283. /* Apply Inverse-DCT in Arai Algorithm (see also aa_idct.png) */
  284. /*-----------------------------------------------------------------------*/
  285. static
  286. void block_idct (
  287. LONG* src, /* Input block data (de-quantized and pre-scaled for Arai Algorithm) */
  288. BYTE* dst /* Pointer to the destination to store the block as byte array */
  289. )
  290. {
  291. const LONG M13 = (LONG)(1.41421*4096), M2 = (LONG)(1.08239*4096), M4 = (LONG)(2.61313*4096), M5 = (LONG)(1.84776*4096);
  292. LONG v0, v1, v2, v3, v4, v5, v6, v7;
  293. LONG t10, t11, t12, t13;
  294. UINT i;
  295. /* Process columns */
  296. for (i = 0; i < 8; i++) {
  297. v0 = src[8 * 0]; /* Get even elements */
  298. v1 = src[8 * 2];
  299. v2 = src[8 * 4];
  300. v3 = src[8 * 6];
  301. t10 = v0 + v2; /* Process the even elements */
  302. t12 = v0 - v2;
  303. t11 = (v1 - v3) * M13 >> 12;
  304. v3 += v1;
  305. t11 -= v3;
  306. v0 = t10 + v3;
  307. v3 = t10 - v3;
  308. v1 = t11 + t12;
  309. v2 = t12 - t11;
  310. v4 = src[8 * 7]; /* Get odd elements */
  311. v5 = src[8 * 1];
  312. v6 = src[8 * 5];
  313. v7 = src[8 * 3];
  314. t10 = v5 - v4; /* Process the odd elements */
  315. t11 = v5 + v4;
  316. t12 = v6 - v7;
  317. v7 += v6;
  318. v5 = (t11 - v7) * M13 >> 12;
  319. v7 += t11;
  320. t13 = (t10 + t12) * M5 >> 12;
  321. v4 = t13 - (t10 * M2 >> 12);
  322. v6 = t13 - (t12 * M4 >> 12) - v7;
  323. v5 -= v6;
  324. v4 -= v5;
  325. src[8 * 0] = v0 + v7; /* Write-back transformed values */
  326. src[8 * 7] = v0 - v7;
  327. src[8 * 1] = v1 + v6;
  328. src[8 * 6] = v1 - v6;
  329. src[8 * 2] = v2 + v5;
  330. src[8 * 5] = v2 - v5;
  331. src[8 * 3] = v3 + v4;
  332. src[8 * 4] = v3 - v4;
  333. src++; /* Next column */
  334. }
  335. /* Process rows */
  336. src -= 8;
  337. for (i = 0; i < 8; i++) {
  338. v0 = src[0] + (128L << 8); /* Get even elements (remove DC offset (-128) here) */
  339. v1 = src[2];
  340. v2 = src[4];
  341. v3 = src[6];
  342. t10 = v0 + v2; /* Process the even elements */
  343. t12 = v0 - v2;
  344. t11 = (v1 - v3) * M13 >> 12;
  345. v3 += v1;
  346. t11 -= v3;
  347. v0 = t10 + v3;
  348. v3 = t10 - v3;
  349. v1 = t11 + t12;
  350. v2 = t12 - t11;
  351. v4 = src[7]; /* Get odd elements */
  352. v5 = src[1];
  353. v6 = src[5];
  354. v7 = src[3];
  355. t10 = v5 - v4; /* Process the odd elements */
  356. t11 = v5 + v4;
  357. t12 = v6 - v7;
  358. v7 += v6;
  359. v5 = (t11 - v7) * M13 >> 12;
  360. v7 += t11;
  361. t13 = (t10 + t12) * M5 >> 12;
  362. v4 = t13 - (t10 * M2 >> 12);
  363. v6 = t13 - (t12 * M4 >> 12) - v7;
  364. v5 -= v6;
  365. v4 -= v5;
  366. dst[0] = BYTECLIP((v0 + v7) >> 8); /* Descale the transformed values 8 bits and output */
  367. dst[7] = BYTECLIP((v0 - v7) >> 8);
  368. dst[1] = BYTECLIP((v1 + v6) >> 8);
  369. dst[6] = BYTECLIP((v1 - v6) >> 8);
  370. dst[2] = BYTECLIP((v2 + v5) >> 8);
  371. dst[5] = BYTECLIP((v2 - v5) >> 8);
  372. dst[3] = BYTECLIP((v3 + v4) >> 8);
  373. dst[4] = BYTECLIP((v3 - v4) >> 8);
  374. dst += 8;
  375. src += 8; /* Next row */
  376. }
  377. }
  378. /*-----------------------------------------------------------------------*/
  379. /* Load all blocks in the MCU into working buffer */
  380. /*-----------------------------------------------------------------------*/
  381. static
  382. JRESULT mcu_load (
  383. JDEC* jd /* Pointer to the decompressor object */
  384. )
  385. {
  386. LONG *tmp = (LONG*)jd->workbuf; /* Block working buffer for de-quantize and IDCT */
  387. UINT blk, nby, nbc, i, z, id, cmp;
  388. INT b, d, e;
  389. BYTE *bp;
  390. const BYTE *hb, *hd;
  391. const WORD *hc;
  392. const LONG *dqf;
  393. nby = jd->msx * jd->msy; /* Number of Y blocks (1, 2 or 4) */
  394. nbc = 2; /* Number of C blocks (2) */
  395. bp = jd->mcubuf; /* Pointer to the first block */
  396. for (blk = 0; blk < nby + nbc; blk++) {
  397. cmp = (blk < nby) ? 0 : blk - nby + 1; /* Component number 0:Y, 1:Cb, 2:Cr */
  398. id = cmp ? 1 : 0; /* Huffman table ID of the component */
  399. /* Extract a DC element from input stream */
  400. hb = jd->huffbits[id][0]; /* Huffman table for the DC element */
  401. hc = jd->huffcode[id][0];
  402. hd = jd->huffdata[id][0];
  403. b = huffext(jd, hb, hc, hd); /* Extract a huffman coded data (bit length) */
  404. if (b < 0) return 0 - b; /* Err: invalid code or input */
  405. d = jd->dcv[cmp]; /* DC value of previous block */
  406. if (b) { /* If there is any difference from previous block */
  407. e = bitext(jd, b); /* Extract data bits */
  408. if (e < 0) return 0 - e; /* Err: input */
  409. b = 1 << (b - 1); /* MSB position */
  410. if (!(e & b)) e -= (b << 1) - 1; /* Restore sign if needed */
  411. d += e; /* Get current value */
  412. jd->dcv[cmp] = (SHORT)d; /* Save current DC value for next block */
  413. }
  414. dqf = jd->qttbl[jd->qtid[cmp]]; /* De-quantizer table ID for this component */
  415. tmp[0] = d * dqf[0] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
  416. /* Extract following 63 AC elements from input stream */
  417. for (i = 1; i < 64; i++) tmp[i] = 0; /* Clear rest of elements */
  418. hb = jd->huffbits[id][1]; /* Huffman table for the AC elements */
  419. hc = jd->huffcode[id][1];
  420. hd = jd->huffdata[id][1];
  421. i = 1; /* Top of the AC elements */
  422. do {
  423. b = huffext(jd, hb, hc, hd); /* Extract a huffman coded value (zero runs and bit length) */
  424. if (b == 0) break; /* EOB? */
  425. if (b < 0) return 0 - b; /* Err: invalid code or input error */
  426. z = (UINT)b >> 4; /* Number of leading zero elements */
  427. if (z) {
  428. i += z; /* Skip zero elements */
  429. if (i >= 64) return JDR_FMT1; /* Too long zero run */
  430. }
  431. if (b &= 0x0F) { /* Bit length */
  432. d = bitext(jd, b); /* Extract data bits */
  433. if (d < 0) return 0 - d; /* Err: input device */
  434. b = 1 << (b - 1); /* MSB position */
  435. if (!(d & b)) d -= (b << 1) - 1;/* Restore negative value if needed */
  436. z = ZIG(i); /* Zigzag-order to raster-order converted index */
  437. tmp[z] = d * dqf[z] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
  438. }
  439. } while (++i < 64); /* Next AC element */
  440. if (JD_USE_SCALE && jd->scale == 3)
  441. *bp = (BYTE)((*tmp / 256) + 128); /* If scale ratio is 1/8, IDCT can be ommited and only DC element is used */
  442. else
  443. block_idct(tmp, bp); /* Apply IDCT and store the block to the MCU buffer */
  444. bp += 64; /* Next block */
  445. }
  446. return JDR_OK; /* All blocks have been loaded successfully */
  447. }
  448. /*-----------------------------------------------------------------------*/
  449. /* Output an MCU: Convert YCrCb to RGB and output it in RGB form */
  450. /*-----------------------------------------------------------------------*/
  451. static
  452. JRESULT mcu_output (
  453. JDEC* jd, /* Pointer to the decompressor object */
  454. UINT (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */
  455. UINT x, /* MCU position in the image (left of the MCU) */
  456. UINT y /* MCU position in the image (top of the MCU) */
  457. )
  458. {
  459. const INT CVACC = (sizeof (INT) > 2) ? 1024 : 128;
  460. UINT ix, iy, mx, my, rx, ry;
  461. INT yy, cb, cr;
  462. BYTE *py, *pc, *rgb24;
  463. JRECT rect;
  464. mx = jd->msx * 8; my = jd->msy * 8; /* MCU size (pixel) */
  465. rx = (x + mx <= jd->width) ? mx : jd->width - x; /* Output rectangular size (it may be clipped at right/bottom end) */
  466. ry = (y + my <= jd->height) ? my : jd->height - y;
  467. if (JD_USE_SCALE) {
  468. rx >>= jd->scale; ry >>= jd->scale;
  469. if (!rx || !ry) return JDR_OK; /* Skip this MCU if all pixel is to be rounded off */
  470. x >>= jd->scale; y >>= jd->scale;
  471. }
  472. rect.left = x; rect.right = x + rx - 1; /* Rectangular area in the frame buffer */
  473. rect.top = y; rect.bottom = y + ry - 1;
  474. if (!JD_USE_SCALE || jd->scale != 3) { /* Not for 1/8 scaling */
  475. /* Build an RGB MCU from discrete comopnents */
  476. rgb24 = (BYTE*)jd->workbuf;
  477. for (iy = 0; iy < my; iy++) {
  478. pc = jd->mcubuf;
  479. py = pc + iy * 8;
  480. if (my == 16) { /* Double block height? */
  481. pc += 64 * 4 + (iy >> 1) * 8;
  482. if (iy >= 8) py += 64;
  483. } else { /* Single block height */
  484. pc += mx * 8 + iy * 8;
  485. }
  486. for (ix = 0; ix < mx; ix++) {
  487. cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */
  488. cr = pc[64] - 128;
  489. if (mx == 16) { /* Double block width? */
  490. if (ix == 8) py += 64 - 8; /* Jump to next block if double block heigt */
  491. pc += ix & 1; /* Increase chroma pointer every two pixels */
  492. } else { /* Single block width */
  493. pc++; /* Increase chroma pointer every pixel */
  494. }
  495. yy = *py++; /* Get Y component */
  496. /* Convert YCbCr to RGB */
  497. *rgb24++ = /* R */ BYTECLIP(yy + ((INT)(1.402 * CVACC) * cr) / CVACC);
  498. *rgb24++ = /* G */ BYTECLIP(yy - ((INT)(0.344 * CVACC) * cb + (INT)(0.714 * CVACC) * cr) / CVACC);
  499. *rgb24++ = /* B */ BYTECLIP(yy + ((INT)(1.772 * CVACC) * cb) / CVACC);
  500. }
  501. }
  502. /* Descale the MCU rectangular if needed */
  503. if (JD_USE_SCALE && jd->scale) {
  504. UINT x, y, r, g, b, s, w, a;
  505. BYTE *op;
  506. /* Get averaged RGB value of each square correcponds to a pixel */
  507. s = jd->scale * 2; /* Bumber of shifts for averaging */
  508. w = 1 << jd->scale; /* Width of square */
  509. a = (mx - w) * 3; /* Bytes to skip for next line in the square */
  510. op = (BYTE*)jd->workbuf;
  511. for (iy = 0; iy < my; iy += w) {
  512. for (ix = 0; ix < mx; ix += w) {
  513. rgb24 = (BYTE*)jd->workbuf + (iy * mx + ix) * 3;
  514. r = g = b = 0;
  515. for (y = 0; y < w; y++) { /* Accumulate RGB value in the square */
  516. for (x = 0; x < w; x++) {
  517. r += *rgb24++;
  518. g += *rgb24++;
  519. b += *rgb24++;
  520. }
  521. rgb24 += a;
  522. } /* Put the averaged RGB value as a pixel */
  523. *op++ = (BYTE)(r >> s);
  524. *op++ = (BYTE)(g >> s);
  525. *op++ = (BYTE)(b >> s);
  526. }
  527. }
  528. }
  529. } else { /* For only 1/8 scaling (left-top pixel in each block are the DC value of the block) */
  530. /* Build a 1/8 descaled RGB MCU from discrete comopnents */
  531. rgb24 = (BYTE*)jd->workbuf;
  532. pc = jd->mcubuf + mx * my;
  533. cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */
  534. cr = pc[64] - 128;
  535. for (iy = 0; iy < my; iy += 8) {
  536. py = jd->mcubuf;
  537. if (iy == 8) py += 64 * 2;
  538. for (ix = 0; ix < mx; ix += 8) {
  539. yy = *py; /* Get Y component */
  540. py += 64;
  541. /* Convert YCbCr to RGB */
  542. *rgb24++ = /* R */ BYTECLIP(yy + ((INT)(1.402 * CVACC) * cr / CVACC));
  543. *rgb24++ = /* G */ BYTECLIP(yy - ((INT)(0.344 * CVACC) * cb + (INT)(0.714 * CVACC) * cr) / CVACC);
  544. *rgb24++ = /* B */ BYTECLIP(yy + ((INT)(1.772 * CVACC) * cb / CVACC));
  545. }
  546. }
  547. }
  548. /* Squeeze up pixel table if a part of MCU is to be truncated */
  549. mx >>= jd->scale;
  550. if (rx < mx) {
  551. BYTE *s, *d;
  552. UINT x, y;
  553. s = d = (BYTE*)jd->workbuf;
  554. for (y = 0; y < ry; y++) {
  555. for (x = 0; x < rx; x++) { /* Copy effective pixels */
  556. *d++ = *s++;
  557. *d++ = *s++;
  558. *d++ = *s++;
  559. }
  560. s += (mx - rx) * 3; /* Skip truncated pixels */
  561. }
  562. }
  563. /* Convert RGB888 to RGB565 if needed */
  564. if (jd->format == 1) {
  565. BYTE *s = (BYTE*)jd->workbuf;
  566. WORD w, *d = (WORD*)s;
  567. UINT n = rx * ry;
  568. do {
  569. w = (*s++ & 0xF8) << 8; /* RRRRR----------- */
  570. w |= (*s++ & 0xFC) << 3; /* -----GGGGGG----- */
  571. w |= *s++ >> 3; /* -----------BBBBB */
  572. *d++ = w;
  573. } while (--n);
  574. }
  575. /* Output the RGB rectangular */
  576. return outfunc(jd, jd->workbuf, &rect) ? JDR_OK : JDR_INTR;
  577. }
  578. /*-----------------------------------------------------------------------*/
  579. /* Process restart interval */
  580. /*-----------------------------------------------------------------------*/
  581. static
  582. JRESULT restart (
  583. JDEC* jd, /* Pointer to the decompressor object */
  584. WORD rstn /* Expected restert sequense number */
  585. )
  586. {
  587. UINT i, dc;
  588. WORD d;
  589. BYTE *dp;
  590. /* Discard padding bits and get two bytes from the input stream */
  591. dp = jd->dptr; dc = jd->dctr;
  592. d = 0;
  593. for (i = 0; i < 2; i++) {
  594. if (!dc) { /* No input data is available, re-fill input buffer */
  595. dp = jd->inbuf;
  596. dc = jd->infunc(jd, dp, JD_SZBUF);
  597. if (!dc) return JDR_INP;
  598. } else {
  599. dp++;
  600. }
  601. dc--;
  602. d = (d << 8) | *dp; /* Get a byte */
  603. }
  604. jd->dptr = dp; jd->dctr = dc; jd->dmsk = 0;
  605. /* Check the marker */
  606. if ((d & 0xFFD8) != 0xFFD0 || (d & 7) != (rstn & 7))
  607. return JDR_FMT1; /* Err: expected RSTn marker is not detected (may be collapted data) */
  608. /* Reset DC offset */
  609. jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0;
  610. return JDR_OK;
  611. }
  612. /*-----------------------------------------------------------------------*/
  613. /* Analyze the JPEG image and Initialize decompressor object */
  614. /*-----------------------------------------------------------------------*/
  615. #define LDB_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr))<<8)|(WORD)*(BYTE*)((ptr)+1))
  616. JRESULT jd_prepare (
  617. JDEC* jd, /* Blank decompressor object */
  618. UINT (*infunc)(JDEC*, BYTE*, UINT), /* JPEG strem input function */
  619. void* pool, /* Working buffer for the decompression session */
  620. UINT sz_pool, /* Size of working buffer */
  621. void* dev /* I/O device identifier for the session */
  622. )
  623. {
  624. BYTE *seg, b;
  625. WORD marker;
  626. DWORD ofs;
  627. UINT n, i, j, len;
  628. JRESULT rc;
  629. if (!pool) return JDR_PAR;
  630. jd->pool = pool; /* Work memroy */
  631. jd->sz_pool = sz_pool; /* Size of given work memory */
  632. jd->infunc = infunc; /* Stream input function */
  633. jd->device = dev; /* I/O device identifier */
  634. jd->nrst = 0; /* No restart interval (default) */
  635. jd->format = 0; /* use RGB888 (3 BYTE/pix) default */
  636. for (i = 0; i < 2; i++) { /* Nulls pointers */
  637. for (j = 0; j < 2; j++) {
  638. jd->huffbits[i][j] = 0;
  639. jd->huffcode[i][j] = 0;
  640. jd->huffdata[i][j] = 0;
  641. }
  642. }
  643. for (i = 0; i < 4; i++) jd->qttbl[i] = 0;
  644. jd->inbuf = seg = alloc_pool(jd, JD_SZBUF); /* Allocate stream input buffer */
  645. if (!seg) return JDR_MEM1;
  646. if (jd->infunc(jd, seg, 2) != 2) return JDR_INP;/* Check SOI marker */
  647. if (LDB_WORD(seg) != 0xFFD8) return JDR_FMT1; /* Err: SOI is not detected */
  648. ofs = 2;
  649. for (;;) {
  650. /* Get a JPEG marker */
  651. if (jd->infunc(jd, seg, 4) != 4) return JDR_INP;
  652. marker = LDB_WORD(seg); /* Marker */
  653. len = LDB_WORD(seg + 2); /* Length field */
  654. if (len <= 2 || (marker >> 8) != 0xFF) return JDR_FMT1;
  655. len -= 2; /* Content size excluding length field */
  656. ofs += 4 + len; /* Number of bytes loaded */
  657. switch (marker & 0xFF) {
  658. case 0xC0: /* SOF0 (baseline JPEG) */
  659. /* Load segment data */
  660. if (len > JD_SZBUF) return JDR_MEM2;
  661. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  662. jd->width = LDB_WORD(seg+3); /* Image width in unit of pixel */
  663. jd->height = LDB_WORD(seg+1); /* Image height in unit of pixel */
  664. if (seg[5] != 3) return JDR_FMT3; /* Err: Supports only Y/Cb/Cr format */
  665. /* Check three image components */
  666. for (i = 0; i < 3; i++) {
  667. b = seg[7 + 3 * i]; /* Get sampling factor */
  668. if (!i) { /* Y component */
  669. if (b != 0x11 && b != 0x22 && b != 0x21)/* Check sampling factor */
  670. return JDR_FMT3; /* Err: Supports only 4:4:4, 4:2:0 or 4:2:2 */
  671. jd->msx = b >> 4; jd->msy = b & 15; /* Size of MCU [blocks] */
  672. } else { /* Cb/Cr component */
  673. if (b != 0x11) return JDR_FMT3; /* Err: Sampling factor of Cr/Cb must be 1 */
  674. }
  675. b = seg[8 + 3 * i]; /* Get dequantizer table ID for this component */
  676. if (b > 3) return JDR_FMT3; /* Err: Invalid ID */
  677. jd->qtid[i] = b;
  678. }
  679. break;
  680. case 0xDD: /* DRI */
  681. /* Load segment data */
  682. if (len > JD_SZBUF) return JDR_MEM2;
  683. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  684. /* Get restart interval (MCUs) */
  685. jd->nrst = LDB_WORD(seg);
  686. break;
  687. case 0xC4: /* DHT */
  688. /* Load segment data */
  689. if (len > JD_SZBUF) return JDR_MEM2;
  690. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  691. /* Create huffman tables */
  692. rc = create_huffman_tbl(jd, seg, len);
  693. if (rc) return rc;
  694. break;
  695. case 0xDB: /* DQT */
  696. /* Load segment data */
  697. if (len > JD_SZBUF) return JDR_MEM2;
  698. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  699. /* Create de-quantizer tables */
  700. rc = create_qt_tbl(jd, seg, len);
  701. if (rc) return rc;
  702. break;
  703. case 0xDA: /* SOS */
  704. /* Load segment data */
  705. if (len > JD_SZBUF) return JDR_MEM2;
  706. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  707. if (!jd->width || !jd->height) return JDR_FMT1; /* Err: Invalid image size */
  708. if (seg[0] != 3) return JDR_FMT3; /* Err: Supports only three color components format */
  709. /* Check if all tables corresponding to each components have been loaded */
  710. for (i = 0; i < 3; i++) {
  711. b = seg[2 + 2 * i]; /* Get huffman table ID */
  712. if (b != 0x00 && b != 0x11) return JDR_FMT3; /* Err: Different table number for DC/AC element */
  713. b = i ? 1 : 0;
  714. if (!jd->huffbits[b][0] || !jd->huffbits[b][1]) /* Check huffman table for this component */
  715. return JDR_FMT1; /* Err: Huffman table not loaded */
  716. if (!jd->qttbl[jd->qtid[i]]) return JDR_FMT1; /* Err: Dequantizer table not loaded */
  717. }
  718. /* Allocate working buffer for MCU and RGB */
  719. n = jd->msy * jd->msx; /* Number of Y blocks in the MCU */
  720. if (!n) return JDR_FMT1; /* Err: SOF0 has not been loaded */
  721. len = n * 64 * 2 + 64; /* Allocate buffer for IDCT and RGB output */
  722. if (len < 256) len = 256; /* but at least 256 byte is required for IDCT */
  723. jd->workbuf = alloc_pool(jd, len); /* and it may occupy a part of following MCU working buffer for RGB output */
  724. if (!jd->workbuf) return JDR_MEM1; /* Err: not enough memory */
  725. jd->mcubuf = alloc_pool(jd, (n + 2) * 64); /* Allocate MCU working buffer */
  726. if (!jd->mcubuf) return JDR_MEM1; /* Err: not enough memory */
  727. /* Pre-load the JPEG data to extract it from the bit stream */
  728. jd->dptr = seg; jd->dctr = 0; jd->dmsk = 0; /* Prepare to read bit stream */
  729. if (ofs %= JD_SZBUF) { /* Align read offset to JD_SZBUF */
  730. jd->dctr = jd->infunc(jd, seg + ofs, JD_SZBUF - (UINT)ofs);
  731. jd->dptr = seg + ofs - 1;
  732. }
  733. return JDR_OK; /* Initialization succeeded. Ready to decompress the JPEG image. */
  734. case 0xC1: /* SOF1 */
  735. case 0xC2: /* SOF2 */
  736. case 0xC3: /* SOF3 */
  737. case 0xC5: /* SOF5 */
  738. case 0xC6: /* SOF6 */
  739. case 0xC7: /* SOF7 */
  740. case 0xC9: /* SOF9 */
  741. case 0xCA: /* SOF10 */
  742. case 0xCB: /* SOF11 */
  743. case 0xCD: /* SOF13 */
  744. case 0xCE: /* SOF14 */
  745. case 0xCF: /* SOF15 */
  746. case 0xD9: /* EOI */
  747. return JDR_FMT3; /* Unsuppoted JPEG standard (may be progressive JPEG) */
  748. case 0xE0: /* APP0 */
  749. case 0xE1: /* APP1 */
  750. case 0xE2: /* APP2 */
  751. case 0xE3: /* APP3 */
  752. case 0xE4: /* APP4 */
  753. case 0xE5: /* APP5 */
  754. case 0xE6: /* APP6 */
  755. case 0xE7: /* APP7 */
  756. case 0xE8: /* APP8 */
  757. case 0xE9: /* APP9 */
  758. case 0xEA: /* APP10 */
  759. case 0xEB: /* APP11 */
  760. case 0xEC: /* APP12 */
  761. case 0xED: /* APP13 */
  762. case 0xEE: /* APP14 */
  763. case 0xEF: /* APP15 */
  764. case 0xFE: /* COM */
  765. /* Load segment data */
  766. if (len > JD_SZBUF) return JDR_MEM2;
  767. if (jd->infunc(jd, seg, len) != len) return JDR_INP;
  768. break;
  769. default: /* Unknown segment (comment, exif or etc..) */
  770. /* Skip segment data */
  771. if (jd->infunc(jd, 0, len) != len) /* Null pointer specifies to skip bytes of stream */
  772. return JDR_INP;
  773. }
  774. }
  775. }
  776. /*-----------------------------------------------------------------------*/
  777. /* Start to decompress the JPEG picture */
  778. /*-----------------------------------------------------------------------*/
  779. JRESULT jd_decomp (
  780. JDEC* jd, /* Initialized decompression object */
  781. UINT (*outfunc)(JDEC*, void*, JRECT*), /* RGB output function */
  782. BYTE scale /* Output de-scaling factor (0 to 3) */
  783. )
  784. {
  785. UINT x, y, mx, my;
  786. WORD rst, rsc;
  787. JRESULT rc;
  788. if (scale > (JD_USE_SCALE ? 3 : 0)) return JDR_PAR;
  789. jd->scale = scale;
  790. mx = jd->msx * 8; my = jd->msy * 8; /* Size of the MCU (pixel) */
  791. jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; /* Initialize DC values */
  792. rst = rsc = 0;
  793. rc = JDR_OK;
  794. for (y = 0; y < jd->height; y += my) { /* Vertical loop of MCUs */
  795. for (x = 0; x < jd->width; x += mx) { /* Horizontal loop of MCUs */
  796. if (jd->nrst && rst++ == jd->nrst) { /* Process restart interval if enabled */
  797. rc = restart(jd, rsc++);
  798. if (rc != JDR_OK) return rc;
  799. rst = 1;
  800. }
  801. rc = mcu_load(jd); /* Load an MCU (decompress huffman coded stream and IDCT) */
  802. if (rc != JDR_OK) return rc;
  803. rc = mcu_output(jd, outfunc, x, y); /* Output the MCU (color space conversion, scaling and output) */
  804. if (rc != JDR_OK) return rc;
  805. }
  806. }
  807. return rc;
  808. }