Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcparam.c
41152 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcparam.c6*7* Copyright (C) 1991-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains optional default-setting code for the JPEG compressor.12* Applications do not have to use this file, but those that don't use it13* must know a lot more about the innards of the JPEG code.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"192021/*22* Quantization table setup routines23*/2425GLOBAL(void)26jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,27const unsigned int *basic_table,28int scale_factor, boolean force_baseline)29/* Define a quantization table equal to the basic_table times30* a scale factor (given as a percentage).31* If force_baseline is TRUE, the computed quantization table entries32* are limited to 1..255 for JPEG baseline compatibility.33*/34{35JQUANT_TBL ** qtblptr;36int i;37long temp;3839/* Safety check to ensure start_compress not called yet. */40if (cinfo->global_state != CSTATE_START)41ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);4243if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)44ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);4546qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];4748if (*qtblptr == NULL)49*qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);5051for (i = 0; i < DCTSIZE2; i++) {52temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;53/* limit the values to the valid range */54if (temp <= 0L) temp = 1L;55if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */56if (force_baseline && temp > 255L)57temp = 255L; /* limit to baseline range if requested */58(*qtblptr)->quantval[i] = (UINT16) temp;59}6061/* Initialize sent_table FALSE so table will be written to JPEG file. */62(*qtblptr)->sent_table = FALSE;63}646566GLOBAL(void)67jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,68boolean force_baseline)69/* Set or change the 'quality' (quantization) setting, using default tables70* and a straight percentage-scaling quality scale. In most cases it's better71* to use jpeg_set_quality (below); this entry point is provided for72* applications that insist on a linear percentage scaling.73*/74{75/* These are the sample quantization tables given in JPEG spec section K.1.76* The spec says that the values given produce "good" quality, and77* when divided by 2, "very good" quality.78*/79static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {8016, 11, 10, 16, 24, 40, 51, 61,8112, 12, 14, 19, 26, 58, 60, 55,8214, 13, 16, 24, 40, 57, 69, 56,8314, 17, 22, 29, 51, 87, 80, 62,8418, 22, 37, 56, 68, 109, 103, 77,8524, 35, 55, 64, 81, 104, 113, 92,8649, 64, 78, 87, 103, 121, 120, 101,8772, 92, 95, 98, 112, 100, 103, 9988};89static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {9017, 18, 24, 47, 99, 99, 99, 99,9118, 21, 26, 66, 99, 99, 99, 99,9224, 26, 56, 99, 99, 99, 99, 99,9347, 66, 99, 99, 99, 99, 99, 99,9499, 99, 99, 99, 99, 99, 99, 99,9599, 99, 99, 99, 99, 99, 99, 99,9699, 99, 99, 99, 99, 99, 99, 99,9799, 99, 99, 99, 99, 99, 99, 9998};99100/* Set up two quantization tables using the specified scaling */101jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,102scale_factor, force_baseline);103jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,104scale_factor, force_baseline);105}106107108GLOBAL(int)109jpeg_quality_scaling (int quality)110/* Convert a user-specified quality rating to a percentage scaling factor111* for an underlying quantization table, using our recommended scaling curve.112* The input 'quality' factor should be 0 (terrible) to 100 (very good).113*/114{115/* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */116if (quality <= 0) quality = 1;117if (quality > 100) quality = 100;118119/* The basic table is used as-is (scaling 100) for a quality of 50.120* Qualities 50..100 are converted to scaling percentage 200 - 2*Q;121* note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table122* to make all the table entries 1 (hence, minimum quantization loss).123* Qualities 1..50 are converted to scaling percentage 5000/Q.124*/125if (quality < 50)126quality = 5000 / quality;127else128quality = 200 - quality*2;129130return quality;131}132133134GLOBAL(void)135jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)136/* Set or change the 'quality' (quantization) setting, using default tables.137* This is the standard quality-adjusting entry point for typical user138* interfaces; only those who want detailed control over quantization tables139* would use the preceding three routines directly.140*/141{142/* Convert user 0-100 rating to percentage scaling */143quality = jpeg_quality_scaling(quality);144145/* Set up standard quality tables */146jpeg_set_linear_quality(cinfo, quality, force_baseline);147}148149150/*151* Huffman table setup routines152*/153154LOCAL(void)155add_huff_table (j_compress_ptr cinfo,156JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)157/* Define a Huffman table */158{159int nsymbols, len;160161if (*htblptr == NULL)162*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);163164/* Copy the number-of-symbols-of-each-code-length counts */165MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));166167/* Validate the counts. We do this here mainly so we can copy the right168* number of symbols from the val[] array, without risking marching off169* the end of memory. jchuff.c will do a more thorough test later.170*/171nsymbols = 0;172for (len = 1; len <= 16; len++)173nsymbols += bits[len];174if (nsymbols < 1 || nsymbols > 256)175ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);176177MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));178179/* Initialize sent_table FALSE so table will be written to JPEG file. */180(*htblptr)->sent_table = FALSE;181}182183184LOCAL(void)185std_huff_tables (j_compress_ptr cinfo)186/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */187/* IMPORTANT: these are only valid for 8-bit data precision! */188{189static const UINT8 bits_dc_luminance[17] =190{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };191static const UINT8 val_dc_luminance[] =192{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };193194static const UINT8 bits_dc_chrominance[17] =195{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };196static const UINT8 val_dc_chrominance[] =197{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };198199static const UINT8 bits_ac_luminance[17] =200{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };201static const UINT8 val_ac_luminance[] =202{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,2030x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,2040x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,2050x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,2060x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,2070x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,2080x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,2090x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,2100x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,2110x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,2120x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,2130x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,2140x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,2150x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,2160xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,2170xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,2180xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,2190xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,2200xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,2210xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,2220xf9, 0xfa };223224static const UINT8 bits_ac_chrominance[17] =225{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };226static const UINT8 val_ac_chrominance[] =227{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,2280x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,2290x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,2300xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,2310x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,2320xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,2330x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,2340x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,2350x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,2360x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,2370x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,2380x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,2390x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,2400x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,2410xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,2420xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,2430xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,2440xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,2450xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,2460xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,2470xf9, 0xfa };248249add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],250bits_dc_luminance, val_dc_luminance);251add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],252bits_ac_luminance, val_ac_luminance);253add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],254bits_dc_chrominance, val_dc_chrominance);255add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],256bits_ac_chrominance, val_ac_chrominance);257}258259260/*261* Default parameter setup for compression.262*263* Applications that don't choose to use this routine must do their264* own setup of all these parameters. Alternately, you can call this265* to establish defaults and then alter parameters selectively. This266* is the recommended approach since, if we add any new parameters,267* your code will still work (they'll be set to reasonable defaults).268*/269270GLOBAL(void)271jpeg_set_defaults (j_compress_ptr cinfo)272{273int i;274275/* Safety check to ensure start_compress not called yet. */276if (cinfo->global_state != CSTATE_START)277ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);278279/* Allocate comp_info array large enough for maximum component count.280* Array is made permanent in case application wants to compress281* multiple images at same param settings.282*/283if (cinfo->comp_info == NULL)284cinfo->comp_info = (jpeg_component_info *)285(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,286MAX_COMPONENTS * SIZEOF(jpeg_component_info));287288/* Initialize everything not dependent on the color space */289290cinfo->data_precision = BITS_IN_JSAMPLE;291/* Set up two quantization tables using default quality of 75 */292jpeg_set_quality(cinfo, 75, TRUE);293/* Set up two Huffman tables */294std_huff_tables(cinfo);295296/* Initialize default arithmetic coding conditioning */297for (i = 0; i < NUM_ARITH_TBLS; i++) {298cinfo->arith_dc_L[i] = 0;299cinfo->arith_dc_U[i] = 1;300cinfo->arith_ac_K[i] = 5;301}302303/* Default is no multiple-scan output */304cinfo->scan_info = NULL;305cinfo->num_scans = 0;306307/* Expect normal source image, not raw downsampled data */308cinfo->raw_data_in = FALSE;309310/* Use Huffman coding, not arithmetic coding, by default */311cinfo->arith_code = FALSE;312313/* By default, don't do extra passes to optimize entropy coding */314cinfo->optimize_coding = FALSE;315/* The standard Huffman tables are only valid for 8-bit data precision.316* If the precision is higher, force optimization on so that usable317* tables will be computed. This test can be removed if default tables318* are supplied that are valid for the desired precision.319*/320if (cinfo->data_precision > 8)321cinfo->optimize_coding = TRUE;322323/* By default, use the simpler non-cosited sampling alignment */324cinfo->CCIR601_sampling = FALSE;325326/* No input smoothing */327cinfo->smoothing_factor = 0;328329/* DCT algorithm preference */330cinfo->dct_method = JDCT_DEFAULT;331332/* No restart markers */333cinfo->restart_interval = 0;334cinfo->restart_in_rows = 0;335336/* Fill in default JFIF marker parameters. Note that whether the marker337* will actually be written is determined by jpeg_set_colorspace.338*339* By default, the library emits JFIF version code 1.01.340* An application that wants to emit JFIF 1.02 extension markers should set341* JFIF_minor_version to 2. We could probably get away with just defaulting342* to 1.02, but there may still be some decoders in use that will complain343* about that; saying 1.01 should minimize compatibility problems.344*/345cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */346cinfo->JFIF_minor_version = 1;347cinfo->density_unit = 0; /* Pixel size is unknown by default */348cinfo->X_density = 1; /* Pixel aspect ratio is square by default */349cinfo->Y_density = 1;350351/* Choose JPEG colorspace based on input space, set defaults accordingly */352353jpeg_default_colorspace(cinfo);354}355356357/*358* Select an appropriate JPEG colorspace for in_color_space.359*/360361GLOBAL(void)362jpeg_default_colorspace (j_compress_ptr cinfo)363{364switch (cinfo->in_color_space) {365case JCS_GRAYSCALE:366jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);367break;368case JCS_RGB:369jpeg_set_colorspace(cinfo, JCS_YCbCr);370break;371case JCS_YCbCr:372jpeg_set_colorspace(cinfo, JCS_YCbCr);373break;374case JCS_CMYK:375jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */376break;377case JCS_YCCK:378jpeg_set_colorspace(cinfo, JCS_YCCK);379break;380case JCS_UNKNOWN:381jpeg_set_colorspace(cinfo, JCS_UNKNOWN);382break;383default:384ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);385}386}387388389/*390* Set the JPEG colorspace, and choose colorspace-dependent default values.391*/392393GLOBAL(void)394jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)395{396jpeg_component_info * compptr;397int ci;398399#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \400(compptr = &cinfo->comp_info[index], \401compptr->component_id = (id), \402compptr->h_samp_factor = (hsamp), \403compptr->v_samp_factor = (vsamp), \404compptr->quant_tbl_no = (quant), \405compptr->dc_tbl_no = (dctbl), \406compptr->ac_tbl_no = (actbl) )407408/* Safety check to ensure start_compress not called yet. */409if (cinfo->global_state != CSTATE_START)410ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);411412/* For all colorspaces, we use Q and Huff tables 0 for luminance components,413* tables 1 for chrominance components.414*/415416cinfo->jpeg_color_space = colorspace;417418cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */419cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */420421switch (colorspace) {422case JCS_GRAYSCALE:423cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */424cinfo->num_components = 1;425/* JFIF specifies component ID 1 */426SET_COMP(0, 1, 1,1, 0, 0,0);427break;428case JCS_RGB:429cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */430cinfo->num_components = 3;431SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);432SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);433SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);434break;435case JCS_YCbCr:436cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */437cinfo->num_components = 3;438/* JFIF specifies component IDs 1,2,3 */439/* We default to 2x2 subsamples of chrominance */440SET_COMP(0, 1, 2,2, 0, 0,0);441SET_COMP(1, 2, 1,1, 1, 1,1);442SET_COMP(2, 3, 1,1, 1, 1,1);443break;444case JCS_CMYK:445cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */446cinfo->num_components = 4;447SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);448SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);449SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);450SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);451break;452case JCS_YCCK:453cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */454cinfo->num_components = 4;455SET_COMP(0, 1, 2,2, 0, 0,0);456SET_COMP(1, 2, 1,1, 1, 1,1);457SET_COMP(2, 3, 1,1, 1, 1,1);458SET_COMP(3, 4, 2,2, 0, 0,0);459break;460case JCS_UNKNOWN:461cinfo->num_components = cinfo->input_components;462if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)463ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,464MAX_COMPONENTS);465for (ci = 0; ci < cinfo->num_components; ci++) {466SET_COMP(ci, ci, 1,1, 0, 0,0);467}468break;469default:470ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);471}472}473474475#ifdef C_PROGRESSIVE_SUPPORTED476477LOCAL(jpeg_scan_info *)478fill_a_scan (jpeg_scan_info * scanptr, int ci,479int Ss, int Se, int Ah, int Al)480/* Support routine: generate one scan for specified component */481{482scanptr->comps_in_scan = 1;483scanptr->component_index[0] = ci;484scanptr->Ss = Ss;485scanptr->Se = Se;486scanptr->Ah = Ah;487scanptr->Al = Al;488scanptr++;489return scanptr;490}491492LOCAL(jpeg_scan_info *)493fill_scans (jpeg_scan_info * scanptr, int ncomps,494int Ss, int Se, int Ah, int Al)495/* Support routine: generate one scan for each component */496{497int ci;498499for (ci = 0; ci < ncomps; ci++) {500scanptr->comps_in_scan = 1;501scanptr->component_index[0] = ci;502scanptr->Ss = Ss;503scanptr->Se = Se;504scanptr->Ah = Ah;505scanptr->Al = Al;506scanptr++;507}508return scanptr;509}510511LOCAL(jpeg_scan_info *)512fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)513/* Support routine: generate interleaved DC scan if possible, else N scans */514{515int ci;516517if (ncomps <= MAX_COMPS_IN_SCAN) {518/* Single interleaved DC scan */519scanptr->comps_in_scan = ncomps;520for (ci = 0; ci < ncomps; ci++)521scanptr->component_index[ci] = ci;522scanptr->Ss = scanptr->Se = 0;523scanptr->Ah = Ah;524scanptr->Al = Al;525scanptr++;526} else {527/* Noninterleaved DC scan for each component */528scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);529}530return scanptr;531}532533534/*535* Create a recommended progressive-JPEG script.536* cinfo->num_components and cinfo->jpeg_color_space must be correct.537*/538539GLOBAL(void)540jpeg_simple_progression (j_compress_ptr cinfo)541{542int ncomps = cinfo->num_components;543int nscans;544jpeg_scan_info * scanptr;545546/* Safety check to ensure start_compress not called yet. */547if (cinfo->global_state != CSTATE_START)548ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);549550/* Figure space needed for script. Calculation must match code below! */551if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {552/* Custom script for YCbCr color images. */553nscans = 10;554} else {555/* All-purpose script for other color spaces. */556if (ncomps > MAX_COMPS_IN_SCAN)557nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */558else559nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */560}561562/* Allocate space for script.563* We need to put it in the permanent pool in case the application performs564* multiple compressions without changing the settings. To avoid a memory565* leak if jpeg_simple_progression is called repeatedly for the same JPEG566* object, we try to re-use previously allocated space, and we allocate567* enough space to handle YCbCr even if initially asked for grayscale.568*/569if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {570cinfo->script_space_size = MAX(nscans, 10);571cinfo->script_space = (jpeg_scan_info *)572(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,573cinfo->script_space_size * SIZEOF(jpeg_scan_info));574}575scanptr = cinfo->script_space;576cinfo->scan_info = scanptr;577cinfo->num_scans = nscans;578579if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {580/* Custom script for YCbCr color images. */581/* Initial DC scan */582scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);583/* Initial AC scan: get some luma data out in a hurry */584scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);585/* Chroma data is too small to be worth expending many scans on */586scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);587scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);588/* Complete spectral selection for luma AC */589scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);590/* Refine next bit of luma AC */591scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);592/* Finish DC successive approximation */593scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);594/* Finish AC successive approximation */595scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);596scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);597/* Luma bottom bit comes last since it's usually largest scan */598scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);599} else {600/* All-purpose script for other color spaces. */601/* Successive approximation first pass */602scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);603scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);604scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);605/* Successive approximation second pass */606scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);607/* Successive approximation final pass */608scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);609scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);610}611}612613#endif /* C_PROGRESSIVE_SUPPORTED */614615616