media/libjpeg/jcparam.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jcparam.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1998, Thomas G. Lane.
michael@0 6 * Modified 2003-2008 by Guido Vollbeding.
michael@0 7 * libjpeg-turbo Modifications:
michael@0 8 * Copyright (C) 2009-2011, D. R. Commander.
michael@0 9 * For conditions of distribution and use, see the accompanying README file.
michael@0 10 *
michael@0 11 * This file contains optional default-setting code for the JPEG compressor.
michael@0 12 * Applications do not have to use this file, but those that don't use it
michael@0 13 * must know a lot more about the innards of the JPEG code.
michael@0 14 */
michael@0 15
michael@0 16 #define JPEG_INTERNALS
michael@0 17 #include "jinclude.h"
michael@0 18 #include "jpeglib.h"
michael@0 19 #include "jstdhuff.c"
michael@0 20
michael@0 21
michael@0 22 /*
michael@0 23 * Quantization table setup routines
michael@0 24 */
michael@0 25
michael@0 26 GLOBAL(void)
michael@0 27 jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
michael@0 28 const unsigned int *basic_table,
michael@0 29 int scale_factor, boolean force_baseline)
michael@0 30 /* Define a quantization table equal to the basic_table times
michael@0 31 * a scale factor (given as a percentage).
michael@0 32 * If force_baseline is TRUE, the computed quantization table entries
michael@0 33 * are limited to 1..255 for JPEG baseline compatibility.
michael@0 34 */
michael@0 35 {
michael@0 36 JQUANT_TBL ** qtblptr;
michael@0 37 int i;
michael@0 38 long temp;
michael@0 39
michael@0 40 /* Safety check to ensure start_compress not called yet. */
michael@0 41 if (cinfo->global_state != CSTATE_START)
michael@0 42 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 43
michael@0 44 if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
michael@0 45 ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
michael@0 46
michael@0 47 qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
michael@0 48
michael@0 49 if (*qtblptr == NULL)
michael@0 50 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
michael@0 51
michael@0 52 for (i = 0; i < DCTSIZE2; i++) {
michael@0 53 temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
michael@0 54 /* limit the values to the valid range */
michael@0 55 if (temp <= 0L) temp = 1L;
michael@0 56 if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
michael@0 57 if (force_baseline && temp > 255L)
michael@0 58 temp = 255L; /* limit to baseline range if requested */
michael@0 59 (*qtblptr)->quantval[i] = (UINT16) temp;
michael@0 60 }
michael@0 61
michael@0 62 /* Initialize sent_table FALSE so table will be written to JPEG file. */
michael@0 63 (*qtblptr)->sent_table = FALSE;
michael@0 64 }
michael@0 65
michael@0 66
michael@0 67 /* These are the sample quantization tables given in JPEG spec section K.1.
michael@0 68 * The spec says that the values given produce "good" quality, and
michael@0 69 * when divided by 2, "very good" quality.
michael@0 70 */
michael@0 71 static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
michael@0 72 16, 11, 10, 16, 24, 40, 51, 61,
michael@0 73 12, 12, 14, 19, 26, 58, 60, 55,
michael@0 74 14, 13, 16, 24, 40, 57, 69, 56,
michael@0 75 14, 17, 22, 29, 51, 87, 80, 62,
michael@0 76 18, 22, 37, 56, 68, 109, 103, 77,
michael@0 77 24, 35, 55, 64, 81, 104, 113, 92,
michael@0 78 49, 64, 78, 87, 103, 121, 120, 101,
michael@0 79 72, 92, 95, 98, 112, 100, 103, 99
michael@0 80 };
michael@0 81 static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
michael@0 82 17, 18, 24, 47, 99, 99, 99, 99,
michael@0 83 18, 21, 26, 66, 99, 99, 99, 99,
michael@0 84 24, 26, 56, 99, 99, 99, 99, 99,
michael@0 85 47, 66, 99, 99, 99, 99, 99, 99,
michael@0 86 99, 99, 99, 99, 99, 99, 99, 99,
michael@0 87 99, 99, 99, 99, 99, 99, 99, 99,
michael@0 88 99, 99, 99, 99, 99, 99, 99, 99,
michael@0 89 99, 99, 99, 99, 99, 99, 99, 99
michael@0 90 };
michael@0 91
michael@0 92
michael@0 93 #if JPEG_LIB_VERSION >= 70
michael@0 94 GLOBAL(void)
michael@0 95 jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)
michael@0 96 /* Set or change the 'quality' (quantization) setting, using default tables
michael@0 97 * and straight percentage-scaling quality scales.
michael@0 98 * This entry point allows different scalings for luminance and chrominance.
michael@0 99 */
michael@0 100 {
michael@0 101 /* Set up two quantization tables using the specified scaling */
michael@0 102 jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
michael@0 103 cinfo->q_scale_factor[0], force_baseline);
michael@0 104 jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
michael@0 105 cinfo->q_scale_factor[1], force_baseline);
michael@0 106 }
michael@0 107 #endif
michael@0 108
michael@0 109
michael@0 110 GLOBAL(void)
michael@0 111 jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
michael@0 112 boolean force_baseline)
michael@0 113 /* Set or change the 'quality' (quantization) setting, using default tables
michael@0 114 * and a straight percentage-scaling quality scale. In most cases it's better
michael@0 115 * to use jpeg_set_quality (below); this entry point is provided for
michael@0 116 * applications that insist on a linear percentage scaling.
michael@0 117 */
michael@0 118 {
michael@0 119 /* Set up two quantization tables using the specified scaling */
michael@0 120 jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
michael@0 121 scale_factor, force_baseline);
michael@0 122 jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
michael@0 123 scale_factor, force_baseline);
michael@0 124 }
michael@0 125
michael@0 126
michael@0 127 GLOBAL(int)
michael@0 128 jpeg_quality_scaling (int quality)
michael@0 129 /* Convert a user-specified quality rating to a percentage scaling factor
michael@0 130 * for an underlying quantization table, using our recommended scaling curve.
michael@0 131 * The input 'quality' factor should be 0 (terrible) to 100 (very good).
michael@0 132 */
michael@0 133 {
michael@0 134 /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
michael@0 135 if (quality <= 0) quality = 1;
michael@0 136 if (quality > 100) quality = 100;
michael@0 137
michael@0 138 /* The basic table is used as-is (scaling 100) for a quality of 50.
michael@0 139 * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
michael@0 140 * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
michael@0 141 * to make all the table entries 1 (hence, minimum quantization loss).
michael@0 142 * Qualities 1..50 are converted to scaling percentage 5000/Q.
michael@0 143 */
michael@0 144 if (quality < 50)
michael@0 145 quality = 5000 / quality;
michael@0 146 else
michael@0 147 quality = 200 - quality*2;
michael@0 148
michael@0 149 return quality;
michael@0 150 }
michael@0 151
michael@0 152
michael@0 153 GLOBAL(void)
michael@0 154 jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
michael@0 155 /* Set or change the 'quality' (quantization) setting, using default tables.
michael@0 156 * This is the standard quality-adjusting entry point for typical user
michael@0 157 * interfaces; only those who want detailed control over quantization tables
michael@0 158 * would use the preceding three routines directly.
michael@0 159 */
michael@0 160 {
michael@0 161 /* Convert user 0-100 rating to percentage scaling */
michael@0 162 quality = jpeg_quality_scaling(quality);
michael@0 163
michael@0 164 /* Set up standard quality tables */
michael@0 165 jpeg_set_linear_quality(cinfo, quality, force_baseline);
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169 /*
michael@0 170 * Default parameter setup for compression.
michael@0 171 *
michael@0 172 * Applications that don't choose to use this routine must do their
michael@0 173 * own setup of all these parameters. Alternately, you can call this
michael@0 174 * to establish defaults and then alter parameters selectively. This
michael@0 175 * is the recommended approach since, if we add any new parameters,
michael@0 176 * your code will still work (they'll be set to reasonable defaults).
michael@0 177 */
michael@0 178
michael@0 179 GLOBAL(void)
michael@0 180 jpeg_set_defaults (j_compress_ptr cinfo)
michael@0 181 {
michael@0 182 int i;
michael@0 183
michael@0 184 /* Safety check to ensure start_compress not called yet. */
michael@0 185 if (cinfo->global_state != CSTATE_START)
michael@0 186 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 187
michael@0 188 /* Allocate comp_info array large enough for maximum component count.
michael@0 189 * Array is made permanent in case application wants to compress
michael@0 190 * multiple images at same param settings.
michael@0 191 */
michael@0 192 if (cinfo->comp_info == NULL)
michael@0 193 cinfo->comp_info = (jpeg_component_info *)
michael@0 194 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
michael@0 195 MAX_COMPONENTS * SIZEOF(jpeg_component_info));
michael@0 196
michael@0 197 /* Initialize everything not dependent on the color space */
michael@0 198
michael@0 199 #if JPEG_LIB_VERSION >= 70
michael@0 200 cinfo->scale_num = 1; /* 1:1 scaling */
michael@0 201 cinfo->scale_denom = 1;
michael@0 202 #endif
michael@0 203 cinfo->data_precision = BITS_IN_JSAMPLE;
michael@0 204 /* Set up two quantization tables using default quality of 75 */
michael@0 205 jpeg_set_quality(cinfo, 75, TRUE);
michael@0 206 /* Set up two Huffman tables */
michael@0 207 std_huff_tables((j_common_ptr) cinfo);
michael@0 208
michael@0 209 /* Initialize default arithmetic coding conditioning */
michael@0 210 for (i = 0; i < NUM_ARITH_TBLS; i++) {
michael@0 211 cinfo->arith_dc_L[i] = 0;
michael@0 212 cinfo->arith_dc_U[i] = 1;
michael@0 213 cinfo->arith_ac_K[i] = 5;
michael@0 214 }
michael@0 215
michael@0 216 /* Default is no multiple-scan output */
michael@0 217 cinfo->scan_info = NULL;
michael@0 218 cinfo->num_scans = 0;
michael@0 219
michael@0 220 /* Expect normal source image, not raw downsampled data */
michael@0 221 cinfo->raw_data_in = FALSE;
michael@0 222
michael@0 223 /* Use Huffman coding, not arithmetic coding, by default */
michael@0 224 cinfo->arith_code = FALSE;
michael@0 225
michael@0 226 /* By default, don't do extra passes to optimize entropy coding */
michael@0 227 cinfo->optimize_coding = FALSE;
michael@0 228 /* The standard Huffman tables are only valid for 8-bit data precision.
michael@0 229 * If the precision is higher, force optimization on so that usable
michael@0 230 * tables will be computed. This test can be removed if default tables
michael@0 231 * are supplied that are valid for the desired precision.
michael@0 232 */
michael@0 233 if (cinfo->data_precision > 8)
michael@0 234 cinfo->optimize_coding = TRUE;
michael@0 235
michael@0 236 /* By default, use the simpler non-cosited sampling alignment */
michael@0 237 cinfo->CCIR601_sampling = FALSE;
michael@0 238
michael@0 239 #if JPEG_LIB_VERSION >= 70
michael@0 240 /* By default, apply fancy downsampling */
michael@0 241 cinfo->do_fancy_downsampling = TRUE;
michael@0 242 #endif
michael@0 243
michael@0 244 /* No input smoothing */
michael@0 245 cinfo->smoothing_factor = 0;
michael@0 246
michael@0 247 /* DCT algorithm preference */
michael@0 248 cinfo->dct_method = JDCT_DEFAULT;
michael@0 249
michael@0 250 /* No restart markers */
michael@0 251 cinfo->restart_interval = 0;
michael@0 252 cinfo->restart_in_rows = 0;
michael@0 253
michael@0 254 /* Fill in default JFIF marker parameters. Note that whether the marker
michael@0 255 * will actually be written is determined by jpeg_set_colorspace.
michael@0 256 *
michael@0 257 * By default, the library emits JFIF version code 1.01.
michael@0 258 * An application that wants to emit JFIF 1.02 extension markers should set
michael@0 259 * JFIF_minor_version to 2. We could probably get away with just defaulting
michael@0 260 * to 1.02, but there may still be some decoders in use that will complain
michael@0 261 * about that; saying 1.01 should minimize compatibility problems.
michael@0 262 */
michael@0 263 cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
michael@0 264 cinfo->JFIF_minor_version = 1;
michael@0 265 cinfo->density_unit = 0; /* Pixel size is unknown by default */
michael@0 266 cinfo->X_density = 1; /* Pixel aspect ratio is square by default */
michael@0 267 cinfo->Y_density = 1;
michael@0 268
michael@0 269 /* Choose JPEG colorspace based on input space, set defaults accordingly */
michael@0 270
michael@0 271 jpeg_default_colorspace(cinfo);
michael@0 272 }
michael@0 273
michael@0 274
michael@0 275 /*
michael@0 276 * Select an appropriate JPEG colorspace for in_color_space.
michael@0 277 */
michael@0 278
michael@0 279 GLOBAL(void)
michael@0 280 jpeg_default_colorspace (j_compress_ptr cinfo)
michael@0 281 {
michael@0 282 switch (cinfo->in_color_space) {
michael@0 283 case JCS_GRAYSCALE:
michael@0 284 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
michael@0 285 break;
michael@0 286 case JCS_RGB:
michael@0 287 case JCS_EXT_RGB:
michael@0 288 case JCS_EXT_RGBX:
michael@0 289 case JCS_EXT_BGR:
michael@0 290 case JCS_EXT_BGRX:
michael@0 291 case JCS_EXT_XBGR:
michael@0 292 case JCS_EXT_XRGB:
michael@0 293 case JCS_EXT_RGBA:
michael@0 294 case JCS_EXT_BGRA:
michael@0 295 case JCS_EXT_ABGR:
michael@0 296 case JCS_EXT_ARGB:
michael@0 297 jpeg_set_colorspace(cinfo, JCS_YCbCr);
michael@0 298 break;
michael@0 299 case JCS_YCbCr:
michael@0 300 jpeg_set_colorspace(cinfo, JCS_YCbCr);
michael@0 301 break;
michael@0 302 case JCS_CMYK:
michael@0 303 jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
michael@0 304 break;
michael@0 305 case JCS_YCCK:
michael@0 306 jpeg_set_colorspace(cinfo, JCS_YCCK);
michael@0 307 break;
michael@0 308 case JCS_UNKNOWN:
michael@0 309 jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
michael@0 310 break;
michael@0 311 default:
michael@0 312 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 313 }
michael@0 314 }
michael@0 315
michael@0 316
michael@0 317 /*
michael@0 318 * Set the JPEG colorspace, and choose colorspace-dependent default values.
michael@0 319 */
michael@0 320
michael@0 321 GLOBAL(void)
michael@0 322 jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
michael@0 323 {
michael@0 324 jpeg_component_info * compptr;
michael@0 325 int ci;
michael@0 326
michael@0 327 #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
michael@0 328 (compptr = &cinfo->comp_info[index], \
michael@0 329 compptr->component_id = (id), \
michael@0 330 compptr->h_samp_factor = (hsamp), \
michael@0 331 compptr->v_samp_factor = (vsamp), \
michael@0 332 compptr->quant_tbl_no = (quant), \
michael@0 333 compptr->dc_tbl_no = (dctbl), \
michael@0 334 compptr->ac_tbl_no = (actbl) )
michael@0 335
michael@0 336 /* Safety check to ensure start_compress not called yet. */
michael@0 337 if (cinfo->global_state != CSTATE_START)
michael@0 338 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 339
michael@0 340 /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
michael@0 341 * tables 1 for chrominance components.
michael@0 342 */
michael@0 343
michael@0 344 cinfo->jpeg_color_space = colorspace;
michael@0 345
michael@0 346 cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
michael@0 347 cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
michael@0 348
michael@0 349 switch (colorspace) {
michael@0 350 case JCS_GRAYSCALE:
michael@0 351 cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
michael@0 352 cinfo->num_components = 1;
michael@0 353 /* JFIF specifies component ID 1 */
michael@0 354 SET_COMP(0, 1, 1,1, 0, 0,0);
michael@0 355 break;
michael@0 356 case JCS_RGB:
michael@0 357 cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
michael@0 358 cinfo->num_components = 3;
michael@0 359 SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
michael@0 360 SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
michael@0 361 SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
michael@0 362 break;
michael@0 363 case JCS_YCbCr:
michael@0 364 cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
michael@0 365 cinfo->num_components = 3;
michael@0 366 /* JFIF specifies component IDs 1,2,3 */
michael@0 367 /* We default to 2x2 subsamples of chrominance */
michael@0 368 SET_COMP(0, 1, 2,2, 0, 0,0);
michael@0 369 SET_COMP(1, 2, 1,1, 1, 1,1);
michael@0 370 SET_COMP(2, 3, 1,1, 1, 1,1);
michael@0 371 break;
michael@0 372 case JCS_CMYK:
michael@0 373 cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
michael@0 374 cinfo->num_components = 4;
michael@0 375 SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
michael@0 376 SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
michael@0 377 SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
michael@0 378 SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
michael@0 379 break;
michael@0 380 case JCS_YCCK:
michael@0 381 cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
michael@0 382 cinfo->num_components = 4;
michael@0 383 SET_COMP(0, 1, 2,2, 0, 0,0);
michael@0 384 SET_COMP(1, 2, 1,1, 1, 1,1);
michael@0 385 SET_COMP(2, 3, 1,1, 1, 1,1);
michael@0 386 SET_COMP(3, 4, 2,2, 0, 0,0);
michael@0 387 break;
michael@0 388 case JCS_UNKNOWN:
michael@0 389 cinfo->num_components = cinfo->input_components;
michael@0 390 if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
michael@0 391 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
michael@0 392 MAX_COMPONENTS);
michael@0 393 for (ci = 0; ci < cinfo->num_components; ci++) {
michael@0 394 SET_COMP(ci, ci, 1,1, 0, 0,0);
michael@0 395 }
michael@0 396 break;
michael@0 397 default:
michael@0 398 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 399 }
michael@0 400 }
michael@0 401
michael@0 402
michael@0 403 #ifdef C_PROGRESSIVE_SUPPORTED
michael@0 404
michael@0 405 LOCAL(jpeg_scan_info *)
michael@0 406 fill_a_scan (jpeg_scan_info * scanptr, int ci,
michael@0 407 int Ss, int Se, int Ah, int Al)
michael@0 408 /* Support routine: generate one scan for specified component */
michael@0 409 {
michael@0 410 scanptr->comps_in_scan = 1;
michael@0 411 scanptr->component_index[0] = ci;
michael@0 412 scanptr->Ss = Ss;
michael@0 413 scanptr->Se = Se;
michael@0 414 scanptr->Ah = Ah;
michael@0 415 scanptr->Al = Al;
michael@0 416 scanptr++;
michael@0 417 return scanptr;
michael@0 418 }
michael@0 419
michael@0 420 LOCAL(jpeg_scan_info *)
michael@0 421 fill_scans (jpeg_scan_info * scanptr, int ncomps,
michael@0 422 int Ss, int Se, int Ah, int Al)
michael@0 423 /* Support routine: generate one scan for each component */
michael@0 424 {
michael@0 425 int ci;
michael@0 426
michael@0 427 for (ci = 0; ci < ncomps; ci++) {
michael@0 428 scanptr->comps_in_scan = 1;
michael@0 429 scanptr->component_index[0] = ci;
michael@0 430 scanptr->Ss = Ss;
michael@0 431 scanptr->Se = Se;
michael@0 432 scanptr->Ah = Ah;
michael@0 433 scanptr->Al = Al;
michael@0 434 scanptr++;
michael@0 435 }
michael@0 436 return scanptr;
michael@0 437 }
michael@0 438
michael@0 439 LOCAL(jpeg_scan_info *)
michael@0 440 fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
michael@0 441 /* Support routine: generate interleaved DC scan if possible, else N scans */
michael@0 442 {
michael@0 443 int ci;
michael@0 444
michael@0 445 if (ncomps <= MAX_COMPS_IN_SCAN) {
michael@0 446 /* Single interleaved DC scan */
michael@0 447 scanptr->comps_in_scan = ncomps;
michael@0 448 for (ci = 0; ci < ncomps; ci++)
michael@0 449 scanptr->component_index[ci] = ci;
michael@0 450 scanptr->Ss = scanptr->Se = 0;
michael@0 451 scanptr->Ah = Ah;
michael@0 452 scanptr->Al = Al;
michael@0 453 scanptr++;
michael@0 454 } else {
michael@0 455 /* Noninterleaved DC scan for each component */
michael@0 456 scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
michael@0 457 }
michael@0 458 return scanptr;
michael@0 459 }
michael@0 460
michael@0 461
michael@0 462 /*
michael@0 463 * Create a recommended progressive-JPEG script.
michael@0 464 * cinfo->num_components and cinfo->jpeg_color_space must be correct.
michael@0 465 */
michael@0 466
michael@0 467 GLOBAL(void)
michael@0 468 jpeg_simple_progression (j_compress_ptr cinfo)
michael@0 469 {
michael@0 470 int ncomps = cinfo->num_components;
michael@0 471 int nscans;
michael@0 472 jpeg_scan_info * scanptr;
michael@0 473
michael@0 474 /* Safety check to ensure start_compress not called yet. */
michael@0 475 if (cinfo->global_state != CSTATE_START)
michael@0 476 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 477
michael@0 478 /* Figure space needed for script. Calculation must match code below! */
michael@0 479 if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
michael@0 480 /* Custom script for YCbCr color images. */
michael@0 481 nscans = 10;
michael@0 482 } else {
michael@0 483 /* All-purpose script for other color spaces. */
michael@0 484 if (ncomps > MAX_COMPS_IN_SCAN)
michael@0 485 nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */
michael@0 486 else
michael@0 487 nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */
michael@0 488 }
michael@0 489
michael@0 490 /* Allocate space for script.
michael@0 491 * We need to put it in the permanent pool in case the application performs
michael@0 492 * multiple compressions without changing the settings. To avoid a memory
michael@0 493 * leak if jpeg_simple_progression is called repeatedly for the same JPEG
michael@0 494 * object, we try to re-use previously allocated space, and we allocate
michael@0 495 * enough space to handle YCbCr even if initially asked for grayscale.
michael@0 496 */
michael@0 497 if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
michael@0 498 cinfo->script_space_size = MAX(nscans, 10);
michael@0 499 cinfo->script_space = (jpeg_scan_info *)
michael@0 500 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
michael@0 501 cinfo->script_space_size * SIZEOF(jpeg_scan_info));
michael@0 502 }
michael@0 503 scanptr = cinfo->script_space;
michael@0 504 cinfo->scan_info = scanptr;
michael@0 505 cinfo->num_scans = nscans;
michael@0 506
michael@0 507 if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
michael@0 508 /* Custom script for YCbCr color images. */
michael@0 509 /* Initial DC scan */
michael@0 510 scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
michael@0 511 /* Initial AC scan: get some luma data out in a hurry */
michael@0 512 scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
michael@0 513 /* Chroma data is too small to be worth expending many scans on */
michael@0 514 scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
michael@0 515 scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
michael@0 516 /* Complete spectral selection for luma AC */
michael@0 517 scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
michael@0 518 /* Refine next bit of luma AC */
michael@0 519 scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
michael@0 520 /* Finish DC successive approximation */
michael@0 521 scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
michael@0 522 /* Finish AC successive approximation */
michael@0 523 scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
michael@0 524 scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
michael@0 525 /* Luma bottom bit comes last since it's usually largest scan */
michael@0 526 scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
michael@0 527 } else {
michael@0 528 /* All-purpose script for other color spaces. */
michael@0 529 /* Successive approximation first pass */
michael@0 530 scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
michael@0 531 scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
michael@0 532 scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
michael@0 533 /* Successive approximation second pass */
michael@0 534 scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
michael@0 535 /* Successive approximation final pass */
michael@0 536 scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
michael@0 537 scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
michael@0 538 }
michael@0 539 }
michael@0 540
michael@0 541 #endif /* C_PROGRESSIVE_SUPPORTED */

mercurial