8 * Copyright (C) 1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains the control logic for the lossy JPEG decompressor.
15 #define JPEG_INTERNALS
22 * Compute output image dimensions and related values.
26 calc_output_dimensions (j_decompress_ptr cinfo
)
28 #ifdef IDCT_SCALING_SUPPORTED
30 jpeg_component_info
*compptr
;
32 /* Compute actual output image dimensions and DCT scaling choices. */
33 if (cinfo
->scale_num
* 8 <= cinfo
->scale_denom
) {
34 /* Provide 1/8 scaling */
35 cinfo
->output_width
= (JDIMENSION
)
36 jdiv_round_up((long) cinfo
->image_width
, 8L);
37 cinfo
->output_height
= (JDIMENSION
)
38 jdiv_round_up((long) cinfo
->image_height
, 8L);
39 cinfo
->min_codec_data_unit
= 1;
40 } else if (cinfo
->scale_num
* 4 <= cinfo
->scale_denom
) {
41 /* Provide 1/4 scaling */
42 cinfo
->output_width
= (JDIMENSION
)
43 jdiv_round_up((long) cinfo
->image_width
, 4L);
44 cinfo
->output_height
= (JDIMENSION
)
45 jdiv_round_up((long) cinfo
->image_height
, 4L);
46 cinfo
->min_codec_data_unit
= 2;
47 } else if (cinfo
->scale_num
* 2 <= cinfo
->scale_denom
) {
48 /* Provide 1/2 scaling */
49 cinfo
->output_width
= (JDIMENSION
)
50 jdiv_round_up((long) cinfo
->image_width
, 2L);
51 cinfo
->output_height
= (JDIMENSION
)
52 jdiv_round_up((long) cinfo
->image_height
, 2L);
53 cinfo
->min_codec_data_unit
= 4;
55 /* Provide 1/1 scaling */
56 cinfo
->output_width
= cinfo
->image_width
;
57 cinfo
->output_height
= cinfo
->image_height
;
58 cinfo
->min_codec_data_unit
= DCTSIZE
;
60 /* In selecting the actual DCT scaling for each component, we try to
61 * scale up the chroma components via IDCT scaling rather than upsampling.
62 * This saves time if the upsampler gets to use 1:1 scaling.
63 * Note this code assumes that the supported DCT scalings are powers of 2.
65 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
67 int ssize
= cinfo
->min_codec_data_unit
;
68 while (ssize
< DCTSIZE
&&
69 (compptr
->h_samp_factor
* ssize
* 2 <=
70 cinfo
->max_h_samp_factor
* cinfo
->min_codec_data_unit
) &&
71 (compptr
->v_samp_factor
* ssize
* 2 <=
72 cinfo
->max_v_samp_factor
* cinfo
->min_codec_data_unit
)) {
75 compptr
->codec_data_unit
= ssize
;
78 /* Recompute downsampled dimensions of components;
79 * application needs to know these if using raw downsampled data.
81 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
83 /* Size in samples, after IDCT scaling */
84 compptr
->downsampled_width
= (JDIMENSION
)
85 jdiv_round_up((long) cinfo
->image_width
*
86 (long) (compptr
->h_samp_factor
* compptr
->codec_data_unit
),
87 (long) (cinfo
->max_h_samp_factor
* DCTSIZE
));
88 compptr
->downsampled_height
= (JDIMENSION
)
89 jdiv_round_up((long) cinfo
->image_height
*
90 (long) (compptr
->v_samp_factor
* compptr
->codec_data_unit
),
91 (long) (cinfo
->max_v_samp_factor
* DCTSIZE
));
94 #else /* !IDCT_SCALING_SUPPORTED */
96 /* Hardwire it to "no scaling" */
97 cinfo
->output_width
= cinfo
->image_width
;
98 cinfo
->output_height
= cinfo
->image_height
;
99 /* jdinput.c has already initialized codec_data_unit to DCTSIZE,
100 * and has computed unscaled downsampled_width and downsampled_height.
103 #endif /* IDCT_SCALING_SUPPORTED */
108 * Save away a copy of the Q-table referenced by each component present
109 * in the current scan, unless already saved during a prior scan.
111 * In a multiple-scan JPEG file, the encoder could assign different components
112 * the same Q-table slot number, but change table definitions between scans
113 * so that each component uses a different Q-table. (The IJG encoder is not
114 * currently capable of doing this, but other encoders might.) Since we want
115 * to be able to dequantize all the components at the end of the file, this
116 * means that we have to save away the table actually used for each component.
117 * We do this by copying the table at the start of the first scan containing
119 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
120 * slot between scans of a component using that slot. If the encoder does so
121 * anyway, this decoder will simply use the Q-table values that were current
122 * at the start of the first scan for the component.
124 * The decompressor output side looks only at the saved quant tables,
125 * not at the current Q-table slots.
129 latch_quant_tables (j_decompress_ptr cinfo
)
132 jpeg_component_info
*compptr
;
135 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
136 compptr
= cinfo
->cur_comp_info
[ci
];
137 /* No work if we already saved Q-table for this component */
138 if (compptr
->quant_table
!= NULL
)
140 /* Make sure specified quantization table is present */
141 qtblno
= compptr
->quant_tbl_no
;
142 if (qtblno
< 0 || qtblno
>= NUM_QUANT_TBLS
||
143 cinfo
->quant_tbl_ptrs
[qtblno
] == NULL
)
144 ERREXIT1(cinfo
, JERR_NO_QUANT_TABLE
, qtblno
);
145 /* OK, save away the quantization table */
146 qtbl
= (JQUANT_TBL
*)
147 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
149 MEMCOPY(qtbl
, cinfo
->quant_tbl_ptrs
[qtblno
], SIZEOF(JQUANT_TBL
));
150 compptr
->quant_table
= qtbl
;
156 * Initialize for an input processing pass.
160 start_input_pass (j_decompress_ptr cinfo
)
162 j_lossy_d_ptr lossyd
= (j_lossy_d_ptr
) cinfo
->codec
;
164 latch_quant_tables(cinfo
);
165 (*lossyd
->entropy_start_pass
) (cinfo
);
166 (*lossyd
->coef_start_input_pass
) (cinfo
);
171 * Initialize for an output processing pass.
175 start_output_pass (j_decompress_ptr cinfo
)
177 j_lossy_d_ptr lossyd
= (j_lossy_d_ptr
) cinfo
->codec
;
179 (*lossyd
->idct_start_pass
) (cinfo
);
180 (*lossyd
->coef_start_output_pass
) (cinfo
);
184 * Initialize the lossy decompression codec.
185 * This is called only once, during master selection.
189 jinit_lossy_d_codec (j_decompress_ptr cinfo
)
191 j_lossy_d_ptr lossyd
;
192 boolean use_c_buffer
;
194 /* Create subobject in permanent pool */
195 lossyd
= (j_lossy_d_ptr
)
196 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
197 SIZEOF(jpeg_lossy_d_codec
));
198 cinfo
->codec
= (struct jpeg_d_codec
*) lossyd
;
200 /* Initialize sub-modules */
203 jinit_inverse_dct(cinfo
);
204 /* Entropy decoding: either Huffman or arithmetic coding. */
205 if (cinfo
->arith_code
) {
206 ERREXIT(cinfo
, JERR_ARITH_NOTIMPL
);
208 if (cinfo
->process
== JPROC_PROGRESSIVE
) {
209 #ifdef D_PROGRESSIVE_SUPPORTED
210 jinit_phuff_decoder(cinfo
);
212 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
215 jinit_shuff_decoder(cinfo
);
218 use_c_buffer
= cinfo
->inputctl
->has_multiple_scans
|| cinfo
->buffered_image
;
219 jinit_d_coef_controller(cinfo
, use_c_buffer
);
221 /* Initialize method pointers.
223 * Note: consume_data and decompress_data are assigned in jdcoefct.c.
225 lossyd
->pub
.calc_output_dimensions
= calc_output_dimensions
;
226 lossyd
->pub
.start_input_pass
= start_input_pass
;
227 lossyd
->pub
.start_output_pass
= start_output_pass
;