jpge.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // jpge.cpp - C++ class for JPEG compression.
  2. // Public domain, Rich Geldreich <richgel99@gmail.com>
  3. // v1.01, Dec. 18, 2010 - Initial release
  4. // v1.02, Apr. 6, 2011 - Removed 2x2 ordered dither in H2V1 chroma subsampling method load_block_16_8_8(). (The rounding factor was 2, when it should have been 1. Either way, it wasn't helping.)
  5. // v1.03, Apr. 16, 2011 - Added support for optimized Huffman code tables, optimized dynamic memory allocation down to only 1 alloc.
  6. // Also from Alex Evans: Added RGBA support, linear memory allocator (no longer needed in v1.03).
  7. // v1.04, May. 19, 2012: Forgot to set m_pFile ptr to NULL in cfile_stream::close(). Thanks to Owen Kaluza for reporting this bug.
  8. // Code tweaks to fix VS2008 static code analysis warnings (all looked harmless).
  9. // Code review revealed method load_block_16_8_8() (used for the non-default H2V1 sampling mode to downsample chroma) somehow didn't get the rounding factor fix from v1.02.
  10. #include "jpge.h"
  11. #include <stdint.h>
  12. #include <stdarg.h>
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include "esp_heap_caps.h"
  19. #define JPGE_MAX(a,b) (((a)>(b))?(a):(b))
  20. #define JPGE_MIN(a,b) (((a)<(b))?(a):(b))
  21. namespace jpge {
  22. static inline void *jpge_malloc(size_t nSize) {
  23. void * b = malloc(nSize);
  24. if(b){
  25. return b;
  26. }
  27. return heap_caps_malloc(nSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  28. }
  29. static inline void jpge_free(void *p) { free(p); }
  30. // Various JPEG enums and tables.
  31. enum { M_SOF0 = 0xC0, M_DHT = 0xC4, M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_APP0 = 0xE0 };
  32. enum { DC_LUM_CODES = 12, AC_LUM_CODES = 256, DC_CHROMA_CODES = 12, AC_CHROMA_CODES = 256, MAX_HUFF_SYMBOLS = 257, MAX_HUFF_CODESIZE = 32 };
  33. static const uint8 s_zag[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 };
  34. static const int16 s_std_lum_quant[64] = { 16,11,12,14,12,10,16,14,13,14,18,17,16,19,24,40,26,24,22,22,24,49,35,37,29,40,58,51,61,60,57,51,56,55,64,72,92,78,64,68,87,69,55,56,80,109,81,87,95,98,103,104,103,62,77,113,121,112,100,120,92,101,103,99 };
  35. static const int16 s_std_croma_quant[64] = { 17,18,18,24,21,24,47,26,26,47,99,66,56,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99 };
  36. static const uint8 s_dc_lum_bits[17] = { 0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0 };
  37. static const uint8 s_dc_lum_val[DC_LUM_CODES] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
  38. static const uint8 s_ac_lum_bits[17] = { 0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d };
  39. static const uint8 s_ac_lum_val[AC_LUM_CODES] = {
  40. 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,
  41. 0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
  42. 0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
  43. 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,
  44. 0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
  45. 0xf9,0xfa
  46. };
  47. static const uint8 s_dc_chroma_bits[17] = { 0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };
  48. static const uint8 s_dc_chroma_val[DC_CHROMA_CODES] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
  49. static const uint8 s_ac_chroma_bits[17] = { 0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77 };
  50. static const uint8 s_ac_chroma_val[AC_CHROMA_CODES] = {
  51. 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,
  52. 0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,
  53. 0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
  54. 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,
  55. 0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
  56. 0xf9,0xfa
  57. };
  58. const int YR = 19595, YG = 38470, YB = 7471, CB_R = -11059, CB_G = -21709, CB_B = 32768, CR_R = 32768, CR_G = -27439, CR_B = -5329;
  59. static int32 m_last_quality = 0;
  60. static int32 m_quantization_tables[2][64];
  61. static bool m_huff_initialized = false;
  62. static uint m_huff_codes[4][256];
  63. static uint8 m_huff_code_sizes[4][256];
  64. static uint8 m_huff_bits[4][17];
  65. static uint8 m_huff_val[4][256];
  66. static inline uint8 clamp(int i) {
  67. if (i < 0) {
  68. i = 0;
  69. } else if (i > 255){
  70. i = 255;
  71. }
  72. return static_cast<uint8>(i);
  73. }
  74. static void RGB_to_YCC(uint8* pDst, const uint8 *pSrc, int num_pixels) {
  75. for ( ; num_pixels; pDst += 3, pSrc += 3, num_pixels--) {
  76. const int r = pSrc[0], g = pSrc[1], b = pSrc[2];
  77. pDst[0] = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);
  78. pDst[1] = clamp(128 + ((r * CB_R + g * CB_G + b * CB_B + 32768) >> 16));
  79. pDst[2] = clamp(128 + ((r * CR_R + g * CR_G + b * CR_B + 32768) >> 16));
  80. }
  81. }
  82. static void RGB_to_Y(uint8* pDst, const uint8 *pSrc, int num_pixels) {
  83. for ( ; num_pixels; pDst++, pSrc += 3, num_pixels--) {
  84. pDst[0] = static_cast<uint8>((pSrc[0] * YR + pSrc[1] * YG + pSrc[2] * YB + 32768) >> 16);
  85. }
  86. }
  87. static void Y_to_YCC(uint8* pDst, const uint8* pSrc, int num_pixels) {
  88. for( ; num_pixels; pDst += 3, pSrc++, num_pixels--) {
  89. pDst[0] = pSrc[0];
  90. pDst[1] = 128;
  91. pDst[2] = 128;
  92. }
  93. }
  94. // Forward DCT - DCT derived from jfdctint.
  95. enum { CONST_BITS = 13, ROW_BITS = 2 };
  96. #define DCT_DESCALE(x, n) (((x) + (((int32)1) << ((n) - 1))) >> (n))
  97. #define DCT_MUL(var, c) (static_cast<int16>(var) * static_cast<int32>(c))
  98. #define DCT1D(s0, s1, s2, s3, s4, s5, s6, s7) \
  99. int32 t0 = s0 + s7, t7 = s0 - s7, t1 = s1 + s6, t6 = s1 - s6, t2 = s2 + s5, t5 = s2 - s5, t3 = s3 + s4, t4 = s3 - s4; \
  100. int32 t10 = t0 + t3, t13 = t0 - t3, t11 = t1 + t2, t12 = t1 - t2; \
  101. int32 u1 = DCT_MUL(t12 + t13, 4433); \
  102. s2 = u1 + DCT_MUL(t13, 6270); \
  103. s6 = u1 + DCT_MUL(t12, -15137); \
  104. u1 = t4 + t7; \
  105. int32 u2 = t5 + t6, u3 = t4 + t6, u4 = t5 + t7; \
  106. int32 z5 = DCT_MUL(u3 + u4, 9633); \
  107. t4 = DCT_MUL(t4, 2446); t5 = DCT_MUL(t5, 16819); \
  108. t6 = DCT_MUL(t6, 25172); t7 = DCT_MUL(t7, 12299); \
  109. u1 = DCT_MUL(u1, -7373); u2 = DCT_MUL(u2, -20995); \
  110. u3 = DCT_MUL(u3, -16069); u4 = DCT_MUL(u4, -3196); \
  111. u3 += z5; u4 += z5; \
  112. s0 = t10 + t11; s1 = t7 + u1 + u4; s3 = t6 + u2 + u3; s4 = t10 - t11; s5 = t5 + u2 + u4; s7 = t4 + u1 + u3;
  113. static void DCT2D(int32 *p) {
  114. int32 c, *q = p;
  115. for (c = 7; c >= 0; c--, q += 8) {
  116. int32 s0 = q[0], s1 = q[1], s2 = q[2], s3 = q[3], s4 = q[4], s5 = q[5], s6 = q[6], s7 = q[7];
  117. DCT1D(s0, s1, s2, s3, s4, s5, s6, s7);
  118. q[0] = s0 << ROW_BITS; q[1] = DCT_DESCALE(s1, CONST_BITS-ROW_BITS); q[2] = DCT_DESCALE(s2, CONST_BITS-ROW_BITS); q[3] = DCT_DESCALE(s3, CONST_BITS-ROW_BITS);
  119. q[4] = s4 << ROW_BITS; q[5] = DCT_DESCALE(s5, CONST_BITS-ROW_BITS); q[6] = DCT_DESCALE(s6, CONST_BITS-ROW_BITS); q[7] = DCT_DESCALE(s7, CONST_BITS-ROW_BITS);
  120. }
  121. for (q = p, c = 7; c >= 0; c--, q++) {
  122. int32 s0 = q[0*8], s1 = q[1*8], s2 = q[2*8], s3 = q[3*8], s4 = q[4*8], s5 = q[5*8], s6 = q[6*8], s7 = q[7*8];
  123. DCT1D(s0, s1, s2, s3, s4, s5, s6, s7);
  124. q[0*8] = DCT_DESCALE(s0, ROW_BITS+3); q[1*8] = DCT_DESCALE(s1, CONST_BITS+ROW_BITS+3); q[2*8] = DCT_DESCALE(s2, CONST_BITS+ROW_BITS+3); q[3*8] = DCT_DESCALE(s3, CONST_BITS+ROW_BITS+3);
  125. q[4*8] = DCT_DESCALE(s4, ROW_BITS+3); q[5*8] = DCT_DESCALE(s5, CONST_BITS+ROW_BITS+3); q[6*8] = DCT_DESCALE(s6, CONST_BITS+ROW_BITS+3); q[7*8] = DCT_DESCALE(s7, CONST_BITS+ROW_BITS+3);
  126. }
  127. }
  128. // Compute the actual canonical Huffman codes/code sizes given the JPEG huff bits and val arrays.
  129. static void compute_huffman_table(uint *codes, uint8 *code_sizes, uint8 *bits, uint8 *val)
  130. {
  131. int i, l, last_p, si;
  132. static uint8 huff_size[257];
  133. static uint huff_code[257];
  134. uint code;
  135. int p = 0;
  136. for (l = 1; l <= 16; l++) {
  137. for (i = 1; i <= bits[l]; i++) {
  138. huff_size[p++] = (char)l;
  139. }
  140. }
  141. huff_size[p] = 0;
  142. last_p = p; // write sentinel
  143. code = 0; si = huff_size[0]; p = 0;
  144. while (huff_size[p]) {
  145. while (huff_size[p] == si) {
  146. huff_code[p++] = code++;
  147. }
  148. code <<= 1;
  149. si++;
  150. }
  151. memset(codes, 0, sizeof(codes[0])*256);
  152. memset(code_sizes, 0, sizeof(code_sizes[0])*256);
  153. for (p = 0; p < last_p; p++) {
  154. codes[val[p]] = huff_code[p];
  155. code_sizes[val[p]] = huff_size[p];
  156. }
  157. }
  158. void jpeg_encoder::flush_output_buffer()
  159. {
  160. if (m_out_buf_left != JPGE_OUT_BUF_SIZE) {
  161. m_all_stream_writes_succeeded = m_all_stream_writes_succeeded && m_pStream->put_buf(m_out_buf, JPGE_OUT_BUF_SIZE - m_out_buf_left);
  162. }
  163. m_pOut_buf = m_out_buf;
  164. m_out_buf_left = JPGE_OUT_BUF_SIZE;
  165. }
  166. void jpeg_encoder::emit_byte(uint8 i)
  167. {
  168. *m_pOut_buf++ = i;
  169. if (--m_out_buf_left == 0) {
  170. flush_output_buffer();
  171. }
  172. }
  173. void jpeg_encoder::put_bits(uint bits, uint len)
  174. {
  175. uint8 c = 0;
  176. m_bit_buffer |= ((uint32)bits << (24 - (m_bits_in += len)));
  177. while (m_bits_in >= 8) {
  178. c = (uint8)((m_bit_buffer >> 16) & 0xFF);
  179. emit_byte(c);
  180. if (c == 0xFF) {
  181. emit_byte(0);
  182. }
  183. m_bit_buffer <<= 8;
  184. m_bits_in -= 8;
  185. }
  186. }
  187. void jpeg_encoder::emit_word(uint i)
  188. {
  189. emit_byte(uint8(i >> 8)); emit_byte(uint8(i & 0xFF));
  190. }
  191. // JPEG marker generation.
  192. void jpeg_encoder::emit_marker(int marker)
  193. {
  194. emit_byte(uint8(0xFF)); emit_byte(uint8(marker));
  195. }
  196. // Emit JFIF marker
  197. void jpeg_encoder::emit_jfif_app0()
  198. {
  199. emit_marker(M_APP0);
  200. emit_word(2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1);
  201. emit_byte(0x4A); emit_byte(0x46); emit_byte(0x49); emit_byte(0x46); /* Identifier: ASCII "JFIF" */
  202. emit_byte(0);
  203. emit_byte(1); /* Major version */
  204. emit_byte(1); /* Minor version */
  205. emit_byte(0); /* Density unit */
  206. emit_word(1);
  207. emit_word(1);
  208. emit_byte(0); /* No thumbnail image */
  209. emit_byte(0);
  210. }
  211. // Emit quantization tables
  212. void jpeg_encoder::emit_dqt()
  213. {
  214. for (int i = 0; i < ((m_num_components == 3) ? 2 : 1); i++)
  215. {
  216. emit_marker(M_DQT);
  217. emit_word(64 + 1 + 2);
  218. emit_byte(static_cast<uint8>(i));
  219. for (int j = 0; j < 64; j++)
  220. emit_byte(static_cast<uint8>(m_quantization_tables[i][j]));
  221. }
  222. }
  223. // Emit start of frame marker
  224. void jpeg_encoder::emit_sof()
  225. {
  226. emit_marker(M_SOF0); /* baseline */
  227. emit_word(3 * m_num_components + 2 + 5 + 1);
  228. emit_byte(8); /* precision */
  229. emit_word(m_image_y);
  230. emit_word(m_image_x);
  231. emit_byte(m_num_components);
  232. for (int i = 0; i < m_num_components; i++)
  233. {
  234. emit_byte(static_cast<uint8>(i + 1)); /* component ID */
  235. emit_byte((m_comp_h_samp[i] << 4) + m_comp_v_samp[i]); /* h and v sampling */
  236. emit_byte(i > 0); /* quant. table num */
  237. }
  238. }
  239. // Emit Huffman table.
  240. void jpeg_encoder::emit_dht(uint8 *bits, uint8 *val, int index, bool ac_flag)
  241. {
  242. emit_marker(M_DHT);
  243. int length = 0;
  244. for (int i = 1; i <= 16; i++)
  245. length += bits[i];
  246. emit_word(length + 2 + 1 + 16);
  247. emit_byte(static_cast<uint8>(index + (ac_flag << 4)));
  248. for (int i = 1; i <= 16; i++)
  249. emit_byte(bits[i]);
  250. for (int i = 0; i < length; i++)
  251. emit_byte(val[i]);
  252. }
  253. // Emit all Huffman tables.
  254. void jpeg_encoder::emit_dhts()
  255. {
  256. emit_dht(m_huff_bits[0+0], m_huff_val[0+0], 0, false);
  257. emit_dht(m_huff_bits[2+0], m_huff_val[2+0], 0, true);
  258. if (m_num_components == 3) {
  259. emit_dht(m_huff_bits[0+1], m_huff_val[0+1], 1, false);
  260. emit_dht(m_huff_bits[2+1], m_huff_val[2+1], 1, true);
  261. }
  262. }
  263. // emit start of scan
  264. void jpeg_encoder::emit_sos()
  265. {
  266. emit_marker(M_SOS);
  267. emit_word(2 * m_num_components + 2 + 1 + 3);
  268. emit_byte(m_num_components);
  269. for (int i = 0; i < m_num_components; i++)
  270. {
  271. emit_byte(static_cast<uint8>(i + 1));
  272. if (i == 0)
  273. emit_byte((0 << 4) + 0);
  274. else
  275. emit_byte((1 << 4) + 1);
  276. }
  277. emit_byte(0); /* spectral selection */
  278. emit_byte(63);
  279. emit_byte(0);
  280. }
  281. void jpeg_encoder::load_block_8_8_grey(int x)
  282. {
  283. uint8 *pSrc;
  284. sample_array_t *pDst = m_sample_array;
  285. x <<= 3;
  286. for (int i = 0; i < 8; i++, pDst += 8)
  287. {
  288. pSrc = m_mcu_lines[i] + x;
  289. pDst[0] = pSrc[0] - 128; pDst[1] = pSrc[1] - 128; pDst[2] = pSrc[2] - 128; pDst[3] = pSrc[3] - 128;
  290. pDst[4] = pSrc[4] - 128; pDst[5] = pSrc[5] - 128; pDst[6] = pSrc[6] - 128; pDst[7] = pSrc[7] - 128;
  291. }
  292. }
  293. void jpeg_encoder::load_block_8_8(int x, int y, int c)
  294. {
  295. uint8 *pSrc;
  296. sample_array_t *pDst = m_sample_array;
  297. x = (x * (8 * 3)) + c;
  298. y <<= 3;
  299. for (int i = 0; i < 8; i++, pDst += 8)
  300. {
  301. pSrc = m_mcu_lines[y + i] + x;
  302. pDst[0] = pSrc[0 * 3] - 128; pDst[1] = pSrc[1 * 3] - 128; pDst[2] = pSrc[2 * 3] - 128; pDst[3] = pSrc[3 * 3] - 128;
  303. pDst[4] = pSrc[4 * 3] - 128; pDst[5] = pSrc[5 * 3] - 128; pDst[6] = pSrc[6 * 3] - 128; pDst[7] = pSrc[7 * 3] - 128;
  304. }
  305. }
  306. void jpeg_encoder::load_block_16_8(int x, int c)
  307. {
  308. uint8 *pSrc1, *pSrc2;
  309. sample_array_t *pDst = m_sample_array;
  310. x = (x * (16 * 3)) + c;
  311. int a = 0, b = 2;
  312. for (int i = 0; i < 16; i += 2, pDst += 8)
  313. {
  314. pSrc1 = m_mcu_lines[i + 0] + x;
  315. pSrc2 = m_mcu_lines[i + 1] + x;
  316. pDst[0] = ((pSrc1[ 0 * 3] + pSrc1[ 1 * 3] + pSrc2[ 0 * 3] + pSrc2[ 1 * 3] + a) >> 2) - 128; pDst[1] = ((pSrc1[ 2 * 3] + pSrc1[ 3 * 3] + pSrc2[ 2 * 3] + pSrc2[ 3 * 3] + b) >> 2) - 128;
  317. pDst[2] = ((pSrc1[ 4 * 3] + pSrc1[ 5 * 3] + pSrc2[ 4 * 3] + pSrc2[ 5 * 3] + a) >> 2) - 128; pDst[3] = ((pSrc1[ 6 * 3] + pSrc1[ 7 * 3] + pSrc2[ 6 * 3] + pSrc2[ 7 * 3] + b) >> 2) - 128;
  318. pDst[4] = ((pSrc1[ 8 * 3] + pSrc1[ 9 * 3] + pSrc2[ 8 * 3] + pSrc2[ 9 * 3] + a) >> 2) - 128; pDst[5] = ((pSrc1[10 * 3] + pSrc1[11 * 3] + pSrc2[10 * 3] + pSrc2[11 * 3] + b) >> 2) - 128;
  319. pDst[6] = ((pSrc1[12 * 3] + pSrc1[13 * 3] + pSrc2[12 * 3] + pSrc2[13 * 3] + a) >> 2) - 128; pDst[7] = ((pSrc1[14 * 3] + pSrc1[15 * 3] + pSrc2[14 * 3] + pSrc2[15 * 3] + b) >> 2) - 128;
  320. int temp = a; a = b; b = temp;
  321. }
  322. }
  323. void jpeg_encoder::load_block_16_8_8(int x, int c)
  324. {
  325. uint8 *pSrc1;
  326. sample_array_t *pDst = m_sample_array;
  327. x = (x * (16 * 3)) + c;
  328. for (int i = 0; i < 8; i++, pDst += 8)
  329. {
  330. pSrc1 = m_mcu_lines[i + 0] + x;
  331. pDst[0] = ((pSrc1[ 0 * 3] + pSrc1[ 1 * 3]) >> 1) - 128; pDst[1] = ((pSrc1[ 2 * 3] + pSrc1[ 3 * 3]) >> 1) - 128;
  332. pDst[2] = ((pSrc1[ 4 * 3] + pSrc1[ 5 * 3]) >> 1) - 128; pDst[3] = ((pSrc1[ 6 * 3] + pSrc1[ 7 * 3]) >> 1) - 128;
  333. pDst[4] = ((pSrc1[ 8 * 3] + pSrc1[ 9 * 3]) >> 1) - 128; pDst[5] = ((pSrc1[10 * 3] + pSrc1[11 * 3]) >> 1) - 128;
  334. pDst[6] = ((pSrc1[12 * 3] + pSrc1[13 * 3]) >> 1) - 128; pDst[7] = ((pSrc1[14 * 3] + pSrc1[15 * 3]) >> 1) - 128;
  335. }
  336. }
  337. void jpeg_encoder::load_quantized_coefficients(int component_num)
  338. {
  339. int32 *q = m_quantization_tables[component_num > 0];
  340. int16 *pDst = m_coefficient_array;
  341. for (int i = 0; i < 64; i++)
  342. {
  343. sample_array_t j = m_sample_array[s_zag[i]];
  344. if (j < 0)
  345. {
  346. if ((j = -j + (*q >> 1)) < *q)
  347. *pDst++ = 0;
  348. else
  349. *pDst++ = static_cast<int16>(-(j / *q));
  350. }
  351. else
  352. {
  353. if ((j = j + (*q >> 1)) < *q)
  354. *pDst++ = 0;
  355. else
  356. *pDst++ = static_cast<int16>((j / *q));
  357. }
  358. q++;
  359. }
  360. }
  361. void jpeg_encoder::code_coefficients_pass_two(int component_num)
  362. {
  363. int i, j, run_len, nbits, temp1, temp2;
  364. int16 *pSrc = m_coefficient_array;
  365. uint *codes[2];
  366. uint8 *code_sizes[2];
  367. if (component_num == 0)
  368. {
  369. codes[0] = m_huff_codes[0 + 0]; codes[1] = m_huff_codes[2 + 0];
  370. code_sizes[0] = m_huff_code_sizes[0 + 0]; code_sizes[1] = m_huff_code_sizes[2 + 0];
  371. }
  372. else
  373. {
  374. codes[0] = m_huff_codes[0 + 1]; codes[1] = m_huff_codes[2 + 1];
  375. code_sizes[0] = m_huff_code_sizes[0 + 1]; code_sizes[1] = m_huff_code_sizes[2 + 1];
  376. }
  377. temp1 = temp2 = pSrc[0] - m_last_dc_val[component_num];
  378. m_last_dc_val[component_num] = pSrc[0];
  379. if (temp1 < 0)
  380. {
  381. temp1 = -temp1; temp2--;
  382. }
  383. nbits = 0;
  384. while (temp1)
  385. {
  386. nbits++; temp1 >>= 1;
  387. }
  388. put_bits(codes[0][nbits], code_sizes[0][nbits]);
  389. if (nbits) put_bits(temp2 & ((1 << nbits) - 1), nbits);
  390. for (run_len = 0, i = 1; i < 64; i++)
  391. {
  392. if ((temp1 = m_coefficient_array[i]) == 0)
  393. run_len++;
  394. else
  395. {
  396. while (run_len >= 16)
  397. {
  398. put_bits(codes[1][0xF0], code_sizes[1][0xF0]);
  399. run_len -= 16;
  400. }
  401. if ((temp2 = temp1) < 0)
  402. {
  403. temp1 = -temp1;
  404. temp2--;
  405. }
  406. nbits = 1;
  407. while (temp1 >>= 1)
  408. nbits++;
  409. j = (run_len << 4) + nbits;
  410. put_bits(codes[1][j], code_sizes[1][j]);
  411. put_bits(temp2 & ((1 << nbits) - 1), nbits);
  412. run_len = 0;
  413. }
  414. }
  415. if (run_len)
  416. put_bits(codes[1][0], code_sizes[1][0]);
  417. }
  418. void jpeg_encoder::code_block(int component_num)
  419. {
  420. DCT2D(m_sample_array);
  421. load_quantized_coefficients(component_num);
  422. code_coefficients_pass_two(component_num);
  423. }
  424. void jpeg_encoder::process_mcu_row()
  425. {
  426. if (m_num_components == 1)
  427. {
  428. for (int i = 0; i < m_mcus_per_row; i++)
  429. {
  430. load_block_8_8_grey(i); code_block(0);
  431. }
  432. }
  433. else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1))
  434. {
  435. for (int i = 0; i < m_mcus_per_row; i++)
  436. {
  437. load_block_8_8(i, 0, 0); code_block(0); load_block_8_8(i, 0, 1); code_block(1); load_block_8_8(i, 0, 2); code_block(2);
  438. }
  439. }
  440. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1))
  441. {
  442. for (int i = 0; i < m_mcus_per_row; i++)
  443. {
  444. load_block_8_8(i * 2 + 0, 0, 0); code_block(0); load_block_8_8(i * 2 + 1, 0, 0); code_block(0);
  445. load_block_16_8_8(i, 1); code_block(1); load_block_16_8_8(i, 2); code_block(2);
  446. }
  447. }
  448. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2))
  449. {
  450. for (int i = 0; i < m_mcus_per_row; i++)
  451. {
  452. load_block_8_8(i * 2 + 0, 0, 0); code_block(0); load_block_8_8(i * 2 + 1, 0, 0); code_block(0);
  453. load_block_8_8(i * 2 + 0, 1, 0); code_block(0); load_block_8_8(i * 2 + 1, 1, 0); code_block(0);
  454. load_block_16_8(i, 1); code_block(1); load_block_16_8(i, 2); code_block(2);
  455. }
  456. }
  457. }
  458. void jpeg_encoder::load_mcu(const void *pSrc)
  459. {
  460. const uint8* Psrc = reinterpret_cast<const uint8*>(pSrc);
  461. uint8* pDst = m_mcu_lines[m_mcu_y_ofs]; // OK to write up to m_image_bpl_xlt bytes to pDst
  462. if (m_num_components == 1) {
  463. if (m_image_bpp == 3)
  464. RGB_to_Y(pDst, Psrc, m_image_x);
  465. else
  466. memcpy(pDst, Psrc, m_image_x);
  467. } else {
  468. if (m_image_bpp == 3)
  469. RGB_to_YCC(pDst, Psrc, m_image_x);
  470. else
  471. Y_to_YCC(pDst, Psrc, m_image_x);
  472. }
  473. // Possibly duplicate pixels at end of scanline if not a multiple of 8 or 16
  474. if (m_num_components == 1)
  475. memset(m_mcu_lines[m_mcu_y_ofs] + m_image_bpl_xlt, pDst[m_image_bpl_xlt - 1], m_image_x_mcu - m_image_x);
  476. else
  477. {
  478. const uint8 y = pDst[m_image_bpl_xlt - 3 + 0], cb = pDst[m_image_bpl_xlt - 3 + 1], cr = pDst[m_image_bpl_xlt - 3 + 2];
  479. uint8 *q = m_mcu_lines[m_mcu_y_ofs] + m_image_bpl_xlt;
  480. for (int i = m_image_x; i < m_image_x_mcu; i++)
  481. {
  482. *q++ = y; *q++ = cb; *q++ = cr;
  483. }
  484. }
  485. if (++m_mcu_y_ofs == m_mcu_y)
  486. {
  487. process_mcu_row();
  488. m_mcu_y_ofs = 0;
  489. }
  490. }
  491. // Quantization table generation.
  492. void jpeg_encoder::compute_quant_table(int32 *pDst, const int16 *pSrc)
  493. {
  494. int32 q;
  495. if (m_params.m_quality < 50)
  496. q = 5000 / m_params.m_quality;
  497. else
  498. q = 200 - m_params.m_quality * 2;
  499. for (int i = 0; i < 64; i++)
  500. {
  501. int32 j = *pSrc++; j = (j * q + 50L) / 100L;
  502. *pDst++ = JPGE_MIN(JPGE_MAX(j, 1), 255);
  503. }
  504. }
  505. // Higher-level methods.
  506. bool jpeg_encoder::jpg_open(int p_x_res, int p_y_res, int src_channels)
  507. {
  508. m_num_components = 3;
  509. switch (m_params.m_subsampling)
  510. {
  511. case Y_ONLY:
  512. {
  513. m_num_components = 1;
  514. m_comp_h_samp[0] = 1; m_comp_v_samp[0] = 1;
  515. m_mcu_x = 8; m_mcu_y = 8;
  516. break;
  517. }
  518. case H1V1:
  519. {
  520. m_comp_h_samp[0] = 1; m_comp_v_samp[0] = 1;
  521. m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;
  522. m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;
  523. m_mcu_x = 8; m_mcu_y = 8;
  524. break;
  525. }
  526. case H2V1:
  527. {
  528. m_comp_h_samp[0] = 2; m_comp_v_samp[0] = 1;
  529. m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;
  530. m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;
  531. m_mcu_x = 16; m_mcu_y = 8;
  532. break;
  533. }
  534. case H2V2:
  535. {
  536. m_comp_h_samp[0] = 2; m_comp_v_samp[0] = 2;
  537. m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;
  538. m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;
  539. m_mcu_x = 16; m_mcu_y = 16;
  540. }
  541. }
  542. m_image_x = p_x_res; m_image_y = p_y_res;
  543. m_image_bpp = src_channels;
  544. m_image_bpl = m_image_x * src_channels;
  545. m_image_x_mcu = (m_image_x + m_mcu_x - 1) & (~(m_mcu_x - 1));
  546. m_image_y_mcu = (m_image_y + m_mcu_y - 1) & (~(m_mcu_y - 1));
  547. m_image_bpl_xlt = m_image_x * m_num_components;
  548. m_image_bpl_mcu = m_image_x_mcu * m_num_components;
  549. m_mcus_per_row = m_image_x_mcu / m_mcu_x;
  550. if ((m_mcu_lines[0] = static_cast<uint8*>(jpge_malloc(m_image_bpl_mcu * m_mcu_y))) == NULL) {
  551. return false;
  552. }
  553. for (int i = 1; i < m_mcu_y; i++)
  554. m_mcu_lines[i] = m_mcu_lines[i-1] + m_image_bpl_mcu;
  555. if(m_last_quality != m_params.m_quality){
  556. m_last_quality = m_params.m_quality;
  557. compute_quant_table(m_quantization_tables[0], s_std_lum_quant);
  558. compute_quant_table(m_quantization_tables[1], s_std_croma_quant);
  559. }
  560. if(!m_huff_initialized){
  561. m_huff_initialized = true;
  562. memcpy(m_huff_bits[0+0], s_dc_lum_bits, 17); memcpy(m_huff_val[0+0], s_dc_lum_val, DC_LUM_CODES);
  563. memcpy(m_huff_bits[2+0], s_ac_lum_bits, 17); memcpy(m_huff_val[2+0], s_ac_lum_val, AC_LUM_CODES);
  564. memcpy(m_huff_bits[0+1], s_dc_chroma_bits, 17); memcpy(m_huff_val[0+1], s_dc_chroma_val, DC_CHROMA_CODES);
  565. memcpy(m_huff_bits[2+1], s_ac_chroma_bits, 17); memcpy(m_huff_val[2+1], s_ac_chroma_val, AC_CHROMA_CODES);
  566. compute_huffman_table(&m_huff_codes[0+0][0], &m_huff_code_sizes[0+0][0], m_huff_bits[0+0], m_huff_val[0+0]);
  567. compute_huffman_table(&m_huff_codes[2+0][0], &m_huff_code_sizes[2+0][0], m_huff_bits[2+0], m_huff_val[2+0]);
  568. compute_huffman_table(&m_huff_codes[0+1][0], &m_huff_code_sizes[0+1][0], m_huff_bits[0+1], m_huff_val[0+1]);
  569. compute_huffman_table(&m_huff_codes[2+1][0], &m_huff_code_sizes[2+1][0], m_huff_bits[2+1], m_huff_val[2+1]);
  570. }
  571. m_out_buf_left = JPGE_OUT_BUF_SIZE;
  572. m_pOut_buf = m_out_buf;
  573. m_bit_buffer = 0;
  574. m_bits_in = 0;
  575. m_mcu_y_ofs = 0;
  576. m_pass_num = 2;
  577. memset(m_last_dc_val, 0, 3 * sizeof(m_last_dc_val[0]));
  578. // Emit all markers at beginning of image file.
  579. emit_marker(M_SOI);
  580. emit_jfif_app0();
  581. emit_dqt();
  582. emit_sof();
  583. emit_dhts();
  584. emit_sos();
  585. return m_all_stream_writes_succeeded;
  586. }
  587. bool jpeg_encoder::process_end_of_image()
  588. {
  589. if (m_mcu_y_ofs) {
  590. if (m_mcu_y_ofs < 16) { // check here just to shut up static analysis
  591. for (int i = m_mcu_y_ofs; i < m_mcu_y; i++) {
  592. memcpy(m_mcu_lines[i], m_mcu_lines[m_mcu_y_ofs - 1], m_image_bpl_mcu);
  593. }
  594. }
  595. process_mcu_row();
  596. }
  597. put_bits(0x7F, 7);
  598. emit_marker(M_EOI);
  599. flush_output_buffer();
  600. m_all_stream_writes_succeeded = m_all_stream_writes_succeeded && m_pStream->put_buf(NULL, 0);
  601. m_pass_num++; // purposely bump up m_pass_num, for debugging
  602. return true;
  603. }
  604. void jpeg_encoder::clear()
  605. {
  606. m_mcu_lines[0] = NULL;
  607. m_pass_num = 0;
  608. m_all_stream_writes_succeeded = true;
  609. }
  610. jpeg_encoder::jpeg_encoder()
  611. {
  612. clear();
  613. }
  614. jpeg_encoder::~jpeg_encoder()
  615. {
  616. deinit();
  617. }
  618. bool jpeg_encoder::init(output_stream *pStream, int width, int height, int src_channels, const params &comp_params)
  619. {
  620. deinit();
  621. if (((!pStream) || (width < 1) || (height < 1)) || ((src_channels != 1) && (src_channels != 3) && (src_channels != 4)) || (!comp_params.check())) return false;
  622. m_pStream = pStream;
  623. m_params = comp_params;
  624. return jpg_open(width, height, src_channels);
  625. }
  626. void jpeg_encoder::deinit()
  627. {
  628. jpge_free(m_mcu_lines[0]);
  629. clear();
  630. }
  631. bool jpeg_encoder::process_scanline(const void* pScanline)
  632. {
  633. if ((m_pass_num < 1) || (m_pass_num > 2)) {
  634. return false;
  635. }
  636. if (m_all_stream_writes_succeeded) {
  637. if (!pScanline) {
  638. if (!process_end_of_image()) {
  639. return false;
  640. }
  641. } else {
  642. load_mcu(pScanline);
  643. }
  644. }
  645. return m_all_stream_writes_succeeded;
  646. }
  647. } // namespace jpge