Coverage Report

Created: 2026-02-26 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/jcmaster.c
Line
Count
Source
1
/*
2
 * jcmaster.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2003-2010 by Guido Vollbeding.
7
 * Lossless JPEG Modifications:
8
 * Copyright (C) 1999, Ken Murchison.
9
 * libjpeg-turbo Modifications:
10
 * Copyright (C) 2010, 2016, 2018, 2022-2024, 2026, D. R. Commander.
11
 * For conditions of distribution and use, see the accompanying README.ijg
12
 * file.
13
 *
14
 * This file contains master control logic for the JPEG compressor.
15
 * These routines are concerned with parameter validation, initial setup,
16
 * and inter-pass control (determining the number of passes and the work
17
 * to be done in each pass).
18
 */
19
20
#define JPEG_INTERNALS
21
#include "jinclude.h"
22
#include "jpeglib.h"
23
#include "jpegapicomp.h"
24
#include "jcmaster.h"
25
#ifdef WITH_PROFILE
26
#include "tjutil.h"
27
#endif
28
29
30
/*
31
 * Support routines that do various essential calculations.
32
 */
33
34
#if JPEG_LIB_VERSION >= 70
35
/*
36
 * Compute JPEG image dimensions and related values.
37
 * NOTE: this is exported for possible use by application.
38
 * Hence it mustn't do anything that can't be done twice.
39
 */
40
41
GLOBAL(void)
42
jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo)
43
/* Do computations that are needed before master selection phase */
44
{
45
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
46
47
  /* Hardwire it to "no scaling" */
48
  cinfo->jpeg_width = cinfo->image_width;
49
  cinfo->jpeg_height = cinfo->image_height;
50
  cinfo->min_DCT_h_scaled_size = data_unit;
51
  cinfo->min_DCT_v_scaled_size = data_unit;
52
}
53
#endif
54
55
56
LOCAL(boolean)
57
using_std_huff_tables(j_compress_ptr cinfo)
58
0
{
59
0
  int i;
60
61
0
  static const UINT8 bits_dc_luminance[17] = {
62
0
    /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
63
0
  };
64
0
  static const UINT8 val_dc_luminance[] = {
65
0
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
66
0
  };
67
68
0
  static const UINT8 bits_dc_chrominance[17] = {
69
0
    /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
70
0
  };
71
0
  static const UINT8 val_dc_chrominance[] = {
72
0
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
73
0
  };
74
75
0
  static const UINT8 bits_ac_luminance[17] = {
76
0
    /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
77
0
  };
78
0
  static const UINT8 val_ac_luminance[] = {
79
0
    0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
80
0
    0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
81
0
    0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
82
0
    0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
83
0
    0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
84
0
    0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
85
0
    0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
86
0
    0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
87
0
    0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
88
0
    0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
89
0
    0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
90
0
    0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
91
0
    0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
92
0
    0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
93
0
    0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
94
0
    0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
95
0
    0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
96
0
    0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
97
0
    0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
98
0
    0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
99
0
    0xf9, 0xfa
100
0
  };
101
102
0
  static const UINT8 bits_ac_chrominance[17] = {
103
0
    /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
104
0
  };
105
0
  static const UINT8 val_ac_chrominance[] = {
106
0
    0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
107
0
    0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
108
0
    0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
109
0
    0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
110
0
    0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
111
0
    0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
112
0
    0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
113
0
    0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
114
0
    0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
115
0
    0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
116
0
    0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
117
0
    0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
118
0
    0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
119
0
    0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
120
0
    0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
121
0
    0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
122
0
    0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
123
0
    0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
124
0
    0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
125
0
    0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
126
0
    0xf9, 0xfa
127
0
  };
128
129
0
  if (cinfo->dc_huff_tbl_ptrs[0] == NULL ||
130
0
      cinfo->ac_huff_tbl_ptrs[0] == NULL ||
131
0
      cinfo->dc_huff_tbl_ptrs[1] == NULL ||
132
0
      cinfo->ac_huff_tbl_ptrs[1] == NULL)
133
0
    return FALSE;
134
135
0
  for (i = 2; i < NUM_HUFF_TBLS; i++) {
136
0
    if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||
137
0
        cinfo->ac_huff_tbl_ptrs[i] != NULL)
138
0
      return FALSE;
139
0
  }
140
141
0
  if (memcmp(cinfo->dc_huff_tbl_ptrs[0]->bits, bits_dc_luminance,
142
0
             sizeof(bits_dc_luminance)) ||
143
0
      memcmp(cinfo->dc_huff_tbl_ptrs[0]->huffval, val_dc_luminance,
144
0
             sizeof(val_dc_luminance)) ||
145
0
      memcmp(cinfo->ac_huff_tbl_ptrs[0]->bits, bits_ac_luminance,
146
0
             sizeof(bits_ac_luminance)) ||
147
0
      memcmp(cinfo->ac_huff_tbl_ptrs[0]->huffval, val_ac_luminance,
148
0
             sizeof(val_ac_luminance)) ||
149
0
      memcmp(cinfo->dc_huff_tbl_ptrs[1]->bits, bits_dc_chrominance,
150
0
             sizeof(bits_dc_chrominance)) ||
151
0
      memcmp(cinfo->dc_huff_tbl_ptrs[1]->huffval, val_dc_chrominance,
152
0
             sizeof(val_dc_chrominance)) ||
153
0
      memcmp(cinfo->ac_huff_tbl_ptrs[1]->bits, bits_ac_chrominance,
154
0
             sizeof(bits_ac_chrominance)) ||
155
0
      memcmp(cinfo->ac_huff_tbl_ptrs[1]->huffval, val_ac_chrominance,
156
0
             sizeof(val_ac_chrominance)))
157
0
    return FALSE;
158
159
0
  return TRUE;
160
0
}
161
162
163
LOCAL(void)
164
initial_setup(j_compress_ptr cinfo, boolean transcode_only)
165
/* Do computations that are needed before master selection phase */
166
19.9k
{
167
19.9k
  int ci;
168
19.9k
  jpeg_component_info *compptr;
169
19.9k
  long samplesperrow;
170
19.9k
  JDIMENSION jd_samplesperrow;
171
19.9k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
172
173
#if JPEG_LIB_VERSION >= 70
174
#if JPEG_LIB_VERSION >= 80
175
  if (!transcode_only)
176
#endif
177
    jpeg_calc_jpeg_dimensions(cinfo);
178
#endif
179
180
  /* Sanity check on image dimensions */
181
19.9k
  if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
182
19.9k
      cinfo->num_components <= 0 || cinfo->input_components <= 0)
183
0
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
184
185
  /* Make sure image isn't bigger than I can handle */
186
19.9k
  if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||
187
19.9k
      (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)
188
49
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
189
190
  /* Width of an input scanline must be representable as JDIMENSION. */
191
19.9k
  samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;
192
19.9k
  jd_samplesperrow = (JDIMENSION)samplesperrow;
193
19.9k
  if ((long)jd_samplesperrow != samplesperrow)
194
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
195
196
  /* Lossy JPEG images must have 8 or 12 bits per sample.  Lossless JPEG images
197
   * can have 2 to 16 bits per sample.
198
   */
199
19.9k
#ifdef C_LOSSLESS_SUPPORTED
200
19.9k
  if (cinfo->master->lossless) {
201
0
    if (cinfo->data_precision < 2 || cinfo->data_precision > 16)
202
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
203
0
  } else
204
19.9k
#endif
205
19.9k
  {
206
19.9k
    if (cinfo->data_precision != 8 && cinfo->data_precision != 12)
207
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
208
19.9k
  }
209
210
  /* Check that number of components won't exceed internal array sizes */
211
19.9k
  if (cinfo->num_components > MAX_COMPONENTS)
212
0
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
213
19.9k
             MAX_COMPONENTS);
214
215
  /* Compute maximum sampling factors; check factor validity */
216
19.9k
  cinfo->max_h_samp_factor = 1;
217
19.9k
  cinfo->max_v_samp_factor = 1;
218
66.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
219
46.4k
       ci++, compptr++) {
220
46.4k
    if (compptr->h_samp_factor <= 0 ||
221
46.4k
        compptr->h_samp_factor > MAX_SAMP_FACTOR ||
222
46.4k
        compptr->v_samp_factor <= 0 ||
223
46.4k
        compptr->v_samp_factor > MAX_SAMP_FACTOR)
224
0
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
225
46.4k
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
226
46.4k
                                   compptr->h_samp_factor);
227
46.4k
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
228
46.4k
                                   compptr->v_samp_factor);
229
46.4k
  }
230
231
  /* Compute dimensions of components */
232
66.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
233
46.4k
       ci++, compptr++) {
234
    /* Fill in the correct component_index value; don't rely on application */
235
46.4k
    compptr->component_index = ci;
236
    /* For compression, we never do DCT scaling. */
237
#if JPEG_LIB_VERSION >= 70
238
    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit;
239
#else
240
46.4k
    compptr->DCT_scaled_size = data_unit;
241
46.4k
#endif
242
    /* Size in data units */
243
46.4k
    compptr->width_in_blocks = (JDIMENSION)
244
46.4k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
245
46.4k
                    (long)(cinfo->max_h_samp_factor * data_unit));
246
46.4k
    compptr->height_in_blocks = (JDIMENSION)
247
46.4k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
248
46.4k
                    (long)(cinfo->max_v_samp_factor * data_unit));
249
    /* Size in samples */
250
46.4k
    compptr->downsampled_width = (JDIMENSION)
251
46.4k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
252
46.4k
                    (long)cinfo->max_h_samp_factor);
253
46.4k
    compptr->downsampled_height = (JDIMENSION)
254
46.4k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
255
46.4k
                    (long)cinfo->max_v_samp_factor);
256
    /* Mark component needed (this flag isn't actually used for compression) */
257
46.4k
    compptr->component_needed = TRUE;
258
46.4k
  }
259
260
  /* Compute number of fully interleaved MCU rows (number of times that
261
   * main controller will call coefficient or difference controller).
262
   */
263
19.9k
  cinfo->total_iMCU_rows = (JDIMENSION)
264
19.9k
    jdiv_round_up((long)cinfo->_jpeg_height,
265
19.9k
                  (long)(cinfo->max_v_samp_factor * data_unit));
266
19.9k
}
267
268
269
#if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED)
270
#define NEED_SCAN_SCRIPT
271
#endif
272
273
#ifdef NEED_SCAN_SCRIPT
274
275
LOCAL(void)
276
validate_script(j_compress_ptr cinfo)
277
/* Verify that the scan script in cinfo->scan_info[] is valid; also
278
 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
279
 */
280
6.56k
{
281
6.56k
  const jpeg_scan_info *scanptr;
282
6.56k
  int scanno, ncomps, ci, coefi, thisi;
283
6.56k
  int Ss, Se, Ah, Al;
284
6.56k
  boolean component_sent[MAX_COMPONENTS];
285
6.56k
#ifdef C_PROGRESSIVE_SUPPORTED
286
6.56k
  int *last_bitpos_ptr;
287
6.56k
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
288
  /* -1 until that coefficient has been seen; then last Al for it */
289
6.56k
#endif
290
291
6.56k
  if (cinfo->num_scans <= 0)
292
0
    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
293
294
#ifndef C_MULTISCAN_FILES_SUPPORTED
295
  if (cinfo->num_scans > 1)
296
    ERREXIT(cinfo, JERR_NOT_COMPILED);
297
#endif
298
299
6.56k
  scanptr = cinfo->scan_info;
300
6.56k
  if (scanptr->Ss != 0 && scanptr->Se == 0) {
301
0
#ifdef C_LOSSLESS_SUPPORTED
302
0
    cinfo->master->lossless = TRUE;
303
0
    cinfo->progressive_mode = FALSE;
304
0
    for (ci = 0; ci < cinfo->num_components; ci++)
305
0
      component_sent[ci] = FALSE;
306
#else
307
    ERREXIT(cinfo, JERR_NOT_COMPILED);
308
#endif
309
0
  }
310
  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
311
   * for progressive JPEG, no scan can have this.
312
   */
313
6.56k
  else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {
314
6.56k
#ifdef C_PROGRESSIVE_SUPPORTED
315
6.56k
    cinfo->progressive_mode = TRUE;
316
6.56k
    cinfo->master->lossless = FALSE;
317
6.56k
    last_bitpos_ptr = &last_bitpos[0][0];
318
19.7k
    for (ci = 0; ci < cinfo->num_components; ci++)
319
859k
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
320
846k
        *last_bitpos_ptr++ = -1;
321
#else
322
    ERREXIT(cinfo, JERR_NOT_COMPILED);
323
#endif
324
6.56k
  } else {
325
0
    cinfo->progressive_mode = cinfo->master->lossless = FALSE;
326
0
    for (ci = 0; ci < cinfo->num_components; ci++)
327
0
      component_sent[ci] = FALSE;
328
0
  }
329
330
59.2k
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
331
    /* Validate component indexes */
332
52.7k
    ncomps = scanptr->comps_in_scan;
333
52.7k
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
334
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
335
118k
    for (ci = 0; ci < ncomps; ci++) {
336
66.0k
      thisi = scanptr->component_index[ci];
337
66.0k
      if (thisi < 0 || thisi >= cinfo->num_components)
338
0
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
339
      /* Components must appear in SOF order within each scan */
340
66.0k
      if (ci > 0 && thisi <= scanptr->component_index[ci - 1])
341
0
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
342
66.0k
    }
343
    /* Validate progression parameters */
344
52.7k
    Ss = scanptr->Ss;
345
52.7k
    Se = scanptr->Se;
346
52.7k
    Ah = scanptr->Ah;
347
52.7k
    Al = scanptr->Al;
348
52.7k
    if (cinfo->progressive_mode) {
349
52.7k
#ifdef C_PROGRESSIVE_SUPPORTED
350
      /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah
351
       * and Al, but that seems wrong: the upper bound ought to depend on data
352
       * precision.  Perhaps they really meant 0..N+1 for N-bit precision.
353
       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
354
       * out-of-range reconstructed DC values during the first DC scan,
355
       * which might cause problems for some decoders.
356
       */
357
52.7k
      int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;
358
359
52.7k
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
360
52.7k
          Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)
361
0
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
362
52.7k
      if (Ss == 0) {
363
13.1k
        if (Se != 0)            /* DC and AC together not OK */
364
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
365
39.5k
      } else {
366
39.5k
        if (ncomps != 1)        /* AC scans must be for only one component */
367
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
368
39.5k
      }
369
118k
      for (ci = 0; ci < ncomps; ci++) {
370
66.0k
        last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
371
66.0k
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
372
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
373
2.17M
        for (coefi = Ss; coefi <= Se; coefi++) {
374
2.10M
          if (last_bitpos_ptr[coefi] < 0) {
375
            /* first scan of this coefficient */
376
846k
            if (Ah != 0)
377
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
378
1.26M
          } else {
379
            /* not first scan */
380
1.26M
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)
381
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
382
1.26M
          }
383
2.10M
          last_bitpos_ptr[coefi] = Al;
384
2.10M
        }
385
66.0k
      }
386
52.7k
#endif
387
52.7k
    } else {
388
0
#ifdef C_LOSSLESS_SUPPORTED
389
0
      if (cinfo->master->lossless) {
390
        /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that
391
         * seems wrong: the upper bound ought to depend on data precision.
392
         * Perhaps they really meant 0..N-1 for N-bit precision, which is what
393
         * we allow here.  Values greater than or equal to the data precision
394
         * will result in a blank image.
395
         */
396
0
        if (Ss < 1 || Ss > 7 ||         /* predictor selection value */
397
0
            Se != 0 || Ah != 0 ||
398
0
            Al < 0 || Al >= cinfo->data_precision) /* point transform */
399
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
400
0
      } else
401
0
#endif
402
0
      {
403
        /* For sequential JPEG, all progression parameters must be these: */
404
0
        if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)
405
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
406
0
      }
407
      /* Make sure components are not sent twice */
408
0
      for (ci = 0; ci < ncomps; ci++) {
409
0
        thisi = scanptr->component_index[ci];
410
0
        if (component_sent[thisi])
411
0
          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
412
0
        component_sent[thisi] = TRUE;
413
0
      }
414
0
    }
415
52.7k
  }
416
417
  /* Now verify that everything got sent. */
418
6.56k
  if (cinfo->progressive_mode) {
419
6.56k
#ifdef C_PROGRESSIVE_SUPPORTED
420
    /* For progressive mode, we only check that at least some DC data
421
     * got sent for each component; the spec does not require that all bits
422
     * of all coefficients be transmitted.  Would it be wiser to enforce
423
     * transmission of all coefficient bits??
424
     */
425
19.7k
    for (ci = 0; ci < cinfo->num_components; ci++) {
426
13.2k
      if (last_bitpos[ci][0] < 0)
427
0
        ERREXIT(cinfo, JERR_MISSING_DATA);
428
13.2k
    }
429
6.56k
#endif
430
6.56k
  } else {
431
0
    for (ci = 0; ci < cinfo->num_components; ci++) {
432
0
      if (!component_sent[ci])
433
0
        ERREXIT(cinfo, JERR_MISSING_DATA);
434
0
    }
435
0
  }
436
6.56k
}
437
438
#endif /* NEED_SCAN_SCRIPT */
439
440
441
LOCAL(void)
442
select_scan_parameters(j_compress_ptr cinfo)
443
/* Set up the scan parameters for the current scan */
444
32.9k
{
445
32.9k
  int ci;
446
447
32.9k
#ifdef NEED_SCAN_SCRIPT
448
32.9k
  if (cinfo->scan_info != NULL) {
449
    /* Prepare for current scan --- the script is already validated */
450
26.3k
    my_master_ptr master = (my_master_ptr)cinfo->master;
451
26.3k
    const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
452
453
26.3k
    cinfo->comps_in_scan = scanptr->comps_in_scan;
454
59.2k
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
455
32.9k
      cinfo->cur_comp_info[ci] =
456
32.9k
        &cinfo->comp_info[scanptr->component_index[ci]];
457
32.9k
    }
458
26.3k
    cinfo->Ss = scanptr->Ss;
459
26.3k
    cinfo->Se = scanptr->Se;
460
26.3k
    cinfo->Ah = scanptr->Ah;
461
26.3k
    cinfo->Al = scanptr->Al;
462
26.3k
  } else
463
6.66k
#endif
464
6.66k
  {
465
    /* Prepare for single sequential-JPEG scan containing all components */
466
6.66k
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
467
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
468
6.66k
               MAX_COMPS_IN_SCAN);
469
6.66k
    cinfo->comps_in_scan = cinfo->num_components;
470
23.3k
    for (ci = 0; ci < cinfo->num_components; ci++) {
471
16.6k
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
472
16.6k
    }
473
6.66k
    if (!cinfo->master->lossless) {
474
6.66k
      cinfo->Ss = 0;
475
6.66k
      cinfo->Se = DCTSIZE2 - 1;
476
6.66k
      cinfo->Ah = 0;
477
6.66k
      cinfo->Al = 0;
478
6.66k
    }
479
6.66k
  }
480
32.9k
}
481
482
483
LOCAL(void)
484
per_scan_setup(j_compress_ptr cinfo)
485
/* Do computations that are needed before processing a JPEG scan */
486
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
487
32.9k
{
488
32.9k
  int ci, mcublks, tmp;
489
32.9k
  jpeg_component_info *compptr;
490
32.9k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
491
492
32.9k
  if (cinfo->comps_in_scan == 1) {
493
494
    /* Noninterleaved (single-component) scan */
495
24.6k
    compptr = cinfo->cur_comp_info[0];
496
497
    /* Overall image size in MCUs */
498
24.6k
    cinfo->MCUs_per_row = compptr->width_in_blocks;
499
24.6k
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
500
501
    /* For noninterleaved scan, always one block per MCU */
502
24.6k
    compptr->MCU_width = 1;
503
24.6k
    compptr->MCU_height = 1;
504
24.6k
    compptr->MCU_blocks = 1;
505
24.6k
    compptr->MCU_sample_width = data_unit;
506
24.6k
    compptr->last_col_width = 1;
507
    /* For noninterleaved scans, it is convenient to define last_row_height
508
     * as the number of block rows present in the last iMCU row.
509
     */
510
24.6k
    tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
511
24.6k
    if (tmp == 0) tmp = compptr->v_samp_factor;
512
24.6k
    compptr->last_row_height = tmp;
513
514
    /* Prepare array describing MCU composition */
515
24.6k
    cinfo->blocks_in_MCU = 1;
516
24.6k
    cinfo->MCU_membership[0] = 0;
517
518
24.6k
  } else {
519
520
    /* Interleaved (multi-component) scan */
521
8.31k
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
522
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
523
8.31k
               MAX_COMPS_IN_SCAN);
524
525
    /* Overall image size in MCUs */
526
8.31k
    cinfo->MCUs_per_row = (JDIMENSION)
527
8.31k
      jdiv_round_up((long)cinfo->_jpeg_width,
528
8.31k
                    (long)(cinfo->max_h_samp_factor * data_unit));
529
8.31k
    cinfo->MCU_rows_in_scan = (JDIMENSION)
530
8.31k
      jdiv_round_up((long)cinfo->_jpeg_height,
531
8.31k
                    (long)(cinfo->max_v_samp_factor * data_unit));
532
533
8.31k
    cinfo->blocks_in_MCU = 0;
534
535
33.2k
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
536
24.9k
      compptr = cinfo->cur_comp_info[ci];
537
      /* Sampling factors give # of blocks of component in each MCU */
538
24.9k
      compptr->MCU_width = compptr->h_samp_factor;
539
24.9k
      compptr->MCU_height = compptr->v_samp_factor;
540
24.9k
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
541
24.9k
      compptr->MCU_sample_width = compptr->MCU_width * data_unit;
542
      /* Figure number of non-dummy blocks in last MCU column & row */
543
24.9k
      tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
544
24.9k
      if (tmp == 0) tmp = compptr->MCU_width;
545
24.9k
      compptr->last_col_width = tmp;
546
24.9k
      tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
547
24.9k
      if (tmp == 0) tmp = compptr->MCU_height;
548
24.9k
      compptr->last_row_height = tmp;
549
      /* Prepare array describing MCU composition */
550
24.9k
      mcublks = compptr->MCU_blocks;
551
24.9k
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
552
0
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
553
63.1k
      while (mcublks-- > 0) {
554
38.2k
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
555
38.2k
      }
556
24.9k
    }
557
558
8.31k
  }
559
560
  /* Convert restart specified in rows to actual MCU count. */
561
  /* Note that count must fit in 16 bits, so we provide limiting. */
562
32.9k
  if (cinfo->restart_in_rows > 0) {
563
0
    long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;
564
0
    cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);
565
0
  }
566
32.9k
}
567
568
569
/*
570
 * Per-pass setup.
571
 * This is called at the beginning of each pass.  We determine which modules
572
 * will be active during this pass and give them appropriate start_pass calls.
573
 * We also set is_last_pass to indicate whether any more passes will be
574
 * required.
575
 */
576
577
METHODDEF(void)
578
prepare_for_pass(j_compress_ptr cinfo)
579
49.5k
{
580
49.5k
  my_master_ptr master = (my_master_ptr)cinfo->master;
581
582
49.5k
  switch (master->pass_type) {
583
9.93k
  case main_pass:
584
    /* Initial pass: will collect input data, and do either Huffman
585
     * optimization or data output for the first scan.
586
     */
587
9.93k
    select_scan_parameters(cinfo);
588
9.93k
    per_scan_setup(cinfo);
589
9.93k
    if (!cinfo->raw_data_in) {
590
0
      (*cinfo->cconvert->start_pass) (cinfo);
591
0
      (*cinfo->downsample->start_pass) (cinfo);
592
0
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
593
0
    }
594
9.93k
    (*cinfo->fdct->start_pass) (cinfo);
595
9.93k
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
596
9.93k
    (*cinfo->coef->start_pass) (cinfo,
597
9.93k
                                (master->total_passes > 1 ?
598
4.99k
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
599
9.93k
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
600
9.93k
    if (cinfo->optimize_coding) {
601
      /* No immediate data output; postpone writing frame/scan headers */
602
3.32k
      master->pub.call_pass_startup = FALSE;
603
6.61k
    } else {
604
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
605
6.61k
      master->pub.call_pass_startup = TRUE;
606
6.61k
    }
607
9.93k
    break;
608
0
#ifdef ENTROPY_OPT_SUPPORTED
609
14.9k
  case huff_opt_pass:
610
    /* Do Huffman optimization for a scan after the first one. */
611
14.9k
    select_scan_parameters(cinfo);
612
14.9k
    per_scan_setup(cinfo);
613
14.9k
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||
614
13.2k
        cinfo->master->lossless) {
615
13.2k
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
616
13.2k
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
617
13.2k
      master->pub.call_pass_startup = FALSE;
618
13.2k
      break;
619
13.2k
    }
620
    /* Special case: Huffman DC refinement scans need no Huffman table
621
     * and therefore we can skip the optimization pass for them.
622
     */
623
1.65k
    master->pass_type = output_pass;
624
1.65k
    master->pass_number++;
625
1.65k
#endif
626
1.65k
    FALLTHROUGH                 /*FALLTHROUGH*/
627
26.3k
  case output_pass:
628
    /* Do a data-output pass. */
629
    /* We need not repeat per-scan setup if prior optimization pass did it. */
630
26.3k
    if (!cinfo->optimize_coding) {
631
8.09k
      select_scan_parameters(cinfo);
632
8.09k
      per_scan_setup(cinfo);
633
8.09k
    }
634
26.3k
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
635
26.3k
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
636
    /* We emit frame/scan headers now */
637
26.3k
    if (master->scan_number == 0)
638
3.32k
      (*cinfo->marker->write_frame_header) (cinfo);
639
26.3k
    (*cinfo->marker->write_scan_header) (cinfo);
640
26.3k
    master->pub.call_pass_startup = FALSE;
641
26.3k
    break;
642
0
  default:
643
0
    ERREXIT(cinfo, JERR_NOT_COMPILED);
644
49.5k
  }
645
646
49.5k
  master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
647
648
  /* Set up progress monitor's pass info if present */
649
49.5k
  if (cinfo->progress != NULL) {
650
0
    cinfo->progress->completed_passes = master->pass_number;
651
0
    cinfo->progress->total_passes = master->total_passes;
652
0
  }
653
49.5k
}
654
655
656
/*
657
 * Special start-of-pass hook.
658
 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
659
 * In single-pass processing, we need this hook because we don't want to
660
 * write frame/scan headers during jpeg_start_compress; we want to let the
661
 * application write COM markers etc. between jpeg_start_compress and the
662
 * jpeg_write_scanlines loop.
663
 * In multi-pass processing, this routine is not used.
664
 */
665
666
METHODDEF(void)
667
pass_startup(j_compress_ptr cinfo)
668
6.61k
{
669
6.61k
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
670
671
6.61k
  (*cinfo->marker->write_frame_header) (cinfo);
672
6.61k
  (*cinfo->marker->write_scan_header) (cinfo);
673
6.61k
}
674
675
676
/*
677
 * Finish up at end of pass.
678
 */
679
680
METHODDEF(void)
681
finish_pass_master(j_compress_ptr cinfo)
682
49.5k
{
683
49.5k
  my_master_ptr master = (my_master_ptr)cinfo->master;
684
685
  /* The entropy coder always needs an end-of-pass call,
686
   * either to analyze statistics or to flush its output buffer.
687
   */
688
49.5k
  (*cinfo->entropy->finish_pass) (cinfo);
689
690
  /* Update state for next pass */
691
49.5k
  switch (master->pass_type) {
692
9.93k
  case main_pass:
693
    /* next pass is either output of scan 0 (after optimization)
694
     * or output of scan 1 (if no optimization).
695
     */
696
9.93k
    master->pass_type = output_pass;
697
9.93k
    if (!cinfo->optimize_coding)
698
6.61k
      master->scan_number++;
699
9.93k
    break;
700
13.2k
  case huff_opt_pass:
701
    /* next pass is always output of current scan */
702
13.2k
    master->pass_type = output_pass;
703
13.2k
    break;
704
26.3k
  case output_pass:
705
    /* next pass is either optimization or output of next scan */
706
26.3k
    if (cinfo->optimize_coding)
707
18.2k
      master->pass_type = huff_opt_pass;
708
26.3k
    master->scan_number++;
709
26.3k
    break;
710
49.5k
  }
711
712
49.5k
  master->pass_number++;
713
49.5k
}
714
715
716
/*
717
 * Initialize master compression control.
718
 */
719
720
GLOBAL(void)
721
jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)
722
19.9k
{
723
19.9k
  my_master_ptr master = (my_master_ptr)cinfo->master;
724
19.9k
  boolean empty_huff_tables = TRUE;
725
19.9k
  int i;
726
727
#ifdef WITH_PROFILE
728
  master->pub.total_start = getTime();
729
#endif
730
731
19.9k
  master->pub.prepare_for_pass = prepare_for_pass;
732
19.9k
  master->pub.pass_startup = pass_startup;
733
19.9k
  master->pub.finish_pass = finish_pass_master;
734
19.9k
  master->pub.is_last_pass = FALSE;
735
736
19.9k
  if (cinfo->scan_info != NULL) {
737
6.56k
#ifdef NEED_SCAN_SCRIPT
738
6.56k
    validate_script(cinfo);
739
#else
740
    ERREXIT(cinfo, JERR_NOT_COMPILED);
741
#endif
742
13.3k
  } else {
743
13.3k
    cinfo->progressive_mode = FALSE;
744
13.3k
    cinfo->num_scans = 1;
745
13.3k
  }
746
747
19.9k
#ifdef C_LOSSLESS_SUPPORTED
748
  /* Disable smoothing and subsampling in lossless mode, since those are lossy
749
   * algorithms.  Set the JPEG colorspace to the input colorspace.  Disable raw
750
   * (downsampled) data input, because it isn't particularly useful without
751
   * subsampling and has not been tested in lossless mode.
752
   */
753
19.9k
  if (cinfo->master->lossless) {
754
0
    int ci;
755
0
    jpeg_component_info *compptr;
756
757
0
    cinfo->raw_data_in = FALSE;
758
0
    cinfo->smoothing_factor = 0;
759
0
    jpeg_default_colorspace(cinfo);
760
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
761
0
         ci++, compptr++)
762
0
      compptr->h_samp_factor = compptr->v_samp_factor = 1;
763
0
  }
764
19.9k
#endif
765
766
  /* Validate parameters, determine derived values */
767
19.9k
  initial_setup(cinfo, transcode_only);
768
769
19.9k
  if (cinfo->arith_code)
770
6.57k
    cinfo->optimize_coding = FALSE;
771
13.3k
  else {
772
13.3k
    if (cinfo->master->lossless ||      /*  TEMPORARY HACK ??? */
773
13.3k
        cinfo->progressive_mode)
774
3.31k
      cinfo->optimize_coding = TRUE; /* assume default tables no good for
775
                                        progressive mode or lossless mode */
776
13.3k
    for (i = 0; i < NUM_HUFF_TBLS; i++) {
777
13.3k
      if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||
778
13.3k
          cinfo->ac_huff_tbl_ptrs[i] != NULL) {
779
13.3k
        empty_huff_tables = FALSE;
780
13.3k
        break;
781
13.3k
      }
782
13.3k
    }
783
13.3k
    if (cinfo->data_precision == 12 && !cinfo->optimize_coding &&
784
0
        (empty_huff_tables || using_std_huff_tables(cinfo)))
785
0
      cinfo->optimize_coding = TRUE; /* assume default tables no good for
786
                                        12-bit data precision */
787
13.3k
  }
788
789
  /* Initialize my private state */
790
19.9k
  if (transcode_only) {
791
    /* no main pass in transcoding */
792
0
    if (cinfo->optimize_coding)
793
0
      master->pass_type = huff_opt_pass;
794
0
    else
795
0
      master->pass_type = output_pass;
796
19.9k
  } else {
797
    /* for normal compression, first pass is always this type: */
798
19.9k
    master->pass_type = main_pass;
799
19.9k
  }
800
19.9k
  master->scan_number = 0;
801
19.9k
  master->pass_number = 0;
802
19.9k
  if (cinfo->optimize_coding)
803
6.65k
    master->total_passes = cinfo->num_scans * 2;
804
13.2k
  else
805
13.2k
    master->total_passes = cinfo->num_scans;
806
807
19.9k
  master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
808
19.9k
}