blob: 025accd91cf06cac7d8c1139ba829845b16dc8b6 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jpegint.h
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00006 * Modified 1997-2009 by Guido Vollbeding.
DRC5033f3e2014-05-18 18:33:44 +00007 * It was modified by The libjpeg-turbo Project to include only code relevant
8 * to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file provides common declarations for the various JPEG modules.
12 * These declarations are considered internal to the JPEG library; most
13 * applications using the library shouldn't need to include this file.
14 */
15
16
17/* Declarations for both compression & decompression */
18
DRC333e9182014-05-12 00:34:08 +000019typedef enum { /* Operating modes for buffer controllers */
20 JBUF_PASS_THRU, /* Plain stripwise operation */
21 /* Remaining modes require a full-image buffer to have been created */
22 JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
23 JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
24 JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025} J_BUF_MODE;
26
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
DRCb7753512014-05-11 09:36:25 +000028#define CSTATE_START 100 /* after create_compress */
29#define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
30#define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
31#define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
32#define DSTATE_START 200 /* after create_decompress */
33#define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
34#define DSTATE_READY 202 /* found SOS, ready for start_decompress */
35#define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
36#define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
37#define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
38#define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
39#define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
40#define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
41#define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
42#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000043
44
45/* Declarations for compression modules */
46
47/* Master control module */
48struct jpeg_comp_master {
DRCbc56b752014-05-16 10:43:44 +000049 void (*prepare_for_pass) (j_compress_ptr cinfo);
50 void (*pass_startup) (j_compress_ptr cinfo);
51 void (*finish_pass) (j_compress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052
53 /* State variables made visible to other modules */
DRCb7753512014-05-11 09:36:25 +000054 boolean call_pass_startup; /* True if pass_startup must be called */
55 boolean is_last_pass; /* True during last pass */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056};
57
58/* Main buffer control (downsampled-data buffer) */
59struct jpeg_c_main_controller {
DRCbc56b752014-05-16 10:43:44 +000060 void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
61 void (*process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
62 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063};
64
65/* Compression preprocessing (downsampling input buffer control) */
66struct jpeg_c_prep_controller {
DRCbc56b752014-05-16 10:43:44 +000067 void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
68 void (*pre_process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
69 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail,
70 JSAMPIMAGE output_buf,
71 JDIMENSION *out_row_group_ctr,
72 JDIMENSION out_row_groups_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073};
74
75/* Coefficient buffer control */
76struct jpeg_c_coef_controller {
DRCbc56b752014-05-16 10:43:44 +000077 void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
78 boolean (*compress_data) (j_compress_ptr cinfo, JSAMPIMAGE input_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079};
80
81/* Colorspace conversion */
82struct jpeg_color_converter {
DRCbc56b752014-05-16 10:43:44 +000083 void (*start_pass) (j_compress_ptr cinfo);
84 void (*color_convert) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
85 JSAMPIMAGE output_buf, JDIMENSION output_row,
86 int num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087};
88
89/* Downsampling */
90struct jpeg_downsampler {
DRCbc56b752014-05-16 10:43:44 +000091 void (*start_pass) (j_compress_ptr cinfo);
92 void (*downsample) (j_compress_ptr cinfo, JSAMPIMAGE input_buf,
93 JDIMENSION in_row_index, JSAMPIMAGE output_buf,
94 JDIMENSION out_row_group_index);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095
DRCb7753512014-05-11 09:36:25 +000096 boolean need_context_rows; /* TRUE if need rows above & below */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097};
98
99/* Forward DCT (also controls coefficient quantization) */
100struct jpeg_forward_dct {
DRCbc56b752014-05-16 10:43:44 +0000101 void (*start_pass) (j_compress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102 /* perhaps this should be an array??? */
DRCbc56b752014-05-16 10:43:44 +0000103 void (*forward_DCT) (j_compress_ptr cinfo, jpeg_component_info * compptr,
104 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
105 JDIMENSION start_row, JDIMENSION start_col,
106 JDIMENSION num_blocks);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107};
108
109/* Entropy encoding */
110struct jpeg_entropy_encoder {
DRCbc56b752014-05-16 10:43:44 +0000111 void (*start_pass) (j_compress_ptr cinfo, boolean gather_statistics);
112 boolean (*encode_mcu) (j_compress_ptr cinfo, JBLOCKROW *MCU_data);
113 void (*finish_pass) (j_compress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114};
115
116/* Marker writing */
117struct jpeg_marker_writer {
DRCbc56b752014-05-16 10:43:44 +0000118 void (*write_file_header) (j_compress_ptr cinfo);
119 void (*write_frame_header) (j_compress_ptr cinfo);
120 void (*write_scan_header) (j_compress_ptr cinfo);
121 void (*write_file_trailer) (j_compress_ptr cinfo);
122 void (*write_tables_only) (j_compress_ptr cinfo);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000123 /* These routines are exported to allow insertion of extra markers */
124 /* Probably only COM and APPn markers should be written this way */
DRCbc56b752014-05-16 10:43:44 +0000125 void (*write_marker_header) (j_compress_ptr cinfo, int marker,
126 unsigned int datalen);
127 void (*write_marker_byte) (j_compress_ptr cinfo, int val);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128};
129
130
131/* Declarations for decompression modules */
132
133/* Master control module */
134struct jpeg_decomp_master {
DRCbc56b752014-05-16 10:43:44 +0000135 void (*prepare_for_output_pass) (j_decompress_ptr cinfo);
136 void (*finish_output_pass) (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137
138 /* State variables made visible to other modules */
DRCb7753512014-05-11 09:36:25 +0000139 boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000140};
141
142/* Input control module */
143struct jpeg_input_controller {
DRCbc56b752014-05-16 10:43:44 +0000144 int (*consume_input) (j_decompress_ptr cinfo);
145 void (*reset_input_controller) (j_decompress_ptr cinfo);
146 void (*start_input_pass) (j_decompress_ptr cinfo);
147 void (*finish_input_pass) (j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000148
149 /* State variables made visible to other modules */
DRCb7753512014-05-11 09:36:25 +0000150 boolean has_multiple_scans; /* True if file has multiple scans */
151 boolean eoi_reached; /* True when EOI has been consumed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152};
153
154/* Main buffer control (downsampled-data buffer) */
155struct jpeg_d_main_controller {
DRCbc56b752014-05-16 10:43:44 +0000156 void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
157 void (*process_data) (j_decompress_ptr cinfo, JSAMPARRAY output_buf,
158 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000159};
160
161/* Coefficient buffer control */
162struct jpeg_d_coef_controller {
DRCbc56b752014-05-16 10:43:44 +0000163 void (*start_input_pass) (j_decompress_ptr cinfo);
164 int (*consume_data) (j_decompress_ptr cinfo);
165 void (*start_output_pass) (j_decompress_ptr cinfo);
166 int (*decompress_data) (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000167 /* Pointer to array of coefficient virtual arrays, or NULL if none */
168 jvirt_barray_ptr *coef_arrays;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169};
170
171/* Decompression postprocessing (color quantization buffer control) */
172struct jpeg_d_post_controller {
DRCbc56b752014-05-16 10:43:44 +0000173 void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
174 void (*post_process_data) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
175 JDIMENSION *in_row_group_ctr,
176 JDIMENSION in_row_groups_avail,
177 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
178 JDIMENSION out_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179};
180
181/* Marker reading & parsing */
182struct jpeg_marker_reader {
DRCbc56b752014-05-16 10:43:44 +0000183 void (*reset_marker_reader) (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184 /* Read markers until SOS or EOI.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000185 * Returns same codes as are defined for jpeg_consume_input:
186 * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 */
DRCbc56b752014-05-16 10:43:44 +0000188 int (*read_markers) (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 /* Read a restart marker --- exported for use by entropy decoder only */
190 jpeg_marker_parser_method read_restart_marker;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191
192 /* State of marker reader --- nominally internal, but applications
193 * supplying COM or APPn handlers might like to know the state.
194 */
DRCb7753512014-05-11 09:36:25 +0000195 boolean saw_SOI; /* found SOI? */
196 boolean saw_SOF; /* found SOF? */
197 int next_restart_num; /* next restart number expected (0-7) */
198 unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000199};
200
201/* Entropy decoding */
202struct jpeg_entropy_decoder {
DRCbc56b752014-05-16 10:43:44 +0000203 void (*start_pass) (j_decompress_ptr cinfo);
204 boolean (*decode_mcu) (j_decompress_ptr cinfo, JBLOCKROW *MCU_data);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000205
206 /* This is here to share code between baseline and progressive decoders; */
207 /* other modules probably should not use it */
DRCb7753512014-05-11 09:36:25 +0000208 boolean insufficient_data; /* set TRUE after emitting warning */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209};
210
211/* Inverse DCT (also performs dequantization) */
DRCbc56b752014-05-16 10:43:44 +0000212typedef void (*inverse_DCT_method_ptr) (j_decompress_ptr cinfo,
213 jpeg_component_info * compptr,
214 JCOEFPTR coef_block,
215 JSAMPARRAY output_buf,
216 JDIMENSION output_col);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217
218struct jpeg_inverse_dct {
DRCbc56b752014-05-16 10:43:44 +0000219 void (*start_pass) (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 /* It is useful to allow each component to have a separate IDCT method. */
221 inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
222};
223
224/* Upsampling (note that upsampler must also call color converter) */
225struct jpeg_upsampler {
DRCbc56b752014-05-16 10:43:44 +0000226 void (*start_pass) (j_decompress_ptr cinfo);
227 void (*upsample) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
228 JDIMENSION *in_row_group_ctr,
229 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
230 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231
DRCb7753512014-05-11 09:36:25 +0000232 boolean need_context_rows; /* TRUE if need rows above & below */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233};
234
235/* Colorspace conversion */
236struct jpeg_color_deconverter {
DRCbc56b752014-05-16 10:43:44 +0000237 void (*start_pass) (j_decompress_ptr cinfo);
238 void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
239 JDIMENSION input_row, JSAMPARRAY output_buf,
240 int num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241};
242
243/* Color quantization or color precision reduction */
244struct jpeg_color_quantizer {
DRCbc56b752014-05-16 10:43:44 +0000245 void (*start_pass) (j_decompress_ptr cinfo, boolean is_pre_scan);
246 void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
247 JSAMPARRAY output_buf, int num_rows);
248 void (*finish_pass) (j_decompress_ptr cinfo);
249 void (*new_color_map) (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250};
251
252
253/* Miscellaneous useful macros */
254
255#undef MAX
DRCb7753512014-05-11 09:36:25 +0000256#define MAX(a,b) ((a) > (b) ? (a) : (b))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257#undef MIN
DRCb7753512014-05-11 09:36:25 +0000258#define MIN(a,b) ((a) < (b) ? (a) : (b))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000259
260
261/* We assume that right shift corresponds to signed division by 2 with
262 * rounding towards minus infinity. This is correct for typical "arithmetic
263 * shift" instructions that shift in copies of the sign bit. But some
264 * C compilers implement >> with an unsigned shift. For these machines you
265 * must define RIGHT_SHIFT_IS_UNSIGNED.
266 * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
267 * It is only applied with constant shift counts. SHIFT_TEMPS must be
268 * included in the variables of any routine using RIGHT_SHIFT.
269 */
270
271#ifdef RIGHT_SHIFT_IS_UNSIGNED
DRCb7753512014-05-11 09:36:25 +0000272#define SHIFT_TEMPS INT32 shift_temp;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000273#define RIGHT_SHIFT(x,shft) \
DRCb7753512014-05-11 09:36:25 +0000274 ((shift_temp = (x)) < 0 ? \
275 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
276 (shift_temp >> (shft)))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277#else
278#define SHIFT_TEMPS
DRCb7753512014-05-11 09:36:25 +0000279#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280#endif
281
282
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283/* Compression module initialization routines */
DRCbc56b752014-05-16 10:43:44 +0000284EXTERN(void) jinit_compress_master (j_compress_ptr cinfo);
285EXTERN(void) jinit_c_master_control (j_compress_ptr cinfo,
286 boolean transcode_only);
287EXTERN(void) jinit_c_main_controller (j_compress_ptr cinfo,
288 boolean need_full_buffer);
289EXTERN(void) jinit_c_prep_controller (j_compress_ptr cinfo,
290 boolean need_full_buffer);
291EXTERN(void) jinit_c_coef_controller (j_compress_ptr cinfo,
292 boolean need_full_buffer);
293EXTERN(void) jinit_color_converter (j_compress_ptr cinfo);
294EXTERN(void) jinit_downsampler (j_compress_ptr cinfo);
295EXTERN(void) jinit_forward_dct (j_compress_ptr cinfo);
296EXTERN(void) jinit_huff_encoder (j_compress_ptr cinfo);
297EXTERN(void) jinit_phuff_encoder (j_compress_ptr cinfo);
298EXTERN(void) jinit_arith_encoder (j_compress_ptr cinfo);
299EXTERN(void) jinit_marker_writer (j_compress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300/* Decompression module initialization routines */
DRCbc56b752014-05-16 10:43:44 +0000301EXTERN(void) jinit_master_decompress (j_decompress_ptr cinfo);
302EXTERN(void) jinit_d_main_controller (j_decompress_ptr cinfo,
303 boolean need_full_buffer);
304EXTERN(void) jinit_d_coef_controller (j_decompress_ptr cinfo,
305 boolean need_full_buffer);
306EXTERN(void) jinit_d_post_controller (j_decompress_ptr cinfo,
307 boolean need_full_buffer);
308EXTERN(void) jinit_input_controller (j_decompress_ptr cinfo);
309EXTERN(void) jinit_marker_reader (j_decompress_ptr cinfo);
310EXTERN(void) jinit_huff_decoder (j_decompress_ptr cinfo);
311EXTERN(void) jinit_phuff_decoder (j_decompress_ptr cinfo);
312EXTERN(void) jinit_arith_decoder (j_decompress_ptr cinfo);
313EXTERN(void) jinit_inverse_dct (j_decompress_ptr cinfo);
314EXTERN(void) jinit_upsampler (j_decompress_ptr cinfo);
315EXTERN(void) jinit_color_deconverter (j_decompress_ptr cinfo);
316EXTERN(void) jinit_1pass_quantizer (j_decompress_ptr cinfo);
317EXTERN(void) jinit_2pass_quantizer (j_decompress_ptr cinfo);
318EXTERN(void) jinit_merged_upsampler (j_decompress_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000319/* Memory manager initialization */
DRCbc56b752014-05-16 10:43:44 +0000320EXTERN(void) jinit_memory_mgr (j_common_ptr cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000321
322/* Utility routines in jutils.c */
DRCbc56b752014-05-16 10:43:44 +0000323EXTERN(long) jdiv_round_up (long a, long b);
324EXTERN(long) jround_up (long a, long b);
325EXTERN(void) jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
326 JSAMPARRAY output_array, int dest_row,
327 int num_rows, JDIMENSION num_cols);
328EXTERN(void) jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
329 JDIMENSION num_blocks);
DRC5033f3e2014-05-18 18:33:44 +0000330EXTERN(void) jzero_far (void * target, size_t bytestozero);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000331/* Constant tables in jutils.c */
DRCb7753512014-05-11 09:36:25 +0000332#if 0 /* This table is not actually needed in v6a */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000333extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000334#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000335extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336
Guido Vollbeding989630f2010-01-10 00:00:00 +0000337/* Arithmetic coding probability estimation tables in jaricom.c */
338extern const INT32 jpeg_aritab[];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000339
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000340/* Suppress undefined-structure complaints if necessary. */
341
342#ifdef INCOMPLETE_TYPES_BROKEN
DRCb7753512014-05-11 09:36:25 +0000343#ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344struct jvirt_sarray_control { long dummy; };
345struct jvirt_barray_control { long dummy; };
346#endif
347#endif /* INCOMPLETE_TYPES_BROKEN */