Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rdswitch.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 7 | * Copyright (C) 2010, 2018, D. R. Commander. |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * This file contains routines to process some of cjpeg's more complicated |
| 12 | * command-line switches. Switches processed here are: |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 13 | * -qtables file Read quantization tables from text file |
| 14 | * -scans file Read scan script from text file |
| 15 | * -quality N[,N,...] Set quality ratings |
| 16 | * -qslots N[,N,...] Set component quantization table selectors |
| 17 | * -sample HxV[,HxV,...] Set component sampling factors |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 20 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 21 | #include <ctype.h> /* to declare isdigit(), isspace() */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 22 | |
| 23 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 24 | LOCAL(int) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 25 | text_getc(FILE *file) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 26 | /* Read next char, skipping over any comments (# to end of line) */ |
| 27 | /* A comment/newline sequence is returned as a newline */ |
| 28 | { |
| 29 | register int ch; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 30 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 31 | ch = getc(file); |
| 32 | if (ch == '#') { |
| 33 | do { |
| 34 | ch = getc(file); |
| 35 | } while (ch != '\n' && ch != EOF); |
| 36 | } |
| 37 | return ch; |
| 38 | } |
| 39 | |
| 40 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 41 | LOCAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 42 | read_text_integer(FILE *file, long *result, int *termchar) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 43 | /* Read an unsigned decimal integer from a file, store it in result */ |
| 44 | /* Reads one trailing character after the integer; returns it in termchar */ |
| 45 | { |
| 46 | register int ch; |
| 47 | register long val; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 48 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 49 | /* Skip any leading whitespace, detect EOF */ |
| 50 | do { |
| 51 | ch = text_getc(file); |
| 52 | if (ch == EOF) { |
| 53 | *termchar = ch; |
| 54 | return FALSE; |
| 55 | } |
| 56 | } while (isspace(ch)); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 57 | |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 58 | if (!isdigit(ch)) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 59 | *termchar = ch; |
| 60 | return FALSE; |
| 61 | } |
| 62 | |
| 63 | val = ch - '0'; |
| 64 | while ((ch = text_getc(file)) != EOF) { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 65 | if (!isdigit(ch)) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 66 | break; |
| 67 | val *= 10; |
| 68 | val += ch - '0'; |
| 69 | } |
| 70 | *result = val; |
| 71 | *termchar = ch; |
| 72 | return TRUE; |
| 73 | } |
| 74 | |
| 75 | |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 76 | #if JPEG_LIB_VERSION < 70 |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 77 | static int q_scale_factor[NUM_QUANT_TBLS] = { 100, 100, 100, 100 }; |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 80 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 81 | read_quant_tables(j_compress_ptr cinfo, char *filename, boolean force_baseline) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 82 | /* Read a set of quantization tables from the specified file. |
| 83 | * The file is plain ASCII text: decimal numbers with whitespace between. |
| 84 | * Comments preceded by '#' may be included in the file. |
| 85 | * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values. |
| 86 | * The tables are implicitly numbered 0,1,etc. |
| 87 | * NOTE: does not affect the qslots mapping, which will default to selecting |
| 88 | * table 0 for luminance (or primary) components, 1 for chrominance components. |
| 89 | * You must use -qslots if you want a different component->table mapping. |
| 90 | */ |
| 91 | { |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 92 | FILE *fp; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 93 | int tblno, i, termchar; |
| 94 | long val; |
| 95 | unsigned int table[DCTSIZE2]; |
| 96 | |
| 97 | if ((fp = fopen(filename, "r")) == NULL) { |
| 98 | fprintf(stderr, "Can't open table file %s\n", filename); |
| 99 | return FALSE; |
| 100 | } |
| 101 | tblno = 0; |
| 102 | |
| 103 | while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */ |
| 104 | if (tblno >= NUM_QUANT_TBLS) { |
| 105 | fprintf(stderr, "Too many tables in file %s\n", filename); |
| 106 | fclose(fp); |
| 107 | return FALSE; |
| 108 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 109 | table[0] = (unsigned int)val; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 110 | for (i = 1; i < DCTSIZE2; i++) { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 111 | if (!read_text_integer(fp, &val, &termchar)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 112 | fprintf(stderr, "Invalid table data in file %s\n", filename); |
| 113 | fclose(fp); |
| 114 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 115 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 116 | table[i] = (unsigned int)val; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 117 | } |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 118 | #if JPEG_LIB_VERSION >= 70 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 119 | jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno], |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 120 | force_baseline); |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 121 | #else |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 122 | jpeg_add_quant_table(cinfo, tblno, table, q_scale_factor[tblno], |
| 123 | force_baseline); |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 124 | #endif |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 125 | tblno++; |
| 126 | } |
| 127 | |
| 128 | if (termchar != EOF) { |
| 129 | fprintf(stderr, "Non-numeric data in file %s\n", filename); |
| 130 | fclose(fp); |
| 131 | return FALSE; |
| 132 | } |
| 133 | |
| 134 | fclose(fp); |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 140 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 141 | LOCAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 142 | read_scan_integer(FILE *file, long *result, int *termchar) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 143 | /* Variant of read_text_integer that always looks for a non-space termchar; |
| 144 | * this simplifies parsing of punctuation in scan scripts. |
| 145 | */ |
| 146 | { |
| 147 | register int ch; |
| 148 | |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 149 | if (!read_text_integer(file, result, termchar)) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 150 | return FALSE; |
| 151 | ch = *termchar; |
| 152 | while (ch != EOF && isspace(ch)) |
| 153 | ch = text_getc(file); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 154 | if (isdigit(ch)) { /* oops, put it back */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 155 | if (ungetc(ch, file) == EOF) |
| 156 | return FALSE; |
| 157 | ch = ' '; |
| 158 | } else { |
| 159 | /* Any separators other than ';' and ':' are ignored; |
| 160 | * this allows user to insert commas, etc, if desired. |
| 161 | */ |
| 162 | if (ch != EOF && ch != ';' && ch != ':') |
| 163 | ch = ' '; |
| 164 | } |
| 165 | *termchar = ch; |
| 166 | return TRUE; |
| 167 | } |
| 168 | |
| 169 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 170 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 171 | read_scan_script(j_compress_ptr cinfo, char *filename) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 172 | /* Read a scan script from the specified text file. |
| 173 | * Each entry in the file defines one scan to be emitted. |
| 174 | * Entries are separated by semicolons ';'. |
| 175 | * An entry contains one to four component indexes, |
| 176 | * optionally followed by a colon ':' and four progressive-JPEG parameters. |
| 177 | * The component indexes denote which component(s) are to be transmitted |
| 178 | * in the current scan. The first component has index 0. |
| 179 | * Sequential JPEG is used if the progressive-JPEG parameters are omitted. |
| 180 | * The file is free format text: any whitespace may appear between numbers |
| 181 | * and the ':' and ';' punctuation marks. Also, other punctuation (such |
| 182 | * as commas or dashes) can be placed between numbers if desired. |
| 183 | * Comments preceded by '#' may be included in the file. |
| 184 | * Note: we do very little validity checking here; |
| 185 | * jcmaster.c will validate the script parameters. |
| 186 | */ |
| 187 | { |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 188 | FILE *fp; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 189 | int scanno, ncomps, termchar; |
| 190 | long val; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 191 | jpeg_scan_info *scanptr; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 192 | #define MAX_SCANS 100 /* quite arbitrary limit */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 193 | jpeg_scan_info scans[MAX_SCANS]; |
| 194 | |
| 195 | if ((fp = fopen(filename, "r")) == NULL) { |
| 196 | fprintf(stderr, "Can't open scan definition file %s\n", filename); |
| 197 | return FALSE; |
| 198 | } |
| 199 | scanptr = scans; |
| 200 | scanno = 0; |
| 201 | |
| 202 | while (read_scan_integer(fp, &val, &termchar)) { |
| 203 | if (scanno >= MAX_SCANS) { |
| 204 | fprintf(stderr, "Too many scans defined in file %s\n", filename); |
| 205 | fclose(fp); |
| 206 | return FALSE; |
| 207 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 208 | scanptr->component_index[0] = (int)val; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 209 | ncomps = 1; |
| 210 | while (termchar == ' ') { |
| 211 | if (ncomps >= MAX_COMPS_IN_SCAN) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 212 | fprintf(stderr, "Too many components in one scan in file %s\n", |
| 213 | filename); |
| 214 | fclose(fp); |
| 215 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 216 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 217 | if (!read_scan_integer(fp, &val, &termchar)) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 218 | goto bogus; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 219 | scanptr->component_index[ncomps] = (int)val; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 220 | ncomps++; |
| 221 | } |
| 222 | scanptr->comps_in_scan = ncomps; |
| 223 | if (termchar == ':') { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 224 | if (!read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 225 | goto bogus; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 226 | scanptr->Ss = (int)val; |
| 227 | if (!read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 228 | goto bogus; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 229 | scanptr->Se = (int)val; |
| 230 | if (!read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 231 | goto bogus; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 232 | scanptr->Ah = (int)val; |
| 233 | if (!read_scan_integer(fp, &val, &termchar)) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 234 | goto bogus; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 235 | scanptr->Al = (int)val; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 236 | } else { |
| 237 | /* set non-progressive parameters */ |
| 238 | scanptr->Ss = 0; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 239 | scanptr->Se = DCTSIZE2 - 1; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 240 | scanptr->Ah = 0; |
| 241 | scanptr->Al = 0; |
| 242 | } |
| 243 | if (termchar != ';' && termchar != EOF) { |
| 244 | bogus: |
| 245 | fprintf(stderr, "Invalid scan entry format in file %s\n", filename); |
| 246 | fclose(fp); |
| 247 | return FALSE; |
| 248 | } |
| 249 | scanptr++, scanno++; |
| 250 | } |
| 251 | |
| 252 | if (termchar != EOF) { |
| 253 | fprintf(stderr, "Non-numeric data in file %s\n", filename); |
| 254 | fclose(fp); |
| 255 | return FALSE; |
| 256 | } |
| 257 | |
| 258 | if (scanno > 0) { |
| 259 | /* Stash completed scan list in cinfo structure. |
| 260 | * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data, |
| 261 | * but if you want to compress multiple images you'd want JPOOL_PERMANENT. |
| 262 | */ |
| 263 | scanptr = (jpeg_scan_info *) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 264 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 265 | scanno * sizeof(jpeg_scan_info)); |
| 266 | MEMCOPY(scanptr, scans, scanno * sizeof(jpeg_scan_info)); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 267 | cinfo->scan_info = scanptr; |
| 268 | cinfo->num_scans = scanno; |
| 269 | } |
| 270 | |
| 271 | fclose(fp); |
| 272 | return TRUE; |
| 273 | } |
| 274 | |
| 275 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
| 276 | |
| 277 | |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 278 | #if JPEG_LIB_VERSION < 70 |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 279 | /* These are the sample quantization tables given in Annex K (Clause K.1) of |
| 280 | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 281 | * The spec says that the values given produce "good" quality, and |
| 282 | * when divided by 2, "very good" quality. |
| 283 | */ |
| 284 | static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = { |
| 285 | 16, 11, 10, 16, 24, 40, 51, 61, |
| 286 | 12, 12, 14, 19, 26, 58, 60, 55, |
| 287 | 14, 13, 16, 24, 40, 57, 69, 56, |
| 288 | 14, 17, 22, 29, 51, 87, 80, 62, |
| 289 | 18, 22, 37, 56, 68, 109, 103, 77, |
| 290 | 24, 35, 55, 64, 81, 104, 113, 92, |
| 291 | 49, 64, 78, 87, 103, 121, 120, 101, |
| 292 | 72, 92, 95, 98, 112, 100, 103, 99 |
| 293 | }; |
| 294 | static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = { |
| 295 | 17, 18, 24, 47, 99, 99, 99, 99, |
| 296 | 18, 21, 26, 66, 99, 99, 99, 99, |
| 297 | 24, 26, 56, 99, 99, 99, 99, 99, |
| 298 | 47, 66, 99, 99, 99, 99, 99, 99, |
| 299 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 300 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 301 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 302 | 99, 99, 99, 99, 99, 99, 99, 99 |
| 303 | }; |
| 304 | |
| 305 | |
| 306 | LOCAL(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 307 | jpeg_default_qtables(j_compress_ptr cinfo, boolean force_baseline) |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 308 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 309 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, q_scale_factor[0], |
| 310 | force_baseline); |
| 311 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, q_scale_factor[1], |
| 312 | force_baseline); |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 313 | } |
| 314 | #endif |
| 315 | |
| 316 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 317 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 318 | set_quality_ratings(j_compress_ptr cinfo, char *arg, boolean force_baseline) |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 319 | /* Process a quality-ratings parameter string, of the form |
| 320 | * N[,N,...] |
| 321 | * If there are more q-table slots than parameters, the last value is replicated. |
| 322 | */ |
| 323 | { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 324 | int val = 75; /* default value */ |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 325 | int tblno; |
| 326 | char ch; |
| 327 | |
| 328 | for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { |
| 329 | if (*arg) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 330 | ch = ','; /* if not set by sscanf, will be ',' */ |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 331 | if (sscanf(arg, "%d%c", &val, &ch) < 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 332 | return FALSE; |
| 333 | if (ch != ',') /* syntax check */ |
| 334 | return FALSE; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 335 | /* Convert user 0-100 rating to percentage scaling */ |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 336 | #if JPEG_LIB_VERSION >= 70 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 337 | cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 338 | #else |
| 339 | q_scale_factor[tblno] = jpeg_quality_scaling(val); |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 340 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 341 | while (*arg && *arg++ != ',') /* advance to next segment of arg string */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 342 | ; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 343 | } else { |
| 344 | /* reached end of parameter, set remaining factors to last value */ |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 345 | #if JPEG_LIB_VERSION >= 70 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 346 | cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); |
DRC | ed16ad1 | 2010-10-11 23:36:04 +0000 | [diff] [blame] | 347 | #else |
| 348 | q_scale_factor[tblno] = jpeg_quality_scaling(val); |
DRC | 294079c | 2010-10-10 07:21:55 +0000 | [diff] [blame] | 349 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | jpeg_default_qtables(cinfo, force_baseline); |
| 353 | return TRUE; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 358 | set_quant_slots(j_compress_ptr cinfo, char *arg) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 359 | /* Process a quantization-table-selectors parameter string, of the form |
| 360 | * N[,N,...] |
| 361 | * If there are more components than parameters, the last value is replicated. |
| 362 | */ |
| 363 | { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 364 | int val = 0; /* default table # */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 365 | int ci; |
| 366 | char ch; |
| 367 | |
| 368 | for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 369 | if (*arg) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 370 | ch = ','; /* if not set by sscanf, will be ',' */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 371 | if (sscanf(arg, "%d%c", &val, &ch) < 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 372 | return FALSE; |
| 373 | if (ch != ',') /* syntax check */ |
| 374 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 375 | if (val < 0 || val >= NUM_QUANT_TBLS) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 376 | fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n", |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 377 | NUM_QUANT_TBLS - 1); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 378 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 379 | } |
| 380 | cinfo->comp_info[ci].quant_tbl_no = val; |
| 381 | while (*arg && *arg++ != ',') /* advance to next segment of arg string */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 382 | ; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 383 | } else { |
| 384 | /* reached end of parameter, set remaining components to last table */ |
| 385 | cinfo->comp_info[ci].quant_tbl_no = val; |
| 386 | } |
| 387 | } |
| 388 | return TRUE; |
| 389 | } |
| 390 | |
| 391 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 392 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame^] | 393 | set_sample_factors(j_compress_ptr cinfo, char *arg) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 394 | /* Process a sample-factors parameter string, of the form |
| 395 | * HxV[,HxV,...] |
| 396 | * If there are more components than parameters, "1x1" is assumed for the rest. |
| 397 | */ |
| 398 | { |
| 399 | int ci, val1, val2; |
| 400 | char ch1, ch2; |
| 401 | |
| 402 | for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 403 | if (*arg) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 404 | ch2 = ','; /* if not set by sscanf, will be ',' */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 405 | if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 406 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 407 | if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 408 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 409 | if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 410 | fprintf(stderr, "JPEG sampling factors must be 1..4\n"); |
| 411 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 412 | } |
| 413 | cinfo->comp_info[ci].h_samp_factor = val1; |
| 414 | cinfo->comp_info[ci].v_samp_factor = val2; |
| 415 | while (*arg && *arg++ != ',') /* advance to next segment of arg string */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 416 | ; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 417 | } else { |
| 418 | /* reached end of parameter, set remaining components to 1x1 sampling */ |
| 419 | cinfo->comp_info[ci].h_samp_factor = 1; |
| 420 | cinfo->comp_info[ci].v_samp_factor = 1; |
| 421 | } |
| 422 | } |
| 423 | return TRUE; |
| 424 | } |