42
43
* Support routines that do various essential calculations.
47
* Compute JPEG image dimensions and related values.
48
* NOTE: this is exported for possible use by application.
49
* Hence it mustn't do anything that can't be done twice.
53
jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
54
/* Do computations that are needed before master selection phase */
56
#ifdef DCT_SCALING_SUPPORTED
58
/* Compute actual JPEG image dimensions and DCT scaling choices. */
59
if (cinfo->scale_num >= cinfo->scale_denom * 8) {
60
/* Provide 8/1 scaling */
61
cinfo->jpeg_width = cinfo->image_width << 3;
62
cinfo->jpeg_height = cinfo->image_height << 3;
63
cinfo->min_DCT_h_scaled_size = 1;
64
cinfo->min_DCT_v_scaled_size = 1;
65
} else if (cinfo->scale_num >= cinfo->scale_denom * 4) {
66
/* Provide 4/1 scaling */
67
cinfo->jpeg_width = cinfo->image_width << 2;
68
cinfo->jpeg_height = cinfo->image_height << 2;
69
cinfo->min_DCT_h_scaled_size = 2;
70
cinfo->min_DCT_v_scaled_size = 2;
71
} else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) {
72
/* Provide 8/3 scaling */
73
cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION)
74
jdiv_round_up((long) cinfo->image_width * 2, 3L);
75
cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION)
76
jdiv_round_up((long) cinfo->image_height * 2, 3L);
77
cinfo->min_DCT_h_scaled_size = 3;
78
cinfo->min_DCT_v_scaled_size = 3;
79
} else if (cinfo->scale_num >= cinfo->scale_denom * 2) {
80
/* Provide 2/1 scaling */
81
cinfo->jpeg_width = cinfo->image_width << 1;
82
cinfo->jpeg_height = cinfo->image_height << 1;
83
cinfo->min_DCT_h_scaled_size = 4;
84
cinfo->min_DCT_v_scaled_size = 4;
85
} else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) {
86
/* Provide 8/5 scaling */
87
cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
88
jdiv_round_up((long) cinfo->image_width * 3, 5L);
89
cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
90
jdiv_round_up((long) cinfo->image_height * 3, 5L);
91
cinfo->min_DCT_h_scaled_size = 5;
92
cinfo->min_DCT_v_scaled_size = 5;
93
} else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) {
94
/* Provide 4/3 scaling */
95
cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
96
jdiv_round_up((long) cinfo->image_width, 3L);
97
cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
98
jdiv_round_up((long) cinfo->image_height, 3L);
99
cinfo->min_DCT_h_scaled_size = 6;
100
cinfo->min_DCT_v_scaled_size = 6;
101
} else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) {
102
/* Provide 8/7 scaling */
103
cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
104
jdiv_round_up((long) cinfo->image_width, 7L);
105
cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
106
jdiv_round_up((long) cinfo->image_height, 7L);
107
cinfo->min_DCT_h_scaled_size = 7;
108
cinfo->min_DCT_v_scaled_size = 7;
109
} else if (cinfo->scale_num >= cinfo->scale_denom) {
110
/* Provide 1/1 scaling */
111
cinfo->jpeg_width = cinfo->image_width;
112
cinfo->jpeg_height = cinfo->image_height;
113
cinfo->min_DCT_h_scaled_size = DCTSIZE;
114
cinfo->min_DCT_v_scaled_size = DCTSIZE;
115
} else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) {
116
/* Provide 8/9 scaling */
117
cinfo->jpeg_width = (JDIMENSION)
118
jdiv_round_up((long) cinfo->image_width * 8, 9L);
119
cinfo->jpeg_height = (JDIMENSION)
120
jdiv_round_up((long) cinfo->image_height * 8, 9L);
121
cinfo->min_DCT_h_scaled_size = 9;
122
cinfo->min_DCT_v_scaled_size = 9;
123
} else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) {
124
/* Provide 4/5 scaling */
125
cinfo->jpeg_width = (JDIMENSION)
126
jdiv_round_up((long) cinfo->image_width * 4, 5L);
127
cinfo->jpeg_height = (JDIMENSION)
128
jdiv_round_up((long) cinfo->image_height * 4, 5L);
129
cinfo->min_DCT_h_scaled_size = 10;
130
cinfo->min_DCT_v_scaled_size = 10;
131
} else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) {
132
/* Provide 8/11 scaling */
133
cinfo->jpeg_width = (JDIMENSION)
134
jdiv_round_up((long) cinfo->image_width * 8, 11L);
135
cinfo->jpeg_height = (JDIMENSION)
136
jdiv_round_up((long) cinfo->image_height * 8, 11L);
137
cinfo->min_DCT_h_scaled_size = 11;
138
cinfo->min_DCT_v_scaled_size = 11;
139
} else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) {
140
/* Provide 2/3 scaling */
141
cinfo->jpeg_width = (JDIMENSION)
142
jdiv_round_up((long) cinfo->image_width * 2, 3L);
143
cinfo->jpeg_height = (JDIMENSION)
144
jdiv_round_up((long) cinfo->image_height * 2, 3L);
145
cinfo->min_DCT_h_scaled_size = 12;
146
cinfo->min_DCT_v_scaled_size = 12;
147
} else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) {
148
/* Provide 8/13 scaling */
149
cinfo->jpeg_width = (JDIMENSION)
150
jdiv_round_up((long) cinfo->image_width * 8, 13L);
151
cinfo->jpeg_height = (JDIMENSION)
152
jdiv_round_up((long) cinfo->image_height * 8, 13L);
153
cinfo->min_DCT_h_scaled_size = 13;
154
cinfo->min_DCT_v_scaled_size = 13;
155
} else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) {
156
/* Provide 4/7 scaling */
157
cinfo->jpeg_width = (JDIMENSION)
158
jdiv_round_up((long) cinfo->image_width * 4, 7L);
159
cinfo->jpeg_height = (JDIMENSION)
160
jdiv_round_up((long) cinfo->image_height * 4, 7L);
161
cinfo->min_DCT_h_scaled_size = 14;
162
cinfo->min_DCT_v_scaled_size = 14;
163
} else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) {
164
/* Provide 8/15 scaling */
165
cinfo->jpeg_width = (JDIMENSION)
166
jdiv_round_up((long) cinfo->image_width * 8, 15L);
167
cinfo->jpeg_height = (JDIMENSION)
168
jdiv_round_up((long) cinfo->image_height * 8, 15L);
169
cinfo->min_DCT_h_scaled_size = 15;
170
cinfo->min_DCT_v_scaled_size = 15;
172
/* Provide 1/2 scaling */
173
cinfo->jpeg_width = (JDIMENSION)
174
jdiv_round_up((long) cinfo->image_width, 2L);
175
cinfo->jpeg_height = (JDIMENSION)
176
jdiv_round_up((long) cinfo->image_height, 2L);
177
cinfo->min_DCT_h_scaled_size = 16;
178
cinfo->min_DCT_v_scaled_size = 16;
181
#else /* !DCT_SCALING_SUPPORTED */
183
/* Hardwire it to "no scaling" */
184
cinfo->jpeg_width = cinfo->image_width;
185
cinfo->jpeg_height = cinfo->image_height;
186
cinfo->min_DCT_h_scaled_size = DCTSIZE;
187
cinfo->min_DCT_v_scaled_size = DCTSIZE;
189
#endif /* DCT_SCALING_SUPPORTED */
46
194
initial_setup (j_compress_ptr cinfo)
47
195
/* Do computations that are needed before master selection phase */
50
198
jpeg_component_info *compptr;
51
199
long samplesperrow;
52
200
JDIMENSION jd_samplesperrow;
202
jpeg_calc_jpeg_dimensions(cinfo);
54
204
/* Sanity check on image dimensions */
55
if (cinfo->image_height <= 0 || cinfo->image_width <= 0
205
if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0
56
206
|| cinfo->num_components <= 0 || cinfo->input_components <= 0)
57
207
ERREXIT(cinfo, JERR_EMPTY_IMAGE);
59
209
/* Make sure image isn't bigger than I can handle */
60
if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
61
(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
210
if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
211
(long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
62
212
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
64
214
/* Width of an input scanline must be representable as JDIMENSION. */
95
245
ci++, compptr++) {
96
246
/* Fill in the correct component_index value; don't rely on application */
97
247
compptr->component_index = ci;
98
/* For compression, we never do DCT scaling. */
99
compptr->DCT_scaled_size = DCTSIZE;
248
/* In selecting the actual DCT scaling for each component, we try to
249
* scale down the chroma components via DCT scaling rather than downsampling.
250
* This saves time if the downsampler gets to use 1:1 scaling.
251
* Note this code adapts subsampling ratios which are powers of 2.
254
#ifdef DCT_SCALING_SUPPORTED
255
while (cinfo->min_DCT_h_scaled_size * ssize <=
256
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
257
(cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
261
compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
263
#ifdef DCT_SCALING_SUPPORTED
264
while (cinfo->min_DCT_v_scaled_size * ssize <=
265
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
266
(cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
270
compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
272
/* We don't support DCT ratios larger than 2. */
273
if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
274
compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
275
else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
276
compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
100
278
/* Size in DCT blocks */
101
279
compptr->width_in_blocks = (JDIMENSION)
102
jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
280
jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
103
281
(long) (cinfo->max_h_samp_factor * DCTSIZE));
104
282
compptr->height_in_blocks = (JDIMENSION)
105
jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
283
jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
106
284
(long) (cinfo->max_v_samp_factor * DCTSIZE));
107
285
/* Size in samples */
108
286
compptr->downsampled_width = (JDIMENSION)
109
jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
110
(long) cinfo->max_h_samp_factor);
287
jdiv_round_up((long) cinfo->jpeg_width *
288
(long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
289
(long) (cinfo->max_h_samp_factor * DCTSIZE));
111
290
compptr->downsampled_height = (JDIMENSION)
112
jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
113
(long) cinfo->max_v_samp_factor);
291
jdiv_round_up((long) cinfo->jpeg_height *
292
(long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
293
(long) (cinfo->max_v_samp_factor * DCTSIZE));
114
294
/* Mark component needed (this flag isn't actually used for compression) */
115
295
compptr->component_needed = TRUE;