File indexing completed on 2025-01-19 04:01:46

0001 /*
0002  * jpegint.h
0003  *
0004  * Copyright (C) 1991-1997, Thomas G. Lane.
0005  * Modified 1997-2009 by Guido Vollbeding.
0006  * This file is part of the Independent JPEG Group's software.
0007  * For conditions of distribution and use, see the accompanying README file.
0008  *
0009  * This file provides common declarations for the various JPEG modules.
0010  * These declarations are considered internal to the JPEG library; most
0011  * applications using the library shouldn't need to include this file.
0012  */
0013 
0014 /* Declarations for both compression & decompression */
0015 
0016 typedef enum {          /* Operating modes for buffer controllers */
0017     JBUF_PASS_THRU,     /* Plain stripwise operation */
0018     /* Remaining modes require a full-image buffer to have been created */
0019     JBUF_SAVE_SOURCE,   /* Run source subobject only, save output */
0020     JBUF_CRANK_DEST,    /* Run dest subobject only, using saved data */
0021     JBUF_SAVE_AND_PASS  /* Run both subobjects, save output */
0022 } J_BUF_MODE;
0023 
0024 /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
0025 #define CSTATE_START    100 /* after create_compress */
0026 #define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
0027 #define CSTATE_RAW_OK   102 /* start_compress done, write_raw_data OK */
0028 #define CSTATE_WRCOEFS  103 /* jpeg_write_coefficients done */
0029 #define DSTATE_START    200 /* after create_decompress */
0030 #define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
0031 #define DSTATE_READY    202 /* found SOS, ready for start_decompress */
0032 #define DSTATE_PRELOAD  203 /* reading multiscan file in start_decompress*/
0033 #define DSTATE_PRESCAN  204 /* performing dummy pass for 2-pass quant */
0034 #define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
0035 #define DSTATE_RAW_OK   206 /* start_decompress done, read_raw_data OK */
0036 #define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
0037 #define DSTATE_BUFPOST  208 /* looking for SOS/EOI in jpeg_finish_output */
0038 #define DSTATE_RDCOEFS  209 /* reading file in jpeg_read_coefficients */
0039 #define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
0040 
0041 /* Declarations for compression modules */
0042 
0043 /* Master control module */
0044 struct jpeg_comp_master {
0045     JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
0046     JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
0047     JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
0048 
0049     /* State variables made visible to other modules */
0050     boolean call_pass_startup;    /* True if pass_startup must be called */
0051     boolean is_last_pass;     /* True during last pass */
0052 };
0053 
0054 /* Main buffer control (downsampled-data buffer) */
0055 struct jpeg_c_main_controller {
0056     JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
0057     JMETHOD(void, process_data, (j_compress_ptr cinfo,
0058                                  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
0059                                  JDIMENSION in_rows_avail));
0060 };
0061 
0062 /* Compression preprocessing (downsampling input buffer control) */
0063 struct jpeg_c_prep_controller {
0064     JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
0065     JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
0066                                      JSAMPARRAY input_buf,
0067                                      JDIMENSION *in_row_ctr,
0068                                      JDIMENSION in_rows_avail,
0069                                      JSAMPIMAGE output_buf,
0070                                      JDIMENSION *out_row_group_ctr,
0071                                      JDIMENSION out_row_groups_avail));
0072 };
0073 
0074 /* Coefficient buffer control */
0075 struct jpeg_c_coef_controller {
0076     JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
0077     JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
0078                                      JSAMPIMAGE input_buf));
0079 };
0080 
0081 /* Colorspace conversion */
0082 struct jpeg_color_converter {
0083     JMETHOD(void, start_pass, (j_compress_ptr cinfo));
0084     JMETHOD(void, color_convert, (j_compress_ptr cinfo,
0085                                   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
0086                                   JDIMENSION output_row, int num_rows));
0087 };
0088 
0089 /* Downsampling */
0090 struct jpeg_downsampler {
0091     JMETHOD(void, start_pass, (j_compress_ptr cinfo));
0092     JMETHOD(void, downsample, (j_compress_ptr cinfo,
0093                                JSAMPIMAGE input_buf, JDIMENSION in_row_index,
0094                                JSAMPIMAGE output_buf,
0095                                JDIMENSION out_row_group_index));
0096 
0097     boolean need_context_rows;    /* TRUE if need rows above & below */
0098 };
0099 
0100 /* Forward DCT (also controls coefficient quantization) */
0101 typedef JMETHOD(void, forward_DCT_ptr,
0102                 (j_compress_ptr cinfo, jpeg_component_info * compptr,
0103                  JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
0104                  JDIMENSION start_row, JDIMENSION start_col,
0105                  JDIMENSION num_blocks));
0106 
0107 struct jpeg_forward_dct {
0108     JMETHOD(void, start_pass, (j_compress_ptr cinfo));
0109     /* It is useful to allow each component to have a separate FDCT method. */
0110     forward_DCT_ptr forward_DCT[MAX_COMPONENTS];
0111 };
0112 
0113 /* Entropy encoding */
0114 struct jpeg_entropy_encoder {
0115     JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
0116     JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
0117     JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
0118 };
0119 
0120 /* Marker writing */
0121 struct jpeg_marker_writer {
0122     JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
0123     JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
0124     JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
0125     JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
0126     JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
0127     /* These routines are exported to allow insertion of extra markers */
0128     /* Probably only COM and APPn markers should be written this way */
0129     JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
0130                                         unsigned int datalen));
0131     JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
0132 };
0133 
0134 /* Declarations for decompression modules */
0135 
0136 /* Master control module */
0137 struct jpeg_decomp_master {
0138     JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
0139     JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
0140 
0141     /* State variables made visible to other modules */
0142     boolean is_dummy_pass;    /* True during 1st pass for 2-pass quant */
0143 };
0144 
0145 /* Input control module */
0146 struct jpeg_input_controller {
0147     JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
0148     JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
0149     JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
0150     JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
0151 
0152     /* State variables made visible to other modules */
0153     boolean has_multiple_scans;   /* True if file has multiple scans */
0154     boolean eoi_reached;      /* True when EOI has been consumed */
0155 };
0156 
0157 /* Main buffer control (downsampled-data buffer) */
0158 struct jpeg_d_main_controller {
0159     JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
0160     JMETHOD(void, process_data, (j_decompress_ptr cinfo,
0161                                  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
0162                                  JDIMENSION out_rows_avail));
0163 };
0164 
0165 /* Coefficient buffer control */
0166 struct jpeg_d_coef_controller {
0167     JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
0168     JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
0169     JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
0170     JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
0171                                    JSAMPIMAGE output_buf));
0172     /* Pointer to array of coefficient virtual arrays, or NULL if none */
0173     jvirt_barray_ptr *coef_arrays;
0174 };
0175 
0176 /* Decompression postprocessing (color quantization buffer control) */
0177 struct jpeg_d_post_controller {
0178     JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
0179     JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
0180                                       JSAMPIMAGE input_buf,
0181                                       JDIMENSION *in_row_group_ctr,
0182                                       JDIMENSION in_row_groups_avail,
0183                                       JSAMPARRAY output_buf,
0184                                       JDIMENSION *out_row_ctr,
0185                                       JDIMENSION out_rows_avail));
0186 };
0187 
0188 /* Marker reading & parsing */
0189 struct jpeg_marker_reader {
0190     JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
0191     /* Read markers until SOS or EOI.
0192      * Returns same codes as are defined for jpeg_consume_input:
0193      * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
0194      */
0195     JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
0196     /* Read a restart marker --- exported for use by entropy decoder only */
0197     jpeg_marker_parser_method read_restart_marker;
0198 
0199     /* State of marker reader --- nominally internal, but applications
0200      * supplying COM or APPn handlers might like to know the state.
0201      */
0202     boolean saw_SOI;      /* found SOI? */
0203     boolean saw_SOF;      /* found SOF? */
0204     int next_restart_num;     /* next restart number expected (0-7) */
0205     unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
0206 };
0207 
0208 /* Entropy decoding */
0209 struct jpeg_entropy_decoder {
0210     JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
0211     JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
0212                                   JBLOCKROW *MCU_data));
0213 };
0214 
0215 /* Inverse DCT (also performs dequantization) */
0216 typedef JMETHOD(void, inverse_DCT_method_ptr,
0217                 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0218                  JCOEFPTR coef_block,
0219                  JSAMPARRAY output_buf, JDIMENSION output_col));
0220 
0221 struct jpeg_inverse_dct {
0222     JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
0223     /* It is useful to allow each component to have a separate IDCT method. */
0224     inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
0225 };
0226 
0227 /* Upsampling (note that upsampler must also call color converter) */
0228 struct jpeg_upsampler {
0229     JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
0230     JMETHOD(void, upsample, (j_decompress_ptr cinfo,
0231                              JSAMPIMAGE input_buf,
0232                              JDIMENSION *in_row_group_ctr,
0233                              JDIMENSION in_row_groups_avail,
0234                              JSAMPARRAY output_buf,
0235                              JDIMENSION *out_row_ctr,
0236                              JDIMENSION out_rows_avail));
0237 
0238     boolean need_context_rows;    /* TRUE if need rows above & below */
0239 };
0240 
0241 /* Colorspace conversion */
0242 struct jpeg_color_deconverter {
0243     JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
0244     JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
0245                                   JSAMPIMAGE input_buf, JDIMENSION input_row,
0246                                   JSAMPARRAY output_buf, int num_rows));
0247 };
0248 
0249 /* Color quantization or color precision reduction */
0250 struct jpeg_color_quantizer {
0251     JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
0252     JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
0253                                    JSAMPARRAY input_buf, JSAMPARRAY output_buf,
0254                                    int num_rows));
0255     JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
0256     JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
0257 };
0258 
0259 /* Miscellaneous useful macros */
0260 
0261 #undef MAX
0262 #define MAX(a,b)    ((a) > (b) ? (a) : (b))
0263 #undef MIN
0264 #define MIN(a,b)    ((a) < (b) ? (a) : (b))
0265 
0266 /* We assume that right shift corresponds to signed division by 2 with
0267  * rounding towards minus infinity.  This is correct for typical "arithmetic
0268  * shift" instructions that shift in copies of the sign bit.  But some
0269  * C compilers implement >> with an unsigned shift.  For these machines you
0270  * must define RIGHT_SHIFT_IS_UNSIGNED.
0271  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
0272  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
0273  * included in the variables of any routine using RIGHT_SHIFT.
0274  */
0275 
0276 #ifdef RIGHT_SHIFT_IS_UNSIGNED
0277 #define SHIFT_TEMPS INT32 shift_temp;
0278 #define RIGHT_SHIFT(x,shft)  \
0279     ((shift_temp = (x)) < 0 ? \
0280      (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
0281      (shift_temp >> (shft)))
0282 #else
0283 #define SHIFT_TEMPS
0284 #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
0285 #endif
0286 
0287 /* Short forms of external names for systems with brain-damaged linkers. */
0288 
0289 #ifdef NEED_SHORT_EXTERNAL_NAMES
0290 #define jinit_compress_master   jICompress
0291 #define jinit_c_master_control  jICMaster
0292 #define jinit_c_main_controller jICMainC
0293 #define jinit_c_prep_controller jICPrepC
0294 #define jinit_c_coef_controller jICCoefC
0295 #define jinit_color_converter   jICColor
0296 #define jinit_downsampler   jIDownsampler
0297 #define jinit_forward_dct   jIFDCT
0298 #define jinit_huff_encoder  jIHEncoder
0299 #define jinit_arith_encoder jIAEncoder
0300 #define jinit_marker_writer jIMWriter
0301 #define jinit_master_decompress jIDMaster
0302 #define jinit_d_main_controller jIDMainC
0303 #define jinit_d_coef_controller jIDCoefC
0304 #define jinit_d_post_controller jIDPostC
0305 #define jinit_input_controller  jIInCtlr
0306 #define jinit_marker_reader jIMReader
0307 #define jinit_huff_decoder  jIHDecoder
0308 #define jinit_arith_decoder jIADecoder
0309 #define jinit_inverse_dct   jIIDCT
0310 #define jinit_upsampler     jIUpsampler
0311 #define jinit_color_deconverter jIDColor
0312 #define jinit_1pass_quantizer   jI1Quant
0313 #define jinit_2pass_quantizer   jI2Quant
0314 #define jinit_merged_upsampler  jIMUpsampler
0315 #define jinit_memory_mgr    jIMemMgr
0316 #define jdiv_round_up       jDivRound
0317 #define jround_up       jRound
0318 #define jcopy_sample_rows   jCopySamples
0319 #define jcopy_block_row     jCopyBlocks
0320 #define jzero_far       jZeroFar
0321 #define jpeg_zigzag_order   jZIGTable
0322 #define jpeg_natural_order  jZAGTable
0323 #define jpeg_natural_order7 jZAGTable7
0324 #define jpeg_natural_order6 jZAGTable6
0325 #define jpeg_natural_order5 jZAGTable5
0326 #define jpeg_natural_order4 jZAGTable4
0327 #define jpeg_natural_order3 jZAGTable3
0328 #define jpeg_natural_order2 jZAGTable2
0329 #define jpeg_aritab     jAriTab
0330 #endif /* NEED_SHORT_EXTERNAL_NAMES */
0331 
0332 /* Compression module initialization routines */
0333 EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
0334 EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
0335                                         boolean transcode_only));
0336 EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
0337         boolean need_full_buffer));
0338 EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
0339         boolean need_full_buffer));
0340 EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
0341         boolean need_full_buffer));
0342 EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
0343 EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
0344 EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
0345 EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
0346 EXTERN(void) jinit_arith_encoder JPP((j_compress_ptr cinfo));
0347 EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
0348 /* Decompression module initialization routines */
0349 EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
0350 EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
0351         boolean need_full_buffer));
0352 EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
0353         boolean need_full_buffer));
0354 EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
0355         boolean need_full_buffer));
0356 EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
0357 EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
0358 EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
0359 EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo));
0360 EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
0361 EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
0362 EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
0363 EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
0364 EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
0365 EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
0366 /* Memory manager initialization */
0367 EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
0368 
0369 /* Utility routines in jutils.c */
0370 EXTERN(long) jdiv_round_up JPP((long a, long b));
0371 EXTERN(long) jround_up JPP((long a, long b));
0372 EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
0373                                     JSAMPARRAY output_array, int dest_row,
0374                                     int num_rows, JDIMENSION num_cols));
0375 EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
0376                                   JDIMENSION num_blocks));
0377 EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
0378 /* Constant tables in jutils.c */
0379 #if 0               /* This table is not actually needed in v6a */
0380 extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
0381 #endif
0382 extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
0383 extern const int jpeg_natural_order7[]; /* zz to natural order for 7x7 block */
0384 extern const int jpeg_natural_order6[]; /* zz to natural order for 6x6 block */
0385 extern const int jpeg_natural_order5[]; /* zz to natural order for 5x5 block */
0386 extern const int jpeg_natural_order4[]; /* zz to natural order for 4x4 block */
0387 extern const int jpeg_natural_order3[]; /* zz to natural order for 3x3 block */
0388 extern const int jpeg_natural_order2[]; /* zz to natural order for 2x2 block */
0389 
0390 /* Arithmetic coding probability estimation tables in jaricom.c */
0391 extern const INT32 jpeg_aritab[];
0392 
0393 /* Suppress undefined-structure complaints if necessary. */
0394 
0395 #ifdef INCOMPLETE_TYPES_BROKEN
0396 #ifndef AM_MEMORY_MANAGER   /* only jmemmgr.c defines these */
0397 struct jvirt_sarray_control {
0398     long dummy;
0399 };
0400 struct jvirt_barray_control {
0401     long dummy;
0402 };
0403 #endif
0404 #endif /* INCOMPLETE_TYPES_BROKEN */