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