tjpgd.c 35 KB

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