| 6 |
| * libjpeg-turbo Modifications: |
| 7 | * Copyright (C) 2010, 2018, D. R. Commander. |
| 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
| 10 | * |
| 11 | * This file contains routines to process some of cjpeg's more complicated |
| 12 | * command-line switches. Switches processed here are: |
| 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 |
| 18 | */ |
| 19 |
| 20 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 21 | #include <ctype.h> /* to declare isdigit(), isspace() */ |
| 22 |
| 23 |
| 24 | LOCAL(int) |
| 25 | text_getc (FILE *file) |
| 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; |
| 30 |
| 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 |
| 41 | LOCAL(boolean) |
| 42 | read_text_integer (FILE *file, long *result, int *termchar) |
| 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; |
| 48 |
| 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)); |
| 57 |
| 58 | if (! isdigit(ch)) { |
| 59 | *termchar = ch; |
| 60 | return FALSE; |
| 61 | } |
| 62 |
| 63 | val = ch - '0'; |
| 64 | while ((ch = text_getc(file)) != EOF) { |
| 65 | if (! isdigit(ch)) |
| 66 | break; |
| 67 | val *= 10; |
| 68 | val += ch - '0'; |
| 69 | } |
| 70 | *result = val; |
| 71 | *termchar = ch; |
| 72 | return TRUE; |
| 73 | } |
| 74 |
| 75 |
| 76 | #if JPEG_LIB_VERSION < 70 |
| 77 | static int q_scale_factor[NUM_QUANT_TBLS] = {100, 100, 100, 100}; |
| 78 | #endif |
| 79 |
| 80 | GLOBAL(boolean) |
| 81 | read_quant_tables(j_compress_ptr cinfo, char *filename, boolean force_baseline) |
| 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 | { |
| 92 | FILE *fp; |
| 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 | } |
| 109 | table[0] = (unsigned int) val; |
| 110 | for (i = 1; i < DCTSIZE2; i++) { |
| 111 | if (! read_text_integer(fp, &val, &termchar)) { |
| 112 | fprintf(stderr, "Invalid table data in file %s\n", filename); |
| 113 | fclose(fp); |
| 114 | return FALSE; |
| 115 | } |
| 116 | table[i] = (unsigned int) val; |
| 117 | } |
| 118 | #if JPEG_LIB_VERSION >= 70 |
| 119 | jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno], |
| 120 | force_baseline); |
| 121 | #else |
| 122 | jpeg_add_quant_table(cinfo, tblno, table, q_scale_factor[tblno], |
| 123 | force_baseline); |
| 124 | #endif |
| 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 |
| 141 | LOCAL(boolean) |
| 142 | read_scan_integer (FILE *file, long *result, int *termchar) |
| 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 |
| 149 | if (! read_text_integer(file, result, termchar)) |
| 150 | return FALSE; |
| 151 | ch = *termchar; |
| 152 | while (ch != EOF && isspace(ch)) |
| 153 | ch = text_getc(file); |
| 154 | if (isdigit(ch)) { /* oops, put it back */ |
| 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 |
| 170 | GLOBAL(boolean) |
| 171 | read_scan_script (j_compress_ptr cinfo, char *filename) |
| 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 | { |
| 188 | FILE *fp; |
| 189 | int scanno, ncomps, termchar; |
| 190 | long val; |
| 191 | jpeg_scan_info *scanptr; |
| 192 | #define MAX_SCANS 100 /* quite arbitrary limit */ |
| 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 | } |
| 208 | scanptr->component_index[0] = (int) val; |
| 209 | ncomps = 1; |
| 210 | while (termchar == ' ') { |
| 211 | if (ncomps >= MAX_COMPS_IN_SCAN) { |
| 212 | fprintf(stderr, "Too many components in one scan in file %s\n", |
| 213 | filename); |
| 214 | fclose(fp); |
| 215 | return FALSE; |
| 216 | } |
| 217 | if (! read_scan_integer(fp, &val, &termchar)) |
| 218 | goto bogus; |
| 219 | scanptr->component_index[ncomps] = (int) val; |
| 220 | ncomps++; |
| 221 | } |
| 222 | scanptr->comps_in_scan = ncomps; |
| 223 | if (termchar == ':') { |
| 224 | if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 225 | goto bogus; |
| 226 | scanptr->Ss = (int) val; |
| 227 | if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 228 | goto bogus; |
| 229 | scanptr->Se = (int) val; |
| 230 | if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 231 | goto bogus; |
| 232 | scanptr->Ah = (int) val; |
| 233 | if (! read_scan_integer(fp, &val, &termchar)) |
| 234 | goto bogus; |
| 235 | scanptr->Al = (int) val; |
| 236 | } else { |
| 237 | /* set non-progressive parameters */ |
| 238 | scanptr->Ss = 0; |
| 239 | scanptr->Se = DCTSIZE2-1; |
| 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 *) |
| 264 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 265 | scanno * sizeof(jpeg_scan_info)); |
| 266 | MEMCOPY(scanptr, scans, scanno * sizeof(jpeg_scan_info)); |
| 267 | cinfo->scan_info = scanptr; |
| 268 | cinfo->num_scans = scanno; |
| 269 |
| 270 | /* Disable scan optimization if using custom scan */ |
| 271 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_OPTIMIZE_SCANS, FALSE); |
| 272 | } |
| 273 |
| 274 | fclose(fp); |
| 275 | return TRUE; |
| 276 | } |
| 277 |
| 278 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
| 279 |
| 280 |
| 281 | #if JPEG_LIB_VERSION < 70 |
| 282 | /* These are the sample quantization tables given in Annex K (Clause K.1) of |
| 283 | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
| 284 | * The spec says that the values given produce "good" quality, and |
| 285 | * when divided by 2, "very good" quality. |
| 286 | */ |
| 287 | static const unsigned int std_luminance_quant_tbl[9][DCTSIZE2] = { |
| 288 | { |
| 289 | /* JPEG Annex K |
| 290 | */ |
| 291 | 16, 11, 10, 16, 24, 40, 51, 61, |
| 292 | 12, 12, 14, 19, 26, 58, 60, 55, |
| 293 | 14, 13, 16, 24, 40, 57, 69, 56, |
| 294 | 14, 17, 22, 29, 51, 87, 80, 62, |
| 295 | 18, 22, 37, 56, 68, 109, 103, 77, |
| 296 | 24, 35, 55, 64, 81, 104, 113, 92, |
| 297 | 49, 64, 78, 87, 103, 121, 120, 101, |
| 298 | 72, 92, 95, 98, 112, 100, 103, 99 |
| 299 | }, |
| 300 | { |
| 301 | /* flat |
| 302 | */ |
| 303 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 304 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 305 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 306 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 307 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 308 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 309 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 310 | 16, 16, 16, 16, 16, 16, 16, 16 |
| 311 | }, |
| 312 | { |
| 313 | 12, 17, 20, 21, 30, 34, 56, 63, |
| 314 | 18, 20, 20, 26, 28, 51, 61, 55, |
| 315 | 19, 20, 21, 26, 33, 58, 69, 55, |
| 316 | 26, 26, 26, 30, 46, 87, 86, 66, |
| 317 | 31, 33, 36, 40, 46, 96, 100, 73, |
| 318 | 40, 35, 46, 62, 81, 100, 111, 91, |
| 319 | 46, 66, 76, 86, 102, 121, 120, 101, |
| 320 | 68, 90, 90, 96, 113, 102, 105, 103 |
| 321 | }, |
| 322 | { |
| 323 | /* From http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20333&p=98008#p98008 |
| 324 | */ |
| 325 | 16, 16, 16, 18, 25, 37, 56, 85, |
| 326 | 16, 17, 20, 27, 34, 40, 53, 75, |
| 327 | 16, 20, 24, 31, 43, 62, 91, 135, |
| 328 | 18, 27, 31, 40, 53, 74, 106, 156, |
| 329 | 25, 34, 43, 53, 69, 94, 131, 189, |
| 330 | 37, 40, 62, 74, 94, 124, 169, 238, |
| 331 | 56, 53, 91, 106, 131, 169, 226, 311, |
| 332 | 85, 75, 135, 156, 189, 238, 311, 418 |
| 333 | }, |
| 334 | { |
| 335 | 9, 10, 12, 14, 27, 32, 51, 62, |
| 336 | 11, 12, 14, 19, 27, 44, 59, 73, |
| 337 | 12, 14, 18, 25, 42, 59, 79, 78, |
| 338 | 17, 18, 25, 42, 61, 92, 87, 92, |
| 339 | 23, 28, 42, 75, 79, 112, 112, 99, |
| 340 | 40, 42, 59, 84, 88, 124, 132, 111, |
| 341 | 42, 64, 78, 95, 105, 126, 125, 99, |
| 342 | 70, 75, 100, 102, 116, 100, 107, 98 |
| 343 | }, |
| 344 | { |
| 345 | /* Relevance of human vision to JPEG-DCT compression (1992) Klein, Silverstein and Carney. |
| 346 | */ |
| 347 | 10, 12, 14, 19, 26, 38, 57, 86, |
| 348 | 12, 18, 21, 28, 35, 41, 54, 76, |
| 349 | 14, 21, 25, 32, 44, 63, 92, 136, |
| 350 | 19, 28, 32, 41, 54, 75, 107, 157, |
| 351 | 26, 35, 44, 54, 70, 95, 132, 190, |
| 352 | 38, 41, 63, 75, 95, 125, 170, 239, |
| 353 | 57, 54, 92, 107, 132, 170, 227, 312, |
| 354 | 86, 76, 136, 157, 190, 239, 312, 419 |
| 355 | }, |
| 356 | { |
| 357 | /* DCTune perceptual optimization of compressed dental X-Rays (1997) Watson, Taylor, Borthwick |
| 358 | */ |
| 359 | 7, 8, 10, 14, 23, 44, 95, 241, |
| 360 | 8, 8, 11, 15, 25, 47, 102, 255, |
| 361 | 10, 11, 13, 19, 31, 58, 127, 255, |
| 362 | 14, 15, 19, 27, 44, 83, 181, 255, |
| 363 | 23, 25, 31, 44, 72, 136, 255, 255, |
| 364 | 44, 47, 58, 83, 136, 255, 255, 255, |
| 365 | 95, 102, 127, 181, 255, 255, 255, 255, |
| 366 | 241, 255, 255, 255, 255, 255, 255, 255 |
| 367 | }, |
| 368 | { |
| 369 | /* A visual detection model for DCT coefficient quantization (12/9/93) Ahumada, Watson, Peterson |
| 370 | */ |
| 371 | 15, 11, 11, 12, 15, 19, 25, 32, |
| 372 | 11, 13, 10, 10, 12, 15, 19, 24, |
| 373 | 11, 10, 14, 14, 16, 18, 22, 27, |
| 374 | 12, 10, 14, 18, 21, 24, 28, 33, |
| 375 | 15, 12, 16, 21, 26, 31, 36, 42, |
| 376 | 19, 15, 18, 24, 31, 38, 45, 53, |
| 377 | 25, 19, 22, 28, 36, 45, 55, 65, |
| 378 | 32, 24, 27, 33, 42, 53, 65, 77 |
| 379 | }, |
| 380 | { |
| 381 | /* An improved detection model for DCT coefficient quantization (1993) Peterson, Ahumada and Watson |
| 382 | */ |
| 383 | 14, 10, 11, 14, 19, 25, 34, 45, |
| 384 | 10, 11, 11, 12, 15, 20, 26, 33, |
| 385 | 11, 11, 15, 18, 21, 25, 31, 38, |
| 386 | 14, 12, 18, 24, 28, 33, 39, 47, |
| 387 | 19, 15, 21, 28, 36, 43, 51, 59, |
| 388 | 25, 20, 25, 33, 43, 54, 64, 74, |
| 389 | 34, 26, 31, 39, 51, 64, 77, 91, |
| 390 | 45, 33, 38, 47, 59, 74, 91, 108 |
| 391 | } |
| 392 | }; |
| 393 |
| 394 | static const unsigned int std_chrominance_quant_tbl[9][DCTSIZE2] = { |
| 395 | { |
| 396 | /* JPEG Annex K |
| 397 | */ |
| 398 | 17, 18, 24, 47, 99, 99, 99, 99, |
| 399 | 18, 21, 26, 66, 99, 99, 99, 99, |
| 400 | 24, 26, 56, 99, 99, 99, 99, 99, |
| 401 | 47, 66, 99, 99, 99, 99, 99, 99, |
| 402 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 403 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 404 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 405 | 99, 99, 99, 99, 99, 99, 99, 99 |
| 406 | }, |
| 407 | { |
| 408 | /* flat |
| 409 | */ |
| 410 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 411 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 412 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 413 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 414 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 415 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 416 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 417 | 16, 16, 16, 16, 16, 16, 16, 16 |
| 418 | }, |
| 419 | { |
| 420 | 8, 12, 15, 15, 86, 96, 96, 98, |
| 421 | 13, 13, 15, 26, 90, 96, 99, 98, |
| 422 | 12, 15, 18, 96, 99, 99, 99, 99, |
| 423 | 17, 16, 90, 96, 99, 99, 99, 99, |
| 424 | 96, 96, 99, 99, 99, 99, 99, 99, |
| 425 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 426 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 427 | 99, 99, 99, 99, 99, 99, 99, 99 |
| 428 | }, |
| 429 | { |
| 430 | /* From http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20333&p=98008#p98008 |
| 431 | */ |
| 432 | 16, 16, 16, 18, 25, 37, 56, 85, |
| 433 | 16, 17, 20, 27, 34, 40, 53, 75, |
| 434 | 16, 20, 24, 31, 43, 62, 91, 135, |
| 435 | 18, 27, 31, 40, 53, 74, 106, 156, |
| 436 | 25, 34, 43, 53, 69, 94, 131, 189, |
| 437 | 37, 40, 62, 74, 94, 124, 169, 238, |
| 438 | 56, 53, 91, 106, 131, 169, 226, 311, |
| 439 | 85, 75, 135, 156, 189, 238, 311, 418 |
| 440 | }, |
| 441 | { |
| 442 | 9, 10, 17, 19, 62, 89, 91, 97, |
| 443 | 12, 13, 18, 29, 84, 91, 88, 98, |
| 444 | 14, 19, 29, 93, 95, 95, 98, 97, |
| 445 | 20, 26, 84, 88, 95, 95, 98, 94, |
| 446 | 26, 86, 91, 93, 97, 99, 98, 99, |
| 447 | 99, 100, 98, 99, 99, 99, 99, 99, |
| 448 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 449 | 97, 97, 99, 99, 99, 99, 97, 99 |
| 450 | }, |
| 451 | { |
| 452 | /* Relevance of human vision to JPEG-DCT compression (1992) Klein, Silverstein and Carney. |
| 453 | * Copied from luma |
| 454 | */ |
| 455 | 10, 12, 14, 19, 26, 38, 57, 86, |
| 456 | 12, 18, 21, 28, 35, 41, 54, 76, |
| 457 | 14, 21, 25, 32, 44, 63, 92, 136, |
| 458 | 19, 28, 32, 41, 54, 75, 107, 157, |
| 459 | 26, 35, 44, 54, 70, 95, 132, 190, |
| 460 | 38, 41, 63, 75, 95, 125, 170, 239, |
| 461 | 57, 54, 92, 107, 132, 170, 227, 312, |
| 462 | 86, 76, 136, 157, 190, 239, 312, 419 |
| 463 | }, |
| 464 | { |
| 465 | /* DCTune perceptual optimization of compressed dental X-Rays (1997) Watson, Taylor, Borthwick |
| 466 | * Copied from luma |
| 467 | */ |
| 468 | 7, 8, 10, 14, 23, 44, 95, 241, |
| 469 | 8, 8, 11, 15, 25, 47, 102, 255, |
| 470 | 10, 11, 13, 19, 31, 58, 127, 255, |
| 471 | 14, 15, 19, 27, 44, 83, 181, 255, |
| 472 | 23, 25, 31, 44, 72, 136, 255, 255, |
| 473 | 44, 47, 58, 83, 136, 255, 255, 255, |
| 474 | 95, 102, 127, 181, 255, 255, 255, 255, |
| 475 | 241, 255, 255, 255, 255, 255, 255, 255 |
| 476 | }, |
| 477 | { |
| 478 | /* A visual detection model for DCT coefficient quantization (12/9/93) Ahumada, Watson, Peterson |
| 479 | * Copied from luma |
| 480 | */ |
| 481 | 15, 11, 11, 12, 15, 19, 25, 32, |
| 482 | 11, 13, 10, 10, 12, 15, 19, 24, |
| 483 | 11, 10, 14, 14, 16, 18, 22, 27, |
| 484 | 12, 10, 14, 18, 21, 24, 28, 33, |
| 485 | 15, 12, 16, 21, 26, 31, 36, 42, |
| 486 | 19, 15, 18, 24, 31, 38, 45, 53, |
| 487 | 25, 19, 22, 28, 36, 45, 55, 65, |
| 488 | 32, 24, 27, 33, 42, 53, 65, 77 |
| 489 | }, |
| 490 | { |
| 491 | /* An improved detection model for DCT coefficient quantization (1993) Peterson, Ahumada and Watson |
| 492 | * Copied from luma |
| 493 | */ |
| 494 | 14, 10, 11, 14, 19, 25, 34, 45, |
| 495 | 10, 11, 11, 12, 15, 20, 26, 33, |
| 496 | 11, 11, 15, 18, 21, 25, 31, 38, |
| 497 | 14, 12, 18, 24, 28, 33, 39, 47, |
| 498 | 19, 15, 21, 28, 36, 43, 51, 59, |
| 499 | 25, 20, 25, 33, 43, 54, 64, 74, |
| 500 | 34, 26, 31, 39, 51, 64, 77, 91, |
| 501 | 45, 33, 38, 47, 59, 74, 91, 108 |
| 502 | } |
| 503 | }; |
| 504 |
| 505 | LOCAL(void) |
| 506 | jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline) |
| 507 | { |
| 508 | int quant_tbl_master_idx = 0; |
| 509 | if (jpeg_c_int_param_supported(cinfo, JINT_BASE_QUANT_TBL_IDX)) |
| 510 | quant_tbl_master_idx = jpeg_c_get_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX); |
| 511 |
| 512 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl[quant_tbl_master_idx], |
| 513 | q_scale_factor[0], force_baseline); |
| 514 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl[quant_tbl_master_idx], |
| 515 | q_scale_factor[1], force_baseline); |
| 516 | } |
| 517 | #endif |
| 518 |
| 519 |
| 520 | GLOBAL(boolean) |
| 521 | set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline) |
| 522 | /* Process a quality-ratings parameter string, of the form |
| 523 | * N[,N,...] |
| 524 | * If there are more q-table slots than parameters, the last value is replicated. |
| 525 | */ |
| 526 | { |
| 527 | float val = 75.f; /* default value */ |
| 528 | int tblno; |
| 529 | char ch; |
| 530 |
| 531 | for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { |
| 532 | if (*arg) { |
| 533 | ch = ','; /* if not set by sscanf, will be ',' */ |
| 534 | if (sscanf(arg, "%f%c", &val, &ch) < 1) |
| 535 | return FALSE; |
| 536 | if (ch != ',') /* syntax check */ |
| 537 | return FALSE; |
| 538 | /* Convert user 0-100 rating to percentage scaling */ |
| 539 | #if JPEG_LIB_VERSION >= 70 |
| 540 | cinfo->q_scale_factor[tblno] = jpeg_float_quality_scaling(val); |
| 541 | #else |
| 542 | q_scale_factor[tblno] = jpeg_float_quality_scaling(val); |
| 543 | #endif |
| 544 | while (*arg && *arg++ != ','); /* advance to next segment of arg |
| 545 | string */ |
| 546 | } else { |
| 547 | /* reached end of parameter, set remaining factors to last value */ |
| 548 | #if JPEG_LIB_VERSION >= 70 |
| 549 | cinfo->q_scale_factor[tblno] = jpeg_float_quality_scaling(val); |
| 550 | #else |
| 551 | q_scale_factor[tblno] = jpeg_float_quality_scaling(val); |
| 552 | #endif |
| 553 | } |
| 554 | } |
| 555 | jpeg_default_qtables(cinfo, force_baseline); |
| 556 |
| 557 | /* For some images chroma subsampling significantly degrades color quality, |
| 558 | making it impossible to achieve high visual quality regardless of quality setting. |
| 559 | To make the quality setting more intuitive, disable subsampling when high-quality |
| 560 | color is desired. */ |
| 561 | if (val >= 90) { |
| 562 | set_sample_factors(cinfo, "1x1"); |
| 563 | } else if (val >= 80) { |
| 564 | set_sample_factors(cinfo, "2x1"); |
| 565 | } |
| 566 |
| 567 | return TRUE; |
| 568 | } |
| 569 |
| 570 |
| 571 | GLOBAL(boolean) |
| 572 | set_quant_slots (j_compress_ptr cinfo, char *arg) |
| 573 | /* Process a quantization-table-selectors parameter string, of the form |
| 574 | * N[,N,...] |
| 575 | * If there are more components than parameters, the last value is replicated. |
| 576 | */ |
| 577 | { |
| 578 | int val = 0; /* default table # */ |
| 579 | int ci; |
| 580 | char ch; |
| 581 |
| 582 | for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 583 | if (*arg) { |
| 584 | ch = ','; /* if not set by sscanf, will be ',' */ |
| 585 | if (sscanf(arg, "%d%c", &val, &ch) < 1) |
| 586 | return FALSE; |
| 587 | if (ch != ',') /* syntax check */ |
| 588 | return FALSE; |
| 589 | if (val < 0 || val >= NUM_QUANT_TBLS) { |
| 590 | fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n", |
| 591 | NUM_QUANT_TBLS-1); |
| 592 | return FALSE; |
| 593 | } |
| 594 | cinfo->comp_info[ci].quant_tbl_no = val; |
| 595 | while (*arg && *arg++ != ','); /* advance to next segment of arg |
| 596 | string */ |
| 597 | } else { |
| 598 | /* reached end of parameter, set remaining components to last table */ |
| 599 | cinfo->comp_info[ci].quant_tbl_no = val; |
| 600 | } |
| 601 | } |
| 602 | return TRUE; |
| 603 | } |
| 604 |
| 605 |
| 606 | GLOBAL(boolean) |
| 607 | set_sample_factors (j_compress_ptr cinfo, char *arg) |
| 608 | /* Process a sample-factors parameter string, of the form |
| 609 | * HxV[,HxV,...] |
| 610 | * If there are more components than parameters, "1x1" is assumed for the rest. |
| 611 | */ |
| 612 | { |
| 613 | int ci, val1, val2; |
| 614 | char ch1, ch2; |
| 615 |
| 616 | for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 617 | if (*arg) { |
| 618 | ch2 = ','; /* if not set by sscanf, will be ',' */ |
| 619 | if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3) |
| 620 | return FALSE; |
| 621 | if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */ |
| 622 | return FALSE; |
| 623 | if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) { |
| 624 | fprintf(stderr, "JPEG sampling factors must be 1..4\n"); |
| 625 | return FALSE; |
| 626 | } |
| 627 | cinfo->comp_info[ci].h_samp_factor = val1; |
| 628 | cinfo->comp_info[ci].v_samp_factor = val2; |
| 629 | while (*arg && *arg++ != ','); /* advance to next segment of arg |
| 630 | string */ |
| 631 | } else { |
| 632 | /* reached end of parameter, set remaining components to 1x1 sampling */ |
| 633 | cinfo->comp_info[ci].h_samp_factor = 1; |
| 634 | cinfo->comp_info[ci].v_samp_factor = 1; |
| 635 | } |
| 636 | } |
| 637 | return TRUE; |
| 638 | } |
| 639 |