12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * jcmaster.c
32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1991-1997, Thomas G. Lane.
6989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * Modified 2003-2010 by Guido Vollbeding.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
836a6eec93250e390d1028b3372078810b4428eafDRC * Copyright (C) 2010, D. R. Commander.
92cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * For conditions of distribution and use, see the accompanying README file.
102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
1136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains master control logic for the JPEG compressor.
12bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * These routines are concerned with parameter validation, initial setup,
13e5eaf37440b8e337ab150c017df7c03faf846c51DRC * and inter-pass control (determining the number of passes and the work
14bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * to be done in each pass).
152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "jinclude.h"
1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
20c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC#include "jpegcomp.h"
212cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
222cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Private state */
242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
25bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanetypedef enum {
26e5eaf37440b8e337ab150c017df7c03faf846c51DRC        main_pass,              /* input data, also do first output step */
27e5eaf37440b8e337ab150c017df7c03faf846c51DRC        huff_opt_pass,          /* Huffman code optimization pass */
28e5eaf37440b8e337ab150c017df7c03faf846c51DRC        output_pass             /* data output pass */
29bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} c_pass_type;
30bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
3136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct {
32e5eaf37440b8e337ab150c017df7c03faf846c51DRC  struct jpeg_comp_master pub;  /* public fields */
332cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
34e5eaf37440b8e337ab150c017df7c03faf846c51DRC  c_pass_type pass_type;        /* the type of the current pass */
35bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
36e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int pass_number;              /* # of passes completed */
37e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int total_passes;             /* total # of passes needed */
38bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
39e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int scan_number;              /* current index in scan_info[] */
4036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_comp_master;
4136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
4236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef my_comp_master * my_master_ptr;
432cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
442cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
4636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Support routines that do various essential calculations.
4736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
482cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4936a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
505996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding/*
515996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * Compute JPEG image dimensions and related values.
525996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * NOTE: this is exported for possible use by application.
535996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * Hence it mustn't do anything that can't be done twice.
545996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding */
555996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
565996a25e2f50d20d6a8f09830724035b49c3927bGuido VollbedingGLOBAL(void)
575996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbedingjpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
585996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding/* Do computations that are needed before master selection phase */
595996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding{
605996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  /* Hardwire it to "no scaling" */
615996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->jpeg_width = cinfo->image_width;
625996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->jpeg_height = cinfo->image_height;
635996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->min_DCT_h_scaled_size = DCTSIZE;
645996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->min_DCT_v_scaled_size = DCTSIZE;
655996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding}
6636a6eec93250e390d1028b3372078810b4428eafDRC#endif
675996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
685996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
69489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
70989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbedinginitial_setup (j_compress_ptr cinfo, boolean transcode_only)
7136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Do computations that are needed before master selection phase */
722cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
7336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  jpeg_component_info *compptr;
7536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  long samplesperrow;
7636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION jd_samplesperrow;
7736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7836a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
79e7fde87ca5d1f908dc211cc8141acc648ddde878DRC#if JPEG_LIB_VERSION >= 80
80c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC  if (!transcode_only)
81e7fde87ca5d1f908dc211cc8141acc648ddde878DRC#endif
82989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding    jpeg_calc_jpeg_dimensions(cinfo);
8336a6eec93250e390d1028b3372078810b4428eafDRC#endif
845996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
8536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Sanity check on image dimensions */
86c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC  if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
8736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
8836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
8936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
9036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Make sure image isn't bigger than I can handle */
91c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC  if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
92c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
9336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
9436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
9536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Width of an input scanline must be representable as JDIMENSION. */
9636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
9736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jd_samplesperrow = (JDIMENSION) samplesperrow;
9836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if ((long) jd_samplesperrow != samplesperrow)
9936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
10036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
10136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* For now, precision must match compiled-in value... */
10236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->data_precision != BITS_IN_JSAMPLE)
10336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
10436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
10536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Check that number of components won't exceed internal array sizes */
10636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->num_components > MAX_COMPONENTS)
10736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
108e5eaf37440b8e337ab150c017df7c03faf846c51DRC             MAX_COMPONENTS);
1092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* Compute maximum sampling factors; check factor validity */
1112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  cinfo->max_h_samp_factor = 1;
1122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  cinfo->max_v_samp_factor = 1;
11336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
11436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       ci++, compptr++) {
1152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
116e5eaf37440b8e337ab150c017df7c03faf846c51DRC        compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
11736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_BAD_SAMPLING);
1182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
119e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                   compptr->h_samp_factor);
1202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
121e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                   compptr->v_samp_factor);
12236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
1232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
12436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Compute dimensions of components */
12536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
12636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       ci++, compptr++) {
127bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Fill in the correct component_index value; don't rely on application */
128bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    compptr->component_index = ci;
12936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* For compression, we never do DCT scaling. */
13036a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
13136a6eec93250e390d1028b3372078810b4428eafDRC    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
13236a6eec93250e390d1028b3372078810b4428eafDRC#else
13336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->DCT_scaled_size = DCTSIZE;
1345996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding#endif
13536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Size in DCT blocks */
13636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->width_in_blocks = (JDIMENSION)
137c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
138e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
13936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->height_in_blocks = (JDIMENSION)
140c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
141e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
14236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Size in samples */
14336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->downsampled_width = (JDIMENSION)
144c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
145e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) cinfo->max_h_samp_factor);
14636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->downsampled_height = (JDIMENSION)
147c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
148e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) cinfo->max_v_samp_factor);
14936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Mark component needed (this flag isn't actually used for compression) */
15036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->component_needed = TRUE;
1512cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
1522cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
15336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Compute number of fully interleaved MCU rows (number of times that
15436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * main controller will call coefficient controller).
15536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
15636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->total_iMCU_rows = (JDIMENSION)
157c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC    jdiv_round_up((long) cinfo->_jpeg_height,
158e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  (long) (cinfo->max_v_samp_factor*DCTSIZE));
15936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
16036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
16136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
162bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_MULTISCAN_FILES_SUPPORTED
163bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
164489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
165bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanevalidate_script (j_compress_ptr cinfo)
166bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Verify that the scan script in cinfo->scan_info[] is valid; also
167bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
168bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
169bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
170bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  const jpeg_scan_info * scanptr;
171bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int scanno, ncomps, ci, coefi, thisi;
172bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int Ss, Se, Ah, Al;
173bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  boolean component_sent[MAX_COMPONENTS];
174bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_PROGRESSIVE_SUPPORTED
175bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int * last_bitpos_ptr;
176bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
177bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* -1 until that coefficient has been seen; then last Al for it */
178bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
179bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
180bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->num_scans <= 0)
181bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
182bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
183bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
184bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * for progressive JPEG, no scan can have this.
185bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
186bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr = cinfo->scan_info;
187bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
188bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_PROGRESSIVE_SUPPORTED
189bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progressive_mode = TRUE;
190bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    last_bitpos_ptr = & last_bitpos[0][0];
191e5eaf37440b8e337ab150c017df7c03faf846c51DRC    for (ci = 0; ci < cinfo->num_components; ci++)
192bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      for (coefi = 0; coefi < DCTSIZE2; coefi++)
193e5eaf37440b8e337ab150c017df7c03faf846c51DRC        *last_bitpos_ptr++ = -1;
194bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#else
195bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT(cinfo, JERR_NOT_COMPILED);
196bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
197bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
198bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progressive_mode = FALSE;
199e5eaf37440b8e337ab150c017df7c03faf846c51DRC    for (ci = 0; ci < cinfo->num_components; ci++)
200bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      component_sent[ci] = FALSE;
201bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
202bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
203bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
204bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Validate component indexes */
205bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ncomps = scanptr->comps_in_scan;
206bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
207bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
208bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < ncomps; ci++) {
209bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      thisi = scanptr->component_index[ci];
210bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (thisi < 0 || thisi >= cinfo->num_components)
211e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
212bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* Components must appear in SOF order within each scan */
213bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
214e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
215bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
216bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Validate progression parameters */
217bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    Ss = scanptr->Ss;
218bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    Se = scanptr->Se;
219bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    Ah = scanptr->Ah;
220bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    Al = scanptr->Al;
221bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->progressive_mode) {
222bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_PROGRESSIVE_SUPPORTED
2235ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
2245ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       * seems wrong: the upper bound ought to depend on data precision.
2255ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       * Perhaps they really meant 0..N+1 for N-bit precision.
2265ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
2275ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       * out-of-range reconstructed DC values during the first DC scan,
2285ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       * which might cause problems for some decoders.
2295ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       */
2305ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#if BITS_IN_JSAMPLE == 8
2315ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#define MAX_AH_AL 10
2325ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#else
2335ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#define MAX_AH_AL 13
2345ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#endif
235bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
236e5eaf37440b8e337ab150c017df7c03faf846c51DRC          Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
237e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
238bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (Ss == 0) {
239e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (Se != 0)            /* DC and AC together not OK */
240e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
241bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      } else {
242e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (ncomps != 1)        /* AC scans must be for only one component */
243e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
244bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
245bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      for (ci = 0; ci < ncomps; ci++) {
246e5eaf37440b8e337ab150c017df7c03faf846c51DRC        last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
247e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
248e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
249e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (coefi = Ss; coefi <= Se; coefi++) {
250e5eaf37440b8e337ab150c017df7c03faf846c51DRC          if (last_bitpos_ptr[coefi] < 0) {
251e5eaf37440b8e337ab150c017df7c03faf846c51DRC            /* first scan of this coefficient */
252e5eaf37440b8e337ab150c017df7c03faf846c51DRC            if (Ah != 0)
253e5eaf37440b8e337ab150c017df7c03faf846c51DRC              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
254e5eaf37440b8e337ab150c017df7c03faf846c51DRC          } else {
255e5eaf37440b8e337ab150c017df7c03faf846c51DRC            /* not first scan */
256e5eaf37440b8e337ab150c017df7c03faf846c51DRC            if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
257e5eaf37440b8e337ab150c017df7c03faf846c51DRC              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
258e5eaf37440b8e337ab150c017df7c03faf846c51DRC          }
259e5eaf37440b8e337ab150c017df7c03faf846c51DRC          last_bitpos_ptr[coefi] = Al;
260e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
261bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
262bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
263bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else {
264bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* For sequential JPEG, all progression parameters must be these: */
265bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
266e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
267bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* Make sure components are not sent twice */
268bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      for (ci = 0; ci < ncomps; ci++) {
269e5eaf37440b8e337ab150c017df7c03faf846c51DRC        thisi = scanptr->component_index[ci];
270e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (component_sent[thisi])
271e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
272e5eaf37440b8e337ab150c017df7c03faf846c51DRC        component_sent[thisi] = TRUE;
273bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
274bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
275bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
276bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
277bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Now verify that everything got sent. */
278bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->progressive_mode) {
279bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_PROGRESSIVE_SUPPORTED
280bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* For progressive mode, we only check that at least some DC data
281bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * got sent for each component; the spec does not require that all bits
282bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * of all coefficients be transmitted.  Would it be wiser to enforce
283bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * transmission of all coefficient bits??
284bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
285bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < cinfo->num_components; ci++) {
286bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (last_bitpos[ci][0] < 0)
287e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_MISSING_DATA);
288bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
289bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
290bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
291bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < cinfo->num_components; ci++) {
292bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (! component_sent[ci])
293e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_MISSING_DATA);
294bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
295bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
296bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
297bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
298bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif /* C_MULTISCAN_FILES_SUPPORTED */
299bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
300bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
301489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
302bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneselect_scan_parameters (j_compress_ptr cinfo)
303bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Set up the scan parameters for the current scan */
304bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
305bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int ci;
306bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
307bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_MULTISCAN_FILES_SUPPORTED
308bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->scan_info != NULL) {
309bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Prepare for current scan --- the script is already validated */
310bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    my_master_ptr master = (my_master_ptr) cinfo->master;
311bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
312bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
313bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->comps_in_scan = scanptr->comps_in_scan;
314bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
315bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->cur_comp_info[ci] =
316e5eaf37440b8e337ab150c017df7c03faf846c51DRC        &cinfo->comp_info[scanptr->component_index[ci]];
317bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
318bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Ss = scanptr->Ss;
319bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Se = scanptr->Se;
320bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Ah = scanptr->Ah;
321bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Al = scanptr->Al;
322bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
323bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  else
324bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
325bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  {
326bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Prepare for single sequential-JPEG scan containing all components */
327bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
328bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
329e5eaf37440b8e337ab150c017df7c03faf846c51DRC               MAX_COMPS_IN_SCAN);
330bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->comps_in_scan = cinfo->num_components;
331bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < cinfo->num_components; ci++) {
332bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
333bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
334bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Ss = 0;
335bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Se = DCTSIZE2-1;
336bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Ah = 0;
337bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->Al = 0;
338bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
339bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
340bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
341bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
342489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
34336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneper_scan_setup (j_compress_ptr cinfo)
34436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Do computations that are needed before processing a JPEG scan */
34536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
34636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
34736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci, mcublks, tmp;
34836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info *compptr;
349e5eaf37440b8e337ab150c017df7c03faf846c51DRC
35036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->comps_in_scan == 1) {
351e5eaf37440b8e337ab150c017df7c03faf846c51DRC
35236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Noninterleaved (single-component) scan */
35336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr = cinfo->cur_comp_info[0];
354e5eaf37440b8e337ab150c017df7c03faf846c51DRC
35536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Overall image size in MCUs */
35636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->MCUs_per_row = compptr->width_in_blocks;
35736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
358e5eaf37440b8e337ab150c017df7c03faf846c51DRC
35936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* For noninterleaved scan, always one block per MCU */
36036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->MCU_width = 1;
36136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->MCU_height = 1;
36236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->MCU_blocks = 1;
36336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->MCU_sample_width = DCTSIZE;
36436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->last_col_width = 1;
365a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane    /* For noninterleaved scans, it is convenient to define last_row_height
366a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane     * as the number of block rows present in the last iMCU row.
367a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane     */
368a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
369a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane    if (tmp == 0) tmp = compptr->v_samp_factor;
370a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane    compptr->last_row_height = tmp;
371e5eaf37440b8e337ab150c017df7c03faf846c51DRC
37236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Prepare array describing MCU composition */
37336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->blocks_in_MCU = 1;
37436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->MCU_membership[0] = 0;
375e5eaf37440b8e337ab150c017df7c03faf846c51DRC
37636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  } else {
377e5eaf37440b8e337ab150c017df7c03faf846c51DRC
37836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Interleaved (multi-component) scan */
37936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
38036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
381e5eaf37440b8e337ab150c017df7c03faf846c51DRC               MAX_COMPS_IN_SCAN);
382e5eaf37440b8e337ab150c017df7c03faf846c51DRC
38336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Overall image size in MCUs */
38436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->MCUs_per_row = (JDIMENSION)
385c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_width,
386e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_h_samp_factor*DCTSIZE));
38736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->MCU_rows_in_scan = (JDIMENSION)
388c04bd3cc97f44fd9030de1e141754c8775d4e5a5DRC      jdiv_round_up((long) cinfo->_jpeg_height,
389e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_v_samp_factor*DCTSIZE));
390e5eaf37440b8e337ab150c017df7c03faf846c51DRC
39136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->blocks_in_MCU = 0;
392e5eaf37440b8e337ab150c017df7c03faf846c51DRC
39336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
39436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr = cinfo->cur_comp_info[ci];
39536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Sampling factors give # of blocks of component in each MCU */
39636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->MCU_width = compptr->h_samp_factor;
39736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->MCU_height = compptr->v_samp_factor;
39836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
39936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
40036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Figure number of non-dummy blocks in last MCU column & row */
40136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
40236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (tmp == 0) tmp = compptr->MCU_width;
40336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->last_col_width = tmp;
40436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
40536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (tmp == 0) tmp = compptr->MCU_height;
40636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      compptr->last_row_height = tmp;
40736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Prepare array describing MCU composition */
40836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      mcublks = compptr->MCU_blocks;
409bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
410e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
41136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      while (mcublks-- > 0) {
412e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
41336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
41436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
415e5eaf37440b8e337ab150c017df7c03faf846c51DRC
41636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
41736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
41836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Convert restart specified in rows to actual MCU count. */
41936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Note that count must fit in 16 bits, so we provide limiting. */
42036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->restart_in_rows > 0) {
42136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
42236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
4232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
4242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
4252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4262cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4272cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
42836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Per-pass setup.
42936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This is called at the beginning of each pass.  We determine which modules
43036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * will be active during this pass and give them appropriate start_pass calls.
43136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * We also set is_last_pass to indicate whether any more passes will be
43236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * required.
43336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
43436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
435489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
43636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneprepare_for_pass (j_compress_ptr cinfo)
43736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
43836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
43936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
440bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  switch (master->pass_type) {
441bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case main_pass:
442bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Initial pass: will collect input data, and do either Huffman
443bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * optimization or data output for the first scan.
444bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
445bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    select_scan_parameters(cinfo);
446bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    per_scan_setup(cinfo);
44736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (! cinfo->raw_data_in) {
44836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->cconvert->start_pass) (cinfo);
44936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->downsample->start_pass) (cinfo);
45036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
45136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
45236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->fdct->start_pass) (cinfo);
453bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
454bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->coef->start_pass) (cinfo,
455e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                (master->total_passes > 1 ?
456e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
45736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
458bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->optimize_coding) {
459bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* No immediate data output; postpone writing frame/scan headers */
46036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      master->pub.call_pass_startup = FALSE;
461bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else {
462bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* Will write frame/scan headers at first jpeg_write_scanlines call */
463bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->pub.call_pass_startup = TRUE;
464bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
465bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    break;
466bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef ENTROPY_OPT_SUPPORTED
467bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case huff_opt_pass:
468bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Do Huffman optimization for a scan after the first one. */
469bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    select_scan_parameters(cinfo);
470bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    per_scan_setup(cinfo);
471bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
47236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->entropy->start_pass) (cinfo, TRUE);
47336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
474bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->pub.call_pass_startup = FALSE;
47536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      break;
47636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
477bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Special case: Huffman DC refinement scans need no Huffman table
478bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * and therefore we can skip the optimization pass for them.
479bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
480bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_type = output_pass;
481bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_number++;
482bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /*FALLTHROUGH*/
483bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
484bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case output_pass:
485bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Do a data-output pass. */
486bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* We need not repeat per-scan setup if prior optimization pass did it. */
487bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (! cinfo->optimize_coding) {
488bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      select_scan_parameters(cinfo);
489bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      per_scan_setup(cinfo);
490bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
491bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->entropy->start_pass) (cinfo, FALSE);
492bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
493bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* We emit frame/scan headers now */
494bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (master->scan_number == 0)
495bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      (*cinfo->marker->write_frame_header) (cinfo);
496bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->marker->write_scan_header) (cinfo);
497bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pub.call_pass_startup = FALSE;
498bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    break;
499bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  default:
500bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT(cinfo, JERR_NOT_COMPILED);
50136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
50236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
503bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
504bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
50536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up progress monitor's pass info if present */
50636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->progress != NULL) {
50736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->progress->completed_passes = master->pass_number;
508bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->total_passes = master->total_passes;
50936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
51036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
5112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
51336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
51436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Special start-of-pass hook.
51536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
51636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In single-pass processing, we need this hook because we don't want to
51736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * write frame/scan headers during jpeg_start_compress; we want to let the
51836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * application write COM markers etc. between jpeg_start_compress and the
51936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jpeg_write_scanlines loop.
52036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In multi-pass processing, this routine is not used.
52136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
52236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
523489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
52436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanepass_startup (j_compress_ptr cinfo)
52536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
52636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
52736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
52836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (*cinfo->marker->write_frame_header) (cinfo);
52936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (*cinfo->marker->write_scan_header) (cinfo);
53036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
53136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
53236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
53336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
53436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Finish up at end of pass.
53536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
53636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
537489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
53836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanefinish_pass_master (j_compress_ptr cinfo)
53936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
540bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
54136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
542bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* The entropy coder always needs an end-of-pass call,
543bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * either to analyze statistics or to flush its output buffer.
54436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
54536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (*cinfo->entropy->finish_pass) (cinfo);
546bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
547bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Update state for next pass */
548bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  switch (master->pass_type) {
549bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case main_pass:
550bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* next pass is either output of scan 0 (after optimization)
551bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * or output of scan 1 (if no optimization).
552bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
553bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_type = output_pass;
554bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (! cinfo->optimize_coding)
555bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->scan_number++;
556bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    break;
557bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case huff_opt_pass:
558bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* next pass is always output of current scan */
559bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_type = output_pass;
560bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    break;
561bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  case output_pass:
562bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* next pass is either optimization or output of next scan */
563bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->optimize_coding)
564bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->pass_type = huff_opt_pass;
565bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->scan_number++;
566bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    break;
567bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
568bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
569bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pass_number++;
57036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
5712cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5722cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
57336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
57436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize master compression control.
57536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
57636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
577489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
578bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
57936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
58036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_master_ptr master;
5812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
58236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master = (my_master_ptr)
58336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
5845de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                  sizeof(my_comp_master));
58536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->master = (struct jpeg_comp_master *) master;
58636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master->pub.prepare_for_pass = prepare_for_pass;
58736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master->pub.pass_startup = pass_startup;
58836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master->pub.finish_pass = finish_pass_master;
589bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pub.is_last_pass = FALSE;
590bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
591bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Validate parameters, determine derived values */
592989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding  initial_setup(cinfo, transcode_only);
5934a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
594bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->scan_info != NULL) {
595bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_MULTISCAN_FILES_SUPPORTED
596bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    validate_script(cinfo);
597bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#else
598bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT(cinfo, JERR_NOT_COMPILED);
599bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
600bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
601bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progressive_mode = FALSE;
602bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->num_scans = 1;
603bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
604bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
605e5eaf37440b8e337ab150c017df7c03faf846c51DRC  if (cinfo->progressive_mode && !cinfo->arith_code)    /*  TEMPORARY HACK ??? */
606bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
607bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
608bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Initialize my private state */
609bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (transcode_only) {
610bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* no main pass in transcoding */
611bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->optimize_coding)
612bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->pass_type = huff_opt_pass;
613bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    else
614bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->pass_type = output_pass;
615bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
616bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* for normal compression, first pass is always this type: */
617bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_type = main_pass;
618bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
619bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->scan_number = 0;
620bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pass_number = 0;
621bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->optimize_coding)
622bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->total_passes = cinfo->num_scans * 2;
623bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  else
624bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->total_passes = cinfo->num_scans;
6252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
626