Path: blob/master/thirdparty/libjpeg-turbo/src/jcparam.c
9904 views
/*1* jcparam.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1998, Thomas G. Lane.5* Modified 2003-2008 by Guido Vollbeding.6* Lossless JPEG Modifications:7* Copyright (C) 1999, Ken Murchison.8* libjpeg-turbo Modifications:9* Copyright (C) 2009-2011, 2018, 2023-2024, D. R. Commander.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains optional default-setting code for the JPEG compressor.14* Applications do not have to use this file, but those that don't use it15* must know a lot more about the innards of the JPEG code.16*/1718#define JPEG_INTERNALS19#include "jinclude.h"20#include "jpeglib.h"21#include "jstdhuff.c"222324/*25* Quantization table setup routines26*/2728GLOBAL(void)29jpeg_add_quant_table(j_compress_ptr cinfo, int which_tbl,30const unsigned int *basic_table, int scale_factor,31boolean force_baseline)32/* Define a quantization table equal to the basic_table times33* a scale factor (given as a percentage).34* If force_baseline is TRUE, the computed quantization table entries35* are limited to 1..255 for JPEG baseline compatibility.36*/37{38JQUANT_TBL **qtblptr;39int i;40long temp;4142/* Safety check to ensure start_compress not called yet. */43if (cinfo->global_state != CSTATE_START)44ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);4546if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)47ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);4849qtblptr = &cinfo->quant_tbl_ptrs[which_tbl];5051if (*qtblptr == NULL)52*qtblptr = jpeg_alloc_quant_table((j_common_ptr)cinfo);5354for (i = 0; i < DCTSIZE2; i++) {55temp = ((long)basic_table[i] * scale_factor + 50L) / 100L;56/* limit the values to the valid range */57if (temp <= 0L) temp = 1L;58if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */59if (force_baseline && temp > 255L)60temp = 255L; /* limit to baseline range if requested */61(*qtblptr)->quantval[i] = (UINT16)temp;62}6364/* Initialize sent_table FALSE so table will be written to JPEG file. */65(*qtblptr)->sent_table = FALSE;66}676869/* These are the sample quantization tables given in Annex K (Clause K.1) of70* Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.71* The spec says that the values given produce "good" quality, and72* when divided by 2, "very good" quality.73*/74static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {7516, 11, 10, 16, 24, 40, 51, 61,7612, 12, 14, 19, 26, 58, 60, 55,7714, 13, 16, 24, 40, 57, 69, 56,7814, 17, 22, 29, 51, 87, 80, 62,7918, 22, 37, 56, 68, 109, 103, 77,8024, 35, 55, 64, 81, 104, 113, 92,8149, 64, 78, 87, 103, 121, 120, 101,8272, 92, 95, 98, 112, 100, 103, 9983};84static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {8517, 18, 24, 47, 99, 99, 99, 99,8618, 21, 26, 66, 99, 99, 99, 99,8724, 26, 56, 99, 99, 99, 99, 99,8847, 66, 99, 99, 99, 99, 99, 99,8999, 99, 99, 99, 99, 99, 99, 99,9099, 99, 99, 99, 99, 99, 99, 99,9199, 99, 99, 99, 99, 99, 99, 99,9299, 99, 99, 99, 99, 99, 99, 9993};949596#if JPEG_LIB_VERSION >= 7097GLOBAL(void)98jpeg_default_qtables(j_compress_ptr cinfo, boolean force_baseline)99/* Set or change the 'quality' (quantization) setting, using default tables100* and straight percentage-scaling quality scales.101* This entry point allows different scalings for luminance and chrominance.102*/103{104/* Set up two quantization tables using the specified scaling */105jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,106cinfo->q_scale_factor[0], force_baseline);107jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,108cinfo->q_scale_factor[1], force_baseline);109}110#endif111112113GLOBAL(void)114jpeg_set_linear_quality(j_compress_ptr cinfo, int scale_factor,115boolean force_baseline)116/* Set or change the 'quality' (quantization) setting, using default tables117* and a straight percentage-scaling quality scale. In most cases it's better118* to use jpeg_set_quality (below); this entry point is provided for119* applications that insist on a linear percentage scaling.120*/121{122/* Set up two quantization tables using the specified scaling */123jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,124scale_factor, force_baseline);125jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,126scale_factor, force_baseline);127}128129130GLOBAL(int)131jpeg_quality_scaling(int quality)132/* Convert a user-specified quality rating to a percentage scaling factor133* for an underlying quantization table, using our recommended scaling curve.134* The input 'quality' factor should be 0 (terrible) to 100 (very good).135*/136{137/* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */138if (quality <= 0) quality = 1;139if (quality > 100) quality = 100;140141/* The basic table is used as-is (scaling 100) for a quality of 50.142* Qualities 50..100 are converted to scaling percentage 200 - 2*Q;143* note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table144* to make all the table entries 1 (hence, minimum quantization loss).145* Qualities 1..50 are converted to scaling percentage 5000/Q.146*/147if (quality < 50)148quality = 5000 / quality;149else150quality = 200 - quality * 2;151152return quality;153}154155156GLOBAL(void)157jpeg_set_quality(j_compress_ptr cinfo, int quality, boolean force_baseline)158/* Set or change the 'quality' (quantization) setting, using default tables.159* This is the standard quality-adjusting entry point for typical user160* interfaces; only those who want detailed control over quantization tables161* would use the preceding three routines directly.162*/163{164/* Convert user 0-100 rating to percentage scaling */165quality = jpeg_quality_scaling(quality);166167/* Set up standard quality tables */168jpeg_set_linear_quality(cinfo, quality, force_baseline);169}170171172/*173* Default parameter setup for compression.174*175* Applications that don't choose to use this routine must do their176* own setup of all these parameters. Alternately, you can call this177* to establish defaults and then alter parameters selectively. This178* is the recommended approach since, if we add any new parameters,179* your code will still work (they'll be set to reasonable defaults).180*/181182GLOBAL(void)183jpeg_set_defaults(j_compress_ptr cinfo)184{185int i;186187/* Safety check to ensure start_compress not called yet. */188if (cinfo->global_state != CSTATE_START)189ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);190191/* Allocate comp_info array large enough for maximum component count.192* Array is made permanent in case application wants to compress193* multiple images at same param settings.194*/195if (cinfo->comp_info == NULL)196cinfo->comp_info = (jpeg_component_info *)197(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,198MAX_COMPONENTS * sizeof(jpeg_component_info));199200/* Initialize everything not dependent on the color space */201202#if JPEG_LIB_VERSION >= 70203cinfo->scale_num = 1; /* 1:1 scaling */204cinfo->scale_denom = 1;205#endif206/* Set up two quantization tables using default quality of 75 */207jpeg_set_quality(cinfo, 75, TRUE);208/* Set up two Huffman tables */209std_huff_tables((j_common_ptr)cinfo);210211/* Initialize default arithmetic coding conditioning */212for (i = 0; i < NUM_ARITH_TBLS; i++) {213cinfo->arith_dc_L[i] = 0;214cinfo->arith_dc_U[i] = 1;215cinfo->arith_ac_K[i] = 5;216}217218/* Default is no multiple-scan output */219cinfo->scan_info = NULL;220cinfo->num_scans = 0;221222/* Default is lossy output */223cinfo->master->lossless = FALSE;224225/* Expect normal source image, not raw downsampled data */226cinfo->raw_data_in = FALSE;227228/* Use Huffman coding, not arithmetic coding, by default */229cinfo->arith_code = FALSE;230231/* By default, don't do extra passes to optimize entropy coding */232cinfo->optimize_coding = FALSE;233/* The standard Huffman tables are only valid for 8-bit data precision.234* If the precision is higher, force optimization on so that usable235* tables will be computed. This test can be removed if default tables236* are supplied that are valid for the desired precision.237*/238if (cinfo->data_precision == 12)239cinfo->optimize_coding = TRUE;240241/* By default, use the simpler non-cosited sampling alignment */242cinfo->CCIR601_sampling = FALSE;243244#if JPEG_LIB_VERSION >= 70245/* By default, apply fancy downsampling */246cinfo->do_fancy_downsampling = TRUE;247#endif248249/* No input smoothing */250cinfo->smoothing_factor = 0;251252/* DCT algorithm preference */253cinfo->dct_method = JDCT_DEFAULT;254255/* No restart markers */256cinfo->restart_interval = 0;257cinfo->restart_in_rows = 0;258259/* Fill in default JFIF marker parameters. Note that whether the marker260* will actually be written is determined by jpeg_set_colorspace.261*262* By default, the library emits JFIF version code 1.01.263* An application that wants to emit JFIF 1.02 extension markers should set264* JFIF_minor_version to 2. We could probably get away with just defaulting265* to 1.02, but there may still be some decoders in use that will complain266* about that; saying 1.01 should minimize compatibility problems.267*/268cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */269cinfo->JFIF_minor_version = 1;270cinfo->density_unit = 0; /* Pixel size is unknown by default */271cinfo->X_density = 1; /* Pixel aspect ratio is square by default */272cinfo->Y_density = 1;273274/* Choose JPEG colorspace based on input space, set defaults accordingly */275276jpeg_default_colorspace(cinfo);277}278279280/*281* Select an appropriate JPEG colorspace for in_color_space.282*/283284GLOBAL(void)285jpeg_default_colorspace(j_compress_ptr cinfo)286{287switch (cinfo->in_color_space) {288case JCS_GRAYSCALE:289jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);290break;291case JCS_RGB:292case JCS_EXT_RGB:293case JCS_EXT_RGBX:294case JCS_EXT_BGR:295case JCS_EXT_BGRX:296case JCS_EXT_XBGR:297case JCS_EXT_XRGB:298case JCS_EXT_RGBA:299case JCS_EXT_BGRA:300case JCS_EXT_ABGR:301case JCS_EXT_ARGB:302#ifdef C_LOSSLESS_SUPPORTED303if (cinfo->master->lossless)304jpeg_set_colorspace(cinfo, JCS_RGB);305else306#endif307jpeg_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_UNKNOWN:319jpeg_set_colorspace(cinfo, JCS_UNKNOWN);320break;321default:322ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);323}324}325326327/*328* Set the JPEG colorspace, and choose colorspace-dependent default values.329*/330331GLOBAL(void)332jpeg_set_colorspace(j_compress_ptr cinfo, J_COLOR_SPACE colorspace)333{334jpeg_component_info *compptr;335int ci;336337#define SET_COMP(index, id, hsamp, vsamp, quant, dctbl, actbl) \338(compptr = &cinfo->comp_info[index], \339compptr->component_id = (id), \340compptr->h_samp_factor = (hsamp), \341compptr->v_samp_factor = (vsamp), \342compptr->quant_tbl_no = (quant), \343compptr->dc_tbl_no = (dctbl), \344compptr->ac_tbl_no = (actbl) )345346/* Safety check to ensure start_compress not called yet. */347if (cinfo->global_state != CSTATE_START)348ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);349350/* For all colorspaces, we use Q and Huff tables 0 for luminance components,351* tables 1 for chrominance components.352*/353354cinfo->jpeg_color_space = colorspace;355356cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */357cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */358359switch (colorspace) {360case JCS_GRAYSCALE:361cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */362cinfo->num_components = 1;363/* JFIF specifies component ID 1 */364SET_COMP(0, 1, 1, 1, 0, 0, 0);365break;366case JCS_RGB:367cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */368cinfo->num_components = 3;369SET_COMP(0, 0x52 /* 'R' */, 1, 1, 0, 0, 0);370SET_COMP(1, 0x47 /* 'G' */, 1, 1, 0, 0, 0);371SET_COMP(2, 0x42 /* 'B' */, 1, 1, 0, 0, 0);372break;373case JCS_YCbCr:374cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */375cinfo->num_components = 3;376/* JFIF specifies component IDs 1,2,3 */377/* We default to 2x2 subsamples of chrominance */378SET_COMP(0, 1, 2, 2, 0, 0, 0);379SET_COMP(1, 2, 1, 1, 1, 1, 1);380SET_COMP(2, 3, 1, 1, 1, 1, 1);381break;382case JCS_CMYK:383cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */384cinfo->num_components = 4;385SET_COMP(0, 0x43 /* 'C' */, 1, 1, 0, 0, 0);386SET_COMP(1, 0x4D /* 'M' */, 1, 1, 0, 0, 0);387SET_COMP(2, 0x59 /* 'Y' */, 1, 1, 0, 0, 0);388SET_COMP(3, 0x4B /* 'K' */, 1, 1, 0, 0, 0);389break;390case JCS_YCCK:391cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */392cinfo->num_components = 4;393SET_COMP(0, 1, 2, 2, 0, 0, 0);394SET_COMP(1, 2, 1, 1, 1, 1, 1);395SET_COMP(2, 3, 1, 1, 1, 1, 1);396SET_COMP(3, 4, 2, 2, 0, 0, 0);397break;398case JCS_UNKNOWN:399cinfo->num_components = cinfo->input_components;400if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)401ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,402MAX_COMPONENTS);403for (ci = 0; ci < cinfo->num_components; ci++) {404SET_COMP(ci, ci, 1, 1, 0, 0, 0);405}406break;407default:408ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);409}410}411412413#ifdef C_PROGRESSIVE_SUPPORTED414415LOCAL(jpeg_scan_info *)416fill_a_scan(jpeg_scan_info *scanptr, int ci, int Ss, int Se, int Ah, int Al)417/* Support routine: generate one scan for specified component */418{419scanptr->comps_in_scan = 1;420scanptr->component_index[0] = ci;421scanptr->Ss = Ss;422scanptr->Se = Se;423scanptr->Ah = Ah;424scanptr->Al = Al;425scanptr++;426return scanptr;427}428429LOCAL(jpeg_scan_info *)430fill_scans(jpeg_scan_info *scanptr, int ncomps, int Ss, int Se, int Ah, int Al)431/* Support routine: generate one scan for each component */432{433int ci;434435for (ci = 0; ci < ncomps; ci++) {436scanptr->comps_in_scan = 1;437scanptr->component_index[0] = ci;438scanptr->Ss = Ss;439scanptr->Se = Se;440scanptr->Ah = Ah;441scanptr->Al = Al;442scanptr++;443}444return scanptr;445}446447LOCAL(jpeg_scan_info *)448fill_dc_scans(jpeg_scan_info *scanptr, int ncomps, int Ah, int Al)449/* Support routine: generate interleaved DC scan if possible, else N scans */450{451int ci;452453if (ncomps <= MAX_COMPS_IN_SCAN) {454/* Single interleaved DC scan */455scanptr->comps_in_scan = ncomps;456for (ci = 0; ci < ncomps; ci++)457scanptr->component_index[ci] = ci;458scanptr->Ss = scanptr->Se = 0;459scanptr->Ah = Ah;460scanptr->Al = Al;461scanptr++;462} else {463/* Noninterleaved DC scan for each component */464scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);465}466return scanptr;467}468469470/*471* Create a recommended progressive-JPEG script.472* cinfo->num_components and cinfo->jpeg_color_space must be correct.473*/474475GLOBAL(void)476jpeg_simple_progression(j_compress_ptr cinfo)477{478int ncomps = cinfo->num_components;479int nscans;480jpeg_scan_info *scanptr;481482/* Safety check to ensure start_compress not called yet. */483if (cinfo->global_state != CSTATE_START)484ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);485486#ifdef C_LOSSLESS_SUPPORTED487if (cinfo->master->lossless) {488cinfo->master->lossless = FALSE;489jpeg_default_colorspace(cinfo);490}491#endif492493/* Figure space needed for script. Calculation must match code below! */494if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {495/* Custom script for YCbCr color images. */496nscans = 10;497} else {498/* All-purpose script for other color spaces. */499if (ncomps > MAX_COMPS_IN_SCAN)500nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */501else502nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */503}504505/* Allocate space for script.506* We need to put it in the permanent pool in case the application performs507* multiple compressions without changing the settings. To avoid a memory508* leak if jpeg_simple_progression is called repeatedly for the same JPEG509* object, we try to re-use previously allocated space, and we allocate510* enough space to handle YCbCr even if initially asked for grayscale.511*/512if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {513cinfo->script_space_size = MAX(nscans, 10);514cinfo->script_space = (jpeg_scan_info *)515(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,516cinfo->script_space_size * sizeof(jpeg_scan_info));517}518scanptr = cinfo->script_space;519cinfo->scan_info = scanptr;520cinfo->num_scans = nscans;521522if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {523/* Custom script for YCbCr color images. */524/* Initial DC scan */525scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);526/* Initial AC scan: get some luma data out in a hurry */527scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);528/* Chroma data is too small to be worth expending many scans on */529scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);530scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);531/* Complete spectral selection for luma AC */532scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);533/* Refine next bit of luma AC */534scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);535/* Finish DC successive approximation */536scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);537/* Finish AC successive approximation */538scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);539scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);540/* Luma bottom bit comes last since it's usually largest scan */541scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);542} else {543/* All-purpose script for other color spaces. */544/* Successive approximation first pass */545scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);546scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);547scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);548/* Successive approximation second pass */549scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);550/* Successive approximation final pass */551scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);552scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);553}554}555556#endif /* C_PROGRESSIVE_SUPPORTED */557558559#ifdef C_LOSSLESS_SUPPORTED560561/*562* Enable lossless mode.563*/564565GLOBAL(void)566jpeg_enable_lossless(j_compress_ptr cinfo, int predictor_selection_value,567int point_transform)568{569/* Safety check to ensure start_compress not called yet. */570if (cinfo->global_state != CSTATE_START)571ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);572573cinfo->master->lossless = TRUE;574cinfo->Ss = predictor_selection_value;575cinfo->Se = 0;576cinfo->Ah = 0;577cinfo->Al = point_transform;578579/* The JPEG spec simply gives the range 0..15 for Al (Pt), but that seems580* wrong: the upper bound ought to depend on data precision. Perhaps they581* really meant 0..N-1 for N-bit precision, which is what we allow here.582* Values greater than or equal to the data precision will result in a blank583* image.584*/585if (cinfo->Ss < 1 || cinfo->Ss > 7 ||586cinfo->Al < 0 || cinfo->Al >= cinfo->data_precision)587ERREXIT4(cinfo, JERR_BAD_PROGRESSION,588cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);589}590591#endif /* C_LOSSLESS_SUPPORTED */592593594