jpge.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // jpge.h - C++ class for JPEG compression.
  2. // Public domain, Rich 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. inline params() : m_quality(85), m_subsampling(H2V2) { }
  19. inline bool check() const {
  20. if ((m_quality < 1) || (m_quality > 100)) {
  21. return false;
  22. }
  23. if ((uint)m_subsampling > (uint)H2V2) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. // Quality: 1-100, higher is better. Typical values are around 50-95.
  29. int m_quality;
  30. // m_subsampling:
  31. // 0 = Y (grayscale) only
  32. // 1 = H1V1 subsampling (YCbCr 1x1x1, 3 blocks per MCU)
  33. // 2 = H2V1 subsampling (YCbCr 2x1x1, 4 blocks per MCU)
  34. // 3 = H2V2 subsampling (YCbCr 4x1x1, 6 blocks per MCU-- very common)
  35. subsampling_t m_subsampling;
  36. };
  37. // Output stream abstract class - used by the jpeg_encoder class to write to the output stream.
  38. // put_buf() is generally called with len==JPGE_OUT_BUF_SIZE bytes, but for headers it'll be called with smaller amounts.
  39. class output_stream {
  40. public:
  41. virtual ~output_stream() { };
  42. virtual bool put_buf(const void* Pbuf, int len) = 0;
  43. virtual uint get_size() const = 0;
  44. };
  45. // Lower level jpeg_encoder class - useful if more control is needed than the above helper functions.
  46. class jpeg_encoder {
  47. public:
  48. jpeg_encoder();
  49. ~jpeg_encoder();
  50. // Initializes the compressor.
  51. // pStream: The stream object to use for writing compressed data.
  52. // params - Compression parameters structure, defined above.
  53. // width, height - Image dimensions.
  54. // channels - May be 1, or 3. 1 indicates grayscale, 3 indicates RGB source data.
  55. // Returns false on out of memory or if a stream write fails.
  56. bool init(output_stream *pStream, int width, int height, int src_channels, const params &comp_params = params());
  57. // Call this method with each source scanline.
  58. // width * src_channels bytes per scanline is expected (RGB or Y format).
  59. // You must call with NULL after all scanlines are processed to finish compression.
  60. // Returns false on out of memory or if a stream write fails.
  61. bool process_scanline(const void* pScanline);
  62. // Deinitializes the compressor, freeing any allocated memory. May be called at any time.
  63. void deinit();
  64. private:
  65. jpeg_encoder(const jpeg_encoder &);
  66. jpeg_encoder &operator =(const jpeg_encoder &);
  67. typedef int32 sample_array_t;
  68. enum { JPGE_OUT_BUF_SIZE = 512 };
  69. output_stream *m_pStream;
  70. params m_params;
  71. uint8 m_num_components;
  72. uint8 m_comp_h_samp[3], m_comp_v_samp[3];
  73. int m_image_x, m_image_y, m_image_bpp, m_image_bpl;
  74. int m_image_x_mcu, m_image_y_mcu;
  75. int m_image_bpl_xlt, m_image_bpl_mcu;
  76. int m_mcus_per_row;
  77. int m_mcu_x, m_mcu_y;
  78. uint8 *m_mcu_lines[16];
  79. uint8 m_mcu_y_ofs;
  80. sample_array_t m_sample_array[64];
  81. int16 m_coefficient_array[64];
  82. int m_last_dc_val[3];
  83. uint8 m_out_buf[JPGE_OUT_BUF_SIZE];
  84. uint8 *m_pOut_buf;
  85. uint m_out_buf_left;
  86. uint32 m_bit_buffer;
  87. uint m_bits_in;
  88. uint8 m_pass_num;
  89. bool m_all_stream_writes_succeeded;
  90. bool jpg_open(int p_x_res, int p_y_res, int src_channels);
  91. void flush_output_buffer();
  92. void put_bits(uint bits, uint len);
  93. void emit_byte(uint8 i);
  94. void emit_word(uint i);
  95. void emit_marker(int marker);
  96. void emit_jfif_app0();
  97. void emit_dqt();
  98. void emit_sof();
  99. void emit_dht(uint8 *bits, uint8 *val, int index, bool ac_flag);
  100. void emit_dhts();
  101. void emit_sos();
  102. void compute_quant_table(int32 *dst, const int16 *src);
  103. void load_quantized_coefficients(int component_num);
  104. void load_block_8_8_grey(int x);
  105. void load_block_8_8(int x, int y, int c);
  106. void load_block_16_8(int x, int c);
  107. void load_block_16_8_8(int x, int c);
  108. void code_coefficients_pass_two(int component_num);
  109. void code_block(int component_num);
  110. void process_mcu_row();
  111. bool process_end_of_image();
  112. void load_mcu(const void* src);
  113. void clear();
  114. void init();
  115. };
  116. } // namespace jpge
  117. #endif // JPEG_ENCODER