jpegint.h 15 KB

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