jpge.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // jpge.h - C++ class for JPEG compression.
  2. // Public Domain or Apache 2.0, Richard Geldreich <richgel99@gmail.com>
  3. // Alex Evans: Added RGBA support, linear memory allocator.
  4. #ifndef JPEG_ENCODER_H
  5. #define JPEG_ENCODER_H
  6. namespace jpge
  7. {
  8. typedef unsigned char uint8;
  9. typedef signed short int16;
  10. typedef signed int int32;
  11. typedef unsigned short uint16;
  12. typedef unsigned int uint32;
  13. typedef unsigned int uint;
  14. // JPEG chroma subsampling factors. Y_ONLY (grayscale images) and H2V2 (color images) are the most common.
  15. enum subsampling_t { Y_ONLY = 0, H1V1 = 1, H2V1 = 2, H2V2 = 3 };
  16. // JPEG compression parameters structure.
  17. struct params
  18. {
  19. inline params() : m_quality(85), m_subsampling(H2V2), m_no_chroma_discrim_flag(false), m_two_pass_flag(false), m_use_std_tables(false) { }
  20. inline bool check() const
  21. {
  22. if ((m_quality < 1) || (m_quality > 100)) return false;
  23. if ((uint)m_subsampling > (uint)H2V2) return false;
  24. return true;
  25. }
  26. // Quality: 1-100, higher is better. Typical values are around 50-95.
  27. int m_quality;
  28. // m_subsampling:
  29. // 0 = Y (grayscale) only
  30. // 1 = YCbCr, no subsampling (H1V1, YCbCr 1x1x1, 3 blocks per MCU)
  31. // 2 = YCbCr, H2V1 subsampling (YCbCr 2x1x1, 4 blocks per MCU)
  32. // 3 = YCbCr, H2V2 subsampling (YCbCr 4x1x1, 6 blocks per MCU-- very common)
  33. subsampling_t m_subsampling;
  34. // Disables CbCr discrimination - only intended for testing.
  35. // If true, the Y quantization table is also used for the CbCr channels.
  36. bool m_no_chroma_discrim_flag;
  37. bool m_two_pass_flag;
  38. // By default we use the same quantization tables as mozjpeg's default.
  39. // Set to true to use the traditional tables from JPEG Annex K.
  40. bool m_use_std_tables;
  41. };
  42. // Writes JPEG image to a file.
  43. // num_channels must be 1 (Y) or 3 (RGB), image pitch must be width*num_channels.
  44. bool compress_image_to_jpeg_file(const char* pFilename, int width, int height, int num_channels, const uint8* pImage_data, const params& comp_params = params());
  45. // Writes JPEG image to memory buffer.
  46. // On entry, buf_size is the size of the output buffer pointed at by pBuf, which should be at least ~1024 bytes.
  47. // If return value is true, buf_size will be set to the size of the compressed data.
  48. bool compress_image_to_jpeg_file_in_memory(void* pBuf, int& buf_size, int width, int height, int num_channels, const uint8* pImage_data, const params& comp_params = params());
  49. // Output stream abstract class - used by the jpeg_encoder class to write to the output stream.
  50. // put_buf() is generally called with len==JPGE_OUT_BUF_SIZE bytes, but for headers it'll be called with smaller amounts.
  51. class output_stream
  52. {
  53. public:
  54. virtual ~output_stream() { };
  55. virtual bool put_buf(const void* Pbuf, int len) = 0;
  56. template<class T> inline bool put_obj(const T& obj) { return put_buf(&obj, sizeof(T)); }
  57. };
  58. // Lower level jpeg_encoder class - useful if more control is needed than the above helper functions.
  59. class jpeg_encoder
  60. {
  61. public:
  62. jpeg_encoder();
  63. ~jpeg_encoder();
  64. // Initializes the compressor.
  65. // pStream: The stream object to use for writing compressed data.
  66. // params - Compression parameters structure, defined above.
  67. // width, height - Image dimensions.
  68. // channels - May be 1, or 3. 1 indicates grayscale, 3 indicates RGB source data.
  69. // Returns false on out of memory or if a stream write fails.
  70. bool init(output_stream* pStream, int width, int height, int src_channels, const params& comp_params = params());
  71. const params& get_params() const { return m_params; }
  72. // Deinitializes the compressor, freeing any allocated memory. May be called at any time.
  73. void deinit();
  74. uint get_total_passes() const { return m_params.m_two_pass_flag ? 2 : 1; }
  75. inline uint get_cur_pass() { return m_pass_num; }
  76. // Call this method with each source scanline.
  77. // width * src_channels bytes per scanline is expected (RGB or Y format).
  78. // You must call with NULL after all scanlines are processed to finish compression.
  79. // Returns false on out of memory or if a stream write fails.
  80. bool process_scanline(const void* pScanline);
  81. private:
  82. jpeg_encoder(const jpeg_encoder&);
  83. jpeg_encoder& operator =(const jpeg_encoder&);
  84. typedef int32 sample_array_t;
  85. output_stream* m_pStream;
  86. params m_params;
  87. uint8 m_num_components;
  88. uint8 m_comp_h_samp[3], m_comp_v_samp[3];
  89. int m_image_x, m_image_y, m_image_bpp, m_image_bpl;
  90. int m_image_x_mcu, m_image_y_mcu;
  91. int m_image_bpl_xlt, m_image_bpl_mcu;
  92. int m_mcus_per_row;
  93. int m_mcu_x, m_mcu_y;
  94. uint8* m_mcu_lines[16];
  95. uint8 m_mcu_y_ofs;
  96. sample_array_t m_sample_array[64];
  97. int16 m_coefficient_array[64];
  98. int32 m_quantization_tables[2][64];
  99. uint m_huff_codes[4][256];
  100. uint8 m_huff_code_sizes[4][256];
  101. uint8 m_huff_bits[4][17];
  102. uint8 m_huff_val[4][256];
  103. uint32 m_huff_count[4][256];
  104. int m_last_dc_val[3];
  105. enum { JPGE_OUT_BUF_SIZE = 2048 };
  106. uint8 m_out_buf[JPGE_OUT_BUF_SIZE];
  107. uint8* m_pOut_buf;
  108. uint m_out_buf_left;
  109. uint32 m_bit_buffer;
  110. uint m_bits_in;
  111. uint8 m_pass_num;
  112. bool m_all_stream_writes_succeeded;
  113. void optimize_huffman_table(int table_num, int table_len);
  114. void emit_byte(uint8 i);
  115. void emit_word(uint i);
  116. void emit_marker(int marker);
  117. void emit_jfif_app0();
  118. void emit_dqt();
  119. void emit_sof();
  120. void emit_dht(uint8* bits, uint8* val, int index, bool ac_flag);
  121. void emit_dhts();
  122. void emit_sos();
  123. void emit_markers();
  124. void compute_huffman_table(uint* codes, uint8* code_sizes, uint8* bits, uint8* val);
  125. void compute_quant_table(int32* dst, int16* src);
  126. void adjust_quant_table(int32* dst, int32* src);
  127. void first_pass_init();
  128. bool second_pass_init();
  129. bool jpg_open(int p_x_res, int p_y_res, int src_channels);
  130. void load_block_8_8_grey(int x);
  131. void load_block_8_8(int x, int y, int c);
  132. void load_block_16_8(int x, int c);
  133. void load_block_16_8_8(int x, int c);
  134. void load_quantized_coefficients(int component_num);
  135. void flush_output_buffer();
  136. void put_bits(uint bits, uint len);
  137. void code_coefficients_pass_one(int component_num);
  138. void code_coefficients_pass_two(int component_num);
  139. void code_block(int component_num);
  140. void process_mcu_row();
  141. bool terminate_pass_one();
  142. bool terminate_pass_two();
  143. bool process_end_of_image();
  144. void load_mcu(const void* src);
  145. void clear();
  146. void init();
  147. };
  148. } // namespace jpge
  149. #endif // JPEG_ENCODER