~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Source/LibJPEG/jcmaster.c

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * jcmaster.c
3
3
 *
4
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
 
5
 * Modified 2003-2009 by Guido Vollbeding.
5
6
 * This file is part of the Independent JPEG Group's software.
6
7
 * For conditions of distribution and use, see the accompanying README file.
7
8
 *
42
43
 * Support routines that do various essential calculations.
43
44
 */
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
  /* Compute actual JPEG image dimensions and DCT scaling choices. */
 
59
  if (cinfo->scale_num >= cinfo->scale_denom * 8) {
 
60
    /* Provide 8/1 scaling */
 
61
    cinfo->jpeg_width = cinfo->image_width << 3;
 
62
    cinfo->jpeg_height = cinfo->image_height << 3;
 
63
    cinfo->min_DCT_h_scaled_size = 1;
 
64
    cinfo->min_DCT_v_scaled_size = 1;
 
65
  } else if (cinfo->scale_num >= cinfo->scale_denom * 4) {
 
66
    /* Provide 4/1 scaling */
 
67
    cinfo->jpeg_width = cinfo->image_width << 2;
 
68
    cinfo->jpeg_height = cinfo->image_height << 2;
 
69
    cinfo->min_DCT_h_scaled_size = 2;
 
70
    cinfo->min_DCT_v_scaled_size = 2;
 
71
  } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) {
 
72
    /* Provide 8/3 scaling */
 
73
    cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION)
 
74
      jdiv_round_up((long) cinfo->image_width * 2, 3L);
 
75
    cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION)
 
76
      jdiv_round_up((long) cinfo->image_height * 2, 3L);
 
77
    cinfo->min_DCT_h_scaled_size = 3;
 
78
    cinfo->min_DCT_v_scaled_size = 3;
 
79
  } else if (cinfo->scale_num >= cinfo->scale_denom * 2) {
 
80
    /* Provide 2/1 scaling */
 
81
    cinfo->jpeg_width = cinfo->image_width << 1;
 
82
    cinfo->jpeg_height = cinfo->image_height << 1;
 
83
    cinfo->min_DCT_h_scaled_size = 4;
 
84
    cinfo->min_DCT_v_scaled_size = 4;
 
85
  } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) {
 
86
    /* Provide 8/5 scaling */
 
87
    cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
 
88
      jdiv_round_up((long) cinfo->image_width * 3, 5L);
 
89
    cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
 
90
      jdiv_round_up((long) cinfo->image_height * 3, 5L);
 
91
    cinfo->min_DCT_h_scaled_size = 5;
 
92
    cinfo->min_DCT_v_scaled_size = 5;
 
93
  } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) {
 
94
    /* Provide 4/3 scaling */
 
95
    cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
 
96
      jdiv_round_up((long) cinfo->image_width, 3L);
 
97
    cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
 
98
      jdiv_round_up((long) cinfo->image_height, 3L);
 
99
    cinfo->min_DCT_h_scaled_size = 6;
 
100
    cinfo->min_DCT_v_scaled_size = 6;
 
101
  } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) {
 
102
    /* Provide 8/7 scaling */
 
103
    cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
 
104
      jdiv_round_up((long) cinfo->image_width, 7L);
 
105
    cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
 
106
      jdiv_round_up((long) cinfo->image_height, 7L);
 
107
    cinfo->min_DCT_h_scaled_size = 7;
 
108
    cinfo->min_DCT_v_scaled_size = 7;
 
109
  } else if (cinfo->scale_num >= cinfo->scale_denom) {
 
110
    /* Provide 1/1 scaling */
 
111
    cinfo->jpeg_width = cinfo->image_width;
 
112
    cinfo->jpeg_height = cinfo->image_height;
 
113
    cinfo->min_DCT_h_scaled_size = DCTSIZE;
 
114
    cinfo->min_DCT_v_scaled_size = DCTSIZE;
 
115
  } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) {
 
116
    /* Provide 8/9 scaling */
 
117
    cinfo->jpeg_width = (JDIMENSION)
 
118
      jdiv_round_up((long) cinfo->image_width * 8, 9L);
 
119
    cinfo->jpeg_height = (JDIMENSION)
 
120
      jdiv_round_up((long) cinfo->image_height * 8, 9L);
 
121
    cinfo->min_DCT_h_scaled_size = 9;
 
122
    cinfo->min_DCT_v_scaled_size = 9;
 
123
  } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) {
 
124
    /* Provide 4/5 scaling */
 
125
    cinfo->jpeg_width = (JDIMENSION)
 
126
      jdiv_round_up((long) cinfo->image_width * 4, 5L);
 
127
    cinfo->jpeg_height = (JDIMENSION)
 
128
      jdiv_round_up((long) cinfo->image_height * 4, 5L);
 
129
    cinfo->min_DCT_h_scaled_size = 10;
 
130
    cinfo->min_DCT_v_scaled_size = 10;
 
131
  } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) {
 
132
    /* Provide 8/11 scaling */
 
133
    cinfo->jpeg_width = (JDIMENSION)
 
134
      jdiv_round_up((long) cinfo->image_width * 8, 11L);
 
135
    cinfo->jpeg_height = (JDIMENSION)
 
136
      jdiv_round_up((long) cinfo->image_height * 8, 11L);
 
137
    cinfo->min_DCT_h_scaled_size = 11;
 
138
    cinfo->min_DCT_v_scaled_size = 11;
 
139
  } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) {
 
140
    /* Provide 2/3 scaling */
 
141
    cinfo->jpeg_width = (JDIMENSION)
 
142
      jdiv_round_up((long) cinfo->image_width * 2, 3L);
 
143
    cinfo->jpeg_height = (JDIMENSION)
 
144
      jdiv_round_up((long) cinfo->image_height * 2, 3L);
 
145
    cinfo->min_DCT_h_scaled_size = 12;
 
146
    cinfo->min_DCT_v_scaled_size = 12;
 
147
  } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) {
 
148
    /* Provide 8/13 scaling */
 
149
    cinfo->jpeg_width = (JDIMENSION)
 
150
      jdiv_round_up((long) cinfo->image_width * 8, 13L);
 
151
    cinfo->jpeg_height = (JDIMENSION)
 
152
      jdiv_round_up((long) cinfo->image_height * 8, 13L);
 
153
    cinfo->min_DCT_h_scaled_size = 13;
 
154
    cinfo->min_DCT_v_scaled_size = 13;
 
155
  } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) {
 
156
    /* Provide 4/7 scaling */
 
157
    cinfo->jpeg_width = (JDIMENSION)
 
158
      jdiv_round_up((long) cinfo->image_width * 4, 7L);
 
159
    cinfo->jpeg_height = (JDIMENSION)
 
160
      jdiv_round_up((long) cinfo->image_height * 4, 7L);
 
161
    cinfo->min_DCT_h_scaled_size = 14;
 
162
    cinfo->min_DCT_v_scaled_size = 14;
 
163
  } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) {
 
164
    /* Provide 8/15 scaling */
 
165
    cinfo->jpeg_width = (JDIMENSION)
 
166
      jdiv_round_up((long) cinfo->image_width * 8, 15L);
 
167
    cinfo->jpeg_height = (JDIMENSION)
 
168
      jdiv_round_up((long) cinfo->image_height * 8, 15L);
 
169
    cinfo->min_DCT_h_scaled_size = 15;
 
170
    cinfo->min_DCT_v_scaled_size = 15;
 
171
  } else {
 
172
    /* Provide 1/2 scaling */
 
173
    cinfo->jpeg_width = (JDIMENSION)
 
174
      jdiv_round_up((long) cinfo->image_width, 2L);
 
175
    cinfo->jpeg_height = (JDIMENSION)
 
176
      jdiv_round_up((long) cinfo->image_height, 2L);
 
177
    cinfo->min_DCT_h_scaled_size = 16;
 
178
    cinfo->min_DCT_v_scaled_size = 16;
 
179
  }
 
180
 
 
181
#else /* !DCT_SCALING_SUPPORTED */
 
182
 
 
183
  /* Hardwire it to "no scaling" */
 
184
  cinfo->jpeg_width = cinfo->image_width;
 
185
  cinfo->jpeg_height = cinfo->image_height;
 
186
  cinfo->min_DCT_h_scaled_size = DCTSIZE;
 
187
  cinfo->min_DCT_v_scaled_size = DCTSIZE;
 
188
 
 
189
#endif /* DCT_SCALING_SUPPORTED */
 
190
}
 
191
 
 
192
 
45
193
LOCAL(void)
46
194
initial_setup (j_compress_ptr cinfo)
47
195
/* Do computations that are needed before master selection phase */
48
196
{
49
 
  int ci;
 
197
  int ci, ssize;
50
198
  jpeg_component_info *compptr;
51
199
  long samplesperrow;
52
200
  JDIMENSION jd_samplesperrow;
53
201
 
 
202
  jpeg_calc_jpeg_dimensions(cinfo);
 
203
 
54
204
  /* Sanity check on image dimensions */
55
 
  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
 
205
  if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0
56
206
      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
57
207
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
58
208
 
59
209
  /* Make sure image isn't bigger than I can handle */
60
 
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
61
 
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
 
210
  if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
 
211
      (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
62
212
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
63
213
 
64
214
  /* Width of an input scanline must be representable as JDIMENSION. */
95
245
       ci++, compptr++) {
96
246
    /* Fill in the correct component_index value; don't rely on application */
97
247
    compptr->component_index = ci;
98
 
    /* For compression, we never do DCT scaling. */
99
 
    compptr->DCT_scaled_size = DCTSIZE;
 
248
    /* In selecting the actual DCT scaling for each component, we try to
 
249
     * scale down the chroma components via DCT scaling rather than downsampling.
 
250
     * This saves time if the downsampler gets to use 1:1 scaling.
 
251
     * Note this code adapts subsampling ratios which are powers of 2.
 
252
     */
 
253
    ssize = 1;
 
254
#ifdef DCT_SCALING_SUPPORTED
 
255
    while (cinfo->min_DCT_h_scaled_size * ssize <=
 
256
           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 
257
           (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
 
258
      ssize = ssize * 2;
 
259
    }
 
260
#endif
 
261
    compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
 
262
    ssize = 1;
 
263
#ifdef DCT_SCALING_SUPPORTED
 
264
    while (cinfo->min_DCT_v_scaled_size * ssize <=
 
265
           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 
266
           (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
 
267
      ssize = ssize * 2;
 
268
    }
 
269
#endif
 
270
    compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
 
271
 
 
272
    /* We don't support DCT ratios larger than 2. */
 
273
    if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
 
274
        compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
 
275
    else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
 
276
        compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
 
277
 
100
278
    /* Size in DCT blocks */
101
279
    compptr->width_in_blocks = (JDIMENSION)
102
 
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
 
280
      jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
103
281
                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
104
282
    compptr->height_in_blocks = (JDIMENSION)
105
 
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
 
283
      jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
106
284
                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
107
285
    /* Size in samples */
108
286
    compptr->downsampled_width = (JDIMENSION)
109
 
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
110
 
                    (long) cinfo->max_h_samp_factor);
 
287
      jdiv_round_up((long) cinfo->jpeg_width *
 
288
                    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
 
289
                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
111
290
    compptr->downsampled_height = (JDIMENSION)
112
 
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
113
 
                    (long) cinfo->max_v_samp_factor);
 
291
      jdiv_round_up((long) cinfo->jpeg_height *
 
292
                    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
 
293
                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
114
294
    /* Mark component needed (this flag isn't actually used for compression) */
115
295
    compptr->component_needed = TRUE;
116
296
  }
119
299
   * main controller will call coefficient controller).
120
300
   */
121
301
  cinfo->total_iMCU_rows = (JDIMENSION)
122
 
    jdiv_round_up((long) cinfo->image_height,
 
302
    jdiv_round_up((long) cinfo->jpeg_height,
123
303
                  (long) (cinfo->max_v_samp_factor*DCTSIZE));
124
304
}
125
305
 
325
505
    compptr->MCU_width = 1;
326
506
    compptr->MCU_height = 1;
327
507
    compptr->MCU_blocks = 1;
328
 
    compptr->MCU_sample_width = DCTSIZE;
 
508
    compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
329
509
    compptr->last_col_width = 1;
330
510
    /* For noninterleaved scans, it is convenient to define last_row_height
331
511
     * as the number of block rows present in the last iMCU row.
347
527
    
348
528
    /* Overall image size in MCUs */
349
529
    cinfo->MCUs_per_row = (JDIMENSION)
350
 
      jdiv_round_up((long) cinfo->image_width,
 
530
      jdiv_round_up((long) cinfo->jpeg_width,
351
531
                    (long) (cinfo->max_h_samp_factor*DCTSIZE));
352
532
    cinfo->MCU_rows_in_scan = (JDIMENSION)
353
 
      jdiv_round_up((long) cinfo->image_height,
 
533
      jdiv_round_up((long) cinfo->jpeg_height,
354
534
                    (long) (cinfo->max_v_samp_factor*DCTSIZE));
355
535
    
356
536
    cinfo->blocks_in_MCU = 0;
361
541
      compptr->MCU_width = compptr->h_samp_factor;
362
542
      compptr->MCU_height = compptr->v_samp_factor;
363
543
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
364
 
      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
 
544
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
365
545
      /* Figure number of non-dummy blocks in last MCU column & row */
366
546
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
367
547
      if (tmp == 0) tmp = compptr->MCU_width;
433
613
    /* Do Huffman optimization for a scan after the first one. */
434
614
    select_scan_parameters(cinfo);
435
615
    per_scan_setup(cinfo);
436
 
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
 
616
    if (cinfo->Ss != 0 || cinfo->Ah == 0) {
437
617
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
438
618
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
439
619
      master->pub.call_pass_startup = FALSE;
567
747
    cinfo->num_scans = 1;
568
748
  }
569
749
 
570
 
  if (cinfo->progressive_mode)  /*  TEMPORARY HACK ??? */
 
750
  if (cinfo->progressive_mode && cinfo->arith_code == 0)        /*  TEMPORARY HACK ??? */
571
751
    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
572
752
 
573
753
  /* Initialize my private state */