vrshoot

diff libs/libjpeg/jcparam.c @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jcparam.c	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,610 @@
     1.4 +/*
     1.5 + * jcparam.c
     1.6 + *
     1.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains optional default-setting code for the JPEG compressor.
    1.12 + * Applications do not have to use this file, but those that don't use it
    1.13 + * must know a lot more about the innards of the JPEG code.
    1.14 + */
    1.15 +
    1.16 +#define JPEG_INTERNALS
    1.17 +#include "jinclude.h"
    1.18 +#include "jpeglib.h"
    1.19 +
    1.20 +
    1.21 +/*
    1.22 + * Quantization table setup routines
    1.23 + */
    1.24 +
    1.25 +GLOBAL(void)
    1.26 +jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
    1.27 +		      const unsigned int *basic_table,
    1.28 +		      int scale_factor, boolean force_baseline)
    1.29 +/* Define a quantization table equal to the basic_table times
    1.30 + * a scale factor (given as a percentage).
    1.31 + * If force_baseline is TRUE, the computed quantization table entries
    1.32 + * are limited to 1..255 for JPEG baseline compatibility.
    1.33 + */
    1.34 +{
    1.35 +  JQUANT_TBL ** qtblptr;
    1.36 +  int i;
    1.37 +  long temp;
    1.38 +
    1.39 +  /* Safety check to ensure start_compress not called yet. */
    1.40 +  if (cinfo->global_state != CSTATE_START)
    1.41 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    1.42 +
    1.43 +  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
    1.44 +    ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
    1.45 +
    1.46 +  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
    1.47 +
    1.48 +  if (*qtblptr == NULL)
    1.49 +    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
    1.50 +
    1.51 +  for (i = 0; i < DCTSIZE2; i++) {
    1.52 +    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
    1.53 +    /* limit the values to the valid range */
    1.54 +    if (temp <= 0L) temp = 1L;
    1.55 +    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
    1.56 +    if (force_baseline && temp > 255L)
    1.57 +      temp = 255L;		/* limit to baseline range if requested */
    1.58 +    (*qtblptr)->quantval[i] = (UINT16) temp;
    1.59 +  }
    1.60 +
    1.61 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
    1.62 +  (*qtblptr)->sent_table = FALSE;
    1.63 +}
    1.64 +
    1.65 +
    1.66 +GLOBAL(void)
    1.67 +jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
    1.68 +			 boolean force_baseline)
    1.69 +/* Set or change the 'quality' (quantization) setting, using default tables
    1.70 + * and a straight percentage-scaling quality scale.  In most cases it's better
    1.71 + * to use jpeg_set_quality (below); this entry point is provided for
    1.72 + * applications that insist on a linear percentage scaling.
    1.73 + */
    1.74 +{
    1.75 +  /* These are the sample quantization tables given in JPEG spec section K.1.
    1.76 +   * The spec says that the values given produce "good" quality, and
    1.77 +   * when divided by 2, "very good" quality.
    1.78 +   */
    1.79 +  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
    1.80 +    16,  11,  10,  16,  24,  40,  51,  61,
    1.81 +    12,  12,  14,  19,  26,  58,  60,  55,
    1.82 +    14,  13,  16,  24,  40,  57,  69,  56,
    1.83 +    14,  17,  22,  29,  51,  87,  80,  62,
    1.84 +    18,  22,  37,  56,  68, 109, 103,  77,
    1.85 +    24,  35,  55,  64,  81, 104, 113,  92,
    1.86 +    49,  64,  78,  87, 103, 121, 120, 101,
    1.87 +    72,  92,  95,  98, 112, 100, 103,  99
    1.88 +  };
    1.89 +  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
    1.90 +    17,  18,  24,  47,  99,  99,  99,  99,
    1.91 +    18,  21,  26,  66,  99,  99,  99,  99,
    1.92 +    24,  26,  56,  99,  99,  99,  99,  99,
    1.93 +    47,  66,  99,  99,  99,  99,  99,  99,
    1.94 +    99,  99,  99,  99,  99,  99,  99,  99,
    1.95 +    99,  99,  99,  99,  99,  99,  99,  99,
    1.96 +    99,  99,  99,  99,  99,  99,  99,  99,
    1.97 +    99,  99,  99,  99,  99,  99,  99,  99
    1.98 +  };
    1.99 +
   1.100 +  /* Set up two quantization tables using the specified scaling */
   1.101 +  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
   1.102 +		       scale_factor, force_baseline);
   1.103 +  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
   1.104 +		       scale_factor, force_baseline);
   1.105 +}
   1.106 +
   1.107 +
   1.108 +GLOBAL(int)
   1.109 +jpeg_quality_scaling (int quality)
   1.110 +/* Convert a user-specified quality rating to a percentage scaling factor
   1.111 + * for an underlying quantization table, using our recommended scaling curve.
   1.112 + * The input 'quality' factor should be 0 (terrible) to 100 (very good).
   1.113 + */
   1.114 +{
   1.115 +  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
   1.116 +  if (quality <= 0) quality = 1;
   1.117 +  if (quality > 100) quality = 100;
   1.118 +
   1.119 +  /* The basic table is used as-is (scaling 100) for a quality of 50.
   1.120 +   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
   1.121 +   * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
   1.122 +   * to make all the table entries 1 (hence, minimum quantization loss).
   1.123 +   * Qualities 1..50 are converted to scaling percentage 5000/Q.
   1.124 +   */
   1.125 +  if (quality < 50)
   1.126 +    quality = 5000 / quality;
   1.127 +  else
   1.128 +    quality = 200 - quality*2;
   1.129 +
   1.130 +  return quality;
   1.131 +}
   1.132 +
   1.133 +
   1.134 +GLOBAL(void)
   1.135 +jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
   1.136 +/* Set or change the 'quality' (quantization) setting, using default tables.
   1.137 + * This is the standard quality-adjusting entry point for typical user
   1.138 + * interfaces; only those who want detailed control over quantization tables
   1.139 + * would use the preceding three routines directly.
   1.140 + */
   1.141 +{
   1.142 +  /* Convert user 0-100 rating to percentage scaling */
   1.143 +  quality = jpeg_quality_scaling(quality);
   1.144 +
   1.145 +  /* Set up standard quality tables */
   1.146 +  jpeg_set_linear_quality(cinfo, quality, force_baseline);
   1.147 +}
   1.148 +
   1.149 +
   1.150 +/*
   1.151 + * Huffman table setup routines
   1.152 + */
   1.153 +
   1.154 +LOCAL(void)
   1.155 +add_huff_table (j_compress_ptr cinfo,
   1.156 +		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
   1.157 +/* Define a Huffman table */
   1.158 +{
   1.159 +  int nsymbols, len;
   1.160 +
   1.161 +  if (*htblptr == NULL)
   1.162 +    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
   1.163 +
   1.164 +  /* Copy the number-of-symbols-of-each-code-length counts */
   1.165 +  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
   1.166 +
   1.167 +  /* Validate the counts.  We do this here mainly so we can copy the right
   1.168 +   * number of symbols from the val[] array, without risking marching off
   1.169 +   * the end of memory.  jchuff.c will do a more thorough test later.
   1.170 +   */
   1.171 +  nsymbols = 0;
   1.172 +  for (len = 1; len <= 16; len++)
   1.173 +    nsymbols += bits[len];
   1.174 +  if (nsymbols < 1 || nsymbols > 256)
   1.175 +    ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
   1.176 +
   1.177 +  MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
   1.178 +
   1.179 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
   1.180 +  (*htblptr)->sent_table = FALSE;
   1.181 +}
   1.182 +
   1.183 +
   1.184 +LOCAL(void)
   1.185 +std_huff_tables (j_compress_ptr cinfo)
   1.186 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
   1.187 +/* IMPORTANT: these are only valid for 8-bit data precision! */
   1.188 +{
   1.189 +  static const UINT8 bits_dc_luminance[17] =
   1.190 +    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
   1.191 +  static const UINT8 val_dc_luminance[] =
   1.192 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
   1.193 +  
   1.194 +  static const UINT8 bits_dc_chrominance[17] =
   1.195 +    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
   1.196 +  static const UINT8 val_dc_chrominance[] =
   1.197 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
   1.198 +  
   1.199 +  static const UINT8 bits_ac_luminance[17] =
   1.200 +    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
   1.201 +  static const UINT8 val_ac_luminance[] =
   1.202 +    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
   1.203 +      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
   1.204 +      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
   1.205 +      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
   1.206 +      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
   1.207 +      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
   1.208 +      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
   1.209 +      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
   1.210 +      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
   1.211 +      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
   1.212 +      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
   1.213 +      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
   1.214 +      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
   1.215 +      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
   1.216 +      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
   1.217 +      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
   1.218 +      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
   1.219 +      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
   1.220 +      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
   1.221 +      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
   1.222 +      0xf9, 0xfa };
   1.223 +  
   1.224 +  static const UINT8 bits_ac_chrominance[17] =
   1.225 +    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
   1.226 +  static const UINT8 val_ac_chrominance[] =
   1.227 +    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
   1.228 +      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
   1.229 +      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
   1.230 +      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
   1.231 +      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
   1.232 +      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
   1.233 +      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
   1.234 +      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
   1.235 +      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
   1.236 +      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
   1.237 +      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
   1.238 +      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
   1.239 +      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
   1.240 +      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
   1.241 +      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
   1.242 +      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
   1.243 +      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
   1.244 +      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
   1.245 +      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
   1.246 +      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
   1.247 +      0xf9, 0xfa };
   1.248 +  
   1.249 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
   1.250 +		 bits_dc_luminance, val_dc_luminance);
   1.251 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
   1.252 +		 bits_ac_luminance, val_ac_luminance);
   1.253 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
   1.254 +		 bits_dc_chrominance, val_dc_chrominance);
   1.255 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
   1.256 +		 bits_ac_chrominance, val_ac_chrominance);
   1.257 +}
   1.258 +
   1.259 +
   1.260 +/*
   1.261 + * Default parameter setup for compression.
   1.262 + *
   1.263 + * Applications that don't choose to use this routine must do their
   1.264 + * own setup of all these parameters.  Alternately, you can call this
   1.265 + * to establish defaults and then alter parameters selectively.  This
   1.266 + * is the recommended approach since, if we add any new parameters,
   1.267 + * your code will still work (they'll be set to reasonable defaults).
   1.268 + */
   1.269 +
   1.270 +GLOBAL(void)
   1.271 +jpeg_set_defaults (j_compress_ptr cinfo)
   1.272 +{
   1.273 +  int i;
   1.274 +
   1.275 +  /* Safety check to ensure start_compress not called yet. */
   1.276 +  if (cinfo->global_state != CSTATE_START)
   1.277 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.278 +
   1.279 +  /* Allocate comp_info array large enough for maximum component count.
   1.280 +   * Array is made permanent in case application wants to compress
   1.281 +   * multiple images at same param settings.
   1.282 +   */
   1.283 +  if (cinfo->comp_info == NULL)
   1.284 +    cinfo->comp_info = (jpeg_component_info *)
   1.285 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.286 +				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));
   1.287 +
   1.288 +  /* Initialize everything not dependent on the color space */
   1.289 +
   1.290 +  cinfo->data_precision = BITS_IN_JSAMPLE;
   1.291 +  /* Set up two quantization tables using default quality of 75 */
   1.292 +  jpeg_set_quality(cinfo, 75, TRUE);
   1.293 +  /* Set up two Huffman tables */
   1.294 +  std_huff_tables(cinfo);
   1.295 +
   1.296 +  /* Initialize default arithmetic coding conditioning */
   1.297 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
   1.298 +    cinfo->arith_dc_L[i] = 0;
   1.299 +    cinfo->arith_dc_U[i] = 1;
   1.300 +    cinfo->arith_ac_K[i] = 5;
   1.301 +  }
   1.302 +
   1.303 +  /* Default is no multiple-scan output */
   1.304 +  cinfo->scan_info = NULL;
   1.305 +  cinfo->num_scans = 0;
   1.306 +
   1.307 +  /* Expect normal source image, not raw downsampled data */
   1.308 +  cinfo->raw_data_in = FALSE;
   1.309 +
   1.310 +  /* Use Huffman coding, not arithmetic coding, by default */
   1.311 +  cinfo->arith_code = FALSE;
   1.312 +
   1.313 +  /* By default, don't do extra passes to optimize entropy coding */
   1.314 +  cinfo->optimize_coding = FALSE;
   1.315 +  /* The standard Huffman tables are only valid for 8-bit data precision.
   1.316 +   * If the precision is higher, force optimization on so that usable
   1.317 +   * tables will be computed.  This test can be removed if default tables
   1.318 +   * are supplied that are valid for the desired precision.
   1.319 +   */
   1.320 +  if (cinfo->data_precision > 8)
   1.321 +    cinfo->optimize_coding = TRUE;
   1.322 +
   1.323 +  /* By default, use the simpler non-cosited sampling alignment */
   1.324 +  cinfo->CCIR601_sampling = FALSE;
   1.325 +
   1.326 +  /* No input smoothing */
   1.327 +  cinfo->smoothing_factor = 0;
   1.328 +
   1.329 +  /* DCT algorithm preference */
   1.330 +  cinfo->dct_method = JDCT_DEFAULT;
   1.331 +
   1.332 +  /* No restart markers */
   1.333 +  cinfo->restart_interval = 0;
   1.334 +  cinfo->restart_in_rows = 0;
   1.335 +
   1.336 +  /* Fill in default JFIF marker parameters.  Note that whether the marker
   1.337 +   * will actually be written is determined by jpeg_set_colorspace.
   1.338 +   *
   1.339 +   * By default, the library emits JFIF version code 1.01.
   1.340 +   * An application that wants to emit JFIF 1.02 extension markers should set
   1.341 +   * JFIF_minor_version to 2.  We could probably get away with just defaulting
   1.342 +   * to 1.02, but there may still be some decoders in use that will complain
   1.343 +   * about that; saying 1.01 should minimize compatibility problems.
   1.344 +   */
   1.345 +  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
   1.346 +  cinfo->JFIF_minor_version = 1;
   1.347 +  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
   1.348 +  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
   1.349 +  cinfo->Y_density = 1;
   1.350 +
   1.351 +  /* Choose JPEG colorspace based on input space, set defaults accordingly */
   1.352 +
   1.353 +  jpeg_default_colorspace(cinfo);
   1.354 +}
   1.355 +
   1.356 +
   1.357 +/*
   1.358 + * Select an appropriate JPEG colorspace for in_color_space.
   1.359 + */
   1.360 +
   1.361 +GLOBAL(void)
   1.362 +jpeg_default_colorspace (j_compress_ptr cinfo)
   1.363 +{
   1.364 +  switch (cinfo->in_color_space) {
   1.365 +  case JCS_GRAYSCALE:
   1.366 +    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
   1.367 +    break;
   1.368 +  case JCS_RGB:
   1.369 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
   1.370 +    break;
   1.371 +  case JCS_YCbCr:
   1.372 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
   1.373 +    break;
   1.374 +  case JCS_CMYK:
   1.375 +    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
   1.376 +    break;
   1.377 +  case JCS_YCCK:
   1.378 +    jpeg_set_colorspace(cinfo, JCS_YCCK);
   1.379 +    break;
   1.380 +  case JCS_UNKNOWN:
   1.381 +    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
   1.382 +    break;
   1.383 +  default:
   1.384 +    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
   1.385 +  }
   1.386 +}
   1.387 +
   1.388 +
   1.389 +/*
   1.390 + * Set the JPEG colorspace, and choose colorspace-dependent default values.
   1.391 + */
   1.392 +
   1.393 +GLOBAL(void)
   1.394 +jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
   1.395 +{
   1.396 +  jpeg_component_info * compptr;
   1.397 +  int ci;
   1.398 +
   1.399 +#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
   1.400 +  (compptr = &cinfo->comp_info[index], \
   1.401 +   compptr->component_id = (id), \
   1.402 +   compptr->h_samp_factor = (hsamp), \
   1.403 +   compptr->v_samp_factor = (vsamp), \
   1.404 +   compptr->quant_tbl_no = (quant), \
   1.405 +   compptr->dc_tbl_no = (dctbl), \
   1.406 +   compptr->ac_tbl_no = (actbl) )
   1.407 +
   1.408 +  /* Safety check to ensure start_compress not called yet. */
   1.409 +  if (cinfo->global_state != CSTATE_START)
   1.410 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.411 +
   1.412 +  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
   1.413 +   * tables 1 for chrominance components.
   1.414 +   */
   1.415 +
   1.416 +  cinfo->jpeg_color_space = colorspace;
   1.417 +
   1.418 +  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
   1.419 +  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
   1.420 +
   1.421 +  switch (colorspace) {
   1.422 +  case JCS_GRAYSCALE:
   1.423 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
   1.424 +    cinfo->num_components = 1;
   1.425 +    /* JFIF specifies component ID 1 */
   1.426 +    SET_COMP(0, 1, 1,1, 0, 0,0);
   1.427 +    break;
   1.428 +  case JCS_RGB:
   1.429 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
   1.430 +    cinfo->num_components = 3;
   1.431 +    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
   1.432 +    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
   1.433 +    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
   1.434 +    break;
   1.435 +  case JCS_YCbCr:
   1.436 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
   1.437 +    cinfo->num_components = 3;
   1.438 +    /* JFIF specifies component IDs 1,2,3 */
   1.439 +    /* We default to 2x2 subsamples of chrominance */
   1.440 +    SET_COMP(0, 1, 2,2, 0, 0,0);
   1.441 +    SET_COMP(1, 2, 1,1, 1, 1,1);
   1.442 +    SET_COMP(2, 3, 1,1, 1, 1,1);
   1.443 +    break;
   1.444 +  case JCS_CMYK:
   1.445 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
   1.446 +    cinfo->num_components = 4;
   1.447 +    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
   1.448 +    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
   1.449 +    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
   1.450 +    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
   1.451 +    break;
   1.452 +  case JCS_YCCK:
   1.453 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
   1.454 +    cinfo->num_components = 4;
   1.455 +    SET_COMP(0, 1, 2,2, 0, 0,0);
   1.456 +    SET_COMP(1, 2, 1,1, 1, 1,1);
   1.457 +    SET_COMP(2, 3, 1,1, 1, 1,1);
   1.458 +    SET_COMP(3, 4, 2,2, 0, 0,0);
   1.459 +    break;
   1.460 +  case JCS_UNKNOWN:
   1.461 +    cinfo->num_components = cinfo->input_components;
   1.462 +    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
   1.463 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   1.464 +	       MAX_COMPONENTS);
   1.465 +    for (ci = 0; ci < cinfo->num_components; ci++) {
   1.466 +      SET_COMP(ci, ci, 1,1, 0, 0,0);
   1.467 +    }
   1.468 +    break;
   1.469 +  default:
   1.470 +    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
   1.471 +  }
   1.472 +}
   1.473 +
   1.474 +
   1.475 +#ifdef C_PROGRESSIVE_SUPPORTED
   1.476 +
   1.477 +LOCAL(jpeg_scan_info *)
   1.478 +fill_a_scan (jpeg_scan_info * scanptr, int ci,
   1.479 +	     int Ss, int Se, int Ah, int Al)
   1.480 +/* Support routine: generate one scan for specified component */
   1.481 +{
   1.482 +  scanptr->comps_in_scan = 1;
   1.483 +  scanptr->component_index[0] = ci;
   1.484 +  scanptr->Ss = Ss;
   1.485 +  scanptr->Se = Se;
   1.486 +  scanptr->Ah = Ah;
   1.487 +  scanptr->Al = Al;
   1.488 +  scanptr++;
   1.489 +  return scanptr;
   1.490 +}
   1.491 +
   1.492 +LOCAL(jpeg_scan_info *)
   1.493 +fill_scans (jpeg_scan_info * scanptr, int ncomps,
   1.494 +	    int Ss, int Se, int Ah, int Al)
   1.495 +/* Support routine: generate one scan for each component */
   1.496 +{
   1.497 +  int ci;
   1.498 +
   1.499 +  for (ci = 0; ci < ncomps; ci++) {
   1.500 +    scanptr->comps_in_scan = 1;
   1.501 +    scanptr->component_index[0] = ci;
   1.502 +    scanptr->Ss = Ss;
   1.503 +    scanptr->Se = Se;
   1.504 +    scanptr->Ah = Ah;
   1.505 +    scanptr->Al = Al;
   1.506 +    scanptr++;
   1.507 +  }
   1.508 +  return scanptr;
   1.509 +}
   1.510 +
   1.511 +LOCAL(jpeg_scan_info *)
   1.512 +fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
   1.513 +/* Support routine: generate interleaved DC scan if possible, else N scans */
   1.514 +{
   1.515 +  int ci;
   1.516 +
   1.517 +  if (ncomps <= MAX_COMPS_IN_SCAN) {
   1.518 +    /* Single interleaved DC scan */
   1.519 +    scanptr->comps_in_scan = ncomps;
   1.520 +    for (ci = 0; ci < ncomps; ci++)
   1.521 +      scanptr->component_index[ci] = ci;
   1.522 +    scanptr->Ss = scanptr->Se = 0;
   1.523 +    scanptr->Ah = Ah;
   1.524 +    scanptr->Al = Al;
   1.525 +    scanptr++;
   1.526 +  } else {
   1.527 +    /* Noninterleaved DC scan for each component */
   1.528 +    scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
   1.529 +  }
   1.530 +  return scanptr;
   1.531 +}
   1.532 +
   1.533 +
   1.534 +/*
   1.535 + * Create a recommended progressive-JPEG script.
   1.536 + * cinfo->num_components and cinfo->jpeg_color_space must be correct.
   1.537 + */
   1.538 +
   1.539 +GLOBAL(void)
   1.540 +jpeg_simple_progression (j_compress_ptr cinfo)
   1.541 +{
   1.542 +  int ncomps = cinfo->num_components;
   1.543 +  int nscans;
   1.544 +  jpeg_scan_info * scanptr;
   1.545 +
   1.546 +  /* Safety check to ensure start_compress not called yet. */
   1.547 +  if (cinfo->global_state != CSTATE_START)
   1.548 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.549 +
   1.550 +  /* Figure space needed for script.  Calculation must match code below! */
   1.551 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
   1.552 +    /* Custom script for YCbCr color images. */
   1.553 +    nscans = 10;
   1.554 +  } else {
   1.555 +    /* All-purpose script for other color spaces. */
   1.556 +    if (ncomps > MAX_COMPS_IN_SCAN)
   1.557 +      nscans = 6 * ncomps;	/* 2 DC + 4 AC scans per component */
   1.558 +    else
   1.559 +      nscans = 2 + 4 * ncomps;	/* 2 DC scans; 4 AC scans per component */
   1.560 +  }
   1.561 +
   1.562 +  /* Allocate space for script.
   1.563 +   * We need to put it in the permanent pool in case the application performs
   1.564 +   * multiple compressions without changing the settings.  To avoid a memory
   1.565 +   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
   1.566 +   * object, we try to re-use previously allocated space, and we allocate
   1.567 +   * enough space to handle YCbCr even if initially asked for grayscale.
   1.568 +   */
   1.569 +  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
   1.570 +    cinfo->script_space_size = MAX(nscans, 10);
   1.571 +    cinfo->script_space = (jpeg_scan_info *)
   1.572 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.573 +			cinfo->script_space_size * SIZEOF(jpeg_scan_info));
   1.574 +  }
   1.575 +  scanptr = cinfo->script_space;
   1.576 +  cinfo->scan_info = scanptr;
   1.577 +  cinfo->num_scans = nscans;
   1.578 +
   1.579 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
   1.580 +    /* Custom script for YCbCr color images. */
   1.581 +    /* Initial DC scan */
   1.582 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
   1.583 +    /* Initial AC scan: get some luma data out in a hurry */
   1.584 +    scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
   1.585 +    /* Chroma data is too small to be worth expending many scans on */
   1.586 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
   1.587 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
   1.588 +    /* Complete spectral selection for luma AC */
   1.589 +    scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
   1.590 +    /* Refine next bit of luma AC */
   1.591 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
   1.592 +    /* Finish DC successive approximation */
   1.593 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
   1.594 +    /* Finish AC successive approximation */
   1.595 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
   1.596 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
   1.597 +    /* Luma bottom bit comes last since it's usually largest scan */
   1.598 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
   1.599 +  } else {
   1.600 +    /* All-purpose script for other color spaces. */
   1.601 +    /* Successive approximation first pass */
   1.602 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
   1.603 +    scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
   1.604 +    scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
   1.605 +    /* Successive approximation second pass */
   1.606 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
   1.607 +    /* Successive approximation final pass */
   1.608 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
   1.609 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
   1.610 +  }
   1.611 +}
   1.612 +
   1.613 +#endif /* C_PROGRESSIVE_SUPPORTED */