home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jpegdata.h < prev    next >
C/C++ Source or Header  |  1992-03-02  |  37KB  |  886 lines

  1. /*
  2.  * jpegdata.h
  3.  *
  4.  * Copyright (C) 1991, 1992, 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 defines shared data structures for the various JPEG modules.
  9.  */
  10.  
  11.  
  12. /*
  13.  * You might need to change some of the following declarations if you are
  14.  * using the JPEG software within a surrounding application program
  15.  * or porting it to an unusual system.
  16.  */
  17.  
  18.  
  19. /* If the source or destination of image data is not to be stdio streams,
  20.  * these types may need work.  You can replace them with some kind of
  21.  * pointer or indicator that is useful to you, or just ignore 'em.
  22.  * Note that the user interface and the various jrdxxx/jwrxxx modules
  23.  * will also need work for non-stdio input/output.
  24.  */
  25.  
  26. typedef FILE * JFILEREF;    /* source or dest of JPEG-compressed data */
  27.  
  28. typedef FILE * IFILEREF;    /* source or dest of non-JPEG image data */
  29.  
  30.  
  31. /* These defines are used in all function definitions and extern declarations.
  32.  * You could modify them if you need to change function linkage conventions,
  33.  * as is shown below for use with C++.  Another application would be to make
  34.  * all functions global for use with code profilers that require it.
  35.  * NOTE: the C++ test does the right thing if you are reading this include
  36.  * file in a C++ application to link to JPEG code that's been compiled with a
  37.  * regular C compiler.  I'm not sure it works if you try to compile the JPEG
  38.  * code with C++.
  39.  */
  40.  
  41. #define METHODDEF static    /* a function called through method pointers */
  42. #define LOCAL      static    /* a function used only in its module */
  43. #define GLOBAL            /* a function referenced thru EXTERNs */
  44. #ifdef __cplusplus
  45. #define EXTERN      extern "C"    /* a reference to a GLOBAL function */
  46. #else
  47. #define EXTERN      extern    /* a reference to a GLOBAL function */
  48. #endif
  49.  
  50.  
  51. /* Here is the pseudo-keyword for declaring pointers that must be "far"
  52.  * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  53.  * by just saying "FAR *" where such a pointer is needed.  In a few places
  54.  * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  55.  */
  56.  
  57. #ifdef NEED_FAR_POINTERS
  58. #define FAR  far
  59. #else
  60. #define FAR
  61. #endif
  62.  
  63.  
  64.  
  65. /* The remaining declarations are not system-dependent, we hope. */
  66.  
  67.  
  68. /*
  69.  * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
  70.  * similarly-named fields in compress_info_struct and decompress_info_struct.
  71.  * If this happens, you can get around it by rearranging the two structs so
  72.  * that the similarly-named fields appear first and in the same order in
  73.  * each struct.  Since such compilers are now pretty rare, we haven't done
  74.  * this in the portable code, preferring to maintain a logical ordering.
  75.  */
  76.  
  77.  
  78.  
  79. /* This macro is used to declare a "method", that is, a function pointer. */
  80. /* We want to supply prototype parameters if the compiler can cope. */
  81. /* Note that the arglist parameter must be parenthesized! */
  82.  
  83. #ifdef PROTO
  84. #define METHOD(type,methodname,arglist)  type (*methodname) arglist
  85. #else
  86. #define METHOD(type,methodname,arglist)  type (*methodname) ()
  87. #endif
  88.  
  89. /* Forward references to lists of method pointers */
  90. typedef struct external_methods_struct * external_methods_ptr;
  91. typedef struct compress_methods_struct * compress_methods_ptr;
  92. typedef struct decompress_methods_struct * decompress_methods_ptr;
  93.  
  94.  
  95. /* Data structures for images containing either samples or coefficients. */
  96. /* Note that the topmost (leftmost) index is always color component. */
  97. /* On 80x86 machines, the image arrays are too big for near pointers, */
  98. /* but the pointer arrays can fit in near memory. */
  99.  
  100. typedef JSAMPLE FAR *JSAMPROW;    /* ptr to one image row of pixel samples. */
  101. typedef JSAMPROW *JSAMPARRAY;    /* ptr to some rows (a 2-D sample array) */
  102. typedef JSAMPARRAY *JSAMPIMAGE;    /* a 3-D sample array: top index is color */
  103.  
  104.  
  105. #define DCTSIZE        8    /* The basic DCT block is 8x8 samples */
  106. #define DCTSIZE2    64    /* DCTSIZE squared; # of elements in a block */
  107.  
  108. typedef JCOEF JBLOCK[DCTSIZE2];    /* one block of coefficients */
  109. typedef JBLOCK FAR *JBLOCKROW;    /* pointer to one row of coefficient blocks */
  110. typedef JBLOCKROW *JBLOCKARRAY;        /* a 2-D array of coefficient blocks */
  111. typedef JBLOCKARRAY *JBLOCKIMAGE;    /* a 3-D array of coefficient blocks */
  112.  
  113. typedef JCOEF FAR *JCOEFPTR;    /* useful in a couple of places */
  114.  
  115.  
  116. /* The input and output data of the DCT transform subroutines are of
  117.  * the following type, which need not be the same as JCOEF.
  118.  * For example, on a machine with fast floating point, it might make sense
  119.  * to recode the DCT routines to use floating point; then DCTELEM would be
  120.  * 'float' or 'double'.
  121.  */
  122.  
  123. typedef JCOEF DCTELEM;
  124. typedef DCTELEM DCTBLOCK[DCTSIZE2];
  125.  
  126.  
  127. /* Types for JPEG compression parameters and working tables. */
  128.  
  129.  
  130. typedef enum {            /* defines known color spaces */
  131.     CS_UNKNOWN,        /* error/unspecified */
  132.     CS_GRAYSCALE,        /* monochrome (only 1 component) */
  133.     CS_RGB,            /* red/green/blue */
  134.     CS_YCbCr,        /* Y/Cb/Cr (also known as YUV) */
  135.     CS_YIQ,            /* Y/I/Q */
  136.     CS_CMYK            /* C/M/Y/K */
  137. } COLOR_SPACE;
  138.  
  139.  
  140. typedef struct {        /* Basic info about one component */
  141.   /* These values are fixed over the whole image */
  142.   /* For compression, they must be supplied by the user interface; */
  143.   /* for decompression, they are read from the SOF marker. */
  144.     short component_id;    /* identifier for this component (0..255) */
  145.     short component_index;    /* its index in SOF or cinfo->comp_info[] */
  146.     short h_samp_factor;    /* horizontal sampling factor (1..4) */
  147.     short v_samp_factor;    /* vertical sampling factor (1..4) */
  148.     short quant_tbl_no;    /* quantization table selector (0..3) */
  149.   /* These values may vary between scans */
  150.   /* For compression, they must be supplied by the user interface; */
  151.   /* for decompression, they are read from the SOS marker. */
  152.     short dc_tbl_no;    /* DC entropy table selector (0..3) */
  153.     short ac_tbl_no;    /* AC entropy table selector (0..3) */
  154.   /* These values are computed during compression or decompression startup */
  155.     long true_comp_width;    /* component's image width in samples */
  156.     long true_comp_height;    /* component's image height in samples */
  157.     /* the above are the logical dimensions of the subsampled image */
  158.   /* These values are computed before starting a scan of the component */
  159.     short MCU_width;    /* number of blocks per MCU, horizontally */
  160.     short MCU_height;    /* number of blocks per MCU, vertically */
  161.     short MCU_blocks;    /* MCU_width * MCU_height */
  162.     long subsampled_width;    /* image width in samples, after expansion */
  163.     long subsampled_height;    /* image height in samples, after expansion */
  164.     /* the above are the true_comp_xxx values rounded up to multiples of */
  165.     /* the MCU dimensions; these are the working dimensions of the array */
  166.     /* as it is passed through the DCT or IDCT step.  NOTE: these values */
  167.     /* differ depending on whether the component is interleaved or not!! */
  168. } jpeg_component_info;
  169.  
  170.  
  171. /* DCT coefficient quantization tables.
  172.  * For 8-bit precision, 'INT16' should be good enough for quantization values;
  173.  * for more precision, we go for the full 16 bits.  'INT16' provides a useful
  174.  * speedup on many machines (multiplication & division of JCOEFs by
  175.  * quantization values is a significant chunk of the runtime).
  176.  * Note: the values in a QUANT_TBL are always given in zigzag order.
  177.  */
  178. #ifdef EIGHT_BIT_SAMPLES
  179. typedef INT16 QUANT_VAL;    /* element of a quantization table */
  180. #else
  181. typedef UINT16 QUANT_VAL;    /* element of a quantization table */
  182. #endif
  183. typedef QUANT_VAL QUANT_TBL[DCTSIZE2];    /* A quantization table */
  184. typedef QUANT_VAL * QUANT_TBL_PTR;    /* pointer to same */
  185.  
  186.  
  187. typedef struct {        /* A Huffman coding table */
  188.   /* These two fields directly represent the contents of a JPEG DHT marker */
  189.     UINT8 bits[17];        /* bits[k] = # of symbols with codes of */
  190.                 /* length k bits; bits[0] is unused */
  191.     UINT8 huffval[256];    /* The symbols, in order of incr code length */
  192.   /* This field is used only during compression.  It's initialized FALSE when
  193.    * the table is created, and set TRUE when it's been output to the file.
  194.    */
  195.     boolean sent_table;    /* TRUE when table has been output */
  196.   /* The remaining fields are computed from the above to allow more efficient
  197.    * coding and decoding.  These fields should be considered private to the
  198.    * Huffman compression & decompression modules.
  199.    */
  200.     /* encoding tables: */
  201.     UINT16 ehufco[256];    /* code for each symbol */
  202.     char ehufsi[256];    /* length of code for each symbol */
  203.     /* decoding tables: (element [0] of each array is unused) */
  204.     UINT16 mincode[17];    /* smallest code of length k */
  205.     INT32 maxcode[18];    /* largest code of length k (-1 if none) */
  206.     /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
  207.     short valptr[17];    /* huffval[] index of 1st symbol of length k */
  208. } HUFF_TBL;
  209.  
  210.  
  211. #define NUM_QUANT_TBLS      4    /* quantization tables are numbered 0..3 */
  212. #define NUM_HUFF_TBLS       4    /* Huffman tables are numbered 0..3 */
  213. #define NUM_ARITH_TBLS      16    /* arith-coding tables are numbered 0..15 */
  214. #define MAX_COMPS_IN_SCAN   4    /* JPEG limit on # of components in one scan */
  215. #define MAX_SAMP_FACTOR     4    /* JPEG limit on sampling factors */
  216. #define MAX_BLOCKS_IN_MCU   10    /* JPEG limit on # of blocks in an MCU */
  217.  
  218.  
  219. /* Working data for compression */
  220.  
  221. struct compress_info_struct {
  222. /*
  223.  * All of these fields shall be established by the user interface before
  224.  * calling jpeg_compress, or by the input_init or c_ui_method_selection
  225.  * methods.
  226.  * Most parameters can be set to reasonable defaults by j_c_defaults.
  227.  * Note that the UI must supply the storage for the main methods struct,
  228.  * though it sets only a few of the methods there.
  229.  */
  230.     compress_methods_ptr methods; /* Points to list of methods to use */
  231.  
  232.     external_methods_ptr emethods; /* Points to list of methods to use */
  233.  
  234.     IFILEREF input_file;    /* tells input routines where to read image */
  235.     JFILEREF output_file;    /* tells output routines where to write JPEG */
  236.  
  237.     long image_width;    /* input image width */
  238.     long image_height;    /* input image height */
  239.     short input_components;    /* # of color components in input image */
  240.  
  241.     short data_precision;    /* bits of precision in image data */
  242.  
  243.     COLOR_SPACE in_color_space; /* colorspace of input file */
  244.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  245.  
  246.     double input_gamma;    /* image gamma of input file */
  247.  
  248.     boolean write_JFIF_header; /* should a JFIF marker be written? */
  249.     /* These three values are not used by the JPEG code, only copied */
  250.     /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  251.     /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  252.     /* ratio is defined by X_density/Y_density even when density_unit=0. */
  253.     UINT8 density_unit;    /* JFIF code for pixel size units */
  254.     UINT16 X_density;    /* Horizontal pixel density */
  255.     UINT16 Y_density;    /* Vertical pixel density */
  256.  
  257.     short num_components;    /* # of color components in JPEG image */
  258.     jpeg_component_info * comp_info;
  259.     /* comp_info[i] describes component that appears i'th in SOF */
  260.  
  261.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  262.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  263.  
  264.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  265.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  266.     /* ptrs to Huffman coding tables, or NULL if not defined */
  267.  
  268.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
  269.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
  270.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
  271.  
  272.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  273.     boolean interleave;    /* TRUE=interleaved output, FALSE=not */
  274.     boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
  275.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  276.  
  277.     UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  278.  
  279. /*
  280.  * These fields are computed during jpeg_compress startup
  281.  */
  282.     short max_h_samp_factor; /* largest h_samp_factor */
  283.     short max_v_samp_factor; /* largest v_samp_factor */
  284.  
  285. /*
  286.  * These fields may be useful for progress monitoring
  287.  */
  288.  
  289.     int total_passes;    /* number of passes expected */
  290.     int completed_passes;    /* number of passes completed so far */
  291.  
  292. /*
  293.  * These fields are valid during any one scan
  294.  */
  295.     short comps_in_scan;    /* # of JPEG components output this time */
  296.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  297.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  298.  
  299.     long MCUs_per_row;    /* # of MCUs across the image */
  300.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  301.  
  302.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  303.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  304.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  305.     /* i'th block in an MCU */
  306.  
  307.     /* these fields are private data for the entropy encoder */
  308.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  309.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  310.     UINT16 restarts_to_go;    /* MDUs left in this restart interval */
  311.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  312. };
  313.  
  314. typedef struct compress_info_struct * compress_info_ptr;
  315.  
  316.  
  317. /* Working data for decompression */
  318.  
  319. struct decompress_info_struct {
  320. /*
  321.  * These fields shall be established by the user interface before
  322.  * calling jpeg_decompress.
  323.  * Most parameters can be set to reasonable defaults by j_d_defaults.
  324.  * Note that the UI must supply the storage for the main methods struct,
  325.  * though it sets only a few of the methods there.
  326.  */
  327.     decompress_methods_ptr methods; /* Points to list of methods to use */
  328.  
  329.     external_methods_ptr emethods; /* Points to list of methods to use */
  330.  
  331.     JFILEREF input_file;    /* tells input routines where to read JPEG */
  332.     IFILEREF output_file;    /* tells output routines where to write image */
  333.  
  334.     /* these can be set at d_ui_method_selection time: */
  335.  
  336.     COLOR_SPACE out_color_space; /* colorspace of output */
  337.  
  338.     double output_gamma;    /* image gamma wanted in output */
  339.  
  340.     boolean quantize_colors; /* T if output is a colormapped format */
  341.     /* the following are ignored if not quantize_colors: */
  342.     boolean two_pass_quantize;    /* use two-pass color quantization? */
  343.     boolean use_dithering;        /* want color dithering? */
  344.     int desired_number_of_colors;    /* max number of colors to use */
  345.  
  346.     boolean do_block_smoothing; /* T = apply cross-block smoothing */
  347.     boolean do_pixel_smoothing; /* T = apply post-subsampling smoothing */
  348.  
  349. /*
  350.  * These fields are used for efficient buffering of data between read_jpeg_data
  351.  * and the entropy decoding object.  By using a shared buffer, we avoid copying
  352.  * data and eliminate the need for an "unget" operation at the end of a scan.
  353.  * The actual source of the data is known only to read_jpeg_data; see the
  354.  * JGETC macro, below.
  355.  * Note: the user interface is expected to allocate the input_buffer and
  356.  * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
  357.  * actually supplies the read_jpeg_data method.  This is all handled by
  358.  * j_d_defaults in a typical implementation.
  359.  */
  360.     char * input_buffer;    /* start of buffer (private to input code) */
  361.     char * next_input_byte;    /* => next byte to read from buffer */
  362.     int bytes_in_buffer;    /* # of bytes remaining in buffer */
  363.  
  364. /*
  365.  * These fields are set by read_file_header or read_scan_header
  366.  */
  367.     long image_width;    /* overall image width */
  368.     long image_height;    /* overall image height */
  369.  
  370.     short data_precision;    /* bits of precision in image data */
  371.  
  372.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  373.  
  374.         /* These three values are not used by the JPEG code, merely copied */
  375.     /* from the JFIF APP0 marker (if any). */
  376.     UINT8 density_unit;    /* JFIF code for pixel size units */
  377.     UINT16 X_density;    /* Horizontal pixel density */
  378.     UINT16 Y_density;    /* Vertical pixel density */
  379.  
  380.     short num_components;    /* # of color components in JPEG image */
  381.     jpeg_component_info * comp_info;
  382.     /* comp_info[i] describes component that appears i'th in SOF */
  383.  
  384.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  385.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  386.  
  387.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  388.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  389.     /* ptrs to Huffman coding tables, or NULL if not defined */
  390.  
  391.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  392.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  393.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  394.  
  395.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  396.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  397.  
  398.     UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  399.  
  400. /*
  401.  * These fields are computed during jpeg_decompress startup
  402.  */
  403.     short max_h_samp_factor; /* largest h_samp_factor */
  404.     short max_v_samp_factor; /* largest v_samp_factor */
  405.  
  406.     short color_out_comps;    /* # of color components output by color_convert */
  407.                 /* (need not match num_components) */
  408.     short final_out_comps;    /* # of color components sent to put_pixel_rows */
  409.     /* (1 when quantizing colors, else same as color_out_comps) */
  410.  
  411. /*
  412.  * When quantizing colors, the color quantizer leaves a pointer to the output
  413.  * colormap in these fields.  The colormap is valid from the time put_color_map
  414.  * is called (must be before any put_pixel_rows calls) until shutdown (more
  415.  * specifically, until free_all is called to release memory).
  416.  */
  417.     int actual_number_of_colors; /* actual number of entries */
  418.     JSAMPARRAY colormap;    /* NULL if not valid */
  419.     /* map has color_out_comps rows * actual_number_of_colors columns */
  420.  
  421. /*
  422.  * These fields may be useful for progress monitoring
  423.  */
  424.  
  425.     int total_passes;    /* number of passes expected */
  426.     int completed_passes;    /* number of passes completed so far */
  427.  
  428. /*
  429.  * These fields are valid during any one scan
  430.  */
  431.     short comps_in_scan;    /* # of JPEG components input this time */
  432.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  433.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  434.  
  435.     long MCUs_per_row;    /* # of MCUs across the image */
  436.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  437.  
  438.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  439.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  440.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  441.     /* i'th block in an MCU */
  442.  
  443.     /* these fields are private data for the entropy encoder */
  444.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  445.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  446.     UINT16 restarts_to_go;    /* MDUs left in this restart interval */
  447.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  448. };
  449.  
  450. typedef struct decompress_info_struct * decompress_info_ptr;
  451.  
  452.  
  453. /* Macros for reading data from the decompression input buffer */
  454.  
  455. #ifdef CHAR_IS_UNSIGNED
  456. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  457.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  458.              (int) (*(cinfo)->next_input_byte++) )
  459. #else
  460. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  461.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  462.              (int) (*(cinfo)->next_input_byte++) & 0xFF )
  463. #endif
  464.  
  465. #define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
  466.                 *(--((cinfo)->next_input_byte)) = (ch))
  467.  
  468. #define MIN_UNGET    4    /* may always do at least 4 JUNGETCs */
  469.  
  470.  
  471. /* A virtual image has a control block whose contents are private to the
  472.  * memory manager module (and may differ between managers).  The rest of the
  473.  * code only refers to virtual images by these pointer types, and never
  474.  * dereferences the pointer.
  475.  */
  476.  
  477. typedef struct big_sarray_control * big_sarray_ptr;
  478. typedef struct big_barray_control * big_barray_ptr;
  479.  
  480. /* Although a real ANSI C compiler can deal perfectly well with pointers to
  481.  * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
  482.  * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
  483.  * add -DINCOMPLETE_TYPES_BROKEN to CFLAGS in your Makefile.  Then we will
  484.  * pseudo-define the structs as containing a single "dummy" field.
  485.  * The memory managers #define AM_MEMORY_MANAGER before including this file,
  486.  * so that they can make their own definitions of the structs.
  487.  */
  488.  
  489. #ifdef INCOMPLETE_TYPES_BROKEN
  490. #ifndef AM_MEMORY_MANAGER
  491. struct big_sarray_control { long dummy; };
  492. struct big_barray_control { long dummy; };
  493. #endif
  494. #endif
  495.  
  496.  
  497. /* Method types that need typedefs */
  498.  
  499. typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
  500.                          JBLOCK *MCU_data));
  501. typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
  502.                          MCU_output_method_ptr output_method));
  503. typedef METHOD(void, subsample_ptr, (compress_info_ptr cinfo,
  504.                      int which_component,
  505.                      long input_cols, int input_rows,
  506.                      long output_cols, int output_rows,
  507.                      JSAMPARRAY above,
  508.                      JSAMPARRAY input_data,
  509.                      JSAMPARRAY below,
  510.                      JSAMPARRAY output_data));
  511. typedef METHOD(void, unsubsample_ptr, (decompress_info_ptr cinfo,
  512.                        int which_component,
  513.                        long input_cols, int input_rows,
  514.                        long output_cols, int output_rows,
  515.                        JSAMPARRAY above,
  516.                        JSAMPARRAY input_data,
  517.                        JSAMPARRAY below,
  518.                        JSAMPARRAY output_data));
  519. typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
  520.                        int num_rows,
  521.                        JSAMPIMAGE input_data,
  522.                        JSAMPARRAY output_workspace));
  523. typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
  524.                        quantize_method_ptr quantize_method));
  525.  
  526.  
  527. /* These structs contain function pointers for the various JPEG methods. */
  528.  
  529. /* Routines to be provided by the surrounding application, rather than the
  530.  * portable JPEG code proper.  These are the same for compression and
  531.  * decompression.
  532.  */
  533.  
  534. struct external_methods_struct {
  535.     /* User interface: error exit and trace message routines */
  536.     /* NOTE: the string msgtext parameters will eventually be replaced */
  537.     /* by an enumerated-type code so that non-English error messages */
  538.     /* can be substituted easily.  This will not be done until all the */
  539.     /* code is in place, so that we know what messages are needed. */
  540.     METHOD(void, error_exit, (const char *msgtext));
  541.     METHOD(void, trace_message, (const char *msgtext));
  542.  
  543.     /* Working data for error/trace facility */
  544.     /* See macros below for the usage of these variables */
  545.     int trace_level;    /* level of detail of tracing messages */
  546.     /* Use level 0 for unsuppressable messages (nonfatal errors) */
  547.     /* Use levels 1, 2, 3 for successively more detailed trace options */
  548.  
  549.     int message_parm[8];    /* store numeric parms for messages here */
  550.  
  551.     /* Memory management */
  552.     /* NB: alloc routines never return NULL. They exit to */
  553.     /* error_exit if not successful. */
  554.     METHOD(void *, alloc_small, (size_t sizeofobject));
  555.     METHOD(void, free_small, (void *ptr));
  556.     METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
  557.     METHOD(void, free_medium, (void FAR *ptr));
  558.     METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
  559.                         long numrows));
  560.     METHOD(void, free_small_sarray, (JSAMPARRAY ptr));
  561.     METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
  562.                          long numrows));
  563.     METHOD(void, free_small_barray, (JBLOCKARRAY ptr));
  564.     METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
  565.                             long numrows,
  566.                             long unitheight));
  567.     METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
  568.                             long numrows,
  569.                             long unitheight));
  570.     METHOD(void, alloc_big_arrays, (long extra_small_samples,
  571.                     long extra_small_blocks,
  572.                     long extra_medium_space));
  573.     METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
  574.                            long start_row,
  575.                            boolean writable));
  576.     METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
  577.                         long start_row,
  578.                         boolean writable));
  579.     METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
  580.     METHOD(void, free_big_barray, (big_barray_ptr ptr));
  581.     METHOD(void, free_all, (void));
  582.  
  583.     long max_memory_to_use;    /* maximum amount of memory to use */
  584. };
  585.  
  586. /* Macros to simplify using the error and trace message stuff */
  587. /* The first parameter is generally cinfo->emethods */
  588.  
  589. #define ERREXIT(emeth,msg)        ((*(emeth)->error_exit) (msg))
  590. #define ERREXIT1(emeth,msg,p1)        ((emeth)->message_parm[0] = (p1), \
  591.                      (*(emeth)->error_exit) (msg))
  592. #define ERREXIT2(emeth,msg,p1,p2)    ((emeth)->message_parm[0] = (p1), \
  593.                      (emeth)->message_parm[1] = (p2), \
  594.                      (*(emeth)->error_exit) (msg))
  595. #define ERREXIT3(emeth,msg,p1,p2,p3)    ((emeth)->message_parm[0] = (p1), \
  596.                      (emeth)->message_parm[1] = (p2), \
  597.                      (emeth)->message_parm[2] = (p3), \
  598.                      (*(emeth)->error_exit) (msg))
  599. #define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
  600.                      (emeth)->message_parm[1] = (p2), \
  601.                      (emeth)->message_parm[2] = (p3), \
  602.                      (emeth)->message_parm[3] = (p4), \
  603.                      (*(emeth)->error_exit) (msg))
  604.  
  605. #define MAKESTMT(stuff)        do { stuff } while (0)
  606.  
  607. #define TRACEMS(emeth,lvl,msg)    \
  608.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  609.         (*(emeth)->trace_message) (msg); } )
  610. #define TRACEMS1(emeth,lvl,msg,p1)    \
  611.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  612.         (emeth)->message_parm[0] = (p1); \
  613.         (*(emeth)->trace_message) (msg); } )
  614. #define TRACEMS2(emeth,lvl,msg,p1,p2)    \
  615.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  616.         (emeth)->message_parm[0] = (p1); \
  617.         (emeth)->message_parm[1] = (p2); \
  618.         (*(emeth)->trace_message) (msg); } )
  619. #define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
  620.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  621.         int * _mp = (emeth)->message_parm; \
  622.         *_mp++ = (p1); *_mp++ = (p2); *_mp = (p3); \
  623.         (*(emeth)->trace_message) (msg); } )
  624. #define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
  625.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  626.         int * _mp = (emeth)->message_parm; \
  627.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp = (p4); \
  628.         (*(emeth)->trace_message) (msg); } )
  629. #define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
  630.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  631.         int * _mp = (emeth)->message_parm; \
  632.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp++ = (p4); \
  633.         *_mp++ = (p5); *_mp++ = (p6); *_mp++ = (p7); *_mp = (p8); \
  634.         (*(emeth)->trace_message) (msg); } )
  635.  
  636.  
  637. /* Methods used during JPEG compression. */
  638.  
  639. struct compress_methods_struct {
  640.     /* Hook for user interface to get control after input_init */
  641.     METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
  642.     /* Hook for user interface to do progress monitoring */
  643.     METHOD(void, progress_monitor, (compress_info_ptr cinfo,
  644.                     long loopcounter, long looplimit));
  645.     /* Input image reading & conversion to standard form */
  646.     METHOD(void, input_init, (compress_info_ptr cinfo));
  647.     METHOD(void, get_input_row, (compress_info_ptr cinfo,
  648.                      JSAMPARRAY pixel_row));
  649.     METHOD(void, input_term, (compress_info_ptr cinfo));
  650.     /* Color space and gamma conversion */
  651.     METHOD(void, colorin_init, (compress_info_ptr cinfo));
  652.     METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
  653.                        int rows_to_read,
  654.                        JSAMPIMAGE image_data));
  655.     METHOD(void, colorin_term, (compress_info_ptr cinfo));
  656.     /* Expand picture data at edges */
  657.     METHOD(void, edge_expand, (compress_info_ptr cinfo,
  658.                    long input_cols, int input_rows,
  659.                    long output_cols, int output_rows,
  660.                    JSAMPIMAGE image_data));
  661.     /* Subsample pixel values of a single component */
  662.     /* There can be a different subsample method for each component */
  663.     METHOD(void, subsample_init, (compress_info_ptr cinfo));
  664.     subsample_ptr subsample[MAX_COMPS_IN_SCAN];
  665.     METHOD(void, subsample_term, (compress_info_ptr cinfo));
  666.     /* Extract samples in MCU order, process & hand off to output_method */
  667.     /* The input is always exactly N MCU rows worth of data */
  668.     METHOD(void, extract_init, (compress_info_ptr cinfo));
  669.     METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
  670.                     JSAMPIMAGE image_data,
  671.                     int num_mcu_rows,
  672.                     MCU_output_method_ptr output_method));
  673.     METHOD(void, extract_term, (compress_info_ptr cinfo));
  674.     /* Entropy encoding parameter optimization */
  675.     METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
  676.                     MCU_output_caller_ptr source_method));
  677.     /* Entropy encoding */
  678.     METHOD(void, entropy_encoder_init, (compress_info_ptr cinfo));
  679.     METHOD(void, entropy_encode, (compress_info_ptr cinfo,
  680.                       JBLOCK *MCU_data));
  681.     METHOD(void, entropy_encoder_term, (compress_info_ptr cinfo));
  682.     /* JPEG file header construction */
  683.     METHOD(void, write_file_header, (compress_info_ptr cinfo));
  684.     METHOD(void, write_scan_header, (compress_info_ptr cinfo));
  685.     METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
  686.                        char *dataptr,
  687.                        int datacount));
  688.     METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
  689.     METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
  690.     /* Pipeline control */
  691.     METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
  692.     METHOD(void, entropy_output, (compress_info_ptr cinfo,
  693.                       char *dataptr,
  694.                       int datacount));
  695.     /* Overall control */
  696.     METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
  697. };
  698.  
  699. /* Methods used during JPEG decompression. */
  700.  
  701. struct decompress_methods_struct {
  702.     /* Hook for user interface to get control after reading file header */
  703.     METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
  704.     /* Hook for user interface to do progress monitoring */
  705.     METHOD(void, progress_monitor, (decompress_info_ptr cinfo,
  706.                     long loopcounter, long looplimit));
  707.     /* JPEG file scanning */
  708.     METHOD(void, read_file_header, (decompress_info_ptr cinfo));
  709.     METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
  710.     METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
  711.     METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
  712.     METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
  713.     /* Entropy decoding */
  714.     METHOD(void, entropy_decoder_init, (decompress_info_ptr cinfo));
  715.     METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
  716.                       JBLOCK *MCU_data));
  717.     METHOD(void, entropy_decoder_term, (decompress_info_ptr cinfo));
  718.     /* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
  719.     /* The reverse_DCT step is in the same module for symmetry reasons */
  720.     METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
  721.     METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
  722.                        JBLOCKIMAGE image_data));
  723.     METHOD(void, reverse_DCT, (decompress_info_ptr cinfo,
  724.                    JBLOCKIMAGE coeff_data,
  725.                    JSAMPIMAGE output_data, int start_row));
  726.     METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
  727.     /* Cross-block smoothing */
  728.     METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
  729.                        jpeg_component_info *compptr,
  730.                        JBLOCKROW above,
  731.                        JBLOCKROW currow,
  732.                        JBLOCKROW below,
  733.                        JBLOCKROW output));
  734.     /* Un-subsample pixel values of a single component */
  735.     /* There can be a different unsubsample method for each component */
  736.     METHOD(void, unsubsample_init, (decompress_info_ptr cinfo));
  737.     unsubsample_ptr unsubsample[MAX_COMPS_IN_SCAN];
  738.     METHOD(void, unsubsample_term, (decompress_info_ptr cinfo));
  739.     /* Color space and gamma conversion */
  740.     METHOD(void, colorout_init, (decompress_info_ptr cinfo));
  741.     METHOD(void, color_convert, (decompress_info_ptr cinfo,
  742.                      int num_rows, long num_cols,
  743.                      JSAMPIMAGE input_data,
  744.                      JSAMPIMAGE output_data));
  745.     METHOD(void, colorout_term, (decompress_info_ptr cinfo));
  746.     /* Color quantization */
  747.     METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
  748.     METHOD(void, color_quantize, (decompress_info_ptr cinfo,
  749.                       int num_rows,
  750.                       JSAMPIMAGE input_data,
  751.                       JSAMPARRAY output_data));
  752.     METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
  753.                        int num_rows,
  754.                        JSAMPIMAGE image_data,
  755.                        JSAMPARRAY workspace));
  756.     METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
  757.                     quantize_caller_ptr source_method));
  758.     METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
  759.     /* Output image writing */
  760.     METHOD(void, output_init, (decompress_info_ptr cinfo));
  761.     METHOD(void, put_color_map, (decompress_info_ptr cinfo,
  762.                      int num_colors, JSAMPARRAY colormap));
  763.     METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
  764.                       int num_rows,
  765.                       JSAMPIMAGE pixel_data));
  766.     METHOD(void, output_term, (decompress_info_ptr cinfo));
  767.     /* Pipeline control */
  768.     METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
  769.     /* Overall control */
  770.     METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
  771. };
  772.  
  773.  
  774. /* External declarations for routines that aren't called via method ptrs. */
  775. /* Note: use "j" as first char of names to minimize namespace pollution. */
  776. /* The PP macro hides prototype parameters from compilers that can't cope. */
  777.  
  778. #ifdef PROTO
  779. #define PP(arglist)    arglist
  780. #else
  781. #define PP(arglist)    ()
  782. #endif
  783.  
  784.  
  785. /* main entry for compression */
  786. EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
  787.  
  788. /* default parameter setup for compression */
  789. EXTERN void j_c_defaults PP((compress_info_ptr cinfo, int quality,
  790.                  boolean force_baseline));
  791. EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
  792. EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
  793.                   boolean force_baseline));
  794.  
  795. /* main entry for decompression */
  796. EXTERN void jpeg_decompress PP((decompress_info_ptr cinfo));
  797.  
  798. /* default parameter setup for decompression */
  799. EXTERN void j_d_defaults PP((decompress_info_ptr cinfo,
  800.                  boolean standard_buffering));
  801.  
  802. /* forward DCT */
  803. EXTERN void j_fwd_dct PP((DCTBLOCK data));
  804. /* inverse DCT */
  805. EXTERN void j_rev_dct PP((DCTBLOCK data));
  806.  
  807. /* utility routines in jutils.c */
  808. EXTERN long jround_up PP((long a, long b));
  809. EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
  810.                   JSAMPARRAY output_array, int dest_row,
  811.                   int num_rows, long num_cols));
  812. EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
  813.                 long num_blocks));
  814. EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
  815.  
  816. /* method selection routines for compression modules */
  817. EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
  818. EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
  819. EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
  820. EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
  821. EXTERN void jselsubsample PP((compress_info_ptr cinfo)); /* jcsample.c */
  822. EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
  823. EXTERN void jselccolor PP((compress_info_ptr cinfo));    /* jccolor.c */
  824. /* The user interface should call one of these to select input format: */
  825. EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
  826. EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
  827. EXTERN void jselrrle PP((compress_info_ptr cinfo)); /* jrdrle.c */
  828. EXTERN void jselrtarga PP((compress_info_ptr cinfo)); /* jrdtarga.c */
  829. /* and one of these to select output header format: */
  830. EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
  831.  
  832. /* method selection routines for decompression modules */
  833. EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
  834. EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
  835. EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
  836. EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
  837. EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
  838. EXTERN void jselunsubsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
  839. EXTERN void jseldcolor PP((decompress_info_ptr cinfo));    /* jdcolor.c */
  840. EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
  841. EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
  842. /* The user interface should call one of these to select input format: */
  843. EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
  844. /* and one of these to select output image format: */
  845. EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
  846. EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
  847. EXTERN void jselwrle PP((decompress_info_ptr cinfo)); /* jwrrle.c */
  848. EXTERN void jselwtarga PP((decompress_info_ptr cinfo)); /* jwrtarga.c */
  849.  
  850. /* method selection routines for system-dependent modules */
  851. EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
  852. EXTERN void jselmemmgr PP((external_methods_ptr emethods)); /* jmemmgr.c */
  853.  
  854.  
  855. /* We assume that right shift corresponds to signed division by 2 with
  856.  * rounding towards minus infinity.  This is correct for typical "arithmetic
  857.  * shift" instructions that shift in copies of the sign bit.  But some
  858.  * C compilers implement >> with an unsigned shift.  For these machines you
  859.  * must define RIGHT_SHIFT_IS_UNSIGNED.
  860.  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  861.  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
  862.  * included in the variables of any routine using RIGHT_SHIFT.
  863.  */
  864.  
  865. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  866. #define SHIFT_TEMPS    INT32 shift_temp;
  867. #define RIGHT_SHIFT(x,shft)  \
  868.     ((shift_temp = (x)) < 0 ? \
  869.      (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  870.      (shift_temp >> (shft)))
  871. #else
  872. #define SHIFT_TEMPS
  873. #define RIGHT_SHIFT(x,shft)    ((x) >> (shft))
  874. #endif
  875.  
  876.  
  877. /* Miscellaneous useful macros */
  878.  
  879. #undef MAX
  880. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  881. #undef MIN
  882. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  883.  
  884.  
  885. #define RST0    0xD0        /* RST0 marker code */
  886.