Merge PR #283 "[USBPORT] Transaction Translator (TT) support bringup"
[reactos.git] / dll / 3rdparty / libjpeg / jcinit.c
1 /*
2 * jcinit.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2003-2017 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains initialization logic for the JPEG compressor.
10 * This routine is in charge of selecting the modules to be executed and
11 * making an initialization call to each one.
12 *
13 * Logically, this code belongs in jcmaster.c. It's split out because
14 * linking this routine implies linking the entire compression library.
15 * For a transcoding-only application, we want to be able to use jcmaster.c
16 * without linking in the whole library.
17 */
18
19 #define JPEG_INTERNALS
20 #include "jinclude.h"
21 #include "jpeglib.h"
22
23
24 /*
25 * Compute JPEG image dimensions and related values.
26 * NOTE: this is exported for possible use by application.
27 * Hence it mustn't do anything that can't be done twice.
28 */
29
30 GLOBAL(void)
31 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
32 /* Do computations that are needed before master selection phase */
33 {
34 /* Sanity check on input image dimensions to prevent overflow in
35 * following calculations.
36 * We do check jpeg_width and jpeg_height in initial_setup in jcmaster.c,
37 * but image_width and image_height can come from arbitrary data,
38 * and we need some space for multiplication by block_size.
39 */
40 if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
41 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
42
43 #ifdef DCT_SCALING_SUPPORTED
44
45 /* Compute actual JPEG image dimensions and DCT scaling choices. */
46 if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
47 /* Provide block_size/1 scaling */
48 cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
49 cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
50 cinfo->min_DCT_h_scaled_size = 1;
51 cinfo->min_DCT_v_scaled_size = 1;
52 } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
53 /* Provide block_size/2 scaling */
54 cinfo->jpeg_width = (JDIMENSION)
55 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
56 cinfo->jpeg_height = (JDIMENSION)
57 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
58 cinfo->min_DCT_h_scaled_size = 2;
59 cinfo->min_DCT_v_scaled_size = 2;
60 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
61 /* Provide block_size/3 scaling */
62 cinfo->jpeg_width = (JDIMENSION)
63 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
64 cinfo->jpeg_height = (JDIMENSION)
65 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
66 cinfo->min_DCT_h_scaled_size = 3;
67 cinfo->min_DCT_v_scaled_size = 3;
68 } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
69 /* Provide block_size/4 scaling */
70 cinfo->jpeg_width = (JDIMENSION)
71 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
72 cinfo->jpeg_height = (JDIMENSION)
73 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
74 cinfo->min_DCT_h_scaled_size = 4;
75 cinfo->min_DCT_v_scaled_size = 4;
76 } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
77 /* Provide block_size/5 scaling */
78 cinfo->jpeg_width = (JDIMENSION)
79 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
80 cinfo->jpeg_height = (JDIMENSION)
81 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
82 cinfo->min_DCT_h_scaled_size = 5;
83 cinfo->min_DCT_v_scaled_size = 5;
84 } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
85 /* Provide block_size/6 scaling */
86 cinfo->jpeg_width = (JDIMENSION)
87 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
88 cinfo->jpeg_height = (JDIMENSION)
89 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
90 cinfo->min_DCT_h_scaled_size = 6;
91 cinfo->min_DCT_v_scaled_size = 6;
92 } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
93 /* Provide block_size/7 scaling */
94 cinfo->jpeg_width = (JDIMENSION)
95 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
96 cinfo->jpeg_height = (JDIMENSION)
97 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
98 cinfo->min_DCT_h_scaled_size = 7;
99 cinfo->min_DCT_v_scaled_size = 7;
100 } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
101 /* Provide block_size/8 scaling */
102 cinfo->jpeg_width = (JDIMENSION)
103 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
104 cinfo->jpeg_height = (JDIMENSION)
105 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
106 cinfo->min_DCT_h_scaled_size = 8;
107 cinfo->min_DCT_v_scaled_size = 8;
108 } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
109 /* Provide block_size/9 scaling */
110 cinfo->jpeg_width = (JDIMENSION)
111 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
112 cinfo->jpeg_height = (JDIMENSION)
113 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
114 cinfo->min_DCT_h_scaled_size = 9;
115 cinfo->min_DCT_v_scaled_size = 9;
116 } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
117 /* Provide block_size/10 scaling */
118 cinfo->jpeg_width = (JDIMENSION)
119 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
120 cinfo->jpeg_height = (JDIMENSION)
121 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
122 cinfo->min_DCT_h_scaled_size = 10;
123 cinfo->min_DCT_v_scaled_size = 10;
124 } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
125 /* Provide block_size/11 scaling */
126 cinfo->jpeg_width = (JDIMENSION)
127 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
128 cinfo->jpeg_height = (JDIMENSION)
129 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
130 cinfo->min_DCT_h_scaled_size = 11;
131 cinfo->min_DCT_v_scaled_size = 11;
132 } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
133 /* Provide block_size/12 scaling */
134 cinfo->jpeg_width = (JDIMENSION)
135 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
136 cinfo->jpeg_height = (JDIMENSION)
137 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
138 cinfo->min_DCT_h_scaled_size = 12;
139 cinfo->min_DCT_v_scaled_size = 12;
140 } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
141 /* Provide block_size/13 scaling */
142 cinfo->jpeg_width = (JDIMENSION)
143 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
144 cinfo->jpeg_height = (JDIMENSION)
145 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
146 cinfo->min_DCT_h_scaled_size = 13;
147 cinfo->min_DCT_v_scaled_size = 13;
148 } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
149 /* Provide block_size/14 scaling */
150 cinfo->jpeg_width = (JDIMENSION)
151 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
152 cinfo->jpeg_height = (JDIMENSION)
153 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
154 cinfo->min_DCT_h_scaled_size = 14;
155 cinfo->min_DCT_v_scaled_size = 14;
156 } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
157 /* Provide block_size/15 scaling */
158 cinfo->jpeg_width = (JDIMENSION)
159 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
160 cinfo->jpeg_height = (JDIMENSION)
161 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
162 cinfo->min_DCT_h_scaled_size = 15;
163 cinfo->min_DCT_v_scaled_size = 15;
164 } else {
165 /* Provide block_size/16 scaling */
166 cinfo->jpeg_width = (JDIMENSION)
167 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
168 cinfo->jpeg_height = (JDIMENSION)
169 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
170 cinfo->min_DCT_h_scaled_size = 16;
171 cinfo->min_DCT_v_scaled_size = 16;
172 }
173
174 #else /* !DCT_SCALING_SUPPORTED */
175
176 /* Hardwire it to "no scaling" */
177 cinfo->jpeg_width = cinfo->image_width;
178 cinfo->jpeg_height = cinfo->image_height;
179 cinfo->min_DCT_h_scaled_size = DCTSIZE;
180 cinfo->min_DCT_v_scaled_size = DCTSIZE;
181
182 #endif /* DCT_SCALING_SUPPORTED */
183 }
184
185
186 /*
187 * Master selection of compression modules.
188 * This is done once at the start of processing an image. We determine
189 * which modules will be used and give them appropriate initialization calls.
190 */
191
192 GLOBAL(void)
193 jinit_compress_master (j_compress_ptr cinfo)
194 {
195 long samplesperrow;
196 JDIMENSION jd_samplesperrow;
197
198 /* For now, precision must match compiled-in value... */
199 if (cinfo->data_precision != BITS_IN_JSAMPLE)
200 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
201
202 /* Sanity check on input image dimensions */
203 if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
204 cinfo->input_components <= 0)
205 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
206
207 /* Width of an input scanline must be representable as JDIMENSION. */
208 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
209 jd_samplesperrow = (JDIMENSION) samplesperrow;
210 if ((long) jd_samplesperrow != samplesperrow)
211 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
212
213 /* Compute JPEG image dimensions and related values. */
214 jpeg_calc_jpeg_dimensions(cinfo);
215
216 /* Initialize master control (includes parameter checking/processing) */
217 jinit_c_master_control(cinfo, FALSE /* full compression */);
218
219 /* Preprocessing */
220 if (! cinfo->raw_data_in) {
221 jinit_color_converter(cinfo);
222 jinit_downsampler(cinfo);
223 jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
224 }
225 /* Forward DCT */
226 jinit_forward_dct(cinfo);
227 /* Entropy encoding: either Huffman or arithmetic coding. */
228 if (cinfo->arith_code)
229 jinit_arith_encoder(cinfo);
230 else {
231 jinit_huff_encoder(cinfo);
232 }
233
234 /* Need a full-image coefficient buffer in any multi-pass mode. */
235 jinit_c_coef_controller(cinfo,
236 (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
237 jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
238
239 jinit_marker_writer(cinfo);
240
241 /* We can now tell the memory manager to allocate virtual arrays. */
242 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
243
244 /* Write the datastream header (SOI) immediately.
245 * Frame and scan headers are postponed till later.
246 * This lets application insert special markers after the SOI.
247 */
248 (*cinfo->marker->write_file_header) (cinfo);
249 }