| 1 | /*
|
|---|
| 2 | * jcparam.c
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) 1991-1998, Thomas G. Lane.
|
|---|
| 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 it
|
|---|
| 10 | * must know a lot more about the innards of the JPEG code.
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #define JPEG_INTERNALS
|
|---|
| 14 | #include "jinclude.h"
|
|---|
| 15 | #include "jpeglib.h"
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | /*
|
|---|
| 19 | * Quantization table setup routines
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | GLOBAL(void)
|
|---|
| 23 | jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
|
|---|
| 24 | const unsigned int *basic_table,
|
|---|
| 25 | int scale_factor, boolean force_baseline)
|
|---|
| 26 | /* Define a quantization table equal to the basic_table times
|
|---|
| 27 | * a scale factor (given as a percentage).
|
|---|
| 28 | * If force_baseline is TRUE, the computed quantization table entries
|
|---|
| 29 | * are limited to 1..255 for JPEG baseline compatibility.
|
|---|
| 30 | */
|
|---|
| 31 | {
|
|---|
| 32 | JQUANT_TBL ** qtblptr;
|
|---|
| 33 | int i;
|
|---|
| 34 | long temp;
|
|---|
| 35 |
|
|---|
| 36 | /* Safety check to ensure start_compress not called yet. */
|
|---|
| 37 | if (cinfo->global_state != CSTATE_START)
|
|---|
| 38 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|---|
| 39 |
|
|---|
| 40 | if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
|
|---|
| 41 | ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
|
|---|
| 42 |
|
|---|
| 43 | qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
|
|---|
| 44 |
|
|---|
| 45 | if (*qtblptr == NULL)
|
|---|
| 46 | *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
|
|---|
| 47 |
|
|---|
| 48 | for (i = 0; i < DCTSIZE2; i++) {
|
|---|
| 49 | temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
|
|---|
| 50 | /* limit the values to the valid range */
|
|---|
| 51 | if (temp <= 0L) temp = 1L;
|
|---|
| 52 | if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
|
|---|
| 53 | if (force_baseline && temp > 255L)
|
|---|
| 54 | temp = 255L; /* limit to baseline range if requested */
|
|---|
| 55 | (*qtblptr)->quantval[i] = (UINT16) temp;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /* Initialize sent_table FALSE so table will be written to JPEG file. */
|
|---|
| 59 | (*qtblptr)->sent_table = FALSE;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | GLOBAL(void)
|
|---|
| 64 | jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
|
|---|
| 65 | boolean force_baseline)
|
|---|
| 66 | /* Set or change the 'quality' (quantization) setting, using default tables
|
|---|
| 67 | * and a straight percentage-scaling quality scale. In most cases it's better
|
|---|
| 68 | * to use jpeg_set_quality (below); this entry point is provided for
|
|---|
| 69 | * applications that insist on a linear percentage scaling.
|
|---|
| 70 | */
|
|---|
| 71 | {
|
|---|
| 72 | /* These are the sample quantization tables given in JPEG spec section K.1.
|
|---|
| 73 | * The spec says that the values given produce "good" quality, and
|
|---|
| 74 | * when divided by 2, "very good" quality.
|
|---|
| 75 | */
|
|---|
| 76 | static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
|
|---|
| 77 | 16, 11, 10, 16, 24, 40, 51, 61,
|
|---|
| 78 | 12, 12, 14, 19, 26, 58, 60, 55,
|
|---|
| 79 | 14, 13, 16, 24, 40, 57, 69, 56,
|
|---|
| 80 | 14, 17, 22, 29, 51, 87, 80, 62,
|
|---|
| 81 | 18, 22, 37, 56, 68, 109, 103, 77,
|
|---|
| 82 | 24, 35, 55, 64, 81, 104, 113, 92,
|
|---|
| 83 | 49, 64, 78, 87, 103, 121, 120, 101,
|
|---|
| 84 | 72, 92, 95, 98, 112, 100, 103, 99
|
|---|
| 85 | };
|
|---|
| 86 | static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
|
|---|
| 87 | 17, 18, 24, 47, 99, 99, 99, 99,
|
|---|
| 88 | 18, 21, 26, 66, 99, 99, 99, 99,
|
|---|
| 89 | 24, 26, 56, 99, 99, 99, 99, 99,
|
|---|
| 90 | 47, 66, 99, 99, 99, 99, 99, 99,
|
|---|
| 91 | 99, 99, 99, 99, 99, 99, 99, 99,
|
|---|
| 92 | 99, 99, 99, 99, 99, 99, 99, 99,
|
|---|
| 93 | 99, 99, 99, 99, 99, 99, 99, 99,
|
|---|
| 94 | 99, 99, 99, 99, 99, 99, 99, 99
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 | /* Set up two quantization tables using the specified scaling */
|
|---|
| 98 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
|
|---|
| 99 | scale_factor, force_baseline);
|
|---|
| 100 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
|
|---|
| 101 | scale_factor, force_baseline);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | GLOBAL(int)
|
|---|
| 106 | jpeg_quality_scaling (int quality)
|
|---|
| 107 | /* Convert a user-specified quality rating to a percentage scaling factor
|
|---|
| 108 | * for an underlying quantization table, using our recommended scaling curve.
|
|---|
| 109 | * The input 'quality' factor should be 0 (terrible) to 100 (very good).
|
|---|
| 110 | */
|
|---|
| 111 | {
|
|---|
| 112 | /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
|
|---|
| 113 | if (quality <= 0) quality = 1;
|
|---|
| 114 | if (quality > 100) quality = 100;
|
|---|
| 115 |
|
|---|
| 116 | /* The basic table is used as-is (scaling 100) for a quality of 50.
|
|---|
| 117 | * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
|
|---|
| 118 | * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
|
|---|
| 119 | * to make all the table entries 1 (hence, minimum quantization loss).
|
|---|
| 120 | * Qualities 1..50 are converted to scaling percentage 5000/Q.
|
|---|
| 121 | */
|
|---|
| 122 | if (quality < 50)
|
|---|
| 123 | quality = 5000 / quality;
|
|---|
| 124 | else
|
|---|
| 125 | quality = 200 - quality*2;
|
|---|
| 126 |
|
|---|
| 127 | return quality;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | GLOBAL(void)
|
|---|
| 132 | jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
|
|---|
| 133 | /* Set or change the 'quality' (quantization) setting, using default tables.
|
|---|
| 134 | * This is the standard quality-adjusting entry point for typical user
|
|---|
| 135 | * interfaces; only those who want detailed control over quantization tables
|
|---|
| 136 | * would use the preceding three routines directly.
|
|---|
| 137 | */
|
|---|
| 138 | {
|
|---|
| 139 | /* Convert user 0-100 rating to percentage scaling */
|
|---|
| 140 | quality = jpeg_quality_scaling(quality);
|
|---|
| 141 |
|
|---|
| 142 | /* Set up standard quality tables */
|
|---|
| 143 | jpeg_set_linear_quality(cinfo, quality, force_baseline);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Huffman table setup routines
|
|---|
| 149 | */
|
|---|
| 150 |
|
|---|
| 151 | LOCAL(void)
|
|---|
| 152 | add_huff_table (j_compress_ptr cinfo,
|
|---|
| 153 | JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
|
|---|
| 154 | /* Define a Huffman table */
|
|---|
| 155 | {
|
|---|
| 156 | int nsymbols, len;
|
|---|
| 157 |
|
|---|
| 158 | if (*htblptr == NULL)
|
|---|
| 159 | *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
|---|
| 160 |
|
|---|
| 161 | /* Copy the number-of-symbols-of-each-code-length counts */
|
|---|
| 162 | MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
|
|---|
| 163 |
|
|---|
| 164 | /* Validate the counts. We do this here mainly so we can copy the right
|
|---|
| 165 | * number of symbols from the val[] array, without risking marching off
|
|---|
| 166 | * the end of memory. jchuff.c will do a more thorough test later.
|
|---|
| 167 | */
|
|---|
| 168 | nsymbols = 0;
|
|---|
| 169 | for (len = 1; len <= 16; len++)
|
|---|
| 170 | nsymbols += bits[len];
|
|---|
| 171 | if (nsymbols < 1 || nsymbols > 256)
|
|---|
| 172 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|---|
| 173 |
|
|---|
| 174 | MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
|
|---|
| 175 |
|
|---|
| 176 | /* Initialize sent_table FALSE so table will be written to JPEG file. */
|
|---|
| 177 | (*htblptr)->sent_table = FALSE;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 | LOCAL(void)
|
|---|
| 182 | std_huff_tables (j_compress_ptr cinfo)
|
|---|
| 183 | /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
|
|---|
| 184 | /* IMPORTANT: these are only valid for 8-bit data precision! */
|
|---|
| 185 | {
|
|---|
| 186 | static const UINT8 bits_dc_luminance[17] =
|
|---|
| 187 | { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
|
|---|
| 188 | static const UINT8 val_dc_luminance[] =
|
|---|
| 189 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
|
|---|
| 190 |
|
|---|
| 191 | static const UINT8 bits_dc_chrominance[17] =
|
|---|
| 192 | { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
|
|---|
| 193 | static const UINT8 val_dc_chrominance[] =
|
|---|
| 194 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
|
|---|
| 195 |
|
|---|
| 196 | static const UINT8 bits_ac_luminance[17] =
|
|---|
| 197 | { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
|
|---|
| 198 | static const UINT8 val_ac_luminance[] =
|
|---|
| 199 | { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
|
|---|
| 200 | 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
|
|---|
| 201 | 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
|
|---|
| 202 | 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
|
|---|
| 203 | 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
|
|---|
| 204 | 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
|
|---|
| 205 | 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
|
|---|
| 206 | 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
|
|---|
| 207 | 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
|
|---|
| 208 | 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
|
|---|
| 209 | 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
|
|---|
| 210 | 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
|
|---|
| 211 | 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
|
|---|
| 212 | 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
|
|---|
| 213 | 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
|
|---|
|
|---|