Imported Upstream version 1.4.2
[platform/upstream/libjpeg-turbo.git] / jpegint.h
1 /*
2  * jpegint.h
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1991-1997, Thomas G. Lane.
6  * Modified 1997-2009 by Guido Vollbeding.
7  * libjpeg-turbo Modifications:
8  * Copyright (C) 2015, D. R. Commander
9  * 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
19 typedef 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 */
25 } J_BUF_MODE;
26
27 /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
28 #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 */
43
44
45 /*
46  * Left shift macro that handles a negative operand without causing any
47  * sanitizer warnings
48  */
49
50 #ifdef __INT32_IS_ACTUALLY_LONG
51 #define LEFT_SHIFT(a, b) ((INT32)((unsigned long)(a) << (b)))
52 #else
53 #define LEFT_SHIFT(a, b) ((INT32)((unsigned int)(a) << (b)))
54 #endif
55
56
57 /* Declarations for compression modules */
58
59 /* Master control module */
60 struct jpeg_comp_master {
61   void (*prepare_for_pass) (j_compress_ptr cinfo);
62   void (*pass_startup) (j_compress_ptr cinfo);
63   void (*finish_pass) (j_compress_ptr cinfo);
64
65   /* State variables made visible to other modules */
66   boolean call_pass_startup;    /* True if pass_startup must be called */
67   boolean is_last_pass;         /* True during last pass */
68 };
69
70 /* Main buffer control (downsampled-data buffer) */
71 struct jpeg_c_main_controller {
72   void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
73   void (*process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
74                         JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail);
75 };
76
77 /* Compression preprocessing (downsampling input buffer control) */
78 struct jpeg_c_prep_controller {
79   void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
80   void (*pre_process_data) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
81                             JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail,
82                             JSAMPIMAGE output_buf,
83                             JDIMENSION *out_row_group_ctr,
84                             JDIMENSION out_row_groups_avail);
85 };
86
87 /* Coefficient buffer control */
88 struct jpeg_c_coef_controller {
89   void (*start_pass) (j_compress_ptr cinfo, J_BUF_MODE pass_mode);
90   boolean (*compress_data) (j_compress_ptr cinfo, JSAMPIMAGE input_buf);
91 };
92
93 /* Colorspace conversion */
94 struct jpeg_color_converter {
95   void (*start_pass) (j_compress_ptr cinfo);
96   void (*color_convert) (j_compress_ptr cinfo, JSAMPARRAY input_buf,
97                          JSAMPIMAGE output_buf, JDIMENSION output_row,
98                          int num_rows);
99 };
100
101 /* Downsampling */
102 struct jpeg_downsampler {
103   void (*start_pass) (j_compress_ptr cinfo);
104   void (*downsample) (j_compress_ptr cinfo, JSAMPIMAGE input_buf,
105                       JDIMENSION in_row_index, JSAMPIMAGE output_buf,
106                       JDIMENSION out_row_group_index);
107
108   boolean need_context_rows;    /* TRUE if need rows above & below */
109 };
110
111 /* Forward DCT (also controls coefficient quantization) */
112 struct jpeg_forward_dct {
113   void (*start_pass) (j_compress_ptr cinfo);
114   /* perhaps this should be an array??? */
115   void (*forward_DCT) (j_compress_ptr cinfo, jpeg_component_info * compptr,
116                        JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
117                        JDIMENSION start_row, JDIMENSION start_col,
118                        JDIMENSION num_blocks);
119 };
120
121 /* Entropy encoding */
122 struct jpeg_entropy_encoder {
123   void (*start_pass) (j_compress_ptr cinfo, boolean gather_statistics);
124   boolean (*encode_mcu) (j_compress_ptr cinfo, JBLOCKROW *MCU_data);
125   void (*finish_pass) (j_compress_ptr cinfo);
126 };
127
128 /* Marker writing */
129 struct jpeg_marker_writer {
130   void (*write_file_header) (j_compress_ptr cinfo);
131   void (*write_frame_header) (j_compress_ptr cinfo);
132   void (*write_scan_header) (j_compress_ptr cinfo);
133   void (*write_file_trailer) (j_compress_ptr cinfo);
134   void (*write_tables_only) (j_compress_ptr cinfo);
135   /* These routines are exported to allow insertion of extra markers */
136   /* Probably only COM and APPn markers should be written this way */
137   void (*write_marker_header) (j_compress_ptr cinfo, int marker,
138                                unsigned int datalen);
139   void (*write_marker_byte) (j_compress_ptr cinfo, int val);
140 };
141
142
143 /* Declarations for decompression modules */
144
145 /* Master control module */
146 struct jpeg_decomp_master {
147   void (*prepare_for_output_pass) (j_decompress_ptr cinfo);
148   void (*finish_output_pass) (j_decompress_ptr cinfo);
149
150   /* State variables made visible to other modules */
151   boolean is_dummy_pass;        /* True during 1st pass for 2-pass quant */
152 };
153
154 /* Input control module */
155 struct jpeg_input_controller {
156   int (*consume_input) (j_decompress_ptr cinfo);
157   void (*reset_input_controller) (j_decompress_ptr cinfo);
158   void (*start_input_pass) (j_decompress_ptr cinfo);
159   void (*finish_input_pass) (j_decompress_ptr cinfo);
160
161   /* State variables made visible to other modules */
162   boolean has_multiple_scans;   /* True if file has multiple scans */
163   boolean eoi_reached;          /* True when EOI has been consumed */
164 };
165
166 /* Main buffer control (downsampled-data buffer) */
167 struct jpeg_d_main_controller {
168   void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
169   void (*process_data) (j_decompress_ptr cinfo, JSAMPARRAY output_buf,
170                         JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
171 };
172
173 /* Coefficient buffer control */
174 struct jpeg_d_coef_controller {
175   void (*start_input_pass) (j_decompress_ptr cinfo);
176   int (*consume_data) (j_decompress_ptr cinfo);
177   void (*start_output_pass) (j_decompress_ptr cinfo);
178   int (*decompress_data) (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
179   /* Pointer to array of coefficient virtual arrays, or NULL if none */
180   jvirt_barray_ptr *coef_arrays;
181 };
182
183 /* Decompression postprocessing (color quantization buffer control) */
184 struct jpeg_d_post_controller {
185   void (*start_pass) (j_decompress_ptr cinfo, J_BUF_MODE pass_mode);
186   void (*post_process_data) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
187                              JDIMENSION *in_row_group_ctr,
188                              JDIMENSION in_row_groups_avail,
189                              JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
190                              JDIMENSION out_rows_avail);
191 };
192
193 /* Marker reading & parsing */
194 struct jpeg_marker_reader {
195   void (*reset_marker_reader) (j_decompress_ptr cinfo);
196   /* Read markers until SOS or EOI.
197    * Returns same codes as are defined for jpeg_consume_input:
198    * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
199    */
200   int (*read_markers) (j_decompress_ptr cinfo);
201   /* Read a restart marker --- exported for use by entropy decoder only */
202   jpeg_marker_parser_method read_restart_marker;
203
204   /* State of marker reader --- nominally internal, but applications
205    * supplying COM or APPn handlers might like to know the state.
206    */
207   boolean saw_SOI;              /* found SOI? */
208   boolean saw_SOF;              /* found SOF? */
209   int next_restart_num;         /* next restart number expected (0-7) */
210   unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
211 };
212
213 /* Entropy decoding */
214 struct jpeg_entropy_decoder {
215   void (*start_pass) (j_decompress_ptr cinfo);
216   boolean (*decode_mcu) (j_decompress_ptr cinfo, JBLOCKROW *MCU_data);
217
218   /* This is here to share code between baseline and progressive decoders; */
219   /* other modules probably should not use it */
220   boolean insufficient_data;    /* set TRUE after emitting warning */
221 };
222
223 /* Inverse DCT (also performs dequantization) */
224 typedef void (*inverse_DCT_method_ptr) (j_decompress_ptr cinfo,
225                                         jpeg_component_info * compptr,
226                                         JCOEFPTR coef_block,
227                                         JSAMPARRAY output_buf,
228                                         JDIMENSION output_col);
229
230 struct jpeg_inverse_dct {
231   void (*start_pass) (j_decompress_ptr cinfo);
232   /* It is useful to allow each component to have a separate IDCT method. */
233   inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
234 };
235
236 /* Upsampling (note that upsampler must also call color converter) */
237 struct jpeg_upsampler {
238   void (*start_pass) (j_decompress_ptr cinfo);
239   void (*upsample) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
240                     JDIMENSION *in_row_group_ctr,
241                     JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
242                     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);
243
244   boolean need_context_rows;    /* TRUE if need rows above & below */
245 };
246
247 /* Colorspace conversion */
248 struct jpeg_color_deconverter {
249   void (*start_pass) (j_decompress_ptr cinfo);
250   void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
251                          JDIMENSION input_row, JSAMPARRAY output_buf,
252                          int num_rows);
253 };
254
255 /* Color quantization or color precision reduction */
256 struct jpeg_color_quantizer {
257   void (*start_pass) (j_decompress_ptr cinfo, boolean is_pre_scan);
258   void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
259                           JSAMPARRAY output_buf, int num_rows);
260   void (*finish_pass) (j_decompress_ptr cinfo);
261   void (*new_color_map) (j_decompress_ptr cinfo);
262 };
263
264
265 /* Miscellaneous useful macros */
266
267 #undef MAX
268 #define MAX(a,b)        ((a) > (b) ? (a) : (b))
269 #undef MIN
270 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
271
272
273 /* We assume that right shift corresponds to signed division by 2 with
274  * rounding towards minus infinity.  This is correct for typical "arithmetic
275  * shift" instructions that shift in copies of the sign bit.  But some
276  * C compilers implement >> with an unsigned shift.  For these machines you
277  * must define RIGHT_SHIFT_IS_UNSIGNED.
278  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
279  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
280  * included in the variables of any routine using RIGHT_SHIFT.
281  */
282
283 #ifdef RIGHT_SHIFT_IS_UNSIGNED
284 #define SHIFT_TEMPS     INT32 shift_temp;
285 #define RIGHT_SHIFT(x,shft)  \
286         ((shift_temp = (x)) < 0 ? \
287          (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
288          (shift_temp >> (shft)))
289 #else
290 #define SHIFT_TEMPS
291 #define RIGHT_SHIFT(x,shft)     ((x) >> (shft))
292 #endif
293
294
295 /* Compression module initialization routines */
296 EXTERN(void) jinit_compress_master (j_compress_ptr cinfo);
297 EXTERN(void) jinit_c_master_control (j_compress_ptr cinfo,
298                                      boolean transcode_only);
299 EXTERN(void) jinit_c_main_controller (j_compress_ptr cinfo,
300                                       boolean need_full_buffer);
301 EXTERN(void) jinit_c_prep_controller (j_compress_ptr cinfo,
302                                       boolean need_full_buffer);
303 EXTERN(void) jinit_c_coef_controller (j_compress_ptr cinfo,
304                                       boolean need_full_buffer);
305 EXTERN(void) jinit_color_converter (j_compress_ptr cinfo);
306 EXTERN(void) jinit_downsampler (j_compress_ptr cinfo);
307 EXTERN(void) jinit_forward_dct (j_compress_ptr cinfo);
308 EXTERN(void) jinit_huff_encoder (j_compress_ptr cinfo);
309 EXTERN(void) jinit_phuff_encoder (j_compress_ptr cinfo);
310 EXTERN(void) jinit_arith_encoder (j_compress_ptr cinfo);
311 EXTERN(void) jinit_marker_writer (j_compress_ptr cinfo);
312 /* Decompression module initialization routines */
313 EXTERN(void) jinit_master_decompress (j_decompress_ptr cinfo);
314 EXTERN(void) jinit_d_main_controller (j_decompress_ptr cinfo,
315                                       boolean need_full_buffer);
316 EXTERN(void) jinit_d_coef_controller (j_decompress_ptr cinfo,
317                                       boolean need_full_buffer);
318 EXTERN(void) jinit_d_post_controller (j_decompress_ptr cinfo,
319                                       boolean need_full_buffer);
320 EXTERN(void) jinit_input_controller (j_decompress_ptr cinfo);
321 EXTERN(void) jinit_marker_reader (j_decompress_ptr cinfo);
322 EXTERN(void) jinit_huff_decoder (j_decompress_ptr cinfo);
323 EXTERN(void) jinit_phuff_decoder (j_decompress_ptr cinfo);
324 EXTERN(void) jinit_arith_decoder (j_decompress_ptr cinfo);
325 EXTERN(void) jinit_inverse_dct (j_decompress_ptr cinfo);
326 EXTERN(void) jinit_upsampler (j_decompress_ptr cinfo);
327 EXTERN(void) jinit_color_deconverter (j_decompress_ptr cinfo);
328 EXTERN(void) jinit_1pass_quantizer (j_decompress_ptr cinfo);
329 EXTERN(void) jinit_2pass_quantizer (j_decompress_ptr cinfo);
330 EXTERN(void) jinit_merged_upsampler (j_decompress_ptr cinfo);
331 /* Memory manager initialization */
332 EXTERN(void) jinit_memory_mgr (j_common_ptr cinfo);
333
334 /* Utility routines in jutils.c */
335 EXTERN(long) jdiv_round_up (long a, long b);
336 EXTERN(long) jround_up (long a, long b);
337 EXTERN(void) jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
338                                 JSAMPARRAY output_array, int dest_row,
339                                 int num_rows, JDIMENSION num_cols);
340 EXTERN(void) jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
341                               JDIMENSION num_blocks);
342 EXTERN(void) jzero_far (void * target, size_t bytestozero);
343 /* Constant tables in jutils.c */
344 #if 0                           /* This table is not actually needed in v6a */
345 extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
346 #endif
347 extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
348
349 /* Arithmetic coding probability estimation tables in jaricom.c */
350 extern const INT32 jpeg_aritab[];
351
352 /* Suppress undefined-structure complaints if necessary. */
353
354 #ifdef INCOMPLETE_TYPES_BROKEN
355 #ifndef AM_MEMORY_MANAGER       /* only jmemmgr.c defines these */
356 struct jvirt_sarray_control { long dummy; };
357 struct jvirt_barray_control { long dummy; };
358 #endif
359 #endif /* INCOMPLETE_TYPES_BROKEN */