PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/libIGraph/tinyjpeg.c

https://bitbucket.org/cosi2/dotnetanywhere-wb
C | 2185 lines | 1461 code | 301 blank | 423 comment | 183 complexity | a9e8772409c3b09384e3ef4fd9a37b0f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Small jpeg decoder library
  3. *
  4. * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
  5. * All rights reserved.
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the author nor the names of its contributors may be
  17. * used to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "libIGraph.h"
  34. #include "tinyjpeg.h"
  35. #include "tinyjpeg-internal.h"
  36. enum std_markers {
  37. DQT = 0xDB, /* Define Quantization Table */
  38. SOF = 0xC0, /* Start of Frame (size information) */
  39. DHT = 0xC4, /* Huffman Table */
  40. SOI = 0xD8, /* Start of Image */
  41. SOS = 0xDA, /* Start of Scan */
  42. RST = 0xD0, /* Reset Marker d0 -> .. */
  43. RST7 = 0xD7, /* Reset Marker .. -> d7 */
  44. EOI = 0xD9, /* End of Image */
  45. DRI = 0xDD, /* Define Restart Interval */
  46. APP0 = 0xE0,
  47. };
  48. #define cY 0
  49. #define cCb 1
  50. #define cCr 2
  51. #define BLACK_Y 0
  52. #define BLACK_U 127
  53. #define BLACK_V 127
  54. /*#if DEBUG
  55. #define trace(fmt, args...) do { \
  56. fprintf(stderr, fmt, ## args); \
  57. fflush(stderr); \
  58. } while(0)
  59. #else
  60. #define trace(fmt, args...) do { } while (0)
  61. #endif*/
  62. #define error0(fmt) do { \
  63. snprintf(error_string, sizeof(error_string), fmt); \
  64. return -1; \
  65. } while(0)
  66. #define error1(fmt, a) do { \
  67. snprintf(error_string, sizeof(error_string), fmt, a); \
  68. return -1; \
  69. } while(0)
  70. #define error2(fmt, a, b) do { \
  71. snprintf(error_string, sizeof(error_string), fmt, a, b); \
  72. return -1; \
  73. } while(0)
  74. #define error4(fmt, a, b, c, d) do { \
  75. snprintf(error_string, sizeof(error_string), fmt, a, b, c, d); \
  76. return -1; \
  77. } while(0)
  78. #if 0
  79. static char *print_bits(unsigned int value, char *bitstr)
  80. {
  81. int i, j;
  82. i=31;
  83. while (i>0)
  84. {
  85. if (value & (1UL<<i))
  86. break;
  87. i--;
  88. }
  89. j=0;
  90. while (i>=0)
  91. {
  92. bitstr[j++] = (value & (1UL<<i))?'1':'0';
  93. i--;
  94. }
  95. bitstr[j] = 0;
  96. return bitstr;
  97. }
  98. static void print_next_16bytes(int offset, const unsigned char *stream)
  99. {
  100. trace("%4.4x: %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
  101. offset,
  102. stream[0], stream[1], stream[2], stream[3],
  103. stream[4], stream[5], stream[6], stream[7],
  104. stream[8], stream[9], stream[10], stream[11],
  105. stream[12], stream[13], stream[14], stream[15]);
  106. }
  107. #endif
  108. /* Global variable to return the last error found while deconding */
  109. static char error_string[256];
  110. static const unsigned char zigzag[64] =
  111. {
  112. 0, 1, 5, 6, 14, 15, 27, 28,
  113. 2, 4, 7, 13, 16, 26, 29, 42,
  114. 3, 8, 12, 17, 25, 30, 41, 43,
  115. 9, 11, 18, 24, 31, 40, 44, 53,
  116. 10, 19, 23, 32, 39, 45, 52, 54,
  117. 20, 22, 33, 38, 46, 51, 55, 60,
  118. 21, 34, 37, 47, 50, 56, 59, 61,
  119. 35, 36, 48, 49, 57, 58, 62, 63
  120. };
  121. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  122. /* IMPORTANT: these are only valid for 8-bit data precision! */
  123. static const unsigned char bits_dc_luminance[17] =
  124. {
  125. 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
  126. };
  127. static const unsigned char val_dc_luminance[] =
  128. {
  129. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  130. };
  131. static const unsigned char bits_dc_chrominance[17] =
  132. {
  133. 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
  134. };
  135. static const unsigned char val_dc_chrominance[] =
  136. {
  137. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  138. };
  139. static const unsigned char bits_ac_luminance[17] =
  140. {
  141. 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
  142. };
  143. static const unsigned char val_ac_luminance[] =
  144. {
  145. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  146. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  147. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  148. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  149. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  150. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  151. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  152. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  153. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  154. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  155. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  156. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  157. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  158. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  159. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  160. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  161. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  162. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  163. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  164. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  165. 0xf9, 0xfa
  166. };
  167. static const unsigned char bits_ac_chrominance[17] =
  168. {
  169. 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
  170. };
  171. static const unsigned char val_ac_chrominance[] =
  172. {
  173. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  174. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  175. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  176. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  177. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  178. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  179. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  180. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  181. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  182. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  183. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  184. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  185. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  186. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  187. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  188. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  189. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  190. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  191. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  192. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  193. 0xf9, 0xfa
  194. };
  195. /*
  196. * 4 functions to manage the stream
  197. *
  198. * fill_nbits: put at least nbits in the reservoir of bits.
  199. * But convert any 0xff,0x00 into 0xff
  200. * get_nbits: read nbits from the stream, and put it in result,
  201. * bits is removed from the stream and the reservoir is filled
  202. * automaticaly. The result is signed according to the number of
  203. * bits.
  204. * look_nbits: read nbits from the stream without marking as read.
  205. * skip_nbits: read nbits from the stream but do not return the result.
  206. *
  207. * stream: current pointer in the jpeg data (read bytes per bytes)
  208. * nbits_in_reservoir: number of bits filled into the reservoir
  209. * reservoir: register that contains bits information. Only nbits_in_reservoir
  210. * is valid.
  211. * nbits_in_reservoir
  212. * <-- 17 bits -->
  213. * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir
  214. * ^
  215. * bit 1
  216. * To get two bits from this example
  217. * result = (reservoir >> 15) & 3
  218. *
  219. */
  220. #define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  221. while (nbits_in_reservoir<nbits_wanted) \
  222. { \
  223. unsigned char c; \
  224. if (stream >= priv->stream_end) \
  225. longjmp(priv->jump_state, -EOI); \
  226. c = *stream++; \
  227. reservoir <<= 8; \
  228. if (c == 0xff && *stream == 0x00) \
  229. stream++; \
  230. reservoir |= c; \
  231. nbits_in_reservoir+=8; \
  232. } \
  233. } while(0);
  234. /* Signed version !!!! */
  235. #define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
  236. fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  237. result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
  238. nbits_in_reservoir -= (nbits_wanted); \
  239. reservoir &= ((1U<<nbits_in_reservoir)-1); \
  240. if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \
  241. result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \
  242. } while(0);
  243. #define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
  244. fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  245. result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
  246. } while(0);
  247. /* To speed up the decoding, we assume that the reservoir have enough bit
  248. * slow version:
  249. * #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  250. * fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  251. * nbits_in_reservoir -= (nbits_wanted); \
  252. * reservoir &= ((1U<<nbits_in_reservoir)-1); \
  253. * } while(0);
  254. */
  255. #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  256. nbits_in_reservoir -= (nbits_wanted); \
  257. reservoir &= ((1U<<nbits_in_reservoir)-1); \
  258. } while(0);
  259. #define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])
  260. static void resync(struct jdec_private *priv);
  261. /**
  262. * Get the next (valid) huffman code in the stream.
  263. *
  264. * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
  265. * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
  266. * and the value by using two lookup table.
  267. * Else if the value is not found, just search (linear) into an array for each
  268. * bits is the code is present.
  269. *
  270. * If the code is not present for any reason, -1 is return.
  271. */
  272. static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
  273. {
  274. int value, hcode;
  275. unsigned int extra_nbits, nbits;
  276. uint16_t *slowtable;
  277. look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
  278. value = huffman_table->lookup[hcode];
  279. if (__likely(value >= 0))
  280. {
  281. unsigned int code_size = huffman_table->code_size[value];
  282. skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
  283. return value;
  284. }
  285. /* Decode more bits each time ... */
  286. for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++)
  287. {
  288. nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
  289. look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
  290. slowtable = huffman_table->slowtable[extra_nbits];
  291. /* Search if the code is in this array */
  292. while (slowtable[0]) {
  293. if (slowtable[0] == hcode) {
  294. skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
  295. return slowtable[1];
  296. }
  297. slowtable+=2;
  298. }
  299. }
  300. return 0;
  301. }
  302. /**
  303. *
  304. * Decode a single block that contains the DCT coefficients.
  305. * The table coefficients is already dezigzaged at the end of the operation.
  306. *
  307. */
  308. static void process_Huffman_data_unit(struct jdec_private *priv, int component)
  309. {
  310. unsigned char j;
  311. unsigned int huff_code;
  312. unsigned char size_val, count_0;
  313. struct component *c = &priv->component_infos[component];
  314. short int DCT[64];
  315. /* Initialize the DCT coef table */
  316. memset(DCT, 0, sizeof(DCT));
  317. /* DC coefficient decoding */
  318. huff_code = get_next_huffman_code(priv, c->DC_table);
  319. //trace("+ %x\n", huff_code);
  320. if (huff_code) {
  321. get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
  322. DCT[0] += c->previous_DC;
  323. c->previous_DC = DCT[0];
  324. } else {
  325. DCT[0] = c->previous_DC;
  326. }
  327. /* AC coefficient decoding */
  328. j = 1;
  329. while (j<64)
  330. {
  331. huff_code = get_next_huffman_code(priv, c->AC_table);
  332. //trace("- %x\n", huff_code);
  333. size_val = huff_code & 0xF;
  334. count_0 = huff_code >> 4;
  335. if (size_val == 0)
  336. { /* RLE */
  337. if (count_0 == 0)
  338. break; /* EOB found, go out */
  339. else if (count_0 == 0xF)
  340. j += 16; /* skip 16 zeros */
  341. }
  342. else
  343. {
  344. j += count_0; /* skip count_0 zeroes */
  345. if (__unlikely(j >= 64))
  346. {
  347. snprintf(error_string, sizeof(error_string), "Bad huffman data (buffer overflow)");
  348. break;
  349. }
  350. get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
  351. j++;
  352. }
  353. }
  354. for (j = 0; j < 64; j++)
  355. c->DCT[j] = DCT[zigzag[j]];
  356. }
  357. /*
  358. * Takes two array of bits, and build the huffman table for size, and code
  359. *
  360. * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
  361. * code_size will be used to known how many bits this symbol is encoded.
  362. * slowtable will be used when the first lookup didn't give the result.
  363. */
  364. static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
  365. {
  366. unsigned int i, j, code, code_size, val, nbits;
  367. unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
  368. unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
  369. int next_free_entry;
  370. /*
  371. * Build a temp array
  372. * huffsize[X] => numbers of bits to write vals[X]
  373. */
  374. hz = huffsize;
  375. for (i=1; i<=16; i++)
  376. {
  377. for (j=1; j<=bits[i]; j++)
  378. *hz++ = i;
  379. }
  380. *hz = 0;
  381. memset(table->lookup, 0xff, sizeof(table->lookup));
  382. for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
  383. table->slowtable[i][0] = 0;
  384. /* Build a temp array
  385. * huffcode[X] => code used to write vals[X]
  386. */
  387. code = 0;
  388. hc = huffcode;
  389. hz = huffsize;
  390. nbits = *hz;
  391. while (*hz)
  392. {
  393. while (*hz == nbits)
  394. {
  395. *hc++ = code++;
  396. hz++;
  397. }
  398. code <<= 1;
  399. nbits++;
  400. }
  401. /*
  402. * Build the lookup table, and the slowtable if needed.
  403. */
  404. next_free_entry = -1;
  405. for (i=0; huffsize[i]; i++)
  406. {
  407. val = vals[i];
  408. code = huffcode[i];
  409. code_size = huffsize[i];
  410. //trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
  411. table->code_size[val] = code_size;
  412. if (code_size <= HUFFMAN_HASH_NBITS)
  413. {
  414. /*
  415. * Good: val can be put in the lookup table, so fill all value of this
  416. * column with value val
  417. */
  418. int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
  419. code <<= HUFFMAN_HASH_NBITS - code_size;
  420. while ( repeat-- )
  421. table->lookup[code++] = val;
  422. }
  423. else
  424. {
  425. /* Perhaps sorting the array will be an optimization */
  426. uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
  427. while(slowtable[0])
  428. slowtable+=2;
  429. slowtable[0] = code;
  430. slowtable[1] = val;
  431. slowtable[2] = 0;
  432. /* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */
  433. }
  434. }
  435. }
  436. static void build_default_huffman_tables(struct jdec_private *priv)
  437. {
  438. if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
  439. && priv->default_huffman_table_initialized)
  440. return;
  441. build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]);
  442. build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]);
  443. build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]);
  444. build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]);
  445. priv->default_huffman_table_initialized = 1;
  446. }
  447. /*******************************************************************************
  448. *
  449. * Colorspace conversion routine
  450. *
  451. *
  452. * Note:
  453. * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  454. * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  455. * The conversion equations to be implemented are therefore
  456. * R = Y + 1.40200 * Cr
  457. * G = Y - 0.34414 * Cb - 0.71414 * Cr
  458. * B = Y + 1.77200 * Cb
  459. *
  460. ******************************************************************************/
  461. static unsigned char clamp(int i)
  462. {
  463. if (i<0)
  464. return 0;
  465. else if (i>255)
  466. return 255;
  467. else
  468. return i;
  469. }
  470. /**
  471. * YCrCb -> YUV420P (1x1)
  472. * .---.
  473. * | 1 |
  474. * `---'
  475. */
  476. static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
  477. {
  478. const unsigned char *s, *y;
  479. unsigned char *p;
  480. int i,j;
  481. p = priv->plane[0];
  482. y = priv->Y;
  483. for (i=0; i<8; i++)
  484. {
  485. memcpy(p, y, 8);
  486. p+=priv->width;
  487. y+=8;
  488. }
  489. p = priv->plane[1];
  490. s = priv->Cb;
  491. for (i=0; i<8; i+=2)
  492. {
  493. for (j=0; j<8; j+=2, s+=2)
  494. *p++ = *s;
  495. s += 8; /* Skip one line */
  496. p += priv->width/2 - 4;
  497. }
  498. p = priv->plane[2];
  499. s = priv->Cr;
  500. for (i=0; i<8; i+=2)
  501. {
  502. for (j=0; j<8; j+=2, s+=2)
  503. *p++ = *s;
  504. s += 8; /* Skip one line */
  505. p += priv->width/2 - 4;
  506. }
  507. }
  508. /**
  509. * YCrCb -> YUV420P (2x1)
  510. * .-------.
  511. * | 1 | 2 |
  512. * `-------'
  513. */
  514. static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
  515. {
  516. unsigned char *p;
  517. const unsigned char *s, *y1;
  518. unsigned int i;
  519. p = priv->plane[0];
  520. y1 = priv->Y;
  521. for (i=0; i<8; i++)
  522. {
  523. memcpy(p, y1, 16);
  524. p += priv->width;
  525. y1 += 16;
  526. }
  527. p = priv->plane[1];
  528. s = priv->Cb;
  529. for (i=0; i<8; i+=2)
  530. {
  531. memcpy(p, s, 8);
  532. s += 16; /* Skip one line */
  533. p += priv->width/2;
  534. }
  535. p = priv->plane[2];
  536. s = priv->Cr;
  537. for (i=0; i<8; i+=2)
  538. {
  539. memcpy(p, s, 8);
  540. s += 16; /* Skip one line */
  541. p += priv->width/2;
  542. }
  543. }
  544. /**
  545. * YCrCb -> YUV420P (1x2)
  546. * .---.
  547. * | 1 |
  548. * |---|
  549. * | 2 |
  550. * `---'
  551. */
  552. static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
  553. {
  554. const unsigned char *s, *y;
  555. unsigned char *p;
  556. int i,j;
  557. p = priv->plane[0];
  558. y = priv->Y;
  559. for (i=0; i<16; i++)
  560. {
  561. memcpy(p, y, 8);
  562. p+=priv->width;
  563. y+=8;
  564. }
  565. p = priv->plane[1];
  566. s = priv->Cb;
  567. for (i=0; i<8; i++)
  568. {
  569. for (j=0; j<8; j+=2, s+=2)
  570. *p++ = *s;
  571. p += priv->width/2 - 4;
  572. }
  573. p = priv->plane[2];
  574. s = priv->Cr;
  575. for (i=0; i<8; i++)
  576. {
  577. for (j=0; j<8; j+=2, s+=2)
  578. *p++ = *s;
  579. p += priv->width/2 - 4;
  580. }
  581. }
  582. /**
  583. * YCrCb -> YUV420P (2x2)
  584. * .-------.
  585. * | 1 | 2 |
  586. * |---+---|
  587. * | 3 | 4 |
  588. * `-------'
  589. */
  590. static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
  591. {
  592. unsigned char *p;
  593. const unsigned char *s, *y1;
  594. unsigned int i;
  595. p = priv->plane[0];
  596. y1 = priv->Y;
  597. for (i=0; i<16; i++)
  598. {
  599. memcpy(p, y1, 16);
  600. p += priv->width;
  601. y1 += 16;
  602. }
  603. p = priv->plane[1];
  604. s = priv->Cb;
  605. for (i=0; i<8; i++)
  606. {
  607. memcpy(p, s, 8);
  608. s += 8;
  609. p += priv->width/2;
  610. }
  611. p = priv->plane[2];
  612. s = priv->Cr;
  613. for (i=0; i<8; i++)
  614. {
  615. memcpy(p, s, 8);
  616. s += 8;
  617. p += priv->width/2;
  618. }
  619. }
  620. /**
  621. * YCrCb -> RGB24 (1x1)
  622. * .---.
  623. * | 1 |
  624. * `---'
  625. */
  626. static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
  627. {
  628. const unsigned char *Y, *Cb, *Cr;
  629. unsigned char *p;
  630. int i,j;
  631. int offset_to_next_row;
  632. #define SCALEBITS 10
  633. #define ONE_HALF (1UL << (SCALEBITS-1))
  634. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  635. p = priv->plane[0];
  636. Y = priv->Y;
  637. Cb = priv->Cb;
  638. Cr = priv->Cr;
  639. offset_to_next_row = priv->width*3 - 8*3;
  640. for (i=0; i<8; i++) {
  641. for (j=0; j<8; j++) {
  642. int y, cb, cr;
  643. int add_r, add_g, add_b;
  644. int r, g , b;
  645. y = (*Y++) << SCALEBITS;
  646. cb = *Cb++ - 128;
  647. cr = *Cr++ - 128;
  648. add_r = FIX(1.40200) * cr + ONE_HALF;
  649. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  650. add_b = FIX(1.77200) * cb + ONE_HALF;
  651. r = (y + add_r) >> SCALEBITS;
  652. *p++ = clamp(r);
  653. g = (y + add_g) >> SCALEBITS;
  654. *p++ = clamp(g);
  655. b = (y + add_b) >> SCALEBITS;
  656. *p++ = clamp(b);
  657. }
  658. p += offset_to_next_row;
  659. }
  660. #undef SCALEBITS
  661. #undef ONE_HALF
  662. #undef FIX
  663. }
  664. /**
  665. * YCrCb -> BGR24 (1x1)
  666. * .---.
  667. * | 1 |
  668. * `---'
  669. */
  670. static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
  671. {
  672. const unsigned char *Y, *Cb, *Cr;
  673. unsigned char *p;
  674. int i,j;
  675. int offset_to_next_row;
  676. #define SCALEBITS 10
  677. #define ONE_HALF (1UL << (SCALEBITS-1))
  678. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  679. p = priv->plane[0];
  680. Y = priv->Y;
  681. Cb = priv->Cb;
  682. Cr = priv->Cr;
  683. offset_to_next_row = priv->width*3 - 8*3;
  684. for (i=0; i<8; i++) {
  685. for (j=0; j<8; j++) {
  686. int y, cb, cr;
  687. int add_r, add_g, add_b;
  688. int r, g , b;
  689. y = (*Y++) << SCALEBITS;
  690. cb = *Cb++ - 128;
  691. cr = *Cr++ - 128;
  692. add_r = FIX(1.40200) * cr + ONE_HALF;
  693. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  694. add_b = FIX(1.77200) * cb + ONE_HALF;
  695. b = (y + add_b) >> SCALEBITS;
  696. *p++ = clamp(b);
  697. g = (y + add_g) >> SCALEBITS;
  698. *p++ = clamp(g);
  699. r = (y + add_r) >> SCALEBITS;
  700. *p++ = clamp(r);
  701. }
  702. p += offset_to_next_row;
  703. }
  704. #undef SCALEBITS
  705. #undef ONE_HALF
  706. #undef FIX
  707. }
  708. /**
  709. * YCrCb -> RGB24 (2x1)
  710. * .-------.
  711. * | 1 | 2 |
  712. * `-------'
  713. */
  714. static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
  715. {
  716. const unsigned char *Y, *Cb, *Cr;
  717. unsigned char *p;
  718. int i,j;
  719. int offset_to_next_row;
  720. #define SCALEBITS 10
  721. #define ONE_HALF (1UL << (SCALEBITS-1))
  722. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  723. p = priv->plane[0];
  724. Y = priv->Y;
  725. Cb = priv->Cb;
  726. Cr = priv->Cr;
  727. offset_to_next_row = priv->width*3 - 16*3;
  728. for (i=0; i<8; i++) {
  729. for (j=0; j<8; j++) {
  730. int y, cb, cr;
  731. int add_r, add_g, add_b;
  732. int r, g , b;
  733. y = (*Y++) << SCALEBITS;
  734. cb = *Cb++ - 128;
  735. cr = *Cr++ - 128;
  736. add_r = FIX(1.40200) * cr + ONE_HALF;
  737. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  738. add_b = FIX(1.77200) * cb + ONE_HALF;
  739. r = (y + add_r) >> SCALEBITS;
  740. *p++ = clamp(r);
  741. g = (y + add_g) >> SCALEBITS;
  742. *p++ = clamp(g);
  743. b = (y + add_b) >> SCALEBITS;
  744. *p++ = clamp(b);
  745. y = (*Y++) << SCALEBITS;
  746. r = (y + add_r) >> SCALEBITS;
  747. *p++ = clamp(r);
  748. g = (y + add_g) >> SCALEBITS;
  749. *p++ = clamp(g);
  750. b = (y + add_b) >> SCALEBITS;
  751. *p++ = clamp(b);
  752. }
  753. p += offset_to_next_row;
  754. }
  755. #undef SCALEBITS
  756. #undef ONE_HALF
  757. #undef FIX
  758. }
  759. /*
  760. * YCrCb -> BGR24 (2x1)
  761. * .-------.
  762. * | 1 | 2 |
  763. * `-------'
  764. */
  765. static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
  766. {
  767. const unsigned char *Y, *Cb, *Cr;
  768. unsigned char *p;
  769. int i,j;
  770. int offset_to_next_row;
  771. #define SCALEBITS 10
  772. #define ONE_HALF (1UL << (SCALEBITS-1))
  773. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  774. p = priv->plane[0];
  775. Y = priv->Y;
  776. Cb = priv->Cb;
  777. Cr = priv->Cr;
  778. offset_to_next_row = priv->width*3 - 16*3;
  779. for (i=0; i<8; i++) {
  780. for (j=0; j<8; j++) {
  781. int y, cb, cr;
  782. int add_r, add_g, add_b;
  783. int r, g , b;
  784. cb = *Cb++ - 128;
  785. cr = *Cr++ - 128;
  786. add_r = FIX(1.40200) * cr + ONE_HALF;
  787. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  788. add_b = FIX(1.77200) * cb + ONE_HALF;
  789. y = (*Y++) << SCALEBITS;
  790. b = (y + add_b) >> SCALEBITS;
  791. *p++ = clamp(b);
  792. g = (y + add_g) >> SCALEBITS;
  793. *p++ = clamp(g);
  794. r = (y + add_r) >> SCALEBITS;
  795. *p++ = clamp(r);
  796. y = (*Y++) << SCALEBITS;
  797. b = (y + add_b) >> SCALEBITS;
  798. *p++ = clamp(b);
  799. g = (y + add_g) >> SCALEBITS;
  800. *p++ = clamp(g);
  801. r = (y + add_r) >> SCALEBITS;
  802. *p++ = clamp(r);
  803. }
  804. p += offset_to_next_row;
  805. }
  806. #undef SCALEBITS
  807. #undef ONE_HALF
  808. #undef FIX
  809. }
  810. /**
  811. * YCrCb -> RGB24 (1x2)
  812. * .---.
  813. * | 1 |
  814. * |---|
  815. * | 2 |
  816. * `---'
  817. */
  818. static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
  819. {
  820. const unsigned char *Y, *Cb, *Cr;
  821. unsigned char *p, *p2;
  822. int i,j;
  823. int offset_to_next_row;
  824. #define SCALEBITS 10
  825. #define ONE_HALF (1UL << (SCALEBITS-1))
  826. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  827. p = priv->plane[0];
  828. p2 = priv->plane[0] + priv->width*3;
  829. Y = priv->Y;
  830. Cb = priv->Cb;
  831. Cr = priv->Cr;
  832. offset_to_next_row = 2*priv->width*3 - 8*3;
  833. for (i=0; i<8; i++) {
  834. for (j=0; j<8; j++) {
  835. int y, cb, cr;
  836. int add_r, add_g, add_b;
  837. int r, g , b;
  838. cb = *Cb++ - 128;
  839. cr = *Cr++ - 128;
  840. add_r = FIX(1.40200) * cr + ONE_HALF;
  841. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  842. add_b = FIX(1.77200) * cb + ONE_HALF;
  843. y = (*Y++) << SCALEBITS;
  844. r = (y + add_r) >> SCALEBITS;
  845. *p++ = clamp(r);
  846. g = (y + add_g) >> SCALEBITS;
  847. *p++ = clamp(g);
  848. b = (y + add_b) >> SCALEBITS;
  849. *p++ = clamp(b);
  850. y = (Y[8-1]) << SCALEBITS;
  851. r = (y + add_r) >> SCALEBITS;
  852. *p2++ = clamp(r);
  853. g = (y + add_g) >> SCALEBITS;
  854. *p2++ = clamp(g);
  855. b = (y + add_b) >> SCALEBITS;
  856. *p2++ = clamp(b);
  857. }
  858. Y += 8;
  859. p += offset_to_next_row;
  860. p2 += offset_to_next_row;
  861. }
  862. #undef SCALEBITS
  863. #undef ONE_HALF
  864. #undef FIX
  865. }
  866. /*
  867. * YCrCb -> BGR24 (1x2)
  868. * .---.
  869. * | 1 |
  870. * |---|
  871. * | 2 |
  872. * `---'
  873. */
  874. static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
  875. {
  876. const unsigned char *Y, *Cb, *Cr;
  877. unsigned char *p, *p2;
  878. int i,j;
  879. int offset_to_next_row;
  880. #define SCALEBITS 10
  881. #define ONE_HALF (1UL << (SCALEBITS-1))
  882. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  883. p = priv->plane[0];
  884. p2 = priv->plane[0] + priv->width*3;
  885. Y = priv->Y;
  886. Cb = priv->Cb;
  887. Cr = priv->Cr;
  888. offset_to_next_row = 2*priv->width*3 - 8*3;
  889. for (i=0; i<8; i++) {
  890. for (j=0; j<8; j++) {
  891. int y, cb, cr;
  892. int add_r, add_g, add_b;
  893. int r, g , b;
  894. cb = *Cb++ - 128;
  895. cr = *Cr++ - 128;
  896. add_r = FIX(1.40200) * cr + ONE_HALF;
  897. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  898. add_b = FIX(1.77200) * cb + ONE_HALF;
  899. y = (*Y++) << SCALEBITS;
  900. b = (y + add_b) >> SCALEBITS;
  901. *p++ = clamp(b);
  902. g = (y + add_g) >> SCALEBITS;
  903. *p++ = clamp(g);
  904. r = (y + add_r) >> SCALEBITS;
  905. *p++ = clamp(r);
  906. y = (Y[8-1]) << SCALEBITS;
  907. b = (y + add_b) >> SCALEBITS;
  908. *p2++ = clamp(b);
  909. g = (y + add_g) >> SCALEBITS;
  910. *p2++ = clamp(g);
  911. r = (y + add_r) >> SCALEBITS;
  912. *p2++ = clamp(r);
  913. }
  914. Y += 8;
  915. p += offset_to_next_row;
  916. p2 += offset_to_next_row;
  917. }
  918. #undef SCALEBITS
  919. #undef ONE_HALF
  920. #undef FIX
  921. }
  922. /**
  923. * YCrCb -> RGB24 (2x2)
  924. * .-------.
  925. * | 1 | 2 |
  926. * |---+---|
  927. * | 3 | 4 |
  928. * `-------'
  929. */
  930. static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
  931. {
  932. const unsigned char *Y, *Cb, *Cr;
  933. unsigned char *p, *p2;
  934. int i,j;
  935. int offset_to_next_row;
  936. #define SCALEBITS 10
  937. #define ONE_HALF (1UL << (SCALEBITS-1))
  938. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  939. p = priv->plane[0];
  940. p2 = priv->plane[0] + priv->width*3;
  941. Y = priv->Y;
  942. Cb = priv->Cb;
  943. Cr = priv->Cr;
  944. offset_to_next_row = (priv->width*3*2) - 16*3;
  945. for (i=0; i<8; i++) {
  946. for (j=0; j<8; j++) {
  947. int y, cb, cr;
  948. int add_r, add_g, add_b;
  949. int r, g , b;
  950. cb = *Cb++ - 128;
  951. cr = *Cr++ - 128;
  952. add_r = FIX(1.40200) * cr + ONE_HALF;
  953. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  954. add_b = FIX(1.77200) * cb + ONE_HALF;
  955. y = (*Y++) << SCALEBITS;
  956. r = (y + add_r) >> SCALEBITS;
  957. *p++ = clamp(r);
  958. g = (y + add_g) >> SCALEBITS;
  959. *p++ = clamp(g);
  960. b = (y + add_b) >> SCALEBITS;
  961. *p++ = clamp(b);
  962. y = (*Y++) << SCALEBITS;
  963. r = (y + add_r) >> SCALEBITS;
  964. *p++ = clamp(r);
  965. g = (y + add_g) >> SCALEBITS;
  966. *p++ = clamp(g);
  967. b = (y + add_b) >> SCALEBITS;
  968. *p++ = clamp(b);
  969. y = (Y[16-2]) << SCALEBITS;
  970. r = (y + add_r) >> SCALEBITS;
  971. *p2++ = clamp(r);
  972. g = (y + add_g) >> SCALEBITS;
  973. *p2++ = clamp(g);
  974. b = (y + add_b) >> SCALEBITS;
  975. *p2++ = clamp(b);
  976. y = (Y[16-1]) << SCALEBITS;
  977. r = (y + add_r) >> SCALEBITS;
  978. *p2++ = clamp(r);
  979. g = (y + add_g) >> SCALEBITS;
  980. *p2++ = clamp(g);
  981. b = (y + add_b) >> SCALEBITS;
  982. *p2++ = clamp(b);
  983. }
  984. Y += 16;
  985. p += offset_to_next_row;
  986. p2 += offset_to_next_row;
  987. }
  988. #undef SCALEBITS
  989. #undef ONE_HALF
  990. #undef FIX
  991. }
  992. /*
  993. * YCrCb -> BGR24 (2x2)
  994. * .-------.
  995. * | 1 | 2 |
  996. * |---+---|
  997. * | 3 | 4 |
  998. * `-------'
  999. */
  1000. static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
  1001. {
  1002. const unsigned char *Y, *Cb, *Cr;
  1003. unsigned char *p, *p2;
  1004. int i,j;
  1005. int offset_to_next_row;
  1006. #define SCALEBITS 10
  1007. #define ONE_HALF (1UL << (SCALEBITS-1))
  1008. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  1009. p = priv->plane[0];
  1010. p2 = priv->plane[0] + priv->width*3;
  1011. Y = priv->Y;
  1012. Cb = priv->Cb;
  1013. Cr = priv->Cr;
  1014. offset_to_next_row = (priv->width*3*2) - 16*3;
  1015. for (i=0; i<8; i++) {
  1016. for (j=0; j<8; j++) {
  1017. int y, cb, cr;
  1018. int add_r, add_g, add_b;
  1019. int r, g , b;
  1020. cb = *Cb++ - 128;
  1021. cr = *Cr++ - 128;
  1022. add_r = FIX(1.40200) * cr + ONE_HALF;
  1023. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  1024. add_b = FIX(1.77200) * cb + ONE_HALF;
  1025. y = (*Y++) << SCALEBITS;
  1026. b = (y + add_b) >> SCALEBITS;
  1027. *p++ = clamp(b);
  1028. g = (y + add_g) >> SCALEBITS;
  1029. *p++ = clamp(g);
  1030. r = (y + add_r) >> SCALEBITS;
  1031. *p++ = clamp(r);
  1032. y = (*Y++) << SCALEBITS;
  1033. b = (y + add_b) >> SCALEBITS;
  1034. *p++ = clamp(b);
  1035. g = (y + add_g) >> SCALEBITS;
  1036. *p++ = clamp(g);
  1037. r = (y + add_r) >> SCALEBITS;
  1038. *p++ = clamp(r);
  1039. y = (Y[16-2]) << SCALEBITS;
  1040. b = (y + add_b) >> SCALEBITS;
  1041. *p2++ = clamp(b);
  1042. g = (y + add_g) >> SCALEBITS;
  1043. *p2++ = clamp(g);
  1044. r = (y + add_r) >> SCALEBITS;
  1045. *p2++ = clamp(r);
  1046. y = (Y[16-1]) << SCALEBITS;
  1047. b = (y + add_b) >> SCALEBITS;
  1048. *p2++ = clamp(b);
  1049. g = (y + add_g) >> SCALEBITS;
  1050. *p2++ = clamp(g);
  1051. r = (y + add_r) >> SCALEBITS;
  1052. *p2++ = clamp(r);
  1053. }
  1054. Y += 16;
  1055. p += offset_to_next_row;
  1056. p2 += offset_to_next_row;
  1057. }
  1058. #undef SCALEBITS
  1059. #undef ONE_HALF
  1060. #undef FIX
  1061. }
  1062. /**
  1063. * YCrCb -> Grey (1x1)
  1064. * .---.
  1065. * | 1 |
  1066. * `---'
  1067. */
  1068. static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
  1069. {
  1070. const unsigned char *y;
  1071. unsigned char *p;
  1072. unsigned int i;
  1073. int offset_to_next_row;
  1074. p = priv->plane[0];
  1075. y = priv->Y;
  1076. offset_to_next_row = priv->width;
  1077. for (i=0; i<8; i++) {
  1078. memcpy(p, y, 8);
  1079. y+=8;
  1080. p += offset_to_next_row;
  1081. }
  1082. }
  1083. /**
  1084. * YCrCb -> Grey (2x1)
  1085. * .-------.
  1086. * | 1 | 2 |
  1087. * `-------'
  1088. */
  1089. static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
  1090. {
  1091. const unsigned char *y;
  1092. unsigned char *p;
  1093. unsigned int i;
  1094. p = priv->plane[0];
  1095. y = priv->Y;
  1096. for (i=0; i<8; i++) {
  1097. memcpy(p, y, 16);
  1098. y += 16;
  1099. p += priv->width;
  1100. }
  1101. }
  1102. /**
  1103. * YCrCb -> Grey (1x2)
  1104. * .---.
  1105. * | 1 |
  1106. * |---|
  1107. * | 2 |
  1108. * `---'
  1109. */
  1110. static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
  1111. {
  1112. const unsigned char *y;
  1113. unsigned char *p;
  1114. unsigned int i;
  1115. p = priv->plane[0];
  1116. y = priv->Y;
  1117. for (i=0; i<16; i++) {
  1118. memcpy(p, y, 8);
  1119. y += 8;
  1120. p += priv->width;
  1121. }
  1122. }
  1123. /**
  1124. * YCrCb -> Grey (2x2)
  1125. * .-------.
  1126. * | 1 | 2 |
  1127. * |---+---|
  1128. * | 3 | 4 |
  1129. * `-------'
  1130. */
  1131. static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
  1132. {
  1133. const unsigned char *y;
  1134. unsigned char *p;
  1135. unsigned int i;
  1136. p = priv->plane[0];
  1137. y = priv->Y;
  1138. for (i=0; i<16; i++) {
  1139. memcpy(p, y, 16);
  1140. y += 16;
  1141. p += priv->width;
  1142. }
  1143. }
  1144. /*
  1145. * Decode all the 3 components for 1x1
  1146. */
  1147. static void decode_MCU_1x1_3planes(struct jdec_private *priv)
  1148. {
  1149. // Y
  1150. process_Huffman_data_unit(priv, cY);
  1151. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1152. // Cb
  1153. process_Huffman_data_unit(priv, cCb);
  1154. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1155. // Cr
  1156. process_Huffman_data_unit(priv, cCr);
  1157. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1158. }
  1159. /*
  1160. * Decode a 1x1 directly in 1 color
  1161. */
  1162. static void decode_MCU_1x1_1plane(struct jdec_private *priv)
  1163. {
  1164. // Y
  1165. process_Huffman_data_unit(priv, cY);
  1166. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1167. // Cb
  1168. process_Huffman_data_unit(priv, cCb);
  1169. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1170. // Cr
  1171. process_Huffman_data_unit(priv, cCr);
  1172. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1173. }
  1174. /*
  1175. * Decode a 2x1
  1176. * .-------.
  1177. * | 1 | 2 |
  1178. * `-------'
  1179. */
  1180. static void decode_MCU_2x1_3planes(struct jdec_private *priv)
  1181. {
  1182. // Y
  1183. process_Huffman_data_unit(priv, cY);
  1184. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1185. process_Huffman_data_unit(priv, cY);
  1186. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1187. // Cb
  1188. process_Huffman_data_unit(priv, cCb);
  1189. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1190. // Cr
  1191. process_Huffman_data_unit(priv, cCr);
  1192. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1193. }
  1194. /*
  1195. * Decode a 2x1
  1196. * .-------.
  1197. * | 1 | 2 |
  1198. * `-------'
  1199. */
  1200. static void decode_MCU_2x1_1plane(struct jdec_private *priv)
  1201. {
  1202. // Y
  1203. process_Huffman_data_unit(priv, cY);
  1204. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1205. process_Huffman_data_unit(priv, cY);
  1206. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1207. // Cb
  1208. process_Huffman_data_unit(priv, cCb);
  1209. // Cr
  1210. process_Huffman_data_unit(priv, cCr);
  1211. }
  1212. /*
  1213. * Decode a 2x2
  1214. * .-------.
  1215. * | 1 | 2 |
  1216. * |---+---|
  1217. * | 3 | 4 |
  1218. * `-------'
  1219. */
  1220. static void decode_MCU_2x2_3planes(struct jdec_private *priv)
  1221. {
  1222. // Y
  1223. process_Huffman_data_unit(priv, cY);
  1224. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1225. process_Huffman_data_unit(priv, cY);
  1226. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1227. process_Huffman_data_unit(priv, cY);
  1228. IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
  1229. process_Huffman_data_unit(priv, cY);
  1230. IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
  1231. // Cb
  1232. process_Huffman_data_unit(priv, cCb);
  1233. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1234. // Cr
  1235. process_Huffman_data_unit(priv, cCr);
  1236. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1237. }
  1238. /*
  1239. * Decode a 2x2 directly in GREY format (8bits)
  1240. * .-------.
  1241. * | 1 | 2 |
  1242. * |---+---|
  1243. * | 3 | 4 |
  1244. * `-------'
  1245. */
  1246. static void decode_MCU_2x2_1plane(struct jdec_private *priv)
  1247. {
  1248. // Y
  1249. process_Huffman_data_unit(priv, cY);
  1250. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1251. process_Huffman_data_unit(priv, cY);
  1252. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1253. process_Huffman_data_unit(priv, cY);
  1254. IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
  1255. process_Huffman_data_unit(priv, cY);
  1256. IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
  1257. // Cb
  1258. process_Huffman_data_unit(priv, cCb);
  1259. // Cr
  1260. process_Huffman_data_unit(priv, cCr);
  1261. }
  1262. /*
  1263. * Decode a 1x2 mcu
  1264. * .---.
  1265. * | 1 |
  1266. * |---|
  1267. * | 2 |
  1268. * `---'
  1269. */
  1270. static void decode_MCU_1x2_3planes(struct jdec_private *priv)
  1271. {
  1272. // Y
  1273. process_Huffman_data_unit(priv, cY);
  1274. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1275. process_Huffman_data_unit(priv, cY);
  1276. IDCT(&priv->component_infos[cY], priv->Y+64, 8);
  1277. // Cb
  1278. process_Huffman_data_unit(priv, cCb);
  1279. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1280. // Cr
  1281. process_Huffman_data_unit(priv, cCr);
  1282. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1283. }
  1284. /*
  1285. * Decode a 1x2 mcu
  1286. * .---.
  1287. * | 1 |
  1288. * |---|
  1289. * | 2 |
  1290. * `---'
  1291. */
  1292. static void decode_MCU_1x2_1plane(struct jdec_private *priv)
  1293. {
  1294. // Y
  1295. process_Huffman_data_unit(priv, cY);
  1296. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1297. process_Huffman_data_unit(priv, cY);
  1298. IDCT(&priv->component_infos[cY], priv->Y+64, 8);
  1299. // Cb
  1300. process_Huffman_data_unit(priv, cCb);
  1301. // Cr
  1302. process_Huffman_data_unit(priv, cCr);
  1303. }
  1304. static void print_SOF(const unsigned char *stream)
  1305. {
  1306. int width, height, nr_components, precision;
  1307. #if DEBUG
  1308. const char *nr_components_to_string[] = {
  1309. "????",
  1310. "Grayscale",
  1311. "????",
  1312. "YCbCr",
  1313. "CYMK"
  1314. };
  1315. #endif
  1316. precision = stream[2];
  1317. height = be16_to_cpu(stream+3);
  1318. width = be16_to_cpu(stream+5);
  1319. nr_components = stream[7];
  1320. /*trace("> SOF marker\n");
  1321. trace("Size:%dx%d nr_components:%d (%s) precision:%d\n",
  1322. width, height,
  1323. nr_components, nr_components_to_string[nr_components],
  1324. precision);*/
  1325. }
  1326. /*******************************************************************************
  1327. *
  1328. * JPEG/JFIF Parsing functions
  1329. *
  1330. * Note: only a small subset of the jpeg file format is supported. No markers,
  1331. * nor progressive stream is supported.
  1332. *
  1333. ******************************************************************************/
  1334. static void build_quantization_table(int *qtable, const unsigned char *ref_table)
  1335. {
  1336. /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
  1337. * For float AA&N IDCT method, divisors are equal to quantization
  1338. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  1339. * scalefactor[0] = 1
  1340. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  1341. * We apply a further scale factor of 8.
  1342. * What's actually stored is 1/divisor so that the inner loop can
  1343. * use a multiplication rather than a division.
  1344. */
  1345. int i, j;
  1346. /*static const double aanscalefactor[8] = {
  1347. 1.0, 1.387039845, 1.306562965, 1.175875602,
  1348. 1.0, 0.785694958, 0.541196100, 0.275899379
  1349. };*/
  1350. static const int aanscalefactor[8] = {
  1351. DCT_FIX(1.0), DCT_FIX(1.387039845), DCT_FIX(1.306562965), DCT_FIX(1.175875602),
  1352. DCT_FIX(1.0), DCT_FIX(0.785694958), DCT_FIX(0.541196100), DCT_FIX(0.275899379)
  1353. };
  1354. const unsigned char *zz = zigzag;
  1355. for (i=0; i<8; i++) {
  1356. for (j=0; j<8; j++) {
  1357. //*qtable++ = (float)(ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j]);
  1358. //float f = (float)(ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j]);
  1359. int value = ref_table[*zz++];
  1360. value *= aanscalefactor[i];
  1361. value *= aanscalefactor[j];
  1362. *qtable++ = DCT_DESCALE1(value);
  1363. }
  1364. }
  1365. }
  1366. static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
  1367. {
  1368. int qi;
  1369. int *table;
  1370. const unsigned char *dqt_block_end;
  1371. //trace("> DQT marker\n");
  1372. dqt_block_end = stream + be16_to_cpu(stream);
  1373. stream += 2; /* Skip length */
  1374. while (stream < dqt_block_end)
  1375. {
  1376. qi = *stream++;
  1377. #if SANITY_CHECK
  1378. if (qi>>4) {
  1379. //error("16 bits quantization table is not supported\n");
  1380. snprintf(error_string, sizeof(error_string), "16 bits quantization table is not supported\n");
  1381. return -1;
  1382. }
  1383. if (qi>4) {
  1384. //error("No more 4 quantization table is supported (got %d)\n", qi);
  1385. snprintf(error_string, sizeof(error_string), "No more 4 quantization table is supported (got %d)\n", qi);
  1386. return -1;
  1387. }
  1388. #endif
  1389. table = priv->Q_tables[qi];
  1390. build_quantization_table(table, stream);
  1391. stream += 64;
  1392. }
  1393. //trace("< DQT marker\n");
  1394. return 0;
  1395. }
  1396. static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
  1397. {
  1398. int i, width, height, nr_components, cid, sampling_factor;
  1399. int Q_table;
  1400. struct component *c;
  1401. // trace("> SOF marker\n");
  1402. print_SOF(stream);
  1403. height = be16_to_cpu(stream+3);
  1404. width = be16_to_cpu(stream+5);
  1405. nr_components = stream[7];
  1406. #if SANITY_CHECK
  1407. if (stream[2] != 8) {
  1408. error0("Precision other than 8 is not supported\n");
  1409. }
  1410. if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT)
  1411. error2("Width and Height (%dx%d) seems suspicious\n", width, height);
  1412. if (nr_components != 3)
  1413. error0("We only support YUV images\n");
  1414. if (height%16)
  1415. error1("Height need to be a multiple of 16 (current height is %d)\n", height);
  1416. if (width%16)
  1417. error1("Width need to be a multiple of 16 (current Width is %d)\n", width);
  1418. #endif
  1419. stream += 8;
  1420. for (i=0; i<nr_components; i++) {
  1421. cid = *stream++;
  1422. sampling_factor = *stream++;
  1423. Q_table = *stream++;
  1424. c = &priv->component_infos[i];
  1425. #if SANITY_CHECK
  1426. c->cid = cid;
  1427. if (Q_table >= COMPONENTS)
  1428. error2("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1);
  1429. #endif
  1430. c->Vfactor = sampling_factor&0xf;
  1431. c->Hfactor = sampling_factor>>4;
  1432. c->Q_table = priv->Q_tables[Q_table];
  1433. //trace("Component:%d factor:%dx%d Quantization table:%d\n",
  1434. // cid, c->Hfactor, c->Hfactor, Q_table );
  1435. }
  1436. priv->width = width;
  1437. priv->height = height;
  1438. //trace("< SOF marker\n");
  1439. return 0;
  1440. }
  1441. static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
  1442. {
  1443. unsigned int i, cid, table;
  1444. unsigned int nr_components = stream[2];
  1445. // trace("> SOS marker\n");
  1446. #if SANITY_CHECK
  1447. if (nr_components != 3)
  1448. error0("We only support YCbCr image\n");
  1449. #endif
  1450. stream += 3;
  1451. for (i=0;i<nr_components;i++) {
  1452. cid = *stream++;
  1453. table = *stream++;
  1454. #if SANITY_CHECK
  1455. if ((table&0xf)>=4)
  1456. error0("We do not support more than 2 AC Huffman table\n");
  1457. if ((table>>4)>=4)
  1458. error0("We do not support more than 2 DC Huffman table\n");
  1459. if (cid != priv->component_infos[i].cid)
  1460. error4("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n",
  1461. i, cid, i, priv->component_infos[i].cid);
  1462. //trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
  1463. #endif
  1464. priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
  1465. priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
  1466. }
  1467. priv->stream = stream+3;
  1468. //trace("< SOS marker\n");
  1469. return 0;
  1470. }
  1471. static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
  1472. {
  1473. unsigned int count, i;
  1474. unsigned char huff_bits[17];
  1475. int length, index;
  1476. length = be16_to_cpu(stream) - 2;
  1477. stream += 2; /* Skip length */
  1478. // trace("> DHT marker (length=%d)\n", length);
  1479. while (length>0) {
  1480. index = *stream++;
  1481. /* We need to calculate the number of bytes 'vals' will takes */
  1482. huff_bits[0] = 0;
  1483. count = 0;
  1484. for (i=1; i<17; i++) {
  1485. huff_bits[i] = *stream++;
  1486. count += huff_bits[i];
  1487. }
  1488. #if SANITY_CHECK
  1489. if (count >= HUFFMAN_BITS_SIZE)
  1490. error1("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
  1491. if ( (index &0xf) >= HUFFMAN_TABLES)
  1492. error2("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
  1493. //trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
  1494. #endif
  1495. if (index & 0xf0 )
  1496. build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
  1497. else
  1498. build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
  1499. length -= 1;
  1500. length -= 16;
  1501. length -= count;
  1502. stream += count;
  1503. }
  1504. //trace("< DHT marker\n");
  1505. return 0;
  1506. }
  1507. static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
  1508. {
  1509. unsigned int length;
  1510. // trace("> DRI marker\n");
  1511. length = be16_to_cpu(stream);
  1512. #if SANITY_CHECK
  1513. if (length != 4)
  1514. error0("Length of DRI marker need to be 4\n");
  1515. #endif
  1516. priv->restart_interval = be16_to_cpu(stream+2);
  1517. #if DEBUG
  1518. trace("Restart interval = %d\n", priv->restart_interval);
  1519. #endif
  1520. //trace("< DRI marker\n");
  1521. return 0;
  1522. }
  1523. static void resync(struct jdec_private *priv)
  1524. {
  1525. int i;
  1526. /* Init DC coefficients */
  1527. for (i=0; i<COMPONENTS; i++)
  1528. priv->component_infos[i].previous_DC = 0;
  1529. priv->reservoir = 0;
  1530. priv->nbits_in_reservoir = 0;
  1531. if (priv->restart_interval > 0)
  1532. priv->restarts_to_go = priv->restart_interval;
  1533. else
  1534. priv->restarts_to_go = -1;
  1535. }
  1536. static int find_next_rst_marker(struct jdec_private *priv)
  1537. {
  1538. int rst_marker_found = 0;
  1539. int marker;
  1540. const unsigned char *stream = priv->stream;
  1541. /* Parse marker */
  1542. while (!rst_marker_found)
  1543. {
  1544. while (*stream++ != 0xff)
  1545. {
  1546. if (stream >= priv->stream_end)
  1547. error0("EOF while search for a RST marker.");
  1548. }
  1549. /* Skip any padding ff byte (this is normal) */
  1550. while (*stream == 0xff)
  1551. stream++;
  1552. marker = *stream++;
  1553. if ((RST+priv->last_rst_marker_seen) == marker)
  1554. rst_marker_found = 1;
  1555. else if (marker >= RST && marker <= RST7)
  1556. error0("Wrong Reset marker found, aborting");
  1557. else if (marker == EOI)
  1558. return 0;
  1559. }
  1560. // trace("RST Marker %d found at offset %d\n", priv->last_rst_marker_seen, stream - priv->stream_begin);
  1561. priv->stream = stream;
  1562. priv->last_rst_marker_seen++;
  1563. priv->last_rst_marker_seen &= 7;
  1564. return 0;
  1565. }
  1566. static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
  1567. {
  1568. int chuck_len;
  1569. int marker;
  1570. int sos_marker_found = 0;
  1571. int dht_marker_found = 0;
  1572. const unsigned char *next_chunck;
  1573. /* Parse marker */
  1574. while (!sos_marker_found)
  1575. {
  1576. if (*stream++ != 0xff)
  1577. goto bogus_jpeg_format;
  1578. /* Skip any padding ff byte (this is normal) */
  1579. while (*stream == 0xff)
  1580. stream++;
  1581. marker = *stream++;
  1582. chuck_len = be16_to_cpu(stream);
  1583. next_chunck = stream + chuck_len;
  1584. switch (marker)
  1585. {
  1586. case SOF:
  1587. if (parse_SOF(priv, stream) < 0)
  1588. return -1;
  1589. break;
  1590. case DQT:
  1591. if (parse_DQT(priv, stream) < 0)
  1592. return -1;
  1593. break;
  1594. case SOS:
  1595. if (parse_SOS(priv, stream) < 0)
  1596. return -1;
  1597. sos_marker_found = 1;
  1598. break;
  1599. case DHT:
  1600. if (parse_DHT(priv, stream) < 0)
  1601. return -1;
  1602. dht_marker_found = 1;
  1603. break;
  1604. case DRI:
  1605. if (parse_DRI(priv, stream) < 0)
  1606. return -1;
  1607. break;
  1608. default:
  1609. // trace("> Unknown marker %2.2x\n", marker);
  1610. break;
  1611. }
  1612. stream = next_chunck;
  1613. }
  1614. if (!dht_marker_found) {
  1615. //trace("No Huffman table loaded, using the default one\n");
  1616. build_default_huffman_tables(priv);
  1617. }
  1618. #ifdef SANITY_CHECK
  1619. if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
  1620. || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
  1621. error0("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
  1622. if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
  1623. || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
  1624. error0("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
  1625. if ( (priv->component_infos[cCb].Hfactor!=1)
  1626. || (priv->component_infos[cCr].Hfactor!=1)
  1627. || (priv->component_infos[cCb].Vfactor!=1)
  1628. || (priv->component_infos[cCr].Vfactor!=1))
  1629. error0("Sampling other than 1x1 for Cr and Cb is not supported");
  1630. #endif
  1631. return 0;
  1632. bogus_jpeg_format:
  1633. //trace("Bogus jpeg format\n");
  1634. return -1;
  1635. }
  1636. /*******************************************************************************
  1637. *
  1638. * Functions exported of the library.
  1639. *
  1640. * Note: Some applications can access directly to internal pointer of the
  1641. * structure. It's is not recommended, but if you have many images to
  1642. * uncompress with the same parameters, some functions can be called to speedup
  1643. * the decoding.
  1644. *
  1645. ******************************************************************************/
  1646. /**
  1647. * Allocate a new tinyjpeg decoder object.
  1648. *
  1649. * Before calling any other functions, an object need to be called.
  1650. */
  1651. struct jdec_private *tinyjpeg_init(void)
  1652. {
  1653. struct jdec_private *priv;
  1654. priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private));
  1655. if (priv == NULL)
  1656. return NULL;
  1657. return priv;
  1658. }
  1659. /**
  1660. * Free a tinyjpeg object.
  1661. *
  1662. * No others function can be called after this one.
  1663. */
  1664. void tinyjpeg_free(struct jdec_private *priv)
  1665. {
  1666. int i;
  1667. for (i=0; i<COMPONENTS; i++) {
  1668. if (priv->components[i])
  1669. free(priv->components[i]);
  1670. priv->components[i] = NULL;
  1671. }
  1672. free(priv);
  1673. }
  1674. /**
  1675. * Initialize the tinyjpeg object and prepare the decoding of the stream.
  1676. *
  1677. * Check if the jpeg can be decoded with this jpeg decoder.
  1678. * Fill some table used for preprocessing.
  1679. */
  1680. int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
  1681. {
  1682. int ret;
  1683. /* Identify the file */
  1684. if ((buf[0] != 0xFF) || (buf[1] != SOI))
  1685. error0("Not a JPG file ?\n");
  1686. priv->stream_begin = buf+2;
  1687. priv->stream_length = size-2;
  1688. priv->stream_end = priv->stream_begin + priv->stream_length;
  1689. ret = parse_JFIF(priv, priv->stream_begin);
  1690. return ret;
  1691. }
  1692. static const decode_MCU_fct decode_mcu_3comp_table[4] = {
  1693. decode_MCU_1x1_3planes,
  1694. decode_MCU_1x2_3planes,
  1695. decode_MCU_2x1_3planes,
  1696. decode_MCU_2x2_3planes,
  1697. };
  1698. static const decode_MCU_fct decode_mcu_1comp_table[4] = {
  1699. decode_MCU_1x1_1plane,
  1700. decode_MCU_1x2_1plane,
  1701. decode_MCU_2x1_1plane,
  1702. decode_MCU_2x2_1plane,
  1703. };
  1704. static const convert_colorspace_fct convert_colorspace_yuv420p[4] = {
  1705. YCrCB_to_YUV420P_1x1,
  1706. YCrCB_to_YUV420P_1x2,
  1707. YCrCB_to_YUV420P_2x1,
  1708. YCrCB_to_YUV420P_2x2,
  1709. };
  1710. static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
  1711. YCrCB_to_RGB24_1x1,
  1712. YCrCB_to_RGB24_1x2,
  1713. YCrCB_to_RGB24_2x1,
  1714. YCrCB_to_RGB24_2x2,
  1715. };
  1716. static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
  1717. YCrCB_to_BGR24_1x1,
  1718. YCrCB_to_BGR24_1x2,
  1719. YCrCB_to_BGR24_2x1,
  1720. YCrCB_to_BGR24_2x2,
  1721. };
  1722. static const convert_colorspace_fct convert_colorspace_grey[4] = {
  1723. YCrCB_to_Grey_1x1,
  1724. YCrCB_to_Grey_1x2,
  1725. YCrCB_to_Grey_2x1,
  1726. YCrCB_to_Grey_2x2,
  1727. };
  1728. /**
  1729. * Decode and convert the jpeg image into @pixfmt@ image
  1730. *
  1731. * Note: components will be automaticaly allocated if no memory is attached.
  1732. */
  1733. int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
  1734. {
  1735. unsigned int x, y, xstride_by_mcu, ystride_by_mcu;
  1736. unsigned int bytes_per_blocklines[3], bytes_per_mcu[3];
  1737. decode_MCU_fct decode_MCU;
  1738. const decode_MCU_fct *decode_mcu_table;
  1739. const convert_colorspace_fct *colorspace_array_conv;
  1740. convert_colorspace_fct convert_to_pixfmt;
  1741. if (setjmp(priv->jump_state))
  1742. return -1;
  1743. /* To keep gcc happy initialize some array */
  1744. bytes_per_mcu[1] = 0;
  1745. bytes_per_mcu[2] = 0;
  1746. bytes_per_blocklines[1] = 0;
  1747. bytes_per_blocklines[2] = 0;
  1748. decode_mcu_table = decode_mcu_3comp_table;
  1749. switch (pixfmt) {
  1750. case TINYJPEG_FMT_YUV420P:
  1751. colorspace_array_conv = convert_colorspace_yuv420p;
  1752. if (priv->components[0] == NULL)
  1753. priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
  1754. if (priv->components[1] == NULL)
  1755. priv->components[1] = (uint8_t *)malloc(priv->width * priv->height/4);
  1756. if (priv->components[2] == NULL)
  1757. priv->components[2] = (uint8_t *)malloc(priv->width * priv->height/4);
  1758. bytes_per_blocklines[0] = priv->width;
  1759. bytes_per_blocklines[1] = priv->width/4;
  1760. bytes_per_blocklines[2] = priv->width/4;
  1761. bytes_per_mcu[0] = 8;
  1762. bytes_per_mcu[1] = 4;
  1763. bytes_per_mcu[2] = 4;
  1764. break;
  1765. case TINYJPEG_FMT_RGB24:
  1766. colorspace_array_conv = convert_colorspace_rgb24;
  1767. if (priv->components[0] == NULL)
  1768. priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
  1769. bytes_per_blocklines[0] = priv->width * 3;
  1770. bytes_per_mcu[0] = 3*8;
  1771. break;
  1772. case TINYJPEG_FMT_BGR24:
  1773. colorspace_array_conv = convert_colorspace_bgr24;
  1774. if (priv->components[0] == NULL)
  1775. priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
  1776. bytes_per_blocklines[0] = priv->width * 3;
  1777. bytes_per_mcu[0] = 3*8;
  1778. break;
  1779. case TINYJPEG_FMT_GREY:
  1780. decode_mcu_table = decode_mcu_1comp_table;
  1781. colorspace_array_conv = convert_colorspace_grey;
  1782. if (priv->components[0] == NULL)
  1783. priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
  1784. bytes_per_blocklines[0] = priv->width;
  1785. bytes_per_mcu[0] = 8;
  1786. break;
  1787. default:
  1788. // trace("Bad pixel format\n");
  1789. return -1;
  1790. }
  1791. xstride_by_mcu = ystride_by_mcu = 8;
  1792. if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
  1793. decode_MCU = decode_mcu_table[0];
  1794. convert_to_pixfmt = colorspace_array_conv[0];
  1795. // trace("Use decode 1x1 sampling\n");
  1796. } else if (priv->component_infos[cY].Hfactor == 1) {
  1797. decode_MCU = decode_mcu_table[1];
  1798. convert_to_pixfmt = colorspace_array_conv[1];
  1799. ystride_by_mcu = 16;
  1800. // trace("Use decode 1x2 sampling (not supported)\n");
  1801. } else if (priv->component_infos[cY].Vfactor == 2) {
  1802. decode_MCU = decode_mcu_table[3];
  1803. convert_to_pixfmt = colorspace_array_conv[3];
  1804. xstride_by_mcu = 16;
  1805. ystride_by_mcu = 16;
  1806. // trace("Use decode 2x2 sampling\n");
  1807. } else {
  1808. decode_MCU = decode_mcu_table[2];
  1809. convert_to_pixfmt = colorspace_array_conv[2];
  1810. xstride_by_mcu = 16;
  1811. // trace("Use decode 2x1 sampling\n");
  1812. }
  1813. resync(priv);
  1814. /* Don't forget to that block can be either 8 or 16 lines */
  1815. bytes_per_blocklines[0] *= ystride_by_mcu;
  1816. bytes_per_blocklines[1] *= ystride_by_

Large files files are truncated, but you can click here to view the full file