1 | // jpgd.h - C++ class for JPEG decompression. |
2 | // Richard Geldreich <richgel99@gmail.com> |
3 | // See jpgd.cpp for license (Public Domain or Apache 2.0). |
4 | #ifndef JPEG_DECODER_H |
5 | #define JPEG_DECODER_H |
6 | |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
9 | #include <setjmp.h> |
10 | #include <assert.h> |
11 | #include <stdint.h> |
12 | |
13 | #ifdef _MSC_VER |
14 | #define JPGD_NORETURN __declspec(noreturn) |
15 | #elif defined(__GNUC__) |
16 | #define JPGD_NORETURN __attribute__ ((noreturn)) |
17 | #else |
18 | #define JPGD_NORETURN |
19 | #endif |
20 | |
21 | #define JPGD_HUFF_TREE_MAX_LENGTH 512 |
22 | #define JPGD_HUFF_CODE_SIZE_MAX_LENGTH 256 |
23 | |
24 | namespace jpgd |
25 | { |
26 | typedef unsigned char uint8; |
27 | typedef signed short int16; |
28 | typedef unsigned short uint16; |
29 | typedef unsigned int uint; |
30 | typedef signed int int32; |
31 | |
32 | // Loads a JPEG image from a memory buffer or a file. |
33 | // req_comps can be 1 (grayscale), 3 (RGB), or 4 (RGBA). |
34 | // On return, width/height will be set to the image's dimensions, and actual_comps will be set to the either 1 (grayscale) or 3 (RGB). |
35 | // Notes: For more control over where and how the source data is read, see the decompress_jpeg_image_from_stream() function below, or call the jpeg_decoder class directly. |
36 | // Requesting a 8 or 32bpp image is currently a little faster than 24bpp because the jpeg_decoder class itself currently always unpacks to either 8 or 32bpp. |
37 | unsigned char* decompress_jpeg_image_from_memory(const unsigned char* pSrc_data, int src_data_size, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags = 0); |
38 | unsigned char* decompress_jpeg_image_from_file(const char* pSrc_filename, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags = 0); |
39 | |
40 | // Success/failure error codes. |
41 | enum jpgd_status |
42 | { |
43 | JPGD_SUCCESS = 0, JPGD_FAILED = -1, JPGD_DONE = 1, |
44 | JPGD_BAD_DHT_COUNTS = -256, JPGD_BAD_DHT_INDEX, JPGD_BAD_DHT_MARKER, JPGD_BAD_DQT_MARKER, JPGD_BAD_DQT_TABLE, |
45 | JPGD_BAD_PRECISION, JPGD_BAD_HEIGHT, JPGD_BAD_WIDTH, JPGD_TOO_MANY_COMPONENTS, |
46 | JPGD_BAD_SOF_LENGTH, JPGD_BAD_VARIABLE_MARKER, JPGD_BAD_DRI_LENGTH, JPGD_BAD_SOS_LENGTH, |
47 | JPGD_BAD_SOS_COMP_ID, , JPGD_NO_ARITHMITIC_SUPPORT, JPGD_UNEXPECTED_MARKER, |
48 | JPGD_NOT_JPEG, JPGD_UNSUPPORTED_MARKER, JPGD_BAD_DQT_LENGTH, JPGD_TOO_MANY_BLOCKS, |
49 | JPGD_UNDEFINED_QUANT_TABLE, JPGD_UNDEFINED_HUFF_TABLE, JPGD_NOT_SINGLE_SCAN, JPGD_UNSUPPORTED_COLORSPACE, |
50 | JPGD_UNSUPPORTED_SAMP_FACTORS, JPGD_DECODE_ERROR, JPGD_BAD_RESTART_MARKER, |
51 | JPGD_BAD_SOS_SPECTRAL, JPGD_BAD_SOS_SUCCESSIVE, JPGD_STREAM_READ, JPGD_NOTENOUGHMEM, JPGD_TOO_MANY_SCANS |
52 | }; |
53 | |
54 | // Input stream interface. |
55 | // Derive from this class to read input data from sources other than files or memory. Set m_eof_flag to true when no more data is available. |
56 | // The decoder is rather greedy: it will keep on calling this method until its internal input buffer is full, or until the EOF flag is set. |
57 | // It the input stream contains data after the JPEG stream's EOI (end of image) marker it will probably be pulled into the internal buffer. |
58 | // Call the get_total_bytes_read() method to determine the actual size of the JPEG stream after successful decoding. |
59 | class jpeg_decoder_stream |
60 | { |
61 | public: |
62 | jpeg_decoder_stream() { } |
63 | virtual ~jpeg_decoder_stream() { } |
64 | |
65 | // The read() method is called when the internal input buffer is empty. |
66 | // Parameters: |
67 | // pBuf - input buffer |
68 | // max_bytes_to_read - maximum bytes that can be written to pBuf |
69 | // pEOF_flag - set this to true if at end of stream (no more bytes remaining) |
70 | // Returns -1 on error, otherwise return the number of bytes actually written to the buffer (which may be 0). |
71 | // Notes: This method will be called in a loop until you set *pEOF_flag to true or the internal buffer is full. |
72 | virtual int read(uint8* pBuf, int max_bytes_to_read, bool* pEOF_flag) = 0; |
73 | }; |
74 | |
75 | // stdio FILE stream class. |
76 | class jpeg_decoder_file_stream : public jpeg_decoder_stream |
77 | { |
78 | jpeg_decoder_file_stream(const jpeg_decoder_file_stream&); |
79 | jpeg_decoder_file_stream& operator =(const jpeg_decoder_file_stream&); |
80 | |
81 | FILE* m_pFile; |
82 | bool m_eof_flag, m_error_flag; |
83 | |
84 | public: |
85 | jpeg_decoder_file_stream(); |
86 | virtual ~jpeg_decoder_file_stream(); |
87 | |
88 | bool open(const char* Pfilename); |
89 | void close(); |
90 | |
91 | virtual int read(uint8* pBuf, int max_bytes_to_read, bool* pEOF_flag); |
92 | }; |
93 | |
94 | // Memory stream class. |
95 | class jpeg_decoder_mem_stream : public jpeg_decoder_stream |
96 | { |
97 | const uint8* m_pSrc_data; |
98 | uint m_ofs, m_size; |
99 | |
100 | public: |
101 | jpeg_decoder_mem_stream() : m_pSrc_data(NULL), m_ofs(0), m_size(0) { } |
102 | jpeg_decoder_mem_stream(const uint8* pSrc_data, uint size) : m_pSrc_data(pSrc_data), m_ofs(0), m_size(size) { } |
103 | |
104 | virtual ~jpeg_decoder_mem_stream() { } |
105 | |
106 | bool open(const uint8* pSrc_data, uint size); |
107 | void close() { m_pSrc_data = NULL; m_ofs = 0; m_size = 0; } |
108 | |
109 | virtual int read(uint8* pBuf, int max_bytes_to_read, bool* pEOF_flag); |
110 | }; |
111 | |
112 | // Loads JPEG file from a jpeg_decoder_stream. |
113 | unsigned char* decompress_jpeg_image_from_stream(jpeg_decoder_stream* pStream, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags = 0); |
114 | |
115 | enum |
116 | { |
117 | JPGD_IN_BUF_SIZE = 8192, JPGD_MAX_BLOCKS_PER_MCU = 10, JPGD_MAX_HUFF_TABLES = 8, JPGD_MAX_QUANT_TABLES = 4, |
118 | JPGD_MAX_COMPONENTS = 4, JPGD_MAX_COMPS_IN_SCAN = 4, JPGD_MAX_BLOCKS_PER_ROW = 16384, JPGD_MAX_HEIGHT = 32768, JPGD_MAX_WIDTH = 32768 |
119 | }; |
120 | |
121 | typedef int16 jpgd_quant_t; |
122 | typedef int16 jpgd_block_coeff_t; |
123 | |
124 | class jpeg_decoder |
125 | { |
126 | public: |
127 | enum |
128 | { |
129 | cFlagBoxChromaFiltering = 1, |
130 | cFlagDisableSIMD = 2 |
131 | }; |
132 | |
133 | // Call get_error_code() after constructing to determine if the stream is valid or not. You may call the get_width(), get_height(), etc. |
134 | // methods after the constructor is called. You may then either destruct the object, or begin decoding the image by calling begin_decoding(), then decode() on each scanline. |
135 | jpeg_decoder(jpeg_decoder_stream* pStream, uint32_t flags = 0); |
136 | |
137 | ~jpeg_decoder(); |
138 | |
139 | // Call this method after constructing the object to begin decompression. |
140 | // If JPGD_SUCCESS is returned you may then call decode() on each scanline. |
141 | |
142 | int begin_decoding(); |
143 | |
144 | // Returns the next scan line. |
145 | // For grayscale images, pScan_line will point to a buffer containing 8-bit pixels (get_bytes_per_pixel() will return 1). |
146 | // Otherwise, it will always point to a buffer containing 32-bit RGBA pixels (A will always be 255, and get_bytes_per_pixel() will return 4). |
147 | // Returns JPGD_SUCCESS if a scan line has been returned. |
148 | // Returns JPGD_DONE if all scan lines have been returned. |
149 | // Returns JPGD_FAILED if an error occurred. Call get_error_code() for a more info. |
150 | int decode(const void** pScan_line, uint* pScan_line_len); |
151 | |
152 | inline jpgd_status get_error_code() const { return m_error_code; } |
153 | |
154 | inline int get_width() const { return m_image_x_size; } |
155 | inline int get_height() const { return m_image_y_size; } |
156 | |
157 | inline int get_num_components() const { return m_comps_in_frame; } |
158 | |
159 | inline int get_bytes_per_pixel() const { return m_dest_bytes_per_pixel; } |
160 | inline int get_bytes_per_scan_line() const { return m_image_x_size * get_bytes_per_pixel(); } |
161 | |
162 | // Returns the total number of bytes actually consumed by the decoder (which should equal the actual size of the JPEG file). |
163 | inline int get_total_bytes_read() const { return m_total_bytes_read; } |
164 | |
165 | private: |
166 | jpeg_decoder(const jpeg_decoder&); |
167 | jpeg_decoder& operator =(const jpeg_decoder&); |
168 | |
169 | typedef void (*pDecode_block_func)(jpeg_decoder*, int, int, int); |
170 | |
171 | struct huff_tables |
172 | { |
173 | bool ac_table; |
174 | uint look_up[256]; |
175 | uint look_up2[256]; |
176 | uint8 code_size[JPGD_HUFF_CODE_SIZE_MAX_LENGTH]; |
177 | uint tree[JPGD_HUFF_TREE_MAX_LENGTH]; |
178 | }; |
179 | |
180 | struct coeff_buf |
181 | { |
182 | uint8* pData; |
183 | int block_num_x, block_num_y; |
184 | int block_len_x, block_len_y; |
185 | int block_size; |
186 | }; |
187 | |
188 | struct mem_block |
189 | { |
190 | mem_block* m_pNext; |
191 | size_t m_used_count; |
192 | size_t m_size; |
193 | char m_data[1]; |
194 | }; |
195 | |
196 | jmp_buf m_jmp_state; |
197 | uint32_t m_flags; |
198 | mem_block* m_pMem_blocks; |
199 | int m_image_x_size; |
200 | int m_image_y_size; |
201 | jpeg_decoder_stream* m_pStream; |
202 | |
203 | int m_progressive_flag; |
204 | |
205 | uint8 m_huff_ac[JPGD_MAX_HUFF_TABLES]; |
206 | uint8* m_huff_num[JPGD_MAX_HUFF_TABLES]; // pointer to number of Huffman codes per bit size |
207 | uint8* m_huff_val[JPGD_MAX_HUFF_TABLES]; // pointer to Huffman codes per bit size |
208 | jpgd_quant_t* m_quant[JPGD_MAX_QUANT_TABLES]; // pointer to quantization tables |
209 | int m_scan_type; // Gray, Yh1v1, Yh1v2, Yh2v1, Yh2v2 (CMYK111, CMYK4114 no longer supported) |
210 | int m_comps_in_frame; // # of components in frame |
211 | int m_comp_h_samp[JPGD_MAX_COMPONENTS]; // component's horizontal sampling factor |
212 | int m_comp_v_samp[JPGD_MAX_COMPONENTS]; // component's vertical sampling factor |
213 | int m_comp_quant[JPGD_MAX_COMPONENTS]; // component's quantization table selector |
214 | int m_comp_ident[JPGD_MAX_COMPONENTS]; // component's ID |
215 | int m_comp_h_blocks[JPGD_MAX_COMPONENTS]; |
216 | int m_comp_v_blocks[JPGD_MAX_COMPONENTS]; |
217 | int m_comps_in_scan; // # of components in scan |
218 | int m_comp_list[JPGD_MAX_COMPS_IN_SCAN]; // components in this scan |
219 | int m_comp_dc_tab[JPGD_MAX_COMPONENTS]; // component's DC Huffman coding table selector |
220 | int m_comp_ac_tab[JPGD_MAX_COMPONENTS]; // component's AC Huffman coding table selector |
221 | int m_spectral_start; // spectral selection start |
222 | int m_spectral_end; // spectral selection end |
223 | int m_successive_low; // successive approximation low |
224 | int m_successive_high; // successive approximation high |
225 | int m_max_mcu_x_size; // MCU's max. X size in pixels |
226 | int m_max_mcu_y_size; // MCU's max. Y size in pixels |
227 | int m_blocks_per_mcu; |
228 | int m_max_blocks_per_row; |
229 | int m_mcus_per_row, m_mcus_per_col; |
230 | int m_mcu_org[JPGD_MAX_BLOCKS_PER_MCU]; |
231 | int m_total_lines_left; // total # lines left in image |
232 | int m_mcu_lines_left; // total # lines left in this MCU |
233 | int m_num_buffered_scanlines; |
234 | int m_real_dest_bytes_per_scan_line; |
235 | int m_dest_bytes_per_scan_line; // rounded up |
236 | int m_dest_bytes_per_pixel; // 4 (RGB) or 1 (Y) |
237 | huff_tables* m_pHuff_tabs[JPGD_MAX_HUFF_TABLES]; |
238 | coeff_buf* m_dc_coeffs[JPGD_MAX_COMPONENTS]; |
239 | coeff_buf* m_ac_coeffs[JPGD_MAX_COMPONENTS]; |
240 | int m_eob_run; |
241 | int m_block_y_mcu[JPGD_MAX_COMPONENTS]; |
242 | uint8* m_pIn_buf_ofs; |
243 | int m_in_buf_left; |
244 | int m_tem_flag; |
245 | |
246 | uint8 m_in_buf_pad_start[64]; |
247 | uint8 m_in_buf[JPGD_IN_BUF_SIZE + 128]; |
248 | uint8 m_in_buf_pad_end[64]; |
249 | |
250 | int m_bits_left; |
251 | uint m_bit_buf; |
252 | int m_restart_interval; |
253 | int m_restarts_left; |
254 | int m_next_restart_num; |
255 | int m_max_mcus_per_row; |
256 | int m_max_blocks_per_mcu; |
257 | |
258 | int m_max_mcus_per_col; |
259 | uint m_last_dc_val[JPGD_MAX_COMPONENTS]; |
260 | jpgd_block_coeff_t* m_pMCU_coefficients; |
261 | int m_mcu_block_max_zag[JPGD_MAX_BLOCKS_PER_MCU]; |
262 | uint8* m_pSample_buf; |
263 | uint8* m_pSample_buf_prev; |
264 | int m_crr[256]; |
265 | int m_cbb[256]; |
266 | int m_crg[256]; |
267 | int m_cbg[256]; |
268 | uint8* m_pScan_line_0; |
269 | uint8* m_pScan_line_1; |
270 | jpgd_status m_error_code; |
271 | int m_total_bytes_read; |
272 | |
273 | bool m_ready_flag; |
274 | bool m_eof_flag; |
275 | bool m_sample_buf_prev_valid; |
276 | bool m_has_sse2; |
277 | |
278 | inline int check_sample_buf_ofs(int ofs) const { assert(ofs >= 0); assert(ofs < m_max_blocks_per_row * 64); return ofs; } |
279 | void free_all_blocks(); |
280 | JPGD_NORETURN void stop_decoding(jpgd_status status); |
281 | void* alloc(size_t n, bool zero = false); |
282 | void* alloc_aligned(size_t nSize, uint32_t align = 16, bool zero = false); |
283 | void word_clear(void* p, uint16 c, uint n); |
284 | void prep_in_buffer(); |
285 | void read_dht_marker(); |
286 | void read_dqt_marker(); |
287 | void read_sof_marker(); |
288 | void skip_variable_marker(); |
289 | void read_dri_marker(); |
290 | void read_sos_marker(); |
291 | int next_marker(); |
292 | int process_markers(); |
293 | void locate_soi_marker(); |
294 | void locate_sof_marker(); |
295 | int locate_sos_marker(); |
296 | void init(jpeg_decoder_stream* pStream, uint32_t flags); |
297 | void create_look_ups(); |
298 | void fix_in_buffer(); |
299 | void transform_mcu(int mcu_row); |
300 | coeff_buf* coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y); |
301 | inline jpgd_block_coeff_t* coeff_buf_getp(coeff_buf* cb, int block_x, int block_y); |
302 | void load_next_row(); |
303 | void decode_next_row(); |
304 | void make_huff_table(int index, huff_tables* pH); |
305 | void check_quant_tables(); |
306 | void check_huff_tables(); |
307 | bool calc_mcu_block_order(); |
308 | int init_scan(); |
309 | void init_frame(); |
310 | void process_restart(); |
311 | void decode_scan(pDecode_block_func decode_block_func); |
312 | void init_progressive(); |
313 | void init_sequential(); |
314 | void decode_start(); |
315 | void decode_init(jpeg_decoder_stream* pStream, uint32_t flags); |
316 | void H2V2Convert(); |
317 | uint32_t H2V2ConvertFiltered(); |
318 | void H2V1Convert(); |
319 | void H2V1ConvertFiltered(); |
320 | void H1V2Convert(); |
321 | void H1V2ConvertFiltered(); |
322 | void H1V1Convert(); |
323 | void gray_convert(); |
324 | void find_eoi(); |
325 | inline uint get_char(); |
326 | inline uint get_char(bool* pPadding_flag); |
327 | inline void stuff_char(uint8 q); |
328 | inline uint8 get_octet(); |
329 | inline uint get_bits(int num_bits); |
330 | inline uint get_bits_no_markers(int numbits); |
331 | inline int huff_decode(huff_tables* pH); |
332 | inline int huff_decode(huff_tables* pH, int& ); |
333 | |
334 | // Clamps a value between 0-255. |
335 | static inline uint8 clamp(int i) |
336 | { |
337 | if (static_cast<uint>(i) > 255) |
338 | i = (((~i) >> 31) & 0xFF); |
339 | return static_cast<uint8>(i); |
340 | } |
341 | int decode_next_mcu_row(); |
342 | |
343 | static void decode_block_dc_first(jpeg_decoder* pD, int component_id, int block_x, int block_y); |
344 | static void decode_block_dc_refine(jpeg_decoder* pD, int component_id, int block_x, int block_y); |
345 | static void decode_block_ac_first(jpeg_decoder* pD, int component_id, int block_x, int block_y); |
346 | static void decode_block_ac_refine(jpeg_decoder* pD, int component_id, int block_x, int block_y); |
347 | }; |
348 | |
349 | } // namespace jpgd |
350 | |
351 | #endif // JPEG_DECODER_H |
352 | |