blob: 03a8b40ea980641392333f72c013461da0e18ba9 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jcmaster.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding989630f2010-01-10 00:00:00 +00006 * Modified 2003-2010 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRC6f241d42016-03-16 07:10:35 -05008 * Copyright (C) 2010, 2016, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000011 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000012 * This file contains master control logic for the JPEG compressor.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 * These routines are concerned with parameter validation, initial setup,
DRCe5eaf372014-05-09 18:00:32 +000014 * and inter-pass control (determining the number of passes and the work
Thomas G. Lanebc79e061995-08-02 00:00:00 +000015 * to be done in each pass).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000016 */
17
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000019#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000020#include "jpeglib.h"
DRCc04bd3c2010-10-10 02:15:56 +000021#include "jpegcomp.h"
DRC6f241d42016-03-16 07:10:35 -050022#include "jconfigint.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000023
24
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025/* Private state */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000026
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027typedef enum {
DRCe5eaf372014-05-09 18:00:32 +000028 main_pass, /* input data, also do first output step */
29 huff_opt_pass, /* Huffman code optimization pass */
30 output_pass /* data output pass */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031} c_pass_type;
32
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000034 struct jpeg_comp_master pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035
DRCe5eaf372014-05-09 18:00:32 +000036 c_pass_type pass_type; /* the type of the current pass */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000037
DRCe5eaf372014-05-09 18:00:32 +000038 int pass_number; /* # of passes completed */
39 int total_passes; /* total # of passes needed */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000040
DRCe5eaf372014-05-09 18:00:32 +000041 int scan_number; /* current index in scan_info[] */
DRC6f241d42016-03-16 07:10:35 -050042
43 /*
44 * This is here so we can add libjpeg-turbo version/build information to the
45 * global string table without introducing a new global symbol. Adding this
46 * information to the global string table allows one to examine a binary
47 * object and determine which version of libjpeg-turbo it was built from or
48 * linked against.
49 */
50 const char *jpeg_version;
51
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052} my_comp_master;
53
DRCbd498032016-02-19 08:53:33 -060054typedef my_comp_master *my_master_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000055
56
57/*
58 * Support routines that do various essential calculations.
59 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000060
DRC36a6eec2010-10-08 08:05:44 +000061#if JPEG_LIB_VERSION >= 70
Guido Vollbeding5996a252009-06-27 00:00:00 +000062/*
63 * Compute JPEG image dimensions and related values.
64 * NOTE: this is exported for possible use by application.
65 * Hence it mustn't do anything that can't be done twice.
66 */
67
68GLOBAL(void)
69jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
70/* Do computations that are needed before master selection phase */
71{
Guido Vollbeding5996a252009-06-27 00:00:00 +000072 /* Hardwire it to "no scaling" */
73 cinfo->jpeg_width = cinfo->image_width;
74 cinfo->jpeg_height = cinfo->image_height;
75 cinfo->min_DCT_h_scaled_size = DCTSIZE;
76 cinfo->min_DCT_v_scaled_size = DCTSIZE;
Guido Vollbeding5996a252009-06-27 00:00:00 +000077}
DRC36a6eec2010-10-08 08:05:44 +000078#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000079
80
Thomas G. Lane489583f1996-02-07 00:00:00 +000081LOCAL(void)
Guido Vollbeding989630f2010-01-10 00:00:00 +000082initial_setup (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000084{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000086 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 long samplesperrow;
88 JDIMENSION jd_samplesperrow;
89
DRC36a6eec2010-10-08 08:05:44 +000090#if JPEG_LIB_VERSION >= 70
DRCe7fde872011-04-03 07:09:49 +000091#if JPEG_LIB_VERSION >= 80
DRCc04bd3c2010-10-10 02:15:56 +000092 if (!transcode_only)
DRCe7fde872011-04-03 07:09:49 +000093#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +000094 jpeg_calc_jpeg_dimensions(cinfo);
DRC36a6eec2010-10-08 08:05:44 +000095#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000096
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 /* Sanity check on image dimensions */
DRCc04bd3c2010-10-10 02:15:56 +000098 if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000099 || cinfo->num_components <= 0 || cinfo->input_components <= 0)
100 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
101
102 /* Make sure image isn't bigger than I can handle */
DRCc04bd3c2010-10-10 02:15:56 +0000103 if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
104 (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
106
107 /* Width of an input scanline must be representable as JDIMENSION. */
108 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
109 jd_samplesperrow = (JDIMENSION) samplesperrow;
110 if ((long) jd_samplesperrow != samplesperrow)
111 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
112
113 /* For now, precision must match compiled-in value... */
114 if (cinfo->data_precision != BITS_IN_JSAMPLE)
115 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
116
117 /* Check that number of components won't exceed internal array sizes */
118 if (cinfo->num_components > MAX_COMPONENTS)
119 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
DRCe5eaf372014-05-09 18:00:32 +0000120 MAX_COMPONENTS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000121
122 /* Compute maximum sampling factors; check factor validity */
123 cinfo->max_h_samp_factor = 1;
124 cinfo->max_v_samp_factor = 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
126 ci++, compptr++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000127 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
DRCe5eaf372014-05-09 18:00:32 +0000128 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129 ERREXIT(cinfo, JERR_BAD_SAMPLING);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000130 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000131 compptr->h_samp_factor);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000132 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000133 compptr->v_samp_factor);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000134 }
135
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000136 /* Compute dimensions of components */
137 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
138 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000139 /* Fill in the correct component_index value; don't rely on application */
140 compptr->component_index = ci;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141 /* For compression, we never do DCT scaling. */
DRC36a6eec2010-10-08 08:05:44 +0000142#if JPEG_LIB_VERSION >= 70
143 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
144#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 compptr->DCT_scaled_size = DCTSIZE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000146#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147 /* Size in DCT blocks */
148 compptr->width_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000149 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000150 (long) (cinfo->max_h_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151 compptr->height_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000152 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000153 (long) (cinfo->max_v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154 /* Size in samples */
155 compptr->downsampled_width = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000156 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000157 (long) cinfo->max_h_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 compptr->downsampled_height = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000159 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000160 (long) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000161 /* Mark component needed (this flag isn't actually used for compression) */
162 compptr->component_needed = TRUE;
163 }
164
165 /* Compute number of fully interleaved MCU rows (number of times that
166 * main controller will call coefficient controller).
167 */
168 cinfo->total_iMCU_rows = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000169 jdiv_round_up((long) cinfo->_jpeg_height,
DRCe5eaf372014-05-09 18:00:32 +0000170 (long) (cinfo->max_v_samp_factor*DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171}
172
173
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174#ifdef C_MULTISCAN_FILES_SUPPORTED
175
Thomas G. Lane489583f1996-02-07 00:00:00 +0000176LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000177validate_script (j_compress_ptr cinfo)
178/* Verify that the scan script in cinfo->scan_info[] is valid; also
179 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
180 */
181{
DRCbd498032016-02-19 08:53:33 -0600182 const jpeg_scan_info *scanptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000183 int scanno, ncomps, ci, coefi, thisi;
184 int Ss, Se, Ah, Al;
185 boolean component_sent[MAX_COMPONENTS];
186#ifdef C_PROGRESSIVE_SUPPORTED
DRCbd498032016-02-19 08:53:33 -0600187 int *last_bitpos_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000188 int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
189 /* -1 until that coefficient has been seen; then last Al for it */
190#endif
191
192 if (cinfo->num_scans <= 0)
193 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
194
195 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
196 * for progressive JPEG, no scan can have this.
197 */
198 scanptr = cinfo->scan_info;
199 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
200#ifdef C_PROGRESSIVE_SUPPORTED
201 cinfo->progressive_mode = TRUE;
202 last_bitpos_ptr = & last_bitpos[0][0];
DRCe5eaf372014-05-09 18:00:32 +0000203 for (ci = 0; ci < cinfo->num_components; ci++)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000204 for (coefi = 0; coefi < DCTSIZE2; coefi++)
DRCe5eaf372014-05-09 18:00:32 +0000205 *last_bitpos_ptr++ = -1;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000206#else
207 ERREXIT(cinfo, JERR_NOT_COMPILED);
208#endif
209 } else {
210 cinfo->progressive_mode = FALSE;
DRCe5eaf372014-05-09 18:00:32 +0000211 for (ci = 0; ci < cinfo->num_components; ci++)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000212 component_sent[ci] = FALSE;
213 }
214
215 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
216 /* Validate component indexes */
217 ncomps = scanptr->comps_in_scan;
218 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
219 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
220 for (ci = 0; ci < ncomps; ci++) {
221 thisi = scanptr->component_index[ci];
222 if (thisi < 0 || thisi >= cinfo->num_components)
DRCe5eaf372014-05-09 18:00:32 +0000223 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000224 /* Components must appear in SOF order within each scan */
225 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
DRCe5eaf372014-05-09 18:00:32 +0000226 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000227 }
228 /* Validate progression parameters */
229 Ss = scanptr->Ss;
230 Se = scanptr->Se;
231 Ah = scanptr->Ah;
232 Al = scanptr->Al;
233 if (cinfo->progressive_mode) {
234#ifdef C_PROGRESSIVE_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000235 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
236 * seems wrong: the upper bound ought to depend on data precision.
237 * Perhaps they really meant 0..N+1 for N-bit precision.
238 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
239 * out-of-range reconstructed DC values during the first DC scan,
240 * which might cause problems for some decoders.
241 */
242#if BITS_IN_JSAMPLE == 8
243#define MAX_AH_AL 10
244#else
245#define MAX_AH_AL 13
246#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000247 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
DRCe5eaf372014-05-09 18:00:32 +0000248 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
249 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000250 if (Ss == 0) {
DRCe5eaf372014-05-09 18:00:32 +0000251 if (Se != 0) /* DC and AC together not OK */
252 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000253 } else {
DRCe5eaf372014-05-09 18:00:32 +0000254 if (ncomps != 1) /* AC scans must be for only one component */
255 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000256 }
257 for (ci = 0; ci < ncomps; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000258 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
259 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
260 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
261 for (coefi = Ss; coefi <= Se; coefi++) {
262 if (last_bitpos_ptr[coefi] < 0) {
263 /* first scan of this coefficient */
264 if (Ah != 0)
265 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
266 } else {
267 /* not first scan */
268 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
269 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
270 }
271 last_bitpos_ptr[coefi] = Al;
272 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000273 }
274#endif
275 } else {
276 /* For sequential JPEG, all progression parameters must be these: */
277 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
DRCe5eaf372014-05-09 18:00:32 +0000278 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000279 /* Make sure components are not sent twice */
280 for (ci = 0; ci < ncomps; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000281 thisi = scanptr->component_index[ci];
282 if (component_sent[thisi])
283 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
284 component_sent[thisi] = TRUE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000285 }
286 }
287 }
288
289 /* Now verify that everything got sent. */
290 if (cinfo->progressive_mode) {
291#ifdef C_PROGRESSIVE_SUPPORTED
292 /* For progressive mode, we only check that at least some DC data
293 * got sent for each component; the spec does not require that all bits
294 * of all coefficients be transmitted. Would it be wiser to enforce
295 * transmission of all coefficient bits??
296 */
297 for (ci = 0; ci < cinfo->num_components; ci++) {
298 if (last_bitpos[ci][0] < 0)
DRCe5eaf372014-05-09 18:00:32 +0000299 ERREXIT(cinfo, JERR_MISSING_DATA);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000300 }
301#endif
302 } else {
303 for (ci = 0; ci < cinfo->num_components; ci++) {
304 if (! component_sent[ci])
DRCe5eaf372014-05-09 18:00:32 +0000305 ERREXIT(cinfo, JERR_MISSING_DATA);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000306 }
307 }
308}
309
310#endif /* C_MULTISCAN_FILES_SUPPORTED */
311
312
Thomas G. Lane489583f1996-02-07 00:00:00 +0000313LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314select_scan_parameters (j_compress_ptr cinfo)
315/* Set up the scan parameters for the current scan */
316{
317 int ci;
318
319#ifdef C_MULTISCAN_FILES_SUPPORTED
320 if (cinfo->scan_info != NULL) {
321 /* Prepare for current scan --- the script is already validated */
322 my_master_ptr master = (my_master_ptr) cinfo->master;
DRCbd498032016-02-19 08:53:33 -0600323 const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000324
325 cinfo->comps_in_scan = scanptr->comps_in_scan;
326 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
327 cinfo->cur_comp_info[ci] =
DRCe5eaf372014-05-09 18:00:32 +0000328 &cinfo->comp_info[scanptr->component_index[ci]];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000329 }
330 cinfo->Ss = scanptr->Ss;
331 cinfo->Se = scanptr->Se;
332 cinfo->Ah = scanptr->Ah;
333 cinfo->Al = scanptr->Al;
334 }
335 else
336#endif
337 {
338 /* Prepare for single sequential-JPEG scan containing all components */
339 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
340 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
DRCe5eaf372014-05-09 18:00:32 +0000341 MAX_COMPS_IN_SCAN);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000342 cinfo->comps_in_scan = cinfo->num_components;
343 for (ci = 0; ci < cinfo->num_components; ci++) {
344 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
345 }
346 cinfo->Ss = 0;
347 cinfo->Se = DCTSIZE2-1;
348 cinfo->Ah = 0;
349 cinfo->Al = 0;
350 }
351}
352
353
Thomas G. Lane489583f1996-02-07 00:00:00 +0000354LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000355per_scan_setup (j_compress_ptr cinfo)
356/* Do computations that are needed before processing a JPEG scan */
357/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
358{
359 int ci, mcublks, tmp;
360 jpeg_component_info *compptr;
DRCe5eaf372014-05-09 18:00:32 +0000361
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362 if (cinfo->comps_in_scan == 1) {
DRCe5eaf372014-05-09 18:00:32 +0000363
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364 /* Noninterleaved (single-component) scan */
365 compptr = cinfo->cur_comp_info[0];
DRCe5eaf372014-05-09 18:00:32 +0000366
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 /* Overall image size in MCUs */
368 cinfo->MCUs_per_row = compptr->width_in_blocks;
369 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
DRCe5eaf372014-05-09 18:00:32 +0000370
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371 /* For noninterleaved scan, always one block per MCU */
372 compptr->MCU_width = 1;
373 compptr->MCU_height = 1;
374 compptr->MCU_blocks = 1;
375 compptr->MCU_sample_width = DCTSIZE;
376 compptr->last_col_width = 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000377 /* For noninterleaved scans, it is convenient to define last_row_height
378 * as the number of block rows present in the last iMCU row.
379 */
380 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
381 if (tmp == 0) tmp = compptr->v_samp_factor;
382 compptr->last_row_height = tmp;
DRCe5eaf372014-05-09 18:00:32 +0000383
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 /* Prepare array describing MCU composition */
385 cinfo->blocks_in_MCU = 1;
386 cinfo->MCU_membership[0] = 0;
DRCe5eaf372014-05-09 18:00:32 +0000387
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 } else {
DRCe5eaf372014-05-09 18:00:32 +0000389
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390 /* Interleaved (multi-component) scan */
391 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
392 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
DRCe5eaf372014-05-09 18:00:32 +0000393 MAX_COMPS_IN_SCAN);
394
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 /* Overall image size in MCUs */
396 cinfo->MCUs_per_row = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000397 jdiv_round_up((long) cinfo->_jpeg_width,
DRCe5eaf372014-05-09 18:00:32 +0000398 (long) (cinfo->max_h_samp_factor*DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 cinfo->MCU_rows_in_scan = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000400 jdiv_round_up((long) cinfo->_jpeg_height,
DRCe5eaf372014-05-09 18:00:32 +0000401 (long) (cinfo->max_v_samp_factor*DCTSIZE));
402
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 cinfo->blocks_in_MCU = 0;
DRCe5eaf372014-05-09 18:00:32 +0000404
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
406 compptr = cinfo->cur_comp_info[ci];
407 /* Sampling factors give # of blocks of component in each MCU */
408 compptr->MCU_width = compptr->h_samp_factor;
409 compptr->MCU_height = compptr->v_samp_factor;
410 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
411 compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
412 /* Figure number of non-dummy blocks in last MCU column & row */
413 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
414 if (tmp == 0) tmp = compptr->MCU_width;
415 compptr->last_col_width = tmp;
416 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
417 if (tmp == 0) tmp = compptr->MCU_height;
418 compptr->last_row_height = tmp;
419 /* Prepare array describing MCU composition */
420 mcublks = compptr->MCU_blocks;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000421 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
DRCe5eaf372014-05-09 18:00:32 +0000422 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000423 while (mcublks-- > 0) {
DRCe5eaf372014-05-09 18:00:32 +0000424 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425 }
426 }
DRCe5eaf372014-05-09 18:00:32 +0000427
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428 }
429
430 /* Convert restart specified in rows to actual MCU count. */
431 /* Note that count must fit in 16 bits, so we provide limiting. */
432 if (cinfo->restart_in_rows > 0) {
433 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
434 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000435 }
436}
437
438
439/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 * Per-pass setup.
441 * This is called at the beginning of each pass. We determine which modules
442 * will be active during this pass and give them appropriate start_pass calls.
443 * We also set is_last_pass to indicate whether any more passes will be
444 * required.
445 */
446
Thomas G. Lane489583f1996-02-07 00:00:00 +0000447METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000448prepare_for_pass (j_compress_ptr cinfo)
449{
450 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000451
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000452 switch (master->pass_type) {
453 case main_pass:
454 /* Initial pass: will collect input data, and do either Huffman
455 * optimization or data output for the first scan.
456 */
457 select_scan_parameters(cinfo);
458 per_scan_setup(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000459 if (! cinfo->raw_data_in) {
460 (*cinfo->cconvert->start_pass) (cinfo);
461 (*cinfo->downsample->start_pass) (cinfo);
462 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
463 }
464 (*cinfo->fdct->start_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000465 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
466 (*cinfo->coef->start_pass) (cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000467 (master->total_passes > 1 ?
468 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000470 if (cinfo->optimize_coding) {
471 /* No immediate data output; postpone writing frame/scan headers */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000472 master->pub.call_pass_startup = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000473 } else {
474 /* Will write frame/scan headers at first jpeg_write_scanlines call */
475 master->pub.call_pass_startup = TRUE;
476 }
477 break;
478#ifdef ENTROPY_OPT_SUPPORTED
479 case huff_opt_pass:
480 /* Do Huffman optimization for a scan after the first one. */
481 select_scan_parameters(cinfo);
482 per_scan_setup(cinfo);
483 if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000484 (*cinfo->entropy->start_pass) (cinfo, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000485 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000486 master->pub.call_pass_startup = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000487 break;
488 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000489 /* Special case: Huffman DC refinement scans need no Huffman table
490 * and therefore we can skip the optimization pass for them.
491 */
492 master->pass_type = output_pass;
493 master->pass_number++;
494 /*FALLTHROUGH*/
495#endif
496 case output_pass:
497 /* Do a data-output pass. */
498 /* We need not repeat per-scan setup if prior optimization pass did it. */
499 if (! cinfo->optimize_coding) {
500 select_scan_parameters(cinfo);
501 per_scan_setup(cinfo);
502 }
503 (*cinfo->entropy->start_pass) (cinfo, FALSE);
504 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
505 /* We emit frame/scan headers now */
506 if (master->scan_number == 0)
507 (*cinfo->marker->write_frame_header) (cinfo);
508 (*cinfo->marker->write_scan_header) (cinfo);
509 master->pub.call_pass_startup = FALSE;
510 break;
511 default:
512 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000513 }
514
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000515 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
516
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000517 /* Set up progress monitor's pass info if present */
518 if (cinfo->progress != NULL) {
519 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000520 cinfo->progress->total_passes = master->total_passes;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000522}
523
524
525/*
526 * Special start-of-pass hook.
527 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
528 * In single-pass processing, we need this hook because we don't want to
529 * write frame/scan headers during jpeg_start_compress; we want to let the
530 * application write COM markers etc. between jpeg_start_compress and the
531 * jpeg_write_scanlines loop.
532 * In multi-pass processing, this routine is not used.
533 */
534
Thomas G. Lane489583f1996-02-07 00:00:00 +0000535METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000536pass_startup (j_compress_ptr cinfo)
537{
538 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
539
540 (*cinfo->marker->write_frame_header) (cinfo);
541 (*cinfo->marker->write_scan_header) (cinfo);
542}
543
544
545/*
546 * Finish up at end of pass.
547 */
548
Thomas G. Lane489583f1996-02-07 00:00:00 +0000549METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550finish_pass_master (j_compress_ptr cinfo)
551{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000552 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000553
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000554 /* The entropy coder always needs an end-of-pass call,
555 * either to analyze statistics or to flush its output buffer.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000556 */
557 (*cinfo->entropy->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000558
559 /* Update state for next pass */
560 switch (master->pass_type) {
561 case main_pass:
562 /* next pass is either output of scan 0 (after optimization)
563 * or output of scan 1 (if no optimization).
564 */
565 master->pass_type = output_pass;
566 if (! cinfo->optimize_coding)
567 master->scan_number++;
568 break;
569 case huff_opt_pass:
570 /* next pass is always output of current scan */
571 master->pass_type = output_pass;
572 break;
573 case output_pass:
574 /* next pass is either optimization or output of next scan */
575 if (cinfo->optimize_coding)
576 master->pass_type = huff_opt_pass;
577 master->scan_number++;
578 break;
579 }
580
581 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000582}
583
584
585/*
586 * Initialize master compression control.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000587 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000588
Thomas G. Lane489583f1996-02-07 00:00:00 +0000589GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000590jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000591{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000592 my_master_ptr master;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000593
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000594 master = (my_master_ptr)
595 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000596 sizeof(my_comp_master));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000597 cinfo->master = (struct jpeg_comp_master *) master;
598 master->pub.prepare_for_pass = prepare_for_pass;
599 master->pub.pass_startup = pass_startup;
600 master->pub.finish_pass = finish_pass_master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000601 master->pub.is_last_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000602
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000603 /* Validate parameters, determine derived values */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000604 initial_setup(cinfo, transcode_only);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000605
606 if (cinfo->scan_info != NULL) {
607#ifdef C_MULTISCAN_FILES_SUPPORTED
608 validate_script(cinfo);
609#else
610 ERREXIT(cinfo, JERR_NOT_COMPILED);
611#endif
612 } else {
613 cinfo->progressive_mode = FALSE;
614 cinfo->num_scans = 1;
615 }
616
DRCe621dfc2016-02-19 10:35:09 -0600617 if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000618 cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
619
620 /* Initialize my private state */
621 if (transcode_only) {
622 /* no main pass in transcoding */
623 if (cinfo->optimize_coding)
624 master->pass_type = huff_opt_pass;
625 else
626 master->pass_type = output_pass;
627 } else {
628 /* for normal compression, first pass is always this type: */
629 master->pass_type = main_pass;
630 }
631 master->scan_number = 0;
632 master->pass_number = 0;
633 if (cinfo->optimize_coding)
634 master->total_passes = cinfo->num_scans * 2;
635 else
636 master->total_passes = cinfo->num_scans;
DRC6f241d42016-03-16 07:10:35 -0500637
638 master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000639}