~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/3rdparty/libjpeg/jcmaster.c

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * jcmaster.c
 
3
 *
 
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
 
5
 * Modified 2003-2011 by Guido Vollbeding.
 
6
 * This file is part of the Independent JPEG Group's software.
 
7
 * For conditions of distribution and use, see the accompanying README file.
 
8
 *
 
9
 * This file contains master control logic for the JPEG compressor.
 
10
 * These routines are concerned with parameter validation, initial setup,
 
11
 * and inter-pass control (determining the number of passes and the work 
 
12
 * to be done in each pass).
 
13
 */
 
14
 
 
15
#define JPEG_INTERNALS
 
16
#include "jinclude.h"
 
17
#include "jpeglib.h"
 
18
 
 
19
 
 
20
/* Private state */
 
21
 
 
22
typedef enum {
 
23
        main_pass,              /* input data, also do first output step */
 
24
        huff_opt_pass,          /* Huffman code optimization pass */
 
25
        output_pass             /* data output pass */
 
26
} c_pass_type;
 
27
 
 
28
typedef struct {
 
29
  struct jpeg_comp_master pub;  /* public fields */
 
30
 
 
31
  c_pass_type pass_type;        /* the type of the current pass */
 
32
 
 
33
  int pass_number;              /* # of passes completed */
 
34
  int total_passes;             /* total # of passes needed */
 
35
 
 
36
  int scan_number;              /* current index in scan_info[] */
 
37
} my_comp_master;
 
38
 
 
39
typedef my_comp_master * my_master_ptr;
 
40
 
 
41
 
 
42
/*
 
43
 * Support routines that do various essential calculations.
 
44
 */
 
45
 
 
46
/*
 
47
 * Compute JPEG image dimensions and related values.
 
48
 * NOTE: this is exported for possible use by application.
 
49
 * Hence it mustn't do anything that can't be done twice.
 
50
 */
 
51
 
 
52
GLOBAL(void)
 
53
jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
 
54
/* Do computations that are needed before master selection phase */
 
55
{
 
56
#ifdef DCT_SCALING_SUPPORTED
 
57
 
 
58
  /* Sanity check on input image dimensions to prevent overflow in
 
59
   * following calculation.
 
60
   * We do check jpeg_width and jpeg_height in initial_setup below,
 
61
   * but image_width and image_height can come from arbitrary data,
 
62
   * and we need some space for multiplication by block_size.
 
63
   */
 
64
  if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
 
65
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
 
66
 
 
67
  /* Compute actual JPEG image dimensions and DCT scaling choices. */
 
68
  if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
 
69
    /* Provide block_size/1 scaling */
 
70
    cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
 
71
    cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
 
72
    cinfo->min_DCT_h_scaled_size = 1;
 
73
    cinfo->min_DCT_v_scaled_size = 1;
 
74
  } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
 
75
    /* Provide block_size/2 scaling */
 
76
    cinfo->jpeg_width = (JDIMENSION)
 
77
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
 
78
    cinfo->jpeg_height = (JDIMENSION)
 
79
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
 
80
    cinfo->min_DCT_h_scaled_size = 2;
 
81
    cinfo->min_DCT_v_scaled_size = 2;
 
82
  } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
 
83
    /* Provide block_size/3 scaling */
 
84
    cinfo->jpeg_width = (JDIMENSION)
 
85
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
 
86
    cinfo->jpeg_height = (JDIMENSION)
 
87
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
 
88
    cinfo->min_DCT_h_scaled_size = 3;
 
89
    cinfo->min_DCT_v_scaled_size = 3;
 
90
  } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
 
91
    /* Provide block_size/4 scaling */
 
92
    cinfo->jpeg_width = (JDIMENSION)
 
93
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
 
94
    cinfo->jpeg_height = (JDIMENSION)
 
95
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
 
96
    cinfo->min_DCT_h_scaled_size = 4;
 
97
    cinfo->min_DCT_v_scaled_size = 4;
 
98
  } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
 
99
    /* Provide block_size/5 scaling */
 
100
    cinfo->jpeg_width = (JDIMENSION)
 
101
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
 
102
    cinfo->jpeg_height = (JDIMENSION)
 
103
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
 
104
    cinfo->min_DCT_h_scaled_size = 5;
 
105
    cinfo->min_DCT_v_scaled_size = 5;
 
106
  } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
 
107
    /* Provide block_size/6 scaling */
 
108
    cinfo->jpeg_width = (JDIMENSION)
 
109
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
 
110
    cinfo->jpeg_height = (JDIMENSION)
 
111
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
 
112
    cinfo->min_DCT_h_scaled_size = 6;
 
113
    cinfo->min_DCT_v_scaled_size = 6;
 
114
  } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
 
115
    /* Provide block_size/7 scaling */
 
116
    cinfo->jpeg_width = (JDIMENSION)
 
117
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
 
118
    cinfo->jpeg_height = (JDIMENSION)
 
119
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
 
120
    cinfo->min_DCT_h_scaled_size = 7;
 
121
    cinfo->min_DCT_v_scaled_size = 7;
 
122
  } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
 
123
    /* Provide block_size/8 scaling */
 
124
    cinfo->jpeg_width = (JDIMENSION)
 
125
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
 
126
    cinfo->jpeg_height = (JDIMENSION)
 
127
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
 
128
    cinfo->min_DCT_h_scaled_size = 8;
 
129
    cinfo->min_DCT_v_scaled_size = 8;
 
130
  } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
 
131
    /* Provide block_size/9 scaling */
 
132
    cinfo->jpeg_width = (JDIMENSION)
 
133
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
 
134
    cinfo->jpeg_height = (JDIMENSION)
 
135
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
 
136
    cinfo->min_DCT_h_scaled_size = 9;
 
137
    cinfo->min_DCT_v_scaled_size = 9;
 
138
  } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
 
139
    /* Provide block_size/10 scaling */
 
140
    cinfo->jpeg_width = (JDIMENSION)
 
141
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
 
142
    cinfo->jpeg_height = (JDIMENSION)
 
143
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
 
144
    cinfo->min_DCT_h_scaled_size = 10;
 
145
    cinfo->min_DCT_v_scaled_size = 10;
 
146
  } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
 
147
    /* Provide block_size/11 scaling */
 
148
    cinfo->jpeg_width = (JDIMENSION)
 
149
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
 
150
    cinfo->jpeg_height = (JDIMENSION)
 
151
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
 
152
    cinfo->min_DCT_h_scaled_size = 11;
 
153
    cinfo->min_DCT_v_scaled_size = 11;
 
154
  } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
 
155
    /* Provide block_size/12 scaling */
 
156
    cinfo->jpeg_width = (JDIMENSION)
 
157
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
 
158
    cinfo->jpeg_height = (JDIMENSION)
 
159
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
 
160
    cinfo->min_DCT_h_scaled_size = 12;
 
161
    cinfo->min_DCT_v_scaled_size = 12;
 
162
  } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
 
163
    /* Provide block_size/13 scaling */
 
164
    cinfo->jpeg_width = (JDIMENSION)
 
165
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
 
166
    cinfo->jpeg_height = (JDIMENSION)
 
167
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
 
168
    cinfo->min_DCT_h_scaled_size = 13;
 
169
    cinfo->min_DCT_v_scaled_size = 13;
 
170
  } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
 
171
    /* Provide block_size/14 scaling */
 
172
    cinfo->jpeg_width = (JDIMENSION)
 
173
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
 
174
    cinfo->jpeg_height = (JDIMENSION)
 
175
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
 
176
    cinfo->min_DCT_h_scaled_size = 14;
 
177
    cinfo->min_DCT_v_scaled_size = 14;
 
178
  } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
 
179
    /* Provide block_size/15 scaling */
 
180
    cinfo->jpeg_width = (JDIMENSION)
 
181
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
 
182
    cinfo->jpeg_height = (JDIMENSION)
 
183
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
 
184
    cinfo->min_DCT_h_scaled_size = 15;
 
185
    cinfo->min_DCT_v_scaled_size = 15;
 
186
  } else {
 
187
    /* Provide block_size/16 scaling */
 
188
    cinfo->jpeg_width = (JDIMENSION)
 
189
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
 
190
    cinfo->jpeg_height = (JDIMENSION)
 
191
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
 
192
    cinfo->min_DCT_h_scaled_size = 16;
 
193
    cinfo->min_DCT_v_scaled_size = 16;
 
194
  }
 
195
 
 
196
#else /* !DCT_SCALING_SUPPORTED */
 
197
 
 
198
  /* Hardwire it to "no scaling" */
 
199
  cinfo->jpeg_width = cinfo->image_width;
 
200
  cinfo->jpeg_height = cinfo->image_height;
 
201
  cinfo->min_DCT_h_scaled_size = DCTSIZE;
 
202
  cinfo->min_DCT_v_scaled_size = DCTSIZE;
 
203
 
 
204
#endif /* DCT_SCALING_SUPPORTED */
 
205
}
 
206
 
 
207
 
 
208
LOCAL(void)
 
209
jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
 
210
{
 
211
  if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
 
212
    ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
 
213
             cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
 
214
 
 
215
  cinfo->block_size = cinfo->min_DCT_h_scaled_size;
 
216
}
 
217
 
 
218
 
 
219
LOCAL(void)
 
220
initial_setup (j_compress_ptr cinfo, boolean transcode_only)
 
221
/* Do computations that are needed before master selection phase */
 
222
{
 
223
  int ci, ssize;
 
224
  jpeg_component_info *compptr;
 
225
  long samplesperrow;
 
226
  JDIMENSION jd_samplesperrow;
 
227
 
 
228
  if (transcode_only)
 
229
    jpeg_calc_trans_dimensions(cinfo);
 
230
  else
 
231
    jpeg_calc_jpeg_dimensions(cinfo);
 
232
 
 
233
  /* Sanity check on block_size */
 
234
  if (cinfo->block_size < 1 || cinfo->block_size > 16)
 
235
    ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size);
 
236
 
 
237
  /* Derive natural_order from block_size */
 
238
  switch (cinfo->block_size) {
 
239
  case 2: cinfo->natural_order = jpeg_natural_order2; break;
 
240
  case 3: cinfo->natural_order = jpeg_natural_order3; break;
 
241
  case 4: cinfo->natural_order = jpeg_natural_order4; break;
 
242
  case 5: cinfo->natural_order = jpeg_natural_order5; break;
 
243
  case 6: cinfo->natural_order = jpeg_natural_order6; break;
 
244
  case 7: cinfo->natural_order = jpeg_natural_order7; break;
 
245
  default: cinfo->natural_order = jpeg_natural_order; break;
 
246
  }
 
247
 
 
248
  /* Derive lim_Se from block_size */
 
249
  cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
 
250
    cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
 
251
 
 
252
  /* Sanity check on image dimensions */
 
253
  if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
 
254
      cinfo->num_components <= 0 || cinfo->input_components <= 0)
 
255
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
 
256
 
 
257
  /* Make sure image isn't bigger than I can handle */
 
258
  if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
 
259
      (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
 
260
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
 
261
 
 
262
  /* Width of an input scanline must be representable as JDIMENSION. */
 
263
  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
 
264
  jd_samplesperrow = (JDIMENSION) samplesperrow;
 
265
  if ((long) jd_samplesperrow != samplesperrow)
 
266
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
 
267
 
 
268
  /* For now, precision must match compiled-in value... */
 
269
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
 
270
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
 
271
 
 
272
  /* Check that number of components won't exceed internal array sizes */
 
273
  if (cinfo->num_components > MAX_COMPONENTS)
 
274
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
 
275
             MAX_COMPONENTS);
 
276
 
 
277
  /* Compute maximum sampling factors; check factor validity */
 
278
  cinfo->max_h_samp_factor = 1;
 
279
  cinfo->max_v_samp_factor = 1;
 
280
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 
281
       ci++, compptr++) {
 
282
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
 
283
        compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
 
284
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
 
285
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
 
286
                                   compptr->h_samp_factor);
 
287
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
 
288
                                   compptr->v_samp_factor);
 
289
  }
 
290
 
 
291
  /* Compute dimensions of components */
 
292
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 
293
       ci++, compptr++) {
 
294
    /* Fill in the correct component_index value; don't rely on application */
 
295
    compptr->component_index = ci;
 
296
    /* In selecting the actual DCT scaling for each component, we try to
 
297
     * scale down the chroma components via DCT scaling rather than downsampling.
 
298
     * This saves time if the downsampler gets to use 1:1 scaling.
 
299
     * Note this code adapts subsampling ratios which are powers of 2.
 
300
     */
 
301
    ssize = 1;
 
302
#ifdef DCT_SCALING_SUPPORTED
 
303
    while (cinfo->min_DCT_h_scaled_size * ssize <=
 
304
           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 
305
           (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
 
306
      ssize = ssize * 2;
 
307
    }
 
308
#endif
 
309
    compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
 
310
    ssize = 1;
 
311
#ifdef DCT_SCALING_SUPPORTED
 
312
    while (cinfo->min_DCT_v_scaled_size * ssize <=
 
313
           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 
314
           (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
 
315
      ssize = ssize * 2;
 
316
    }
 
317
#endif
 
318
    compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
 
319
 
 
320
    /* We don't support DCT ratios larger than 2. */
 
321
    if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
 
322
        compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
 
323
    else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
 
324
        compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
 
325
 
 
326
    /* Size in DCT blocks */
 
327
    compptr->width_in_blocks = (JDIMENSION)
 
328
      jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
 
329
                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
 
330
    compptr->height_in_blocks = (JDIMENSION)
 
331
      jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
 
332
                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 
333
    /* Size in samples */
 
334
    compptr->downsampled_width = (JDIMENSION)
 
335
      jdiv_round_up((long) cinfo->jpeg_width *
 
336
                    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
 
337
                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
 
338
    compptr->downsampled_height = (JDIMENSION)
 
339
      jdiv_round_up((long) cinfo->jpeg_height *
 
340
                    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
 
341
                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 
342
    /* Mark component needed (this flag isn't actually used for compression) */
 
343
    compptr->component_needed = TRUE;
 
344
  }
 
345
 
 
346
  /* Compute number of fully interleaved MCU rows (number of times that
 
347
   * main controller will call coefficient controller).
 
348
   */
 
349
  cinfo->total_iMCU_rows = (JDIMENSION)
 
350
    jdiv_round_up((long) cinfo->jpeg_height,
 
351
                  (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 
352
}
 
353
 
 
354
 
 
355
#ifdef C_MULTISCAN_FILES_SUPPORTED
 
356
 
 
357
LOCAL(void)
 
358
validate_script (j_compress_ptr cinfo)
 
359
/* Verify that the scan script in cinfo->scan_info[] is valid; also
 
360
 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
 
361
 */
 
362
{
 
363
  const jpeg_scan_info * scanptr;
 
364
  int scanno, ncomps, ci, coefi, thisi;
 
365
  int Ss, Se, Ah, Al;
 
366
  boolean component_sent[MAX_COMPONENTS];
 
367
#ifdef C_PROGRESSIVE_SUPPORTED
 
368
  int * last_bitpos_ptr;
 
369
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
 
370
  /* -1 until that coefficient has been seen; then last Al for it */
 
371
#endif
 
372
 
 
373
  if (cinfo->num_scans <= 0)
 
374
    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
 
375
 
 
376
  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
 
377
   * for progressive JPEG, no scan can have this.
 
378
   */
 
379
  scanptr = cinfo->scan_info;
 
380
  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
 
381
#ifdef C_PROGRESSIVE_SUPPORTED
 
382
    cinfo->progressive_mode = TRUE;
 
383
    last_bitpos_ptr = & last_bitpos[0][0];
 
384
    for (ci = 0; ci < cinfo->num_components; ci++) 
 
385
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
 
386
        *last_bitpos_ptr++ = -1;
 
387
#else
 
388
    ERREXIT(cinfo, JERR_NOT_COMPILED);
 
389
#endif
 
390
  } else {
 
391
    cinfo->progressive_mode = FALSE;
 
392
    for (ci = 0; ci < cinfo->num_components; ci++) 
 
393
      component_sent[ci] = FALSE;
 
394
  }
 
395
 
 
396
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
 
397
    /* Validate component indexes */
 
398
    ncomps = scanptr->comps_in_scan;
 
399
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
 
400
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
 
401
    for (ci = 0; ci < ncomps; ci++) {
 
402
      thisi = scanptr->component_index[ci];
 
403
      if (thisi < 0 || thisi >= cinfo->num_components)
 
404
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 
405
      /* Components must appear in SOF order within each scan */
 
406
      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
 
407
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 
408
    }
 
409
    /* Validate progression parameters */
 
410
    Ss = scanptr->Ss;
 
411
    Se = scanptr->Se;
 
412
    Ah = scanptr->Ah;
 
413
    Al = scanptr->Al;
 
414
    if (cinfo->progressive_mode) {
 
415
#ifdef C_PROGRESSIVE_SUPPORTED
 
416
      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
 
417
       * seems wrong: the upper bound ought to depend on data precision.
 
418
       * Perhaps they really meant 0..N+1 for N-bit precision.
 
419
       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
 
420
       * out-of-range reconstructed DC values during the first DC scan,
 
421
       * which might cause problems for some decoders.
 
422
       */
 
423
#if BITS_IN_JSAMPLE == 8
 
424
#define MAX_AH_AL 10
 
425
#else
 
426
#define MAX_AH_AL 13
 
427
#endif
 
428
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
 
429
          Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
 
430
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
431
      if (Ss == 0) {
 
432
        if (Se != 0)            /* DC and AC together not OK */
 
433
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
434
      } else {
 
435
        if (ncomps != 1)        /* AC scans must be for only one component */
 
436
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
437
      }
 
438
      for (ci = 0; ci < ncomps; ci++) {
 
439
        last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
 
440
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
 
441
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
442
        for (coefi = Ss; coefi <= Se; coefi++) {
 
443
          if (last_bitpos_ptr[coefi] < 0) {
 
444
            /* first scan of this coefficient */
 
445
            if (Ah != 0)
 
446
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
447
          } else {
 
448
            /* not first scan */
 
449
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
 
450
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
451
          }
 
452
          last_bitpos_ptr[coefi] = Al;
 
453
        }
 
454
      }
 
455
#endif
 
456
    } else {
 
457
      /* For sequential JPEG, all progression parameters must be these: */
 
458
      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
 
459
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 
460
      /* Make sure components are not sent twice */
 
461
      for (ci = 0; ci < ncomps; ci++) {
 
462
        thisi = scanptr->component_index[ci];
 
463
        if (component_sent[thisi])
 
464
          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 
465
        component_sent[thisi] = TRUE;
 
466
      }
 
467
    }
 
468
  }
 
469
 
 
470
  /* Now verify that everything got sent. */
 
471
  if (cinfo->progressive_mode) {
 
472
#ifdef C_PROGRESSIVE_SUPPORTED
 
473
    /* For progressive mode, we only check that at least some DC data
 
474
     * got sent for each component; the spec does not require that all bits
 
475
     * of all coefficients be transmitted.  Would it be wiser to enforce
 
476
     * transmission of all coefficient bits??
 
477
     */
 
478
    for (ci = 0; ci < cinfo->num_components; ci++) {
 
479
      if (last_bitpos[ci][0] < 0)
 
480
        ERREXIT(cinfo, JERR_MISSING_DATA);
 
481
    }
 
482
#endif
 
483
  } else {
 
484
    for (ci = 0; ci < cinfo->num_components; ci++) {
 
485
      if (! component_sent[ci])
 
486
        ERREXIT(cinfo, JERR_MISSING_DATA);
 
487
    }
 
488
  }
 
489
}
 
490
 
 
491
 
 
492
LOCAL(void)
 
493
reduce_script (j_compress_ptr cinfo)
 
494
/* Adapt scan script for use with reduced block size;
 
495
 * assume that script has been validated before.
 
496
 */
 
497
{
 
498
  jpeg_scan_info * scanptr;
 
499
  int idxout, idxin;
 
500
 
 
501
  /* Circumvent const declaration for this function */
 
502
  scanptr = (jpeg_scan_info *) cinfo->scan_info;
 
503
  idxout = 0;
 
504
 
 
505
  for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
 
506
    /* After skipping, idxout becomes smaller than idxin */
 
507
    if (idxin != idxout)
 
508
      /* Copy rest of data;
 
509
       * note we stay in given chunk of allocated memory.
 
510
       */
 
511
      scanptr[idxout] = scanptr[idxin];
 
512
    if (scanptr[idxout].Ss > cinfo->lim_Se)
 
513
      /* Entire scan out of range - skip this entry */
 
514
      continue;
 
515
    if (scanptr[idxout].Se > cinfo->lim_Se)
 
516
      /* Limit scan to end of block */
 
517
      scanptr[idxout].Se = cinfo->lim_Se;
 
518
    idxout++;
 
519
  }
 
520
 
 
521
  cinfo->num_scans = idxout;
 
522
}
 
523
 
 
524
#endif /* C_MULTISCAN_FILES_SUPPORTED */
 
525
 
 
526
 
 
527
LOCAL(void)
 
528
select_scan_parameters (j_compress_ptr cinfo)
 
529
/* Set up the scan parameters for the current scan */
 
530
{
 
531
  int ci;
 
532
 
 
533
#ifdef C_MULTISCAN_FILES_SUPPORTED
 
534
  if (cinfo->scan_info != NULL) {
 
535
    /* Prepare for current scan --- the script is already validated */
 
536
    my_master_ptr master = (my_master_ptr) cinfo->master;
 
537
    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
 
538
 
 
539
    cinfo->comps_in_scan = scanptr->comps_in_scan;
 
540
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
 
541
      cinfo->cur_comp_info[ci] =
 
542
        &cinfo->comp_info[scanptr->component_index[ci]];
 
543
    }
 
544
    if (cinfo->progressive_mode) {
 
545
      cinfo->Ss = scanptr->Ss;
 
546
      cinfo->Se = scanptr->Se;
 
547
      cinfo->Ah = scanptr->Ah;
 
548
      cinfo->Al = scanptr->Al;
 
549
      return;
 
550
    }
 
551
  }
 
552
  else
 
553
#endif
 
554
  {
 
555
    /* Prepare for single sequential-JPEG scan containing all components */
 
556
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
 
557
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
 
558
               MAX_COMPS_IN_SCAN);
 
559
    cinfo->comps_in_scan = cinfo->num_components;
 
560
    for (ci = 0; ci < cinfo->num_components; ci++) {
 
561
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
 
562
    }
 
563
  }
 
564
  cinfo->Ss = 0;
 
565
  cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
 
566
  cinfo->Ah = 0;
 
567
  cinfo->Al = 0;
 
568
}
 
569
 
 
570
 
 
571
LOCAL(void)
 
572
per_scan_setup (j_compress_ptr cinfo)
 
573
/* Do computations that are needed before processing a JPEG scan */
 
574
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
 
575
{
 
576
  int ci, mcublks, tmp;
 
577
  jpeg_component_info *compptr;
 
578
  
 
579
  if (cinfo->comps_in_scan == 1) {
 
580
    
 
581
    /* Noninterleaved (single-component) scan */
 
582
    compptr = cinfo->cur_comp_info[0];
 
583
    
 
584
    /* Overall image size in MCUs */
 
585
    cinfo->MCUs_per_row = compptr->width_in_blocks;
 
586
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
 
587
    
 
588
    /* For noninterleaved scan, always one block per MCU */
 
589
    compptr->MCU_width = 1;
 
590
    compptr->MCU_height = 1;
 
591
    compptr->MCU_blocks = 1;
 
592
    compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
 
593
    compptr->last_col_width = 1;
 
594
    /* For noninterleaved scans, it is convenient to define last_row_height
 
595
     * as the number of block rows present in the last iMCU row.
 
596
     */
 
597
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
 
598
    if (tmp == 0) tmp = compptr->v_samp_factor;
 
599
    compptr->last_row_height = tmp;
 
600
    
 
601
    /* Prepare array describing MCU composition */
 
602
    cinfo->blocks_in_MCU = 1;
 
603
    cinfo->MCU_membership[0] = 0;
 
604
    
 
605
  } else {
 
606
    
 
607
    /* Interleaved (multi-component) scan */
 
608
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
 
609
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
 
610
               MAX_COMPS_IN_SCAN);
 
611
    
 
612
    /* Overall image size in MCUs */
 
613
    cinfo->MCUs_per_row = (JDIMENSION)
 
614
      jdiv_round_up((long) cinfo->jpeg_width,
 
615
                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
 
616
    cinfo->MCU_rows_in_scan = (JDIMENSION)
 
617
      jdiv_round_up((long) cinfo->jpeg_height,
 
618
                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 
619
    
 
620
    cinfo->blocks_in_MCU = 0;
 
621
    
 
622
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
 
623
      compptr = cinfo->cur_comp_info[ci];
 
624
      /* Sampling factors give # of blocks of component in each MCU */
 
625
      compptr->MCU_width = compptr->h_samp_factor;
 
626
      compptr->MCU_height = compptr->v_samp_factor;
 
627
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
 
628
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
 
629
      /* Figure number of non-dummy blocks in last MCU column & row */
 
630
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
 
631
      if (tmp == 0) tmp = compptr->MCU_width;
 
632
      compptr->last_col_width = tmp;
 
633
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
 
634
      if (tmp == 0) tmp = compptr->MCU_height;
 
635
      compptr->last_row_height = tmp;
 
636
      /* Prepare array describing MCU composition */
 
637
      mcublks = compptr->MCU_blocks;
 
638
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
 
639
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
 
640
      while (mcublks-- > 0) {
 
641
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
 
642
      }
 
643
    }
 
644
    
 
645
  }
 
646
 
 
647
  /* Convert restart specified in rows to actual MCU count. */
 
648
  /* Note that count must fit in 16 bits, so we provide limiting. */
 
649
  if (cinfo->restart_in_rows > 0) {
 
650
    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
 
651
    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
 
652
  }
 
653
}
 
654
 
 
655
 
 
656
/*
 
657
 * Per-pass setup.
 
658
 * This is called at the beginning of each pass.  We determine which modules
 
659
 * will be active during this pass and give them appropriate start_pass calls.
 
660
 * We also set is_last_pass to indicate whether any more passes will be
 
661
 * required.
 
662
 */
 
663
 
 
664
METHODDEF(void)
 
665
prepare_for_pass (j_compress_ptr cinfo)
 
666
{
 
667
  my_master_ptr master = (my_master_ptr) cinfo->master;
 
668
 
 
669
  switch (master->pass_type) {
 
670
  case main_pass:
 
671
    /* Initial pass: will collect input data, and do either Huffman
 
672
     * optimization or data output for the first scan.
 
673
     */
 
674
    select_scan_parameters(cinfo);
 
675
    per_scan_setup(cinfo);
 
676
    if (! cinfo->raw_data_in) {
 
677
      (*cinfo->cconvert->start_pass) (cinfo);
 
678
      (*cinfo->downsample->start_pass) (cinfo);
 
679
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
 
680
    }
 
681
    (*cinfo->fdct->start_pass) (cinfo);
 
682
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
 
683
    (*cinfo->coef->start_pass) (cinfo,
 
684
                                (master->total_passes > 1 ?
 
685
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
 
686
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
 
687
    if (cinfo->optimize_coding) {
 
688
      /* No immediate data output; postpone writing frame/scan headers */
 
689
      master->pub.call_pass_startup = FALSE;
 
690
    } else {
 
691
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
 
692
      master->pub.call_pass_startup = TRUE;
 
693
    }
 
694
    break;
 
695
#ifdef ENTROPY_OPT_SUPPORTED
 
696
  case huff_opt_pass:
 
697
    /* Do Huffman optimization for a scan after the first one. */
 
698
    select_scan_parameters(cinfo);
 
699
    per_scan_setup(cinfo);
 
700
    if (cinfo->Ss != 0 || cinfo->Ah == 0) {
 
701
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
 
702
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
 
703
      master->pub.call_pass_startup = FALSE;
 
704
      break;
 
705
    }
 
706
    /* Special case: Huffman DC refinement scans need no Huffman table
 
707
     * and therefore we can skip the optimization pass for them.
 
708
     */
 
709
    master->pass_type = output_pass;
 
710
    master->pass_number++;
 
711
    /*FALLTHROUGH*/
 
712
#endif
 
713
  case output_pass:
 
714
    /* Do a data-output pass. */
 
715
    /* We need not repeat per-scan setup if prior optimization pass did it. */
 
716
    if (! cinfo->optimize_coding) {
 
717
      select_scan_parameters(cinfo);
 
718
      per_scan_setup(cinfo);
 
719
    }
 
720
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
 
721
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
 
722
    /* We emit frame/scan headers now */
 
723
    if (master->scan_number == 0)
 
724
      (*cinfo->marker->write_frame_header) (cinfo);
 
725
    (*cinfo->marker->write_scan_header) (cinfo);
 
726
    master->pub.call_pass_startup = FALSE;
 
727
    break;
 
728
  default:
 
729
    ERREXIT(cinfo, JERR_NOT_COMPILED);
 
730
  }
 
731
 
 
732
  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
 
733
 
 
734
  /* Set up progress monitor's pass info if present */
 
735
  if (cinfo->progress != NULL) {
 
736
    cinfo->progress->completed_passes = master->pass_number;
 
737
    cinfo->progress->total_passes = master->total_passes;
 
738
  }
 
739
}
 
740
 
 
741
 
 
742
/*
 
743
 * Special start-of-pass hook.
 
744
 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
 
745
 * In single-pass processing, we need this hook because we don't want to
 
746
 * write frame/scan headers during jpeg_start_compress; we want to let the
 
747
 * application write COM markers etc. between jpeg_start_compress and the
 
748
 * jpeg_write_scanlines loop.
 
749
 * In multi-pass processing, this routine is not used.
 
750
 */
 
751
 
 
752
METHODDEF(void)
 
753
pass_startup (j_compress_ptr cinfo)
 
754
{
 
755
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
 
756
 
 
757
  (*cinfo->marker->write_frame_header) (cinfo);
 
758
  (*cinfo->marker->write_scan_header) (cinfo);
 
759
}
 
760
 
 
761
 
 
762
/*
 
763
 * Finish up at end of pass.
 
764
 */
 
765
 
 
766
METHODDEF(void)
 
767
finish_pass_master (j_compress_ptr cinfo)
 
768
{
 
769
  my_master_ptr master = (my_master_ptr) cinfo->master;
 
770
 
 
771
  /* The entropy coder always needs an end-of-pass call,
 
772
   * either to analyze statistics or to flush its output buffer.
 
773
   */
 
774
  (*cinfo->entropy->finish_pass) (cinfo);
 
775
 
 
776
  /* Update state for next pass */
 
777
  switch (master->pass_type) {
 
778
  case main_pass:
 
779
    /* next pass is either output of scan 0 (after optimization)
 
780
     * or output of scan 1 (if no optimization).
 
781
     */
 
782
    master->pass_type = output_pass;
 
783
    if (! cinfo->optimize_coding)
 
784
      master->scan_number++;
 
785
    break;
 
786
  case huff_opt_pass:
 
787
    /* next pass is always output of current scan */
 
788
    master->pass_type = output_pass;
 
789
    break;
 
790
  case output_pass:
 
791
    /* next pass is either optimization or output of next scan */
 
792
    if (cinfo->optimize_coding)
 
793
      master->pass_type = huff_opt_pass;
 
794
    master->scan_number++;
 
795
    break;
 
796
  }
 
797
 
 
798
  master->pass_number++;
 
799
}
 
800
 
 
801
 
 
802
/*
 
803
 * Initialize master compression control.
 
804
 */
 
805
 
 
806
GLOBAL(void)
 
807
jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
 
808
{
 
809
  my_master_ptr master;
 
810
 
 
811
  master = (my_master_ptr)
 
812
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 
813
                                  SIZEOF(my_comp_master));
 
814
  cinfo->master = (struct jpeg_comp_master *) master;
 
815
  master->pub.prepare_for_pass = prepare_for_pass;
 
816
  master->pub.pass_startup = pass_startup;
 
817
  master->pub.finish_pass = finish_pass_master;
 
818
  master->pub.is_last_pass = FALSE;
 
819
 
 
820
  /* Validate parameters, determine derived values */
 
821
  initial_setup(cinfo, transcode_only);
 
822
 
 
823
  if (cinfo->scan_info != NULL) {
 
824
#ifdef C_MULTISCAN_FILES_SUPPORTED
 
825
    validate_script(cinfo);
 
826
    if (cinfo->block_size < DCTSIZE)
 
827
      reduce_script(cinfo);
 
828
#else
 
829
    ERREXIT(cinfo, JERR_NOT_COMPILED);
 
830
#endif
 
831
  } else {
 
832
    cinfo->progressive_mode = FALSE;
 
833
    cinfo->num_scans = 1;
 
834
  }
 
835
 
 
836
  if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
 
837
      !cinfo->arith_code)                       /*  TEMPORARY HACK ??? */
 
838
    /* assume default tables no good for progressive or downscale mode */
 
839
    cinfo->optimize_coding = TRUE;
 
840
 
 
841
  /* Initialize my private state */
 
842
  if (transcode_only) {
 
843
    /* no main pass in transcoding */
 
844
    if (cinfo->optimize_coding)
 
845
      master->pass_type = huff_opt_pass;
 
846
    else
 
847
      master->pass_type = output_pass;
 
848
  } else {
 
849
    /* for normal compression, first pass is always this type: */
 
850
    master->pass_type = main_pass;
 
851
  }
 
852
  master->scan_number = 0;
 
853
  master->pass_number = 0;
 
854
  if (cinfo->optimize_coding)
 
855
    master->total_passes = cinfo->num_scans * 2;
 
856
  else
 
857
    master->total_passes = cinfo->num_scans;
 
858
}