/*1* jcparam.c2*3* Copyright (C) 1991-1998, Thomas G. Lane.4* Modified 2003-2022 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains optional default-setting code for the JPEG compressor.9* Applications do not have to use this file, but those that don't use it10* must know a lot more about the innards of the JPEG code.11*/1213#define JPEG_INTERNALS14#include "jinclude.h"15#include "jpeglib.h"161718/*19* Quantization table setup routines20*/2122GLOBAL(void)23jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,24const unsigned int *basic_table,25int scale_factor, boolean force_baseline)26/* Define a quantization table equal to the basic_table times27* a scale factor (given as a percentage).28* If force_baseline is TRUE, the computed quantization table entries29* are limited to 1..255 for JPEG baseline compatibility.30*/31{32JQUANT_TBL ** qtblptr;33int i;34long temp;3536/* Safety check to ensure start_compress not called yet. */37if (cinfo->global_state != CSTATE_START)38ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);3940if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)41ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);4243qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];4445if (*qtblptr == NULL)46*qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);4748for (i = 0; i < DCTSIZE2; i++) {49temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;50/* limit the values to the valid range */51if (temp <= 0L) temp = 1L;52if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */53if (force_baseline && temp > 255L)54temp = 255L; /* limit to baseline range if requested */55(*qtblptr)->quantval[i] = (UINT16) temp;56}5758/* Initialize sent_table FALSE so table will be written to JPEG file. */59(*qtblptr)->sent_table = FALSE;60}616263/* These are the sample quantization tables given in JPEG spec section K.1.64* NOTE: chrominance DC value is changed from 17 to 16 for lossless support.65* The spec says that the values given produce "good" quality,66* and when divided by 2, "very good" quality.67*/68static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {6916, 11, 10, 16, 24, 40, 51, 61,7012, 12, 14, 19, 26, 58, 60, 55,7114, 13, 16, 24, 40, 57, 69, 56,7214, 17, 22, 29, 51, 87, 80, 62,7318, 22, 37, 56, 68, 109, 103, 77,7424, 35, 55, 64, 81, 104, 113, 92,7549, 64, 78, 87, 103, 121, 120, 101,7672, 92, 95, 98, 112, 100, 103, 9977};78static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {7916, 18, 24, 47, 99, 99, 99, 99,8018, 21, 26, 66, 99, 99, 99, 99,8124, 26, 56, 99, 99, 99, 99, 99,8247, 66, 99, 99, 99, 99, 99, 99,8399, 99, 99, 99, 99, 99, 99, 99,8499, 99, 99, 99, 99, 99, 99, 99,8599, 99, 99, 99, 99, 99, 99, 99,8699, 99, 99, 99, 99, 99, 99, 9987};888990GLOBAL(void)91jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)92/* Set or change the 'quality' (quantization) setting, using default tables93* and straight percentage-scaling quality scales.94* This entry point allows different scalings for luminance and chrominance.95*/96{97/* Set up two quantization tables using the specified scaling */98jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,99cinfo->q_scale_factor[0], force_baseline);100jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,101cinfo->q_scale_factor[1], force_baseline);102}103104105GLOBAL(void)106jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,107boolean force_baseline)108/* Set or change the 'quality' (quantization) setting, using default tables109* and a straight percentage-scaling quality scale. In most cases it's better110* to use jpeg_set_quality (below); this entry point is provided for111* applications that insist on a linear percentage scaling.112*/113{114/* Set up two quantization tables using the specified scaling */115jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,116scale_factor, force_baseline);117jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,118scale_factor, force_baseline);119}120121122GLOBAL(int)123jpeg_quality_scaling (int quality)124/* Convert a user-specified quality rating to a percentage scaling factor125* for an underlying quantization table, using our recommended scaling curve.126* The input 'quality' factor should be 0 (terrible) to 100 (very good).127*/128{129/* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */130if (quality <= 0) quality = 1;131if (quality > 100) quality = 100;132133/* The basic table is used as-is (scaling 100) for a quality of 50.134* Qualities 50..100 are converted to scaling percentage 200 - 2*Q;135* note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table136* to make all the table entries 1 (hence, minimum quantization loss).137* Qualities 1..50 are converted to scaling percentage 5000/Q.138*/139if (quality < 50)140quality = 5000 / quality;141else142quality = 200 - quality*2;143144return quality;145}146147148GLOBAL(void)149jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)150/* Set or change the 'quality' (quantization) setting, using default tables.151* This is the standard quality-adjusting entry point for typical user152* interfaces; only those who want detailed control over quantization tables153* would use the preceding routines directly.154*/155{156/* Convert user 0-100 rating to percentage scaling */157quality = jpeg_quality_scaling(quality);158159/* Set up standard quality tables */160jpeg_set_linear_quality(cinfo, quality, force_baseline);161}162163164/*165* Reset standard Huffman tables166*/167168LOCAL(void)169std_huff_tables (j_compress_ptr cinfo)170{171if (cinfo->dc_huff_tbl_ptrs[0] != NULL)172(void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 0);173174if (cinfo->ac_huff_tbl_ptrs[0] != NULL)175(void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 0);176177if (cinfo->dc_huff_tbl_ptrs[1] != NULL)178(void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 1);179180if (cinfo->ac_huff_tbl_ptrs[1] != NULL)181(void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 1);182}183184185/*186* Default parameter setup for compression.187*188* Applications that don't choose to use this routine must do their189* own setup of all these parameters. Alternately, you can call this190* to establish defaults and then alter parameters selectively. This191* is the recommended approach since, if we add any new parameters,192* your code will still work (they'll be set to reasonable defaults).193*/194195GLOBAL(void)196jpeg_set_defaults (j_compress_ptr cinfo)197{198int i;199200/* Safety check to ensure start_compress not called yet. */201if (cinfo->global_state != CSTATE_START)202ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);203204/* Allocate comp_info array large enough for maximum component count.205* Array is made permanent in case application wants to compress206* multiple images at same param settings.207*/208if (cinfo->comp_info == NULL)209cinfo->comp_info = (jpeg_component_info *)210(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,211MAX_COMPONENTS * SIZEOF(jpeg_component_info));212213/* Initialize everything not dependent on the color space */214215cinfo->scale_num = 1; /* 1:1 scaling */216cinfo->scale_denom = 1;217cinfo->data_precision = BITS_IN_JSAMPLE;218/* Set up two quantization tables using default quality of 75 */219jpeg_set_quality(cinfo, 75, TRUE);220/* Reset standard Huffman tables */221std_huff_tables(cinfo);222223/* Initialize default arithmetic coding conditioning */224for (i = 0; i < NUM_ARITH_TBLS; i++) {225cinfo->arith_dc_L[i] = 0;226cinfo->arith_dc_U[i] = 1;227cinfo->arith_ac_K[i] = 5;228}229230/* Default is no multiple-scan output */231cinfo->scan_info = NULL;232cinfo->num_scans = 0;233234/* Expect normal source image, not raw downsampled data */235cinfo->raw_data_in = FALSE;236237/* The standard Huffman tables are only valid for 8-bit data precision.238* If the precision is higher, use arithmetic coding.239* (Alternatively, using Huffman coding would be possible with forcing240* optimization on so that usable tables will be computed, or by241* supplying default tables that are valid for the desired precision.)242* Otherwise, use Huffman coding by default.243*/244cinfo->arith_code = cinfo->data_precision > 8 ? TRUE : FALSE;245246/* By default, don't do extra passes to optimize entropy coding */247cinfo->optimize_coding = FALSE;248249/* By default, use the simpler non-cosited sampling alignment */250cinfo->CCIR601_sampling = FALSE;251252/* By default, apply fancy downsampling */253cinfo->do_fancy_downsampling = TRUE;254255/* No input smoothing */256cinfo->smoothing_factor = 0;257258/* DCT algorithm preference */259cinfo->dct_method = JDCT_DEFAULT;260261/* No restart markers */262cinfo->restart_interval = 0;263cinfo->restart_in_rows = 0;264265/* Fill in default JFIF marker parameters. Note that whether the marker266* will actually be written is determined by jpeg_set_colorspace.267*268* By default, the library emits JFIF version code 1.01.269* An application that wants to emit JFIF 1.02 extension markers should set270* JFIF_minor_version to 2. We could probably get away with just defaulting271* to 1.02, but there may still be some decoders in use that will complain272* about that; saying 1.01 should minimize compatibility problems.273*274* For wide gamut colorspaces (BG_RGB and BG_YCC), the major version will be275* overridden by jpeg_set_colorspace and set to 2.276*/277cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */278cinfo->JFIF_minor_version = 1;279cinfo->density_unit = 0; /* Pixel size is unknown by default */280cinfo->X_density = 1; /* Pixel aspect ratio is square by default */281cinfo->Y_density = 1;282283/* No color transform */284cinfo->color_transform = JCT_NONE;285286/* Choose JPEG colorspace based on input space, set defaults accordingly */287288jpeg_default_colorspace(cinfo);289}290291292/*293* Select an appropriate JPEG colorspace for in_color_space.294*/295296GLOBAL(void)297jpeg_default_colorspace (j_compress_ptr cinfo)298{299switch (cinfo->in_color_space) {300case JCS_UNKNOWN:301jpeg_set_colorspace(cinfo, JCS_UNKNOWN);302break;303case JCS_GRAYSCALE:304jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);305break;306case JCS_RGB:307jpeg_set_colorspace(cinfo, JCS_YCbCr);308break;309case JCS_YCbCr:310jpeg_set_colorspace(cinfo, JCS_YCbCr);311break;312case JCS_CMYK:313jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */314break;315case JCS_YCCK:316jpeg_set_colorspace(cinfo, JCS_YCCK);317break;318case JCS_BG_RGB:319/* No translation for now -- conversion to BG_YCC not yet supportet */320jpeg_set_colorspace(cinfo, JCS_BG_RGB);321break;322case JCS_BG_YCC:323jpeg_set_colorspace(cinfo, JCS_BG_YCC);324break;325default:326ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);327}328}329330331/*332* Set the JPEG colorspace, and choose colorspace-dependent default values.333*/334335GLOBAL(void)336jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)337{338jpeg_component_info * compptr;339int ci;340341#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \342(compptr = &cinfo->comp_info[index], \343compptr->component_id = (id), \344compptr->h_samp_factor = (hsamp), \345compptr->v_samp_factor = (vsamp), \346compptr->quant_tbl_no = (quant), \347compptr->dc_tbl_no = (dctbl), \348compptr->ac_tbl_no = (actbl) )349350/* Safety check to ensure start_compress not called yet. */351if (cinfo->global_state != CSTATE_START)352ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);353354/* For all colorspaces, we use Q and Huff tables 0 for luminance components,355* tables 1 for chrominance components.356*/357358cinfo->jpeg_color_space = colorspace;359360cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */361cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */362363switch (colorspace) {364case JCS_UNKNOWN:365cinfo->num_components = cinfo->input_components;366if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)367ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,368MAX_COMPONENTS);369for (ci = 0; ci < cinfo->num_components; ci++) {370SET_COMP(ci, ci, 1,1, 0, 0,0);371}372break;373case JCS_GRAYSCALE:374cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */375cinfo->num_components = 1;376/* JFIF specifies component ID 1 */377SET_COMP(0, 0x01, 1,1, 0, 0,0);378break;379case JCS_RGB:380cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */381cinfo->num_components = 3;382SET_COMP(0, 0x52 /* 'R' */, 1,1,383cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,384cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,385cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);386SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);387SET_COMP(2, 0x42 /* 'B' */, 1,1,388cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,389cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,390cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);391break;392case JCS_YCbCr:393cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */394cinfo->num_components = 3;395/* JFIF specifies component IDs 1,2,3 */396/* We default to 2x2 subsamples of chrominance */397SET_COMP(0, 0x01, 2,2, 0, 0,0);398SET_COMP(1, 0x02, 1,1, 1, 1,1);399SET_COMP(2, 0x03, 1,1, 1, 1,1);400break;401case JCS_CMYK:402cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */403cinfo->num_components = 4;404SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);405SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);406SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);407SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);408break;409case JCS_YCCK:410cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */411cinfo->num_components = 4;412SET_COMP(0, 0x01, 2,2, 0, 0,0);413SET_COMP(1, 0x02, 1,1, 1, 1,1);414SET_COMP(2, 0x03, 1,1, 1, 1,1);415SET_COMP(3, 0x04, 2,2, 0, 0,0);416break;417case JCS_BG_RGB:418cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */419cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */420cinfo->num_components = 3;421/* Add offset 0x20 to the normal R/G/B component IDs */422SET_COMP(0, 0x72 /* 'r' */, 1,1,423cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,424cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,425cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);426SET_COMP(1, 0x67 /* 'g' */, 1,1, 0, 0,0);427SET_COMP(2, 0x62 /* 'b' */, 1,1,428cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,429cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,430cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);431break;432case JCS_BG_YCC:433cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */434cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */435cinfo->num_components = 3;436/* Add offset 0x20 to the normal Cb/Cr component IDs */437/* We default to 2x2 subsamples of chrominance */438SET_COMP(0, 0x01, 2,2, 0, 0,0);439SET_COMP(1, 0x22, 1,1, 1, 1,1);440SET_COMP(2, 0x23, 1,1, 1, 1,1);441break;442default:443ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);444}445}446447448#ifdef C_PROGRESSIVE_SUPPORTED449450LOCAL(jpeg_scan_info *)451fill_a_scan (jpeg_scan_info * scanptr, int ci,452int Ss, int Se, int Ah, int Al)453/* Support routine: generate one scan for specified component */454{455scanptr->comps_in_scan = 1;456scanptr->component_index[0] = ci;457scanptr->Ss = Ss;458scanptr->Se = Se;459scanptr->Ah = Ah;460scanptr->Al = Al;461scanptr++;462return scanptr;463}464465LOCAL(jpeg_scan_info *)466fill_scans (jpeg_scan_info * scanptr, int ncomps,467int Ss, int Se, int Ah, int Al)468/* Support routine: generate one scan for each component */469{470int ci;471472for (ci = 0; ci < ncomps; ci++) {473scanptr->comps_in_scan = 1;474scanptr->component_index[0] = ci;475scanptr->Ss = Ss;476scanptr->Se = Se;477scanptr->Ah = Ah;478scanptr->Al = Al;479scanptr++;480}481return scanptr;482}483484LOCAL(jpeg_scan_info *)485fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)486/* Support routine: generate interleaved DC scan if possible, else N scans */487{488int ci;489490if (ncomps <= MAX_COMPS_IN_SCAN) {491/* Single interleaved DC scan */492scanptr->comps_in_scan = ncomps;493for (ci = 0; ci < ncomps; ci++)494scanptr->component_index[ci] = ci;495scanptr->Ss = scanptr->Se = 0;496scanptr->Ah = Ah;497scanptr->Al = Al;498scanptr++;499} else {500/* Noninterleaved DC scan for each component */501scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);502}503return scanptr;504}505506507/*508* Create a recommended progressive-JPEG script.509* cinfo->num_components and cinfo->jpeg_color_space must be correct.510*/511512GLOBAL(void)513jpeg_simple_progression (j_compress_ptr cinfo)514{515int ncomps = cinfo->num_components;516int nscans;517jpeg_scan_info * scanptr;518519/* Safety check to ensure start_compress not called yet. */520if (cinfo->global_state != CSTATE_START)521ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);522523/* Figure space needed for script. Calculation must match code below! */524if (ncomps == 3 &&525(cinfo->jpeg_color_space == JCS_YCbCr ||526cinfo->jpeg_color_space == JCS_BG_YCC)) {527/* Custom script for YCC color images. */528nscans = 10;529} else {530/* All-purpose script for other color spaces. */531if (ncomps > MAX_COMPS_IN_SCAN)532nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */533else534nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */535}536537/* Allocate space for script.538* We need to put it in the permanent pool in case the application performs539* multiple compressions without changing the settings. To avoid a memory540* leak if jpeg_simple_progression is called repeatedly for the same JPEG541* object, we try to re-use previously allocated space, and we allocate542* enough space to handle YCC even if initially asked for grayscale.543*/544if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {545cinfo->script_space_size = MAX(nscans, 10);546cinfo->script_space = (jpeg_scan_info *)547(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,548cinfo->script_space_size * SIZEOF(jpeg_scan_info));549}550scanptr = cinfo->script_space;551cinfo->scan_info = scanptr;552cinfo->num_scans = nscans;553554if (ncomps == 3 &&555(cinfo->jpeg_color_space == JCS_YCbCr ||556cinfo->jpeg_color_space == JCS_BG_YCC)) {557/* Custom script for YCC color images. */558/* Initial DC scan */559scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);560/* Initial AC scan: get some luma data out in a hurry */561scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);562/* Chroma data is too small to be worth expending many scans on */563scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);564scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);565/* Complete spectral selection for luma AC */566scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);567/* Refine next bit of luma AC */568scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);569/* Finish DC successive approximation */570scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);571/* Finish AC successive approximation */572scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);573scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);574/* Luma bottom bit comes last since it's usually largest scan */575scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);576} else {577/* All-purpose script for other color spaces. */578/* Successive approximation first pass */579scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);580scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);581scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);582/* Successive approximation second pass */583scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);584/* Successive approximation final pass */585scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);586scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);587}588}589590#endif /* C_PROGRESSIVE_SUPPORTED */591592593