2
0

tjpgd.c 34 KB

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