jpegint.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * jpegint.h
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 1997-2009 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2015-2016, 2019, 2021, D. R. Commander.
  9. * Copyright (C) 2015, Google, Inc.
  10. * Copyright (C) 2021, Alex Richardson.
  11. * For conditions of distribution and use, see the accompanying README.ijg
  12. * file.
  13. *
  14. * This file provides common declarations for the various JPEG modules.
  15. * These declarations are considered internal to the JPEG library; most
  16. * applications using the library shouldn't need to include this file.
  17. */
  18. /* Declarations for both compression & decompression */
  19. typedef enum { /* Operating modes for buffer controllers */
  20. JBUF_PASS_THRU, /* Plain stripwise operation */
  21. /* Remaining modes require a full-image buffer to have been created */
  22. JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
  23. JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
  24. JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
  25. } J_BUF_MODE;
  26. /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
  27. #define CSTATE_START 100 /* after create_compress */
  28. #define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
  29. #define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
  30. #define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
  31. #define DSTATE_START 200 /* after create_decompress */
  32. #define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
  33. #define DSTATE_READY 202 /* found SOS, ready for start_decompress */
  34. #define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
  35. #define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
  36. #define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
  37. #define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
  38. #define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
  39. #define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
  40. #define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
  41. #define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
  42. /* JLONG must hold at least signed 32-bit values. */
  43. typedef long JLONG;
  44. /* JUINTPTR must hold pointer values. */
  45. #ifdef __UINTPTR_TYPE__
  46. /*
  47. * __UINTPTR_TYPE__ is GNU-specific and available in GCC 4.6+ and Clang 3.0+.
  48. * Fortunately, that is sufficient to support the few architectures for which
  49. * sizeof(void *) != sizeof(size_t). The only other options would require C99
  50. * or Clang-specific builtins.
  51. */
  52. typedef __UINTPTR_TYPE__ JUINTPTR;
  53. #else
  54. typedef size_t JUINTPTR;
  55. #endif
  56. /*
  57. * Left shift macro that handles a negative operand without causing any
  58. * sanitizer warnings
  59. */
  60. #define LEFT_SHIFT(a, b) ((JLONG)((unsigned long)(a) << (b)))
  61. /* Declarations for compression modules */
  62. /* Master control module */
  63. struct jpeg_comp_master {
  64. void (*prepare_for_pass) (j_compress_ptr cinfo);
  65. void (*pass_startup) (j_compress_ptr cinfo);
  66. void (*finish_pass) (j_compress_ptr cinfo);
  67. /* State variables made visible to other modules */
  68. boolean call_pass_startup; /* True if pass_startup must be called */
  69. boolean is_last_pass; /* True during last pass */
  70. };
  71. /* Main buffer control (downsampled-data buffer) */
  72. struct jpeg_c_main_controller {
  73. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  74. void (*process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  75. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
  76. };
  77. /* Compression preprocessing (downsampling input buffer control) */
  78. struct jpeg_c_prep_controller {
  79. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  80. void (*pre_process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  81. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail,
  82. JSAMPIMAGE output_buf,
  83. JDIMENSION *out_row_group_ctr,
  84. JDIMENSION out_row_groups_avail);
  85. };
  86. /* Coefficient buffer control */
  87. struct jpeg_c_coef_controller {
  88. void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
  89. boolean (*compress_data) (j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  90. };
  91. /* Colorspace conversion */
  92. struct jpeg_color_converter {
  93. void (*start_pass) (j_compress_ptr cinfo);
  94. void (*color_convert) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
  95. JSAMPIMAGE output_buf, JDIMENSION output_row,
  96. int num_rows);
  97. };
  98. /* Downsampling */
  99. struct jpeg_downsampler {
  100. void (*start_pass) (j_compress_ptr cinfo);
  101. void (*downsample) (j_compress_ptr cinfo, JSAMPIMAGE input_buf,
  102. JDIMENSION in_row_index, JSAMPIMAGE output_buf,
  103. JDIMENSION out_row_group_index);
  104. boolean need_context_rows; /* TRUE if need rows above & below */
  105. };
  106. /* Forward DCT (also controls coefficient quantization) */
  107. struct jpeg_forward_dct {
  108. void (*start_pass) (j_compress_ptr cinfo);
  109. /* perhaps this should be an array??? */
  110. void (*forward_DCT) (j_compress_ptr cinfo, jpeg_component_info *compptr,
  111. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  112. JDIMENSION start_row, JDIMENSION start_col,
  113. JDIMENSION num_blocks);
  114. };
  115. /* Entropy encoding */
  116. struct jpeg_entropy_encoder {
  117. void (*start_pass) (j_compress_ptr cinfo, boolean gather_statistics);
  118. boolean (*encode_mcu) (j_compress_ptr cinfo, JBLOCKROW *MCU_data);
  119. void (*finish_pass) (j_compress_ptr cinfo);
  120. };
  121. /* Marker writing */
  122. struct jpeg_marker_writer {
  123. void (*write_file_header) (j_compress_ptr cinfo);
  124. void (*write_frame_header) (j_compress_ptr cinfo);
  125. void (*write_scan_header) (j_compress_ptr cinfo);
  126. void (*write_file_trailer) (j_compress_ptr cinfo);
  127. void (*write_tables_only) (j_compress_ptr cinfo);
  128. /* These routines are exported to allow insertion of extra markers */
  129. /* Probably only COM and APPn markers should be written this way */
  130. void (*write_marker_header) (j_compress_ptr cinfo, int marker,
  131. unsigned int datalen);
  132. void (*write_marker_byte) (j_compress_ptr cinfo, int val);
  133. };
  134. /* Declarations for decompression modules */
  135. /* Master control module */
  136. struct jpeg_decomp_master {
  137. void (*prepare_for_output_pass) (j_decompress_ptr cinfo);
  138. void (*finish_output_pass) (j_decompress_ptr cinfo);
  139. /* State variables made visible to other modules */
  140. boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
  141. /* Partial decompression variables */
  142. JDIMENSION first_iMCU_col;
  143. JDIMENSION last_iMCU_col;
  144. JDIMENSION first_MCU_col[MAX_COMPONENTS];
  145. JDIMENSION last_MCU_col[MAX_COMPONENTS];
  146. boolean jinit_upsampler_no_alloc;
  147. /* Last iMCU row that was successfully decoded */
  148. JDIMENSION last_good_iMCU_row;
  149. };
  150. /* Input control module */
  151. struct jpeg_input_controller {
  152. int (*consume_input) (j_decompress_ptr cinfo);
  153. void (*reset_input_controller) (j_decompress_ptr cinfo);
  154. void (*start_input_pass) (j_decompress_ptr cinfo);
  155. void (*finish_input_pass) (j_decompress_ptr cinfo);
  156. /* State variables made visible to other modules */
  157. boolean has_multiple_scans; /* True if file has multiple scans */
  158. boolean eoi_reached; /* True when EOI has been consumed */
  159. };
  160. /* Main buffer control (downsampled-data buffer) */
  161. struct jpeg_d_main_controller {
  162. void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
  163. void (*process_data) (j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  164. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
  165. };
  166. /* Coefficient buffer control */
  167. struct jpeg_d_coef_controller {
  168. void (*start_input_pass) (j_decompress_ptr cinfo);
  169. int (*consume_data) (j_decompress_ptr cinfo);
  170. void (*start_output_pass) (j_decompress_ptr cinfo);
  171. int (*decompress_data) (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
  172. /* Pointer to array of coefficient virtual arrays, or NULL if none */
  173. jvirt_barray_ptr *coef_arrays;
  174. };
  175. /* Decompression postprocessing (color quantization buffer control) */
  176. struct jpeg_d_post_controller {
  177. void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
  178. void (*post_process_data) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  179. JDIMENSION *in_row_group_ctr,
  180. JDIMENSION in_row_groups_avail,
  181. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  182. JDIMENSION out_rows_avail);
  183. };
  184. /* Marker reading & parsing */
  185. struct jpeg_marker_reader {
  186. void (*reset_marker_reader) (j_decompress_ptr cinfo);
  187. /* Read markers until SOS or EOI.
  188. * Returns same codes as are defined for jpeg_consume_input:
  189. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  190. */
  191. int (*read_markers) (j_decompress_ptr cinfo);
  192. /* Read a restart marker --- exported for use by entropy decoder only */
  193. jpeg_marker_parser_method read_restart_marker;
  194. /* State of marker reader --- nominally internal, but applications
  195. * supplying COM or APPn handlers might like to know the state.
  196. */
  197. boolean saw_SOI; /* found SOI? */
  198. boolean saw_SOF; /* found SOF? */
  199. int next_restart_num; /* next restart number expected (0-7) */
  200. unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
  201. };
  202. /* Entropy decoding */
  203. struct jpeg_entropy_decoder {
  204. void (*start_pass) (j_decompress_ptr cinfo);
  205. boolean (*decode_mcu) (j_decompress_ptr cinfo, JBLOCKROW *MCU_data);
  206. /* This is here to share code between baseline and progressive decoders; */
  207. /* other modules probably should not use it */
  208. boolean insufficient_data; /* set TRUE after emitting warning */
  209. };
  210. /* Inverse DCT (also performs dequantization) */
  211. typedef void (*inverse_DCT_method_ptr) (j_decompress_ptr cinfo,
  212. jpeg_component_info *compptr,
  213. JCOEFPTR coef_block,
  214. JSAMPARRAY output_buf,
  215. JDIMENSION output_col);
  216. struct jpeg_inverse_dct {
  217. void (*start_pass) (j_decompress_ptr cinfo);
  218. /* It is useful to allow each component to have a separate IDCT method. */
  219. inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
  220. };
  221. /* Upsampling (note that upsampler must also call color converter) */
  222. struct jpeg_upsampler {
  223. void (*start_pass) (j_decompress_ptr cinfo);
  224. void (*upsample) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  225. JDIMENSION *in_row_group_ctr,
  226. JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
  227. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
  228. boolean need_context_rows; /* TRUE if need rows above & below */
  229. };
  230. /* Colorspace conversion */
  231. struct jpeg_color_deconverter {
  232. void (*start_pass) (j_decompress_ptr cinfo);
  233. void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  234. JDIMENSION input_row, JSAMPARRAY output_buf,
  235. int num_rows);
  236. };
  237. /* Color quantization or color precision reduction */
  238. struct jpeg_color_quantizer {
  239. void (*start_pass) (j_decompress_ptr cinfo, boolean is_pre_scan);
  240. void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  241. JSAMPARRAY output_buf, int num_rows);
  242. void (*finish_pass) (j_decompress_ptr cinfo);
  243. void (*new_color_map) (j_decompress_ptr cinfo);
  244. };
  245. /* Miscellaneous useful macros */
  246. #undef MAX
  247. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  248. #undef MIN
  249. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  250. /* We assume that right shift corresponds to signed division by 2 with
  251. * rounding towards minus infinity. This is correct for typical "arithmetic
  252. * shift" instructions that shift in copies of the sign bit. But some
  253. * C compilers implement >> with an unsigned shift. For these machines you
  254. * must define RIGHT_SHIFT_IS_UNSIGNED.
  255. * RIGHT_SHIFT provides a proper signed right shift of a JLONG quantity.
  256. * It is only applied with constant shift counts. SHIFT_TEMPS must be
  257. * included in the variables of any routine using RIGHT_SHIFT.
  258. */
  259. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  260. #define SHIFT_TEMPS JLONG shift_temp;
  261. #define RIGHT_SHIFT(x, shft) \
  262. ((shift_temp = (x)) < 0 ? \
  263. (shift_temp >> (shft)) | ((~((JLONG)0)) << (32 - (shft))) : \
  264. (shift_temp >> (shft)))
  265. #else
  266. #define SHIFT_TEMPS
  267. #define RIGHT_SHIFT(x, shft) ((x) >> (shft))
  268. #endif
  269. /* Compression module initialization routines */
  270. EXTERN(void) jinit_compress_master(j_compress_ptr cinfo);
  271. EXTERN(void) jinit_c_master_control(j_compress_ptr cinfo,
  272. boolean transcode_only);
  273. EXTERN(void) jinit_c_main_controller(j_compress_ptr cinfo,
  274. boolean need_full_buffer);
  275. EXTERN(void) jinit_c_prep_controller(j_compress_ptr cinfo,
  276. boolean need_full_buffer);
  277. EXTERN(void) jinit_c_coef_controller(j_compress_ptr cinfo,
  278. boolean need_full_buffer);
  279. EXTERN(void) jinit_color_converter(j_compress_ptr cinfo);
  280. EXTERN(void) jinit_downsampler(j_compress_ptr cinfo);
  281. EXTERN(void) jinit_forward_dct(j_compress_ptr cinfo);
  282. EXTERN(void) jinit_huff_encoder(j_compress_ptr cinfo);
  283. EXTERN(void) jinit_phuff_encoder(j_compress_ptr cinfo);
  284. EXTERN(void) jinit_arith_encoder(j_compress_ptr cinfo);
  285. EXTERN(void) jinit_marker_writer(j_compress_ptr cinfo);
  286. /* Decompression module initialization routines */
  287. EXTERN(void) jinit_master_decompress(j_decompress_ptr cinfo);
  288. EXTERN(void) jinit_d_main_controller(j_decompress_ptr cinfo,
  289. boolean need_full_buffer);
  290. EXTERN(void) jinit_d_coef_controller(j_decompress_ptr cinfo,
  291. boolean need_full_buffer);
  292. EXTERN(void) jinit_d_post_controller(j_decompress_ptr cinfo,
  293. boolean need_full_buffer);
  294. EXTERN(void) jinit_input_controller(j_decompress_ptr cinfo);
  295. EXTERN(void) jinit_marker_reader(j_decompress_ptr cinfo);
  296. EXTERN(void) jinit_huff_decoder(j_decompress_ptr cinfo);
  297. EXTERN(void) jinit_phuff_decoder(j_decompress_ptr cinfo);
  298. EXTERN(void) jinit_arith_decoder(j_decompress_ptr cinfo);
  299. EXTERN(void) jinit_inverse_dct(j_decompress_ptr cinfo);
  300. EXTERN(void) jinit_upsampler(j_decompress_ptr cinfo);
  301. EXTERN(void) jinit_color_deconverter(j_decompress_ptr cinfo);
  302. EXTERN(void) jinit_1pass_quantizer(j_decompress_ptr cinfo);
  303. EXTERN(void) jinit_2pass_quantizer(j_decompress_ptr cinfo);
  304. EXTERN(void) jinit_merged_upsampler(j_decompress_ptr cinfo);
  305. /* Memory manager initialization */
  306. EXTERN(void) jinit_memory_mgr(j_common_ptr cinfo);
  307. /* Utility routines in jutils.c */
  308. EXTERN(long) jdiv_round_up(long a, long b);
  309. EXTERN(long) jround_up(long a, long b);
  310. EXTERN(void) jcopy_sample_rows(JSAMPARRAY input_array, int source_row,
  311. JSAMPARRAY output_array, int dest_row,
  312. int num_rows, JDIMENSION num_cols);
  313. EXTERN(void) jcopy_block_row(JBLOCKROW input_row, JBLOCKROW output_row,
  314. JDIMENSION num_blocks);
  315. EXTERN(void) jzero_far(void *target, size_t bytestozero);
  316. /* Constant tables in jutils.c */
  317. #if 0 /* This table is not actually needed in v6a */
  318. extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
  319. #endif
  320. extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
  321. /* Arithmetic coding probability estimation tables in jaricom.c */
  322. extern const JLONG jpeg_aritab[];