jcparam.c
Go to the documentation of this file.
00001 /*
00002  * jcparam.c
00003  *
00004  * Copyright (C) 1991-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains optional default-setting code for the JPEG compressor.
00009  * Applications do not have to use this file, but those that don't use it
00010  * must know a lot more about the innards of the JPEG code.
00011  */
00012 
00013 #define JPEG_INTERNALS
00014 #include "jinclude.h"
00015 #include "jpeglib.h"
00016 
00017 
00018 /*
00019  * Quantization table setup routines
00020  */
00021 
00022 GLOBAL(void)
00023 jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
00024                       const unsigned int *basic_table,
00025                       int scale_factor, boolean force_baseline)
00026 /* Define a quantization table equal to the basic_table times
00027  * a scale factor (given as a percentage).
00028  * If force_baseline is TRUE, the computed quantization table entries
00029  * are limited to 1..255 for JPEG baseline compatibility.
00030  */
00031 {
00032   JQUANT_TBL ** qtblptr;
00033   int i;
00034   long temp;
00035 
00036   /* Safety check to ensure start_compress not called yet. */
00037   if (cinfo->global_state != CSTATE_START)
00038     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00039 
00040   if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
00041     ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
00042 
00043   qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
00044 
00045   if (*qtblptr == NULL)
00046     *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
00047 
00048   for (i = 0; i < DCTSIZE2; i++) {
00049     temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
00050     /* limit the values to the valid range */
00051     if (temp <= 0L) temp = 1L;
00052     if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
00053     if (force_baseline && temp > 255L)
00054       temp = 255L;              /* limit to baseline range if requested */
00055     (*qtblptr)->quantval[i] = (UINT16) temp;
00056   }
00057 
00058   /* Initialize sent_table FALSE so table will be written to JPEG file. */
00059   (*qtblptr)->sent_table = FALSE;
00060 }
00061 
00062 
00063 GLOBAL(void)
00064 jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
00065                          boolean force_baseline)
00066 /* Set or change the 'quality' (quantization) setting, using default tables
00067  * and a straight percentage-scaling quality scale.  In most cases it's better
00068  * to use jpeg_set_quality (below); this entry point is provided for
00069  * applications that insist on a linear percentage scaling.
00070  */
00071 {
00072   /* These are the sample quantization tables given in JPEG spec section K.1.
00073    * The spec says that the values given produce "good" quality, and
00074    * when divided by 2, "very good" quality.
00075    */
00076   static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
00077     16,  11,  10,  16,  24,  40,  51,  61,
00078     12,  12,  14,  19,  26,  58,  60,  55,
00079     14,  13,  16,  24,  40,  57,  69,  56,
00080     14,  17,  22,  29,  51,  87,  80,  62,
00081     18,  22,  37,  56,  68, 109, 103,  77,
00082     24,  35,  55,  64,  81, 104, 113,  92,
00083     49,  64,  78,  87, 103, 121, 120, 101,
00084     72,  92,  95,  98, 112, 100, 103,  99
00085   };
00086   static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
00087     17,  18,  24,  47,  99,  99,  99,  99,
00088     18,  21,  26,  66,  99,  99,  99,  99,
00089     24,  26,  56,  99,  99,  99,  99,  99,
00090     47,  66,  99,  99,  99,  99,  99,  99,
00091     99,  99,  99,  99,  99,  99,  99,  99,
00092     99,  99,  99,  99,  99,  99,  99,  99,
00093     99,  99,  99,  99,  99,  99,  99,  99,
00094     99,  99,  99,  99,  99,  99,  99,  99
00095   };
00096 
00097   /* Set up two quantization tables using the specified scaling */
00098   jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
00099                        scale_factor, force_baseline);
00100   jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
00101                        scale_factor, force_baseline);
00102 }
00103 
00104 
00105 GLOBAL(int)
00106 jpeg_quality_scaling (int quality)
00107 /* Convert a user-specified quality rating to a percentage scaling factor
00108  * for an underlying quantization table, using our recommended scaling curve.
00109  * The input 'quality' factor should be 0 (terrible) to 100 (very good).
00110  */
00111 {
00112   /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
00113   if (quality <= 0) quality = 1;
00114   if (quality > 100) quality = 100;
00115 
00116   /* The basic table is used as-is (scaling 100) for a quality of 50.
00117    * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
00118    * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
00119    * to make all the table entries 1 (hence, minimum quantization loss).
00120    * Qualities 1..50 are converted to scaling percentage 5000/Q.
00121    */
00122   if (quality < 50)
00123     quality = 5000 / quality;
00124   else
00125     quality = 200 - quality*2;
00126 
00127   return quality;
00128 }
00129 
00130 
00131 GLOBAL(void)
00132 jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
00133 /* Set or change the 'quality' (quantization) setting, using default tables.
00134  * This is the standard quality-adjusting entry point for typical user
00135  * interfaces; only those who want detailed control over quantization tables
00136  * would use the preceding three routines directly.
00137  */
00138 {
00139   /* Convert user 0-100 rating to percentage scaling */
00140   quality = jpeg_quality_scaling(quality);
00141 
00142   /* Set up standard quality tables */
00143   jpeg_set_linear_quality(cinfo, quality, force_baseline);
00144 }
00145 
00146 
00147 /*
00148  * Huffman table setup routines
00149  */
00150 
00151 LOCAL(void)
00152 add_huff_table (j_compress_ptr cinfo,
00153                 JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
00154 /* Define a Huffman table */
00155 {
00156   int nsymbols, len;
00157 
00158   if (*htblptr == NULL)
00159     *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
00160 
00161   /* Copy the number-of-symbols-of-each-code-length counts */
00162   MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
00163 
00164   /* Validate the counts.  We do this here mainly so we can copy the right
00165    * number of symbols from the val[] array, without risking marching off
00166    * the end of memory.  jchuff.c will do a more thorough test later.
00167    */
00168   nsymbols = 0;
00169   for (len = 1; len <= 16; len++)
00170     nsymbols += bits[len];
00171   if (nsymbols < 1 || nsymbols > 256)
00172     ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00173 
00174   MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
00175 
00176   /* Initialize sent_table FALSE so table will be written to JPEG file. */
00177   (*htblptr)->sent_table = FALSE;
00178 }
00179 
00180 
00181 LOCAL(void)
00182 std_huff_tables (j_compress_ptr cinfo)
00183 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
00184 /* IMPORTANT: these are only valid for 8-bit data precision! */
00185 {
00186   static const UINT8 bits_dc_luminance[17] =
00187     { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
00188   static const UINT8 val_dc_luminance[] =
00189     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00190   
00191   static const UINT8 bits_dc_chrominance[17] =
00192     { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00193   static const UINT8 val_dc_chrominance[] =
00194     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00195   
00196   static const UINT8 bits_ac_luminance[17] =
00197     { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
00198   static const UINT8 val_ac_luminance[] =
00199     { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
00200       0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00201       0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
00202       0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
00203       0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
00204       0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
00205       0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
00206       0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00207       0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00208       0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00209       0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00210       0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00211       0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
00212       0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
00213       0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
00214       0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
00215       0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
00216       0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
00217       0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
00218       0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00219       0xf9, 0xfa };
00220   
00221   static const UINT8 bits_ac_chrominance[17] =
00222     { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
00223   static const UINT8 val_ac_chrominance[] =
00224     { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
00225       0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00226       0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
00227       0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
00228       0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
00229       0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
00230       0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
00231       0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00232       0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00233       0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00234       0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
00235       0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00236       0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
00237       0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
00238       0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
00239       0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
00240       0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
00241       0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
00242       0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
00243       0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00244       0xf9, 0xfa };
00245   
00246   add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
00247                  bits_dc_luminance, val_dc_luminance);
00248   add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
00249                  bits_ac_luminance, val_ac_luminance);
00250   add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
00251                  bits_dc_chrominance, val_dc_chrominance);
00252   add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
00253                  bits_ac_chrominance, val_ac_chrominance);
00254 }
00255 
00256 
00257 /*
00258  * Default parameter setup for compression.
00259  *
00260  * Applications that don't choose to use this routine must do their
00261  * own setup of all these parameters.  Alternately, you can call this
00262  * to establish defaults and then alter parameters selectively.  This
00263  * is the recommended approach since, if we add any new parameters,
00264  * your code will still work (they'll be set to reasonable defaults).
00265  */
00266 
00267 GLOBAL(void)
00268 jpeg_set_defaults (j_compress_ptr cinfo)
00269 {
00270   int i;
00271 
00272   /* Safety check to ensure start_compress not called yet. */
00273   if (cinfo->global_state != CSTATE_START)
00274     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00275 
00276   /* Allocate comp_info array large enough for maximum component count.
00277    * Array is made permanent in case application wants to compress
00278    * multiple images at same param settings.
00279    */
00280   if (cinfo->comp_info == NULL)
00281     cinfo->comp_info = (jpeg_component_info *)
00282       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00283                                   MAX_COMPONENTS * SIZEOF(jpeg_component_info));
00284 
00285   /* Initialize everything not dependent on the color space */
00286 
00287   cinfo->data_precision = BITS_IN_JSAMPLE;
00288   /* Set up two quantization tables using default quality of 75 */
00289   jpeg_set_quality(cinfo, 75, TRUE);
00290   /* Set up two Huffman tables */
00291   std_huff_tables(cinfo);
00292 
00293   /* Initialize default arithmetic coding conditioning */
00294   for (i = 0; i < NUM_ARITH_TBLS; i++) {
00295     cinfo->arith_dc_L[i] = 0;
00296     cinfo->arith_dc_U[i] = 1;
00297     cinfo->arith_ac_K[i] = 5;
00298   }
00299 
00300   /* Default is no multiple-scan output */
00301   cinfo->scan_info = NULL;
00302   cinfo->num_scans = 0;
00303 
00304   /* Expect normal source image, not raw downsampled data */
00305   cinfo->raw_data_in = FALSE;
00306 
00307   /* Use Huffman coding, not arithmetic coding, by default */
00308   cinfo->arith_code = FALSE;
00309 
00310   /* By default, don't do extra passes to optimize entropy coding */
00311   cinfo->optimize_coding = FALSE;
00312   /* The standard Huffman tables are only valid for 8-bit data precision.
00313    * If the precision is higher, force optimization on so that usable
00314    * tables will be computed.  This test can be removed if default tables
00315    * are supplied that are valid for the desired precision.
00316    */
00317   if (cinfo->data_precision > 8)
00318     cinfo->optimize_coding = TRUE;
00319 
00320   /* By default, use the simpler non-cosited sampling alignment */
00321   cinfo->CCIR601_sampling = FALSE;
00322 
00323   /* No input smoothing */
00324   cinfo->smoothing_factor = 0;
00325 
00326   /* DCT algorithm preference */
00327   cinfo->dct_method = JDCT_DEFAULT;
00328 
00329   /* No restart markers */
00330   cinfo->restart_interval = 0;
00331   cinfo->restart_in_rows = 0;
00332 
00333   /* Fill in default JFIF marker parameters.  Note that whether the marker
00334    * will actually be written is determined by jpeg_set_colorspace.
00335    *
00336    * By default, the library emits JFIF version code 1.01.
00337    * An application that wants to emit JFIF 1.02 extension markers should set
00338    * JFIF_minor_version to 2.  We could probably get away with just defaulting
00339    * to 1.02, but there may still be some decoders in use that will complain
00340    * about that; saying 1.01 should minimize compatibility problems.
00341    */
00342   cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
00343   cinfo->JFIF_minor_version = 1;
00344   cinfo->density_unit = 0;      /* Pixel size is unknown by default */
00345   cinfo->X_density = 1;         /* Pixel aspect ratio is square by default */
00346   cinfo->Y_density = 1;
00347 
00348   /* Choose JPEG colorspace based on input space, set defaults accordingly */
00349 
00350   jpeg_default_colorspace(cinfo);
00351 }
00352 
00353 
00354 /*
00355  * Select an appropriate JPEG colorspace for in_color_space.
00356  */
00357 
00358 GLOBAL(void)
00359 jpeg_default_colorspace (j_compress_ptr cinfo)
00360 {
00361   switch (cinfo->in_color_space) {
00362   case JCS_GRAYSCALE:
00363     jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
00364     break;
00365   case JCS_RGB:
00366     jpeg_set_colorspace(cinfo, JCS_YCbCr);
00367     break;
00368   case JCS_YCbCr:
00369     jpeg_set_colorspace(cinfo, JCS_YCbCr);
00370     break;
00371   case JCS_CMYK:
00372     jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
00373     break;
00374   case JCS_YCCK:
00375     jpeg_set_colorspace(cinfo, JCS_YCCK);
00376     break;
00377   case JCS_UNKNOWN:
00378     jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
00379     break;
00380   default:
00381     ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
00382   }
00383 }
00384 
00385 
00386 /*
00387  * Set the JPEG colorspace, and choose colorspace-dependent default values.
00388  */
00389 
00390 GLOBAL(void)
00391 jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
00392 {
00393   jpeg_component_info * compptr;
00394   int ci;
00395 
00396 #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
00397   (compptr = &cinfo->comp_info[index], \
00398    compptr->component_id = (id), \
00399    compptr->h_samp_factor = (hsamp), \
00400    compptr->v_samp_factor = (vsamp), \
00401    compptr->quant_tbl_no = (quant), \
00402    compptr->dc_tbl_no = (dctbl), \
00403    compptr->ac_tbl_no = (actbl) )
00404 
00405   /* Safety check to ensure start_compress not called yet. */
00406   if (cinfo->global_state != CSTATE_START)
00407     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00408 
00409   /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
00410    * tables 1 for chrominance components.
00411    */
00412 
00413   cinfo->jpeg_color_space = colorspace;
00414 
00415   cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
00416   cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
00417 
00418   switch (colorspace) {
00419   case JCS_GRAYSCALE:
00420     cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
00421     cinfo->num_components = 1;
00422     /* JFIF specifies component ID 1 */
00423     SET_COMP(0, 1, 1,1, 0, 0,0);
00424     break;
00425   case JCS_RGB:
00426     cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
00427     cinfo->num_components = 3;
00428     SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
00429     SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
00430     SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
00431     break;
00432   case JCS_YCbCr:
00433     cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
00434     cinfo->num_components = 3;
00435     /* JFIF specifies component IDs 1,2,3 */
00436     /* We default to 2x2 subsamples of chrominance */
00437     SET_COMP(0, 1, 2,2, 0, 0,0);
00438     SET_COMP(1, 2, 1,1, 1, 1,1);
00439     SET_COMP(2, 3, 1,1, 1, 1,1);
00440     break;
00441   case JCS_CMYK:
00442     cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
00443     cinfo->num_components = 4;
00444     SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
00445     SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
00446     SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
00447     SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
00448     break;
00449   case JCS_YCCK:
00450     cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
00451     cinfo->num_components = 4;
00452     SET_COMP(0, 1, 2,2, 0, 0,0);
00453     SET_COMP(1, 2, 1,1, 1, 1,1);
00454     SET_COMP(2, 3, 1,1, 1, 1,1);
00455     SET_COMP(3, 4, 2,2, 0, 0,0);
00456     break;
00457   case JCS_UNKNOWN:
00458     cinfo->num_components = cinfo->input_components;
00459     if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
00460       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00461                MAX_COMPONENTS);
00462     for (ci = 0; ci < cinfo->num_components; ci++) {
00463       SET_COMP(ci, ci, 1,1, 0, 0,0);
00464     }
00465     break;
00466   default:
00467     ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
00468   }
00469 }
00470 
00471 
00472 #ifdef C_PROGRESSIVE_SUPPORTED
00473 
00474 LOCAL(jpeg_scan_info *)
00475 fill_a_scan (jpeg_scan_info * scanptr, int ci,
00476              int Ss, int Se, int Ah, int Al)
00477 /* Support routine: generate one scan for specified component */
00478 {
00479   scanptr->comps_in_scan = 1;
00480   scanptr->component_index[0] = ci;
00481   scanptr->Ss = Ss;
00482   scanptr->Se = Se;
00483   scanptr->Ah = Ah;
00484   scanptr->Al = Al;
00485   scanptr++;
00486   return scanptr;
00487 }
00488 
00489 LOCAL(jpeg_scan_info *)
00490 fill_scans (jpeg_scan_info * scanptr, int ncomps,
00491             int Ss, int Se, int Ah, int Al)
00492 /* Support routine: generate one scan for each component */
00493 {
00494   int ci;
00495 
00496   for (ci = 0; ci < ncomps; ci++) {
00497     scanptr->comps_in_scan = 1;
00498     scanptr->component_index[0] = ci;
00499     scanptr->Ss = Ss;
00500     scanptr->Se = Se;
00501     scanptr->Ah = Ah;
00502     scanptr->Al = Al;
00503     scanptr++;
00504   }
00505   return scanptr;
00506 }
00507 
00508 LOCAL(jpeg_scan_info *)
00509 fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
00510 /* Support routine: generate interleaved DC scan if possible, else N scans */
00511 {
00512   int ci;
00513 
00514   if (ncomps <= MAX_COMPS_IN_SCAN) {
00515     /* Single interleaved DC scan */
00516     scanptr->comps_in_scan = ncomps;
00517     for (ci = 0; ci < ncomps; ci++)
00518       scanptr->component_index[ci] = ci;
00519     scanptr->Ss = scanptr->Se = 0;
00520     scanptr->Ah = Ah;
00521     scanptr->Al = Al;
00522     scanptr++;
00523   } else {
00524     /* Noninterleaved DC scan for each component */
00525     scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
00526   }
00527   return scanptr;
00528 }
00529 
00530 
00531 /*
00532  * Create a recommended progressive-JPEG script.
00533  * cinfo->num_components and cinfo->jpeg_color_space must be correct.
00534  */
00535 
00536 GLOBAL(void)
00537 jpeg_simple_progression (j_compress_ptr cinfo)
00538 {
00539   int ncomps = cinfo->num_components;
00540   int nscans;
00541   jpeg_scan_info * scanptr;
00542 
00543   /* Safety check to ensure start_compress not called yet. */
00544   if (cinfo->global_state != CSTATE_START)
00545     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00546 
00547   /* Figure space needed for script.  Calculation must match code below! */
00548   if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
00549     /* Custom script for YCbCr color images. */
00550     nscans = 10;
00551   } else {
00552     /* All-purpose script for other color spaces. */
00553     if (ncomps > MAX_COMPS_IN_SCAN)
00554       nscans = 6 * ncomps;      /* 2 DC + 4 AC scans per component */
00555     else
00556       nscans = 2 + 4 * ncomps;  /* 2 DC scans; 4 AC scans per component */
00557   }
00558 
00559   /* Allocate space for script.
00560    * We need to put it in the permanent pool in case the application performs
00561    * multiple compressions without changing the settings.  To avoid a memory
00562    * leak if jpeg_simple_progression is called repeatedly for the same JPEG
00563    * object, we try to re-use previously allocated space, and we allocate
00564    * enough space to handle YCbCr even if initially asked for grayscale.
00565    */
00566   if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
00567     cinfo->script_space_size = MAX(nscans, 10);
00568     cinfo->script_space = (jpeg_scan_info *)
00569       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00570                         cinfo->script_space_size * SIZEOF(jpeg_scan_info));
00571   }
00572   scanptr = cinfo->script_space;
00573   cinfo->scan_info = scanptr;
00574   cinfo->num_scans = nscans;
00575 
00576   if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
00577     /* Custom script for YCbCr color images. */
00578     /* Initial DC scan */
00579     scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
00580     /* Initial AC scan: get some luma data out in a hurry */
00581     scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
00582     /* Chroma data is too small to be worth expending many scans on */
00583     scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
00584     scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
00585     /* Complete spectral selection for luma AC */
00586     scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
00587     /* Refine next bit of luma AC */
00588     scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
00589     /* Finish DC successive approximation */
00590     scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
00591     /* Finish AC successive approximation */
00592     scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
00593     scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
00594     /* Luma bottom bit comes last since it's usually largest scan */
00595     scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
00596   } else {
00597     /* All-purpose script for other color spaces. */
00598     /* Successive approximation first pass */
00599     scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
00600     scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
00601     scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
00602     /* Successive approximation second pass */
00603     scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
00604     /* Successive approximation final pass */
00605     scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
00606     scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
00607   }
00608 }
00609 
00610 #endif /* C_PROGRESSIVE_SUPPORTED */


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17