Home | History | Annotate | Download | only in jpeg
      1 /*
      2  * jpegint.h
      3  *
      4  * Copyright (C) 1991-1997, Thomas G. Lane.
      5  * This file is part of the Independent JPEG Group's software.
      6  * For conditions of distribution and use, see the accompanying README file.
      7  *
      8  * This file provides common declarations for the various JPEG modules.
      9  * These declarations are considered internal to the JPEG library; most
     10  * applications using the library shouldn't need to include this file.
     11  */
     12 
     13 
     14 /* Declarations for both compression & decompression */
     15 
     16 typedef enum {			/* Operating modes for buffer controllers */
     17 	JBUF_PASS_THRU,		/* Plain stripwise operation */
     18 	/* Remaining modes require a full-image buffer to have been created */
     19 	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
     20 	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
     21 	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
     22 } J_BUF_MODE;
     23 
     24 /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
     25 #define CSTATE_START	100	/* after create_compress */
     26 #define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
     27 #define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
     28 #define CSTATE_WRCOEFS	103	/* jpeg_write_coefficients done */
     29 #define DSTATE_START	200	/* after create_decompress */
     30 #define DSTATE_INHEADER	201	/* reading header markers, no SOS yet */
     31 #define DSTATE_READY	202	/* found SOS, ready for start_decompress */
     32 #define DSTATE_PRELOAD	203	/* reading multiscan file in start_decompress*/
     33 #define DSTATE_PRESCAN	204	/* performing dummy pass for 2-pass quant */
     34 #define DSTATE_SCANNING	205	/* start_decompress done, read_scanlines OK */
     35 #define DSTATE_RAW_OK	206	/* start_decompress done, read_raw_data OK */
     36 #define DSTATE_BUFIMAGE	207	/* expecting jpeg_start_output */
     37 #define DSTATE_BUFPOST	208	/* looking for SOS/EOI in jpeg_finish_output */
     38 #define DSTATE_RDCOEFS	209	/* reading file in jpeg_read_coefficients */
     39 #define DSTATE_STOPPING	210	/* looking for EOI in jpeg_finish_decompress */
     40 
     41 
     42 /* Declarations for compression modules */
     43 
     44 /* Master control module */
     45 struct jpeg_comp_master {
     46   JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
     47   JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
     48   JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
     49 
     50   /* State variables made visible to other modules */
     51   boolean call_pass_startup;	/* True if pass_startup must be called */
     52   boolean is_last_pass;		/* True during last pass */
     53 };
     54 
     55 /* Main buffer control (downsampled-data buffer) */
     56 struct jpeg_c_main_controller {
     57   JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
     58   JMETHOD(void, process_data, (j_compress_ptr cinfo,
     59 			       JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
     60 			       JDIMENSION in_rows_avail));
     61 };
     62 
     63 /* Compression preprocessing (downsampling input buffer control) */
     64 struct jpeg_c_prep_controller {
     65   JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
     66   JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
     67 				   JSAMPARRAY input_buf,
     68 				   JDIMENSION *in_row_ctr,
     69 				   JDIMENSION in_rows_avail,
     70 				   JSAMPIMAGE output_buf,
     71 				   JDIMENSION *out_row_group_ctr,
     72 				   JDIMENSION out_row_groups_avail));
     73 };
     74 
     75 /* Coefficient buffer control */
     76 struct jpeg_c_coef_controller {
     77   JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
     78   JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
     79 				   JSAMPIMAGE input_buf));
     80 };
     81 
     82 /* Colorspace conversion */
     83 struct jpeg_color_converter {
     84   JMETHOD(void, start_pass, (j_compress_ptr cinfo));
     85   JMETHOD(void, color_convert, (j_compress_ptr cinfo,
     86 				JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
     87 				JDIMENSION output_row, int num_rows));
     88 };
     89 
     90 /* Downsampling */
     91 struct jpeg_downsampler {
     92   JMETHOD(void, start_pass, (j_compress_ptr cinfo));
     93   JMETHOD(void, downsample, (j_compress_ptr cinfo,
     94 			     JSAMPIMAGE input_buf, JDIMENSION in_row_index,
     95 			     JSAMPIMAGE output_buf,
     96 			     JDIMENSION out_row_group_index));
     97 
     98   boolean need_context_rows;	/* TRUE if need rows above & below */
     99 };
    100 
    101 /* Forward DCT (also controls coefficient quantization) */
    102 struct jpeg_forward_dct {
    103   JMETHOD(void, start_pass, (j_compress_ptr cinfo));
    104   /* perhaps this should be an array??? */
    105   JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
    106 			      jpeg_component_info * compptr,
    107 			      JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
    108 			      JDIMENSION start_row, JDIMENSION start_col,
    109 			      JDIMENSION num_blocks));
    110 };
    111 
    112 /* Entropy encoding */
    113 struct jpeg_entropy_encoder {
    114   JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
    115   JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
    116   JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
    117 };
    118 
    119 /* Marker writing */
    120 struct jpeg_marker_writer {
    121   JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
    122   JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
    123   JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
    124   JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
    125   JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
    126   /* These routines are exported to allow insertion of extra markers */
    127   /* Probably only COM and APPn markers should be written this way */
    128   JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
    129 				      unsigned int datalen));
    130   JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
    131 };
    132 
    133 
    134 /* Declarations for decompression modules */
    135 
    136 /* Master control module */
    137 struct jpeg_decomp_master {
    138   JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
    139   JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
    140 
    141   /* State variables made visible to other modules */
    142   boolean is_dummy_pass;	/* True during 1st pass for 2-pass quant */
    143 };
    144 
    145 /* Input control module */
    146 struct jpeg_input_controller {
    147   JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
    148   JMETHOD(int, consume_input_build_huffman_index, (j_decompress_ptr cinfo,
    149                     huffman_index *index, int scan_count));
    150   JMETHOD(int, consume_markers, (j_decompress_ptr cinfo,
    151                     huffman_index *index, int scan_count));
    152   JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
    153   JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
    154   JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
    155 
    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 
    161 /* Main buffer control (downsampled-data buffer) */
    162 struct jpeg_d_main_controller {
    163   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
    164   JMETHOD(void, process_data, (j_decompress_ptr cinfo,
    165 			       JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
    166 			       JDIMENSION out_rows_avail));
    167 };
    168 
    169 /* Coefficient buffer control */
    170 struct jpeg_d_coef_controller {
    171   JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
    172   JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
    173   JMETHOD(int, consume_data_build_huffman_index, (j_decompress_ptr cinfo,
    174                     huffman_index* index, int scan_count));
    175   JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
    176   JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
    177 				 JSAMPIMAGE output_buf));
    178   /* Pointer to array of coefficient virtual arrays, or NULL if none */
    179   jvirt_barray_ptr *coef_arrays;
    180 
    181   /* column number of the first and last tile, respectively */
    182   int column_left_boundary;
    183   int column_right_boundary;
    184 
    185   /* column number of the first and last MCU, respectively */
    186   int MCU_column_left_boundary;
    187   int MCU_column_right_boundary;
    188 
    189   /* the number of MCU columns to skip from the indexed MCU, iM,
    190    * to the requested MCU boundary, rM, where iM is the MCU that we sample
    191    * into our index and is the nearest one to the left of rM.
    192    */
    193   int MCU_columns_to_skip;
    194 };
    195 
    196 /* Decompression postprocessing (color quantization buffer control) */
    197 struct jpeg_d_post_controller {
    198   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
    199   JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
    200 				    JSAMPIMAGE input_buf,
    201 				    JDIMENSION *in_row_group_ctr,
    202 				    JDIMENSION in_row_groups_avail,
    203 				    JSAMPARRAY output_buf,
    204 				    JDIMENSION *out_row_ctr,
    205 				    JDIMENSION out_rows_avail));
    206 };
    207 
    208 /* Marker reading & parsing */
    209 struct jpeg_marker_reader {
    210   JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
    211   /* Read markers until SOS or EOI.
    212    * Returns same codes as are defined for jpeg_consume_input:
    213    * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
    214    */
    215   JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
    216   JMETHOD(void, get_sos_marker_position, (j_decompress_ptr cinfo,
    217                     huffman_index *index));
    218   /* Read a restart marker --- exported for use by entropy decoder only */
    219   jpeg_marker_parser_method read_restart_marker;
    220 
    221   /* State of marker reader --- nominally internal, but applications
    222    * supplying COM or APPn handlers might like to know the state.
    223    */
    224   boolean saw_SOI;		/* found SOI? */
    225   boolean saw_SOF;		/* found SOF? */
    226   int next_restart_num;		/* next restart number expected (0-7) */
    227   int current_sos_marker_position;
    228   unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
    229 };
    230 
    231 /* Entropy decoding */
    232 struct jpeg_entropy_decoder {
    233   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
    234   JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
    235 				JBLOCKROW *MCU_data));
    236   JMETHOD(boolean, decode_mcu_discard_coef, (j_decompress_ptr cinfo));
    237   JMETHOD(void, configure_huffman_decoder, (j_decompress_ptr cinfo,
    238                     huffman_offset_data offset));
    239   JMETHOD(void, get_huffman_decoder_configuration, (j_decompress_ptr cinfo,
    240                     huffman_offset_data *offset));
    241 
    242   /* This is here to share code between baseline and progressive decoders; */
    243   /* other modules probably should not use it */
    244   boolean insufficient_data;	/* set TRUE after emitting warning */
    245 
    246   huffman_index *index;
    247 };
    248 
    249 /* Inverse DCT (also performs dequantization) */
    250 typedef JMETHOD(void, inverse_DCT_method_ptr,
    251 		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
    252 		 JCOEFPTR coef_block,
    253 		 JSAMPARRAY output_buf, JDIMENSION output_col));
    254 
    255 struct jpeg_inverse_dct {
    256   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
    257   /* It is useful to allow each component to have a separate IDCT method. */
    258   inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
    259 };
    260 
    261 /* Upsampling (note that upsampler must also call color converter) */
    262 struct jpeg_upsampler {
    263   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
    264   JMETHOD(void, upsample, (j_decompress_ptr cinfo,
    265 			   JSAMPIMAGE input_buf,
    266 			   JDIMENSION *in_row_group_ctr,
    267 			   JDIMENSION in_row_groups_avail,
    268 			   JSAMPARRAY output_buf,
    269 			   JDIMENSION *out_row_ctr,
    270 			   JDIMENSION out_rows_avail));
    271 
    272   boolean need_context_rows;	/* TRUE if need rows above & below */
    273 };
    274 
    275 /* Colorspace conversion */
    276 struct jpeg_color_deconverter {
    277   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
    278   JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
    279 				JSAMPIMAGE input_buf, JDIMENSION input_row,
    280 				JSAMPARRAY output_buf, int num_rows));
    281 };
    282 
    283 /* Color quantization or color precision reduction */
    284 struct jpeg_color_quantizer {
    285   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
    286   JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
    287 				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
    288 				 int num_rows));
    289   JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
    290   JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
    291 };
    292 
    293 
    294 /* Miscellaneous useful macros */
    295 
    296 #undef MAX
    297 #define MAX(a,b)	((a) > (b) ? (a) : (b))
    298 #undef MIN
    299 #define MIN(a,b)	((a) < (b) ? (a) : (b))
    300 
    301 
    302 /* We assume that right shift corresponds to signed division by 2 with
    303  * rounding towards minus infinity.  This is correct for typical "arithmetic
    304  * shift" instructions that shift in copies of the sign bit.  But some
    305  * C compilers implement >> with an unsigned shift.  For these machines you
    306  * must define RIGHT_SHIFT_IS_UNSIGNED.
    307  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
    308  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
    309  * included in the variables of any routine using RIGHT_SHIFT.
    310  */
    311 
    312 #ifdef RIGHT_SHIFT_IS_UNSIGNED
    313 #define SHIFT_TEMPS	INT32 shift_temp;
    314 #define RIGHT_SHIFT(x,shft)  \
    315 	((shift_temp = (x)) < 0 ? \
    316 	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
    317 	 (shift_temp >> (shft)))
    318 #else
    319 #define SHIFT_TEMPS
    320 #define RIGHT_SHIFT(x,shft)	((x) >> (shft))
    321 #endif
    322 
    323 
    324 /* Short forms of external names for systems with brain-damaged linkers. */
    325 
    326 #ifdef NEED_SHORT_EXTERNAL_NAMES
    327 #define jinit_compress_master	jICompress
    328 #define jinit_c_master_control	jICMaster
    329 #define jinit_c_main_controller	jICMainC
    330 #define jinit_c_prep_controller	jICPrepC
    331 #define jinit_c_coef_controller	jICCoefC
    332 #define jinit_color_converter	jICColor
    333 #define jinit_downsampler	jIDownsampler
    334 #define jinit_forward_dct	jIFDCT
    335 #define jinit_huff_encoder	jIHEncoder
    336 #define jinit_phuff_encoder	jIPHEncoder
    337 #define jinit_marker_writer	jIMWriter
    338 #define jinit_master_decompress	jIDMaster
    339 #define jinit_d_main_controller	jIDMainC
    340 #define jinit_d_coef_controller	jIDCoefC
    341 #define jinit_d_post_controller	jIDPostC
    342 #define jinit_input_controller	jIInCtlr
    343 #define jinit_marker_reader	jIMReader
    344 #define jinit_huff_decoder	jIHDecoder
    345 #define jinit_phuff_decoder	jIPHDecoder
    346 #define jinit_inverse_dct	jIIDCT
    347 #define jinit_upsampler		jIUpsampler
    348 #define jinit_color_deconverter	jIDColor
    349 #define jinit_1pass_quantizer	jI1Quant
    350 #define jinit_2pass_quantizer	jI2Quant
    351 #define jinit_merged_upsampler	jIMUpsampler
    352 #define jinit_memory_mgr	jIMemMgr
    353 #define jdiv_round_up		jDivRound
    354 #define jround_up		jRound
    355 #define jcopy_sample_rows	jCopySamples
    356 #define jcopy_block_row		jCopyBlocks
    357 #define jzero_far		jZeroFar
    358 #define jpeg_zigzag_order	jZIGTable
    359 #define jpeg_natural_order	jZAGTable
    360 #endif /* NEED_SHORT_EXTERNAL_NAMES */
    361 
    362 
    363 /* Compression module initialization routines */
    364 EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
    365 EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
    366 					 boolean transcode_only));
    367 EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
    368 					  boolean need_full_buffer));
    369 EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
    370 					  boolean need_full_buffer));
    371 EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
    372 					  boolean need_full_buffer));
    373 EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
    374 EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
    375 EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
    376 EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
    377 EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
    378 EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
    379 /* Decompression module initialization routines */
    380 EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
    381 EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
    382 					  boolean need_full_buffer));
    383 EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
    384 					  boolean need_full_buffer));
    385 EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
    386 					  boolean need_full_buffer));
    387 EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
    388 EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
    389 EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
    390 EXTERN(void) jinit_huff_decoder_no_data JPP((j_decompress_ptr cinfo));
    391 EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
    392 EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
    393 EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
    394 EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
    395 EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
    396 EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
    397 EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
    398 EXTERN(void) jpeg_decompress_per_scan_setup (j_decompress_ptr cinfo);
    399 /* Memory manager initialization */
    400 EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
    401 
    402 /* Utility routines in jutils.c */
    403 EXTERN(long) jdiv_round_up JPP((long a, long b));
    404 EXTERN(long) jround_up JPP((long a, long b));
    405 EXTERN(long) jmin JPP((long a, long b));
    406 EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
    407 				    JSAMPARRAY output_array, int dest_row,
    408 				    int num_rows, JDIMENSION num_cols));
    409 EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
    410 				  JDIMENSION num_blocks));
    411 EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
    412 
    413 EXTERN(void) jset_input_stream_position JPP((j_decompress_ptr cinfo,
    414                     int offset));
    415 EXTERN(void) jset_input_stream_position_bit JPP((j_decompress_ptr cinfo,
    416                     int byte_offset, int bit_left, INT32 buf));
    417 
    418 EXTERN(int) jget_input_stream_position JPP((j_decompress_ptr cinfo));
    419 /* Constant tables in jutils.c */
    420 #if 0				/* This table is not actually needed in v6a */
    421 extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
    422 #endif
    423 extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
    424 
    425 /* Suppress undefined-structure complaints if necessary. */
    426 
    427 #ifdef INCOMPLETE_TYPES_BROKEN
    428 #ifndef AM_MEMORY_MANAGER	/* only jmemmgr.c defines these */
    429 struct jvirt_sarray_control { long dummy; };
    430 struct jvirt_barray_control { long dummy; };
    431 #endif
    432 #endif /* INCOMPLETE_TYPES_BROKEN */
    433