Coverage Report

Created: 2024-08-27 12:18

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