1 /*
2  * jpegint.h
3  *
4  * Copyright (C) 1991-1997, 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 provides common declarations for the various JPEG modules.
9  * These declarations are considered internal to the JPEG library; most
10  * applications using the library shouldn't need to include this file.
11  */
12 
13 
14 /* Declarations for both compression & decompression */
15 
16 typedef enum {			/* Operating modes for buffer controllers */
17 	JBUF_PASS_THRU,		/* Plain stripwise operation */
18 	/* Remaining modes require a full-image buffer to have been created */
19 	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
20 	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
21 	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
22 } J_BUF_MODE;
23 
24 /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
25 #define CSTATE_START	100	/* after create_compress */
26 #define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
27 #define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
28 #define CSTATE_WRCOEFS	103	/* jpeg_write_coefficients done */
29 #define DSTATE_START	200	/* after create_decompress */
30 #define DSTATE_INHEADER	201	/* reading header markers, no SOS yet */
31 #define DSTATE_READY	202	/* found SOS, ready for start_decompress */
32 #define DSTATE_PRELOAD	203	/* reading multiscan file in start_decompress*/
33 #define DSTATE_PRESCAN	204	/* performing dummy pass for 2-pass quant */
34 #define DSTATE_SCANNING	205	/* start_decompress done, read_scanlines OK */
35 //#define DSTATE_RAW_OK	206	/* start_decompress done, read_raw_data OK */
36 //#define DSTATE_BUFIMAGE	207	/* expecting jpeg_start_output */
37 #define DSTATE_BUFPOST	208	/* looking for SOS/EOI in jpeg_finish_output */
38 #define DSTATE_RDCOEFS	209	/* reading file in jpeg_read_coefficients */
39 #define DSTATE_STOPPING	210	/* looking for EOI in jpeg_finish_decompress */
40 
41 
42 /* Declarations for decompression modules */
43 
44 /* Master control module */
45 struct jpeg_decomp_master {
46   JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
47   JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
48 };
49 
50 /* Input control module */
51 struct jpeg_input_controller {
52   JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
53   JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
54   JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
55   JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
56 
57   /* State variables made visible to other modules */
58   boolean has_multiple_scans;	/* True if file has multiple scans */
59   boolean eoi_reached;		/* True when EOI has been consumed */
60 };
61 
62 /* Main buffer control (downsampled-data buffer) */
63 struct jpeg_d_main_controller {
64   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
65   JMETHOD(void, process_data, (j_decompress_ptr cinfo,
66 			       JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
67 			       JDIMENSION out_rows_avail));
68 };
69 
70 /* Coefficient buffer control */
71 struct jpeg_d_coef_controller {
72   JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
73   JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
74   JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
75   JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
76 				 JSAMPIMAGE output_buf));
77   /* Pointer to array of coefficient virtual arrays, or NULL if none */
78   jvirt_barray_ptr *coef_arrays;
79 };
80 
81 /* Decompression postprocessing (color quantization buffer control) */
82 struct jpeg_d_post_controller {
83   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
84   JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
85 				    JSAMPIMAGE input_buf,
86 				    JDIMENSION *in_row_group_ctr,
87 				    JDIMENSION in_row_groups_avail,
88 				    JSAMPARRAY output_buf,
89 				    JDIMENSION *out_row_ctr,
90 				    JDIMENSION out_rows_avail));
91 };
92 
93 /* Marker reading & parsing */
94 struct jpeg_marker_reader {
95   JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
96   /* Read markers until SOS or EOI.
97    * Returns same codes as are defined for jpeg_consume_input:
98    * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
99    */
100   JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
101   /* Read a restart marker --- exported for use by entropy decoder only */
102   jpeg_marker_parser_method read_restart_marker;
103 
104   /* State of marker reader --- nominally internal, but applications
105    * supplying COM or APPn handlers might like to know the state.
106    */
107   boolean saw_SOI;		/* found SOI? */
108   boolean saw_SOF;		/* found SOF? */
109   int next_restart_num;		/* next restart number expected (0-7) */
110   unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
111 };
112 
113 /* Entropy decoding */
114 struct jpeg_entropy_decoder {
115   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
116   JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
117 				JBLOCKROW *MCU_data));
118 
119   /* This is here to share code between baseline and progressive decoders; */
120   /* other modules probably should not use it */
121   boolean insufficient_data;	/* set TRUE after emitting warning */
122 };
123 
124 /* Inverse DCT (also performs dequantization) */
125 typedef JMETHOD(void, inverse_DCT_method_ptr,
126 		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
127 		 JCOEFPTR coef_block,
128 		 JSAMPARRAY output_buf, JDIMENSION output_col));
129 
130 struct jpeg_inverse_dct {
131   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
132   /* It is useful to allow each component to have a separate IDCT method. */
133   inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
134 };
135 
136 /* Upsampling (note that upsampler must also call color converter) */
137 struct jpeg_upsampler {
138   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
139   JMETHOD(void, upsample, (j_decompress_ptr cinfo,
140 			   JSAMPIMAGE input_buf,
141 			   JDIMENSION *in_row_group_ctr,
142 			   JDIMENSION in_row_groups_avail,
143 			   JSAMPARRAY output_buf,
144 			   JDIMENSION *out_row_ctr,
145 			   JDIMENSION out_rows_avail));
146 
147   boolean need_context_rows;	/* TRUE if need rows above & below */
148 };
149 
150 /* Colorspace conversion */
151 struct jpeg_color_deconverter {
152   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
153   JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
154 				JSAMPIMAGE input_buf, JDIMENSION input_row,
155 				JSAMPARRAY output_buf, int num_rows));
156 };
157 
158 /* Color quantization or color precision reduction */
159 struct jpeg_color_quantizer {
160   JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
161   JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
162 				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
163 				 int num_rows));
164   JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
165   JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
166 };
167 
168 
169 /* Miscellaneous useful macros */
170 
171 #undef MAX
172 #define MAX(a,b)	((a) > (b) ? (a) : (b))
173 #undef MIN
174 #define MIN(a,b)	((a) < (b) ? (a) : (b))
175 
176 
177 /* We assume that right shift corresponds to signed division by 2 with
178  * rounding towards minus infinity.  This is correct for typical "arithmetic
179  * shift" instructions that shift in copies of the sign bit.  But some
180  * C compilers implement >> with an unsigned shift.  For these machines you
181  * must define RIGHT_SHIFT_IS_UNSIGNED.
182  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
183  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
184  * included in the variables of any routine using RIGHT_SHIFT.
185  */
186 
187 #ifdef RIGHT_SHIFT_IS_UNSIGNED
188 #define SHIFT_TEMPS	INT32 shift_temp;
189 #define RIGHT_SHIFT(x,shft)  \
190 	((shift_temp = (x)) < 0 ? \
191 	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
192 	 (shift_temp >> (shft)))
193 #else
194 #define SHIFT_TEMPS
195 #define RIGHT_SHIFT(x,shft)	((x) >> (shft))
196 #endif
197 
198 
199 /* Compression module initialization routines */
200 EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
201 EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
202 					 boolean transcode_only));
203 EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
204 					  boolean need_full_buffer));
205 EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
206 					  boolean need_full_buffer));
207 EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
208 					  boolean need_full_buffer));
209 EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
210 EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
211 EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
212 EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
213 EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
214 EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
215 /* Decompression module initialization routines */
216 EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
217 EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
218 					  boolean need_full_buffer));
219 EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
220 					  boolean need_full_buffer));
221 EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
222 					  boolean need_full_buffer));
223 EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
224 EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
225 EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
226 EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
227 EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
228 EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
229 EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
230 EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
231 EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
232 EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
233 /* Memory manager initialization */
234 EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
235 
236 /* Utility routines in jutils.c */
jdiv_round_up(long a,long b)237 static INLINE long jdiv_round_up (long a, long b)
238 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
239 /* Assumes a >= 0, b > 0 */
240 {
241 	return (a + b - 1L) / b;
242 }
243 
jround_up(long a,long b)244 static INLINE long jround_up (long a, long b)
245 /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
246 /* Assumes a >= 0, b > 0 */
247 {
248 	a += b - 1L;
249 	return a - (a % b);
250 }
251 
252 EXTERN(long) jround_up JPP((long a, long b));
253 EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
254 				    JSAMPARRAY output_array, int dest_row,
255 				    int num_rows, JDIMENSION num_cols));
256 
257 /* Constant tables in jutils.c */
258 #if 0				/* This table is not actually needed in v6a */
259 extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
260 #endif
261 extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
262