jpgd.h 14 KB

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