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