File indexing completed on 2025-01-05 04:02:11

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