| 6 |
| * Modified 2003-2008 by Guido Vollbeding. |
| 7 | * libjpeg-turbo Modifications: |
| 8 | * Copyright (C) 2009-2011, 2018, D. R. Commander. |
| 9 | * mozjpeg Modifications: |
| 10 | * Copyright (C) 2014, Mozilla Corporation. |
| 11 | * For conditions of distribution and use, see the accompanying README 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 it |
| 15 | * must know a lot more about the innards of the JPEG code. |
| 16 | */ |
| 17 |
| 18 | #define JPEG_INTERNALS |
| 19 | #include "jinclude.h" |
| 20 | #include "jpeglib.h" |
| 21 | #include "jstdhuff.c" |
| 22 |
| 23 |
| 24 | /* |
| 25 | * Quantization table setup routines |
| 26 | */ |
| 27 |
| 28 | GLOBAL(void) |
| 29 | jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, |
| 30 | const unsigned int *basic_table, int scale_factor, |
| 31 | boolean force_baseline) |
| 32 | /* Define a quantization table equal to the basic_table times |
| 33 | * a scale factor (given as a percentage). |
| 34 | * If force_baseline is TRUE, the computed quantization table entries |
| 35 | * are limited to 1..255 for JPEG baseline compatibility. |
| 36 | */ |
| 37 | { |
| 38 | JQUANT_TBL **qtblptr; |
| 39 | int i; |
| 40 | long temp; |
| 41 |
| 42 | /* Safety check to ensure start_compress not called yet. */ |
| 43 | if (cinfo->global_state != CSTATE_START) |
| 44 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 45 |
| 46 | if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS) |
| 47 | ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl); |
| 48 |
| 49 | qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; |
| 50 |
| 51 | if (*qtblptr == NULL) |
| 52 | *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); |
| 53 |
| 54 | for (i = 0; i < DCTSIZE2; i++) { |
| 55 | temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; |
| 56 | /* limit the values to the valid range */ |
| 57 | if (temp <= 0L) temp = 1L; |
| 58 | if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ |
| 59 | if (force_baseline && temp > 255L) |
| 60 | temp = 255L; /* limit to baseline range if requested */ |
| 61 | (*qtblptr)->quantval[i] = (UINT16) temp; |
| 62 | } |
| 63 |
| 64 | /* Initialize sent_table FALSE so table will be written to JPEG file. */ |
| 65 | (*qtblptr)->sent_table = FALSE; |
| 66 | } |
| 67 |
| 68 |
| 69 | /* These are the sample quantization tables given in Annex K (Clause K.1) of |
| 70 | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
| 71 | * The spec says that the values given produce "good" quality, and |
| 72 | * when divided by 2, "very good" quality. |
| 73 | */ |
| 74 | static const unsigned int std_luminance_quant_tbl[9][DCTSIZE2] = { |
| 75 | { |
| 76 | /* JPEG Annex K |
| 77 | */ |
| 78 | 16, 11, 10, 16, 24, 40, 51, 61, |
| 79 | 12, 12, 14, 19, 26, 58, 60, 55, |
| 80 | 14, 13, 16, 24, 40, 57, 69, 56, |
| 81 | 14, 17, 22, 29, 51, 87, 80, 62, |
| 82 | 18, 22, 37, 56, 68, 109, 103, 77, |
| 83 | 24, 35, 55, 64, 81, 104, 113, 92, |
| 84 | 49, 64, 78, 87, 103, 121, 120, 101, |
| 85 | 72, 92, 95, 98, 112, 100, 103, 99 |
| 86 | }, |
| 87 | { |
| 88 | /* flat |
| 89 | */ |
| 90 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 91 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 92 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 93 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 94 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 95 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 96 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 97 | 16, 16, 16, 16, 16, 16, 16, 16 |
| 98 | }, |
| 99 | { |
| 100 | 12, 17, 20, 21, 30, 34, 56, 63, |
| 101 | 18, 20, 20, 26, 28, 51, 61, 55, |
| 102 | 19, 20, 21, 26, 33, 58, 69, 55, |
| 103 | 26, 26, 26, 30, 46, 87, 86, 66, |
| 104 | 31, 33, 36, 40, 46, 96, 100, 73, |
| 105 | 40, 35, 46, 62, 81, 100, 111, 91, |
| 106 | 46, 66, 76, 86, 102, 121, 120, 101, |
| 107 | 68, 90, 90, 96, 113, 102, 105, 103 |
| 108 | }, |
| 109 | { |
| 110 | /* From http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20333&p=98008#p98008 |
| 111 | */ |
| 112 | 16, 16, 16, 18, 25, 37, 56, 85, |
| 113 | 16, 17, 20, 27, 34, 40, 53, 75, |
| 114 | 16, 20, 24, 31, 43, 62, 91, 135, |
| 115 | 18, 27, 31, 40, 53, 74, 106, 156, |
| 116 | 25, 34, 43, 53, 69, 94, 131, 189, |
| 117 | 37, 40, 62, 74, 94, 124, 169, 238, |
| 118 | 56, 53, 91, 106, 131, 169, 226, 311, |
| 119 | 85, 75, 135, 156, 189, 238, 311, 418 |
| 120 | }, |
| 121 | { |
| 122 | 9, 10, 12, 14, 27, 32, 51, 62, |
| 123 | 11, 12, 14, 19, 27, 44, 59, 73, |
| 124 | 12, 14, 18, 25, 42, 59, 79, 78, |
| 125 | 17, 18, 25, 42, 61, 92, 87, 92, |
| 126 | 23, 28, 42, 75, 79, 112, 112, 99, |
| 127 | 40, 42, 59, 84, 88, 124, 132, 111, |
| 128 | 42, 64, 78, 95, 105, 126, 125, 99, |
| 129 | 70, 75, 100, 102, 116, 100, 107, 98 |
| 130 | }, |
| 131 | { |
| 132 | /* Relevance of human vision to JPEG-DCT compression (1992) Klein, Silverstein and Carney. |
| 133 | */ |
| 134 | 10, 12, 14, 19, 26, 38, 57, 86, |
| 135 | 12, 18, 21, 28, 35, 41, 54, 76, |
| 136 | 14, 21, 25, 32, 44, 63, 92, 136, |
| 137 | 19, 28, 32, 41, 54, 75, 107, 157, |
| 138 | 26, 35, 44, 54, 70, 95, 132, 190, |
| 139 | 38, 41, 63, 75, 95, 125, 170, 239, |
| 140 | 57, 54, 92, 107, 132, 170, 227, 312, |
| 141 | 86, 76, 136, 157, 190, 239, 312, 419 |
| 142 | }, |
| 143 | { |
| 144 | /* DCTune perceptual optimization of compressed dental X-Rays (1997) Watson, Taylor, Borthwick |
| 145 | */ |
| 146 | 7, 8, 10, 14, 23, 44, 95, 241, |
| 147 | 8, 8, 11, 15, 25, 47, 102, 255, |
| 148 | 10, 11, 13, 19, 31, 58, 127, 255, |
| 149 | 14, 15, 19, 27, 44, 83, 181, 255, |
| 150 | 23, 25, 31, 44, 72, 136, 255, 255, |
| 151 | 44, 47, 58, 83, 136, 255, 255, 255, |
| 152 | 95, 102, 127, 181, 255, 255, 255, 255, |
| 153 | 241, 255, 255, 255, 255, 255, 255, 255 |
| 154 | }, |
| 155 | { |
| 156 | /* A visual detection model for DCT coefficient quantization (12/9/93) Ahumada, Watson, Peterson |
| 157 | */ |
| 158 | 15, 11, 11, 12, 15, 19, 25, 32, |
| 159 | 11, 13, 10, 10, 12, 15, 19, 24, |
| 160 | 11, 10, 14, 14, 16, 18, 22, 27, |
| 161 | 12, 10, 14, 18, 21, 24, 28, 33, |
| 162 | 15, 12, 16, 21, 26, 31, 36, 42, |
| 163 | 19, 15, 18, 24, 31, 38, 45, 53, |
| 164 | 25, 19, 22, 28, 36, 45, 55, 65, |
| 165 | 32, 24, 27, 33, 42, 53, 65, 77 |
| 166 | }, |
| 167 | { |
| 168 | /* An improved detection model for DCT coefficient quantization (1993) Peterson, Ahumada and Watson |
| 169 | */ |
| 170 | 14, 10, 11, 14, 19, 25, 34, 45, |
| 171 | 10, 11, 11, 12, 15, 20, 26, 33, |
| 172 | 11, 11, 15, 18, 21, 25, 31, 38, |
| 173 | 14, 12, 18, 24, 28, 33, 39, 47, |
| 174 | 19, 15, 21, 28, 36, 43, 51, 59, |
| 175 | 25, 20, 25, 33, 43, 54, 64, 74, |
| 176 | 34, 26, 31, 39, 51, 64, 77, 91, |
| 177 | 45, 33, 38, 47, 59, 74, 91, 108 |
| 178 | } |
| 179 | }; |
| 180 |
| 181 | static const unsigned int std_chrominance_quant_tbl[9][DCTSIZE2] = { |
| 182 | { |
| 183 | /* JPEG Annex K |
| 184 | */ |
| 185 | 17, 18, 24, 47, 99, 99, 99, 99, |
| 186 | 18, 21, 26, 66, 99, 99, 99, 99, |
| 187 | 24, 26, 56, 99, 99, 99, 99, 99, |
| 188 | 47, 66, 99, 99, 99, 99, 99, 99, |
| 189 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 190 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 191 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 192 | 99, 99, 99, 99, 99, 99, 99, 99 |
| 193 | }, |
| 194 | { |
| 195 | /* flat |
| 196 | */ |
| 197 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 198 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 199 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 200 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 201 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 202 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 203 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 204 | 16, 16, 16, 16, 16, 16, 16, 16 |
| 205 | }, |
| 206 | { |
| 207 | 8, 12, 15, 15, 86, 96, 96, 98, |
| 208 | 13, 13, 15, 26, 90, 96, 99, 98, |
| 209 | 12, 15, 18, 96, 99, 99, 99, 99, |
| 210 | 17, 16, 90, 96, 99, 99, 99, 99, |
| 211 | 96, 96, 99, 99, 99, 99, 99, 99, |
| 212 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 213 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 214 | 99, 99, 99, 99, 99, 99, 99, 99 |
| 215 | }, |
| 216 | { |
| 217 | /* From http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20333&p=98008#p98008 |
| 218 | */ |
| 219 | 16, 16, 16, 18, 25, 37, 56, 85, |
| 220 | 16, 17, 20, 27, 34, 40, 53, 75, |
| 221 | 16, 20, 24, 31, 43, 62, 91, 135, |
| 222 | 18, 27, 31, 40, 53, 74, 106, 156, |
| 223 | 25, 34, 43, 53, 69, 94, 131, 189, |
| 224 | 37, 40, 62, 74, 94, 124, 169, 238, |
| 225 | 56, 53, 91, 106, 131, 169, 226, 311, |
| 226 | 85, 75, 135, 156, 189, 238, 311, 418 |
| 227 | }, |
| 228 | { |
| 229 | 9, 10, 17, 19, 62, 89, 91, 97, |
| 230 | 12, 13, 18, 29, 84, 91, 88, 98, |
| 231 | 14, 19, 29, 93, 95, 95, 98, 97, |
| 232 | 20, 26, 84, 88, 95, 95, 98, 94, |
| 233 | 26, 86, 91, 93, 97, 99, 98, 99, |
| 234 | 99, 100, 98, 99, 99, 99, 99, 99, |
| 235 | 99, 99, 99, 99, 99, 99, 99, 99, |
| 236 | 97, 97, 99, 99, 99, 99, 97, 99 |
| 237 | }, |
| 238 | { |
| 239 | /* Relevance of human vision to JPEG-DCT compression (1992) Klein, Silverstein and Carney. |
| 240 | * Copied from luma |
| 241 | */ |
| 242 | 10, 12, 14, 19, 26, 38, 57, 86, |
| 243 | 12, 18, 21, 28, 35, 41, 54, 76, |
| 244 | 14, 21, 25, 32, 44, 63, 92, 136, |
| 245 | 19, 28, 32, 41, 54, 75, 107, 157, |
| 246 | 26, 35, 44, 54, 70, 95, 132, 190, |
| 247 | 38, 41, 63, 75, 95, 125, 170, 239, |
| 248 | 57, 54, 92, 107, 132, 170, 227, 312, |
| 249 | 86, 76, 136, 157, 190, 239, 312, 419 |
| 250 | }, |
| 251 | { |
| 252 | /* DCTune perceptual optimization of compressed dental X-Rays (1997) Watson, Taylor, Borthwick |
| 253 | * Copied from luma |
| 254 | */ |
| 255 | 7, 8, 10, 14, 23, 44, 95, 241, |
| 256 | 8, 8, 11, 15, 25, 47, 102, 255, |
| 257 | 10, 11, 13, 19, 31, 58, 127, 255, |
| 258 | 14, 15, 19, 27, 44, 83, 181, 255, |
| 259 | 23, 25, 31, 44, 72, 136, 255, 255, |
| 260 | 44, 47, 58, 83, 136, 255, 255, 255, |
| 261 | 95, 102, 127, 181, 255, 255, 255, 255, |
| 262 | 241, 255, 255, 255, 255, 255, 255, 255 |
| 263 | }, |
| 264 | { |
| 265 | /* A visual detection model for DCT coefficient quantization (12/9/93) Ahumada, Watson, Peterson |
| 266 | * Copied from luma |
| 267 | */ |
| 268 | 15, 11, 11, 12, 15, 19, 25, 32, |
| 269 | 11, 13, 10, 10, 12, 15, 19, 24, |
| 270 | 11, 10, 14, 14, 16, 18, 22, 27, |
| 271 | 12, 10, 14, 18, 21, 24, 28, 33, |
| 272 | 15, 12, 16, 21, 26, 31, 36, 42, |
| 273 | 19, 15, 18, 24, 31, 38, 45, 53, |
| 274 | 25, 19, 22, 28, 36, 45, 55, 65, |
| 275 | 32, 24, 27, 33, 42, 53, 65, 77 |
| 276 | }, |
| 277 | { |
| 278 | /* An improved detection model for DCT coefficient quantization (1993) Peterson, Ahumada and Watson |
| 279 | * Copied from luma |
| 280 | */ |
| 281 | 14, 10, 11, 14, 19, 25, 34, 45, |
| 282 | 10, 11, 11, 12, 15, 20, 26, 33, |
| 283 | 11, 11, 15, 18, 21, 25, 31, 38, |
| 284 | 14, 12, 18, 24, 28, 33, 39, 47, |
| 285 | 19, 15, 21, 28, 36, 43, 51, 59, |
| 286 | 25, 20, 25, 33, 43, 54, 64, 74, |
| 287 | 34, 26, 31, 39, 51, 64, 77, 91, |
| 288 | 45, 33, 38, 47, 59, 74, 91, 108 |
| 289 | } |
| 290 | }; |
| 291 |
| 292 | #if JPEG_LIB_VERSION >= 70 |
| 293 | GLOBAL(void) |
| 294 | jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline) |
| 295 | /* Set or change the 'quality' (quantization) setting, using default tables |
| 296 | * and straight percentage-scaling quality scales. |
| 297 | * This entry point allows different scalings for luminance and chrominance. |
| 298 | */ |
| 299 | { |
| 300 | /* Set up two quantization tables using the specified scaling */ |
| 301 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl[cinfo->master->quant_tbl_master_idx], |
| 302 | cinfo->q_scale_factor[0], force_baseline); |
| 303 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl[cinfo->master->quant_tbl_master_idx], |
| 304 | cinfo->q_scale_factor[1], force_baseline); |
| 305 | } |
| 306 | #endif |
| 307 |
| 308 |
| 309 | GLOBAL(void) |
| 310 | jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor, |
| 311 | boolean force_baseline) |
| 312 | /* Set or change the 'quality' (quantization) setting, using default tables |
| 313 | * and a straight percentage-scaling quality scale. In most cases it's better |
| 314 | * to use jpeg_set_quality (below); this entry point is provided for |
| 315 | * applications that insist on a linear percentage scaling. |
| 316 | */ |
| 317 | { |
| 318 | /* Set up two quantization tables using the specified scaling */ |
| 319 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl[cinfo->master->quant_tbl_master_idx], |
| 320 | scale_factor, force_baseline); |
| 321 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl[cinfo->master->quant_tbl_master_idx], |
| 322 | scale_factor, force_baseline); |
| 323 | } |
| 324 |
| 325 |
| 326 | GLOBAL(int) |
| 327 | jpeg_quality_scaling (int quality) |
| 328 | { |
| 329 | return jpeg_float_quality_scaling(quality); |
| 330 | } |
| 331 |
| 332 | GLOBAL(float) |
| 333 | jpeg_float_quality_scaling(float quality) |
| 334 | /* Convert a user-specified quality rating to a percentage scaling factor |
| 335 | * for an underlying quantization table, using our recommended scaling curve. |
| 336 | * The input 'quality' factor should be 0 (terrible) to 100 (very good). |
| 337 | */ |
| 338 | { |
| 339 | /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */ |
| 340 | if (quality <= 0.f) quality = 1.f; |
| 341 | if (quality > 100.f) quality = 100.f; |
| 342 |
| 343 | /* The basic table is used as-is (scaling 100) for a quality of 50. |
| 344 | * Qualities 50..100 are converted to scaling percentage 200 - 2*Q; |
| 345 | * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table |
| 346 | * to make all the table entries 1 (hence, minimum quantization loss). |
| 347 | * Qualities 1..50 are converted to scaling percentage 5000/Q. |
| 348 | */ |
| 349 | if (quality < 50.f) |
| 350 | quality = 5000.f / quality; |
| 351 | else |
| 352 | quality = 200.f - quality*2.f; |
| 353 |
| 354 | return quality; |
| 355 | } |
| 356 |
| 357 |
| 358 | GLOBAL(void) |
| 359 | jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline) |
| 360 | /* Set or change the 'quality' (quantization) setting, using default tables. |
| 361 | * This is the standard quality-adjusting entry point for typical user |
| 362 | * interfaces; only those who want detailed control over quantization tables |
| 363 | * would use the preceding three routines directly. |
| 364 | */ |
| 365 | { |
| 366 | /* Convert user 0-100 rating to percentage scaling */ |
| 367 | quality = jpeg_quality_scaling(quality); |
| 368 |
| 369 | /* Set up standard quality tables */ |
| 370 | jpeg_set_linear_quality(cinfo, quality, force_baseline); |
| 371 | } |
| 372 |
| 373 |
| 374 | /* |
| 375 | * Default parameter setup for compression. |
| 376 | * |
| 377 | * Applications that don't choose to use this routine must do their |
| 378 | * own setup of all these parameters. Alternately, you can call this |
| 379 | * to establish defaults and then alter parameters selectively. This |
| 380 | * is the recommended approach since, if we add any new parameters, |
| 381 | * your code will still work (they'll be set to reasonable defaults). |
| 382 | */ |
| 383 |
| 384 | GLOBAL(void) |
| 385 | jpeg_set_defaults (j_compress_ptr cinfo) |
| 386 | { |
| 387 | int i; |
| 388 |
| 389 | /* Safety check to ensure start_compress not called yet. */ |
| 390 | if (cinfo->global_state != CSTATE_START) |
| 391 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 392 |
| 393 | /* Allocate comp_info array large enough for maximum component count. |
| 394 | * Array is made permanent in case application wants to compress |
| 395 | * multiple images at same param settings. |
| 396 | */ |
| 397 | if (cinfo->comp_info == NULL) |
| 398 | cinfo->comp_info = (jpeg_component_info *) |
| 399 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 400 | MAX_COMPONENTS * sizeof(jpeg_component_info)); |
| 401 |
| 402 | /* Initialize everything not dependent on the color space */ |
| 403 |
| 404 | #if JPEG_LIB_VERSION >= 70 |
| 405 | cinfo->scale_num = 1; /* 1:1 scaling */ |
| 406 | cinfo->scale_denom = 1; |
| 407 | #endif |
| 408 | cinfo->data_precision = BITS_IN_JSAMPLE; |
| 409 | /* Set up two quantization tables using default quality of 75 */ |
| 410 | jpeg_set_quality(cinfo, 75, TRUE); |
| 411 | /* Set up two Huffman tables */ |
| 412 | std_huff_tables((j_common_ptr) cinfo); |
| 413 |
| 414 | /* Initialize default arithmetic coding conditioning */ |
| 415 | for (i = 0; i < NUM_ARITH_TBLS; i++) { |
| 416 | cinfo->arith_dc_L[i] = 0; |
| 417 | cinfo->arith_dc_U[i] = 1; |
| 418 | cinfo->arith_ac_K[i] = 5; |
| 419 | } |
| 420 |
| 421 | /* Default is no multiple-scan output */ |
| 422 | cinfo->scan_info = NULL; |
| 423 | cinfo->num_scans = 0; |
| 424 |
| 425 | /* Expect normal source image, not raw downsampled data */ |
| 426 | cinfo->raw_data_in = FALSE; |
| 427 |
| 428 | /* Use Huffman coding, not arithmetic coding, by default */ |
| 429 | cinfo->arith_code = FALSE; |
| 430 |
| 431 | #ifdef ENTROPY_OPT_SUPPORTED |
| 432 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) |
| 433 | /* By default, do extra passes to optimize entropy coding */ |
| 434 | cinfo->optimize_coding = TRUE; |
| 435 | else |
| 436 | /* By default, don't do extra passes to optimize entropy coding */ |
| 437 | cinfo->optimize_coding = FALSE; |
| 438 | #else |
| 439 | /* By default, don't do extra passes to optimize entropy coding */ |
| 440 | cinfo->optimize_coding = FALSE; |
| 441 | #endif |
| 442 |
| 443 | /* The standard Huffman tables are only valid for 8-bit data precision. |
| 444 | * If the precision is higher, force optimization on so that usable |
| 445 | * tables will be computed. This test can be removed if default tables |
| 446 | * are supplied that are valid for the desired precision. |
| 447 | */ |
| 448 | if (cinfo->data_precision > 8) |
| 449 | cinfo->optimize_coding = TRUE; |
| 450 |
| 451 | /* By default, use the simpler non-cosited sampling alignment */ |
| 452 | cinfo->CCIR601_sampling = FALSE; |
| 453 |
| 454 | #if JPEG_LIB_VERSION >= 70 |
| 455 | /* By default, apply fancy downsampling */ |
| 456 | cinfo->do_fancy_downsampling = TRUE; |
| 457 | #endif |
| 458 |
| 459 | cinfo->master->overshoot_deringing = |
| 460 | cinfo->master->compress_profile == JCP_MAX_COMPRESSION; |
| 461 |
| 462 | /* No input smoothing */ |
| 463 | cinfo->smoothing_factor = 0; |
| 464 |
| 465 | /* DCT algorithm preference */ |
| 466 | cinfo->dct_method = JDCT_DEFAULT; |
| 467 |
| 468 | /* No restart markers */ |
| 469 | cinfo->restart_interval = 0; |
| 470 | cinfo->restart_in_rows = 0; |
| 471 |
| 472 | /* Fill in default JFIF marker parameters. Note that whether the marker |
| 473 | * will actually be written is determined by jpeg_set_colorspace. |
| 474 | * |
| 475 | * By default, the library emits JFIF version code 1.01. |
| 476 | * An application that wants to emit JFIF 1.02 extension markers should set |
| 477 | * JFIF_minor_version to 2. We could probably get away with just defaulting |
| 478 | * to 1.02, but there may still be some decoders in use that will complain |
| 479 | * about that; saying 1.01 should minimize compatibility problems. |
| 480 | */ |
| 481 | cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */ |
| 482 | cinfo->JFIF_minor_version = 1; |
| 483 | cinfo->density_unit = 0; /* Pixel size is unknown by default */ |
| 484 | cinfo->X_density = 1; /* Pixel aspect ratio is square by default */ |
| 485 | cinfo->Y_density = 1; |
| 486 |
| 487 | /* Choose JPEG colorspace based on input space, set defaults accordingly */ |
| 488 |
| 489 | jpeg_default_colorspace(cinfo); |
| 490 |
| 491 | cinfo->master->dc_scan_opt_mode = 1; |
| 492 |
| 493 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 494 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) { |
| 495 | cinfo->master->optimize_scans = TRUE; |
| 496 | jpeg_simple_progression(cinfo); |
| 497 | } else |
| 498 | cinfo->master->optimize_scans = FALSE; |
| 499 | #endif |
| 500 |
| 501 | cinfo->master->trellis_quant = |
| 502 | cinfo->master->compress_profile == JCP_MAX_COMPRESSION; |
| 503 | cinfo->master->lambda_log_scale1 = 14.75; |
| 504 | cinfo->master->lambda_log_scale2 = 16.5; |
| 505 | cinfo->master->quant_tbl_master_idx = |
| 506 | cinfo->master->compress_profile == JCP_MAX_COMPRESSION ? 3 : 0; |
| 507 |
| 508 | cinfo->master->use_lambda_weight_tbl = TRUE; |
| 509 | cinfo->master->use_scans_in_trellis = FALSE; |
| 510 | cinfo->master->trellis_freq_split = 8; |
| 511 | cinfo->master->trellis_num_loops = 1; |
| 512 | cinfo->master->trellis_q_opt = FALSE; |
| 513 | cinfo->master->trellis_quant_dc = TRUE; |
| 514 | cinfo->master->trellis_delta_dc_weight = 0.0; |
| 515 | } |
| 516 |
| 517 |
| 518 | /* |
| 519 | * Select an appropriate JPEG colorspace for in_color_space. |
| 520 | */ |
| 521 |
| 522 | GLOBAL(void) |
| 523 | jpeg_default_colorspace (j_compress_ptr cinfo) |
| 524 | { |
| 525 | switch (cinfo->in_color_space) { |
| 526 | case JCS_GRAYSCALE: |
| 527 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 528 | break; |
| 529 | case JCS_RGB: |
| 530 | case JCS_EXT_RGB: |
| 531 | case JCS_EXT_RGBX: |
| 532 | case JCS_EXT_BGR: |
| 533 | case JCS_EXT_BGRX: |
| 534 | case JCS_EXT_XBGR: |
| 535 | case JCS_EXT_XRGB: |
| 536 | case JCS_EXT_RGBA: |
| 537 | case JCS_EXT_BGRA: |
| 538 | case JCS_EXT_ABGR: |
| 539 | case JCS_EXT_ARGB: |
| 540 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
| 541 | break; |
| 542 | case JCS_YCbCr: |
| 543 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
| 544 | break; |
| 545 | case JCS_CMYK: |
| 546 | jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */ |
| 547 | break; |
| 548 | case JCS_YCCK: |
| 549 | jpeg_set_colorspace(cinfo, JCS_YCCK); |
| 550 | break; |
| 551 | case JCS_UNKNOWN: |
| 552 | jpeg_set_colorspace(cinfo, JCS_UNKNOWN); |
| 553 | break; |
| 554 | default: |
| 555 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| 556 | } |
| 557 | } |
| 558 |
| 559 |
| 560 | /* |
| 561 | * Set the JPEG colorspace, and choose colorspace-dependent default values. |
| 562 | */ |
| 563 |
| 564 | GLOBAL(void) |
| 565 | jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace) |
| 566 | { |
| 567 | jpeg_component_info *compptr; |
| 568 | int ci; |
| 569 |
| 570 | #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \ |
| 571 | (compptr = &cinfo->comp_info[index], \ |
| 572 | compptr->component_id = (id), \ |
| 573 | compptr->h_samp_factor = (hsamp), \ |
| 574 | compptr->v_samp_factor = (vsamp), \ |
| 575 | compptr->quant_tbl_no = (quant), \ |
| 576 | compptr->dc_tbl_no = (dctbl), \ |
| 577 | compptr->ac_tbl_no = (actbl) ) |
| 578 |
| 579 | /* Safety check to ensure start_compress not called yet. */ |
| 580 | if (cinfo->global_state != CSTATE_START) |
| 581 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 582 |
| 583 | /* For all colorspaces, we use Q and Huff tables 0 for luminance components, |
| 584 | * tables 1 for chrominance components. |
| 585 | */ |
| 586 |
| 587 | cinfo->jpeg_color_space = colorspace; |
| 588 |
| 589 | cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */ |
| 590 | cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */ |
| 591 |
| 592 | switch (colorspace) { |
| 593 | case JCS_GRAYSCALE: |
| 594 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
| 595 | cinfo->num_components = 1; |
| 596 | /* JFIF specifies component ID 1 */ |
| 597 | SET_COMP(0, 1, 1,1, 0, 0,0); |
| 598 | break; |
| 599 | case JCS_RGB: |
| 600 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */ |
| 601 | cinfo->num_components = 3; |
| 602 | SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0); |
| 603 | SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0); |
| 604 | SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0); |
| 605 | break; |
| 606 | case JCS_YCbCr: |
| 607 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
| 608 | cinfo->num_components = 3; |
| 609 | /* JFIF specifies component IDs 1,2,3 */ |
| 610 | /* We default to 2x2 subsamples of chrominance */ |
| 611 | SET_COMP(0, 1, 2,2, 0, 0,0); |
| 612 | SET_COMP(1, 2, 1,1, 1, 1,1); |
| 613 | SET_COMP(2, 3, 1,1, 1, 1,1); |
| 614 | break; |
| 615 | case JCS_CMYK: |
| 616 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */ |
| 617 | cinfo->num_components = 4; |
| 618 | SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0); |
| 619 | SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0); |
| 620 | SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0); |
| 621 | SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0); |
| 622 | break; |
| 623 | case JCS_YCCK: |
| 624 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */ |
| 625 | cinfo->num_components = 4; |
| 626 | SET_COMP(0, 1, 2,2, 0, 0,0); |
| 627 | SET_COMP(1, 2, 1,1, 1, 1,1); |
| 628 | SET_COMP(2, 3, 1,1, 1, 1,1); |
| 629 | SET_COMP(3, 4, 2,2, 0, 0,0); |
| 630 | break; |
| 631 | case JCS_UNKNOWN: |
| 632 | cinfo->num_components = cinfo->input_components; |
| 633 | if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS) |
| 634 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
| 635 | MAX_COMPONENTS); |
| 636 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 637 | SET_COMP(ci, ci, 1,1, 0, 0,0); |
| 638 | } |
| 639 | break; |
| 640 | default: |
| 641 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 642 | } |
| 643 | } |
| 644 |
| 645 |
| 646 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 647 |
| 648 | LOCAL(jpeg_scan_info *) |
| 649 | fill_a_scan(jpeg_scan_info *scanptr, int ci, int Ss, int Se, int Ah, int Al) |
| 650 | /* Support routine: generate one scan for specified component */ |
| 651 | { |
| 652 | scanptr->comps_in_scan = 1; |
| 653 | scanptr->component_index[0] = ci; |
| 654 | scanptr->Ss = Ss; |
| 655 | scanptr->Se = Se; |
| 656 | scanptr->Ah = Ah; |
| 657 | scanptr->Al = Al; |
| 658 | scanptr++; |
| 659 | return scanptr; |
| 660 | } |
| 661 |
| 662 | LOCAL(jpeg_scan_info *) |
| 663 | fill_a_scan_pair (jpeg_scan_info * scanptr, int ci, |
| 664 | int Ss, int Se, int Ah, int Al) |
| 665 | /* Support routine: generate one scan for pair of components */ |
| 666 | { |
| 667 | scanptr->comps_in_scan = 2; |
| 668 | scanptr->component_index[0] = ci; |
| 669 | scanptr->component_index[1] = ci + 1; |
| 670 | scanptr->Ss = Ss; |
| 671 | scanptr->Se = Se; |
| 672 | scanptr->Ah = Ah; |
| 673 | scanptr->Al = Al; |
| 674 | scanptr++; |
| 675 | return scanptr; |
| 676 | } |
| 677 |
| 678 | LOCAL(jpeg_scan_info *) |
| 679 | fill_scans (jpeg_scan_info *scanptr, int ncomps, |
| 680 | int Ss, int Se, int Ah, int Al) |
| 681 | /* Support routine: generate one scan for each component */ |
| 682 | { |
| 683 | int ci; |
| 684 |
| 685 | for (ci = 0; ci < ncomps; ci++) { |
| 686 | scanptr->comps_in_scan = 1; |
| 687 | scanptr->component_index[0] = ci; |
| 688 | scanptr->Ss = Ss; |
| 689 | scanptr->Se = Se; |
| 690 | scanptr->Ah = Ah; |
| 691 | scanptr->Al = Al; |
| 692 | scanptr++; |
| 693 | } |
| 694 | return scanptr; |
| 695 | } |
| 696 |
| 697 | LOCAL(jpeg_scan_info *) |
| 698 | fill_dc_scans (jpeg_scan_info *scanptr, int ncomps, int Ah, int Al) |
| 699 | /* Support routine: generate interleaved DC scan if possible, else N scans */ |
| 700 | { |
| 701 | int ci; |
| 702 |
| 703 | if (ncomps <= MAX_COMPS_IN_SCAN) { |
| 704 | /* Single interleaved DC scan */ |
| 705 | scanptr->comps_in_scan = ncomps; |
| 706 | for (ci = 0; ci < ncomps; ci++) |
| 707 | scanptr->component_index[ci] = ci; |
| 708 | scanptr->Ss = scanptr->Se = 0; |
| 709 | scanptr->Ah = Ah; |
| 710 | scanptr->Al = Al; |
| 711 | scanptr++; |
| 712 | } else { |
| 713 | /* Noninterleaved DC scan for each component */ |
| 714 | scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al); |
| 715 | } |
| 716 | return scanptr; |
| 717 | } |
| 718 |
| 719 |
| 720 | /* |
| 721 | * List of scans to be tested |
| 722 | * cinfo->num_components and cinfo->jpeg_color_space must be correct. |
| 723 | */ |
| 724 |
| 725 | LOCAL(boolean) |
| 726 | jpeg_search_progression (j_compress_ptr cinfo) |
| 727 | { |
| 728 | int ncomps = cinfo->num_components; |
| 729 | int nscans; |
| 730 | jpeg_scan_info * scanptr; |
| 731 | int Al; |
| 732 | int frequency_split[] = { 2, 8, 5, 12, 18 }; |
| 733 | int i; |
| 734 |
| 735 | /* Safety check to ensure start_compress not called yet. */ |
| 736 | if (cinfo->global_state != CSTATE_START) |
| 737 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 738 |
| 739 | /* Figure space needed for script. Calculation must match code below! */ |
| 740 | if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { |
| 741 | /* Custom script for YCbCr color images. */ |
| 742 | nscans = 64; |
| 743 | } else if (ncomps == 1) { |
| 744 | nscans = 23; |
| 745 | } else { |
| 746 | cinfo->master->num_scans_luma = 0; |
| 747 | return FALSE; |
| 748 | } |
| 749 |
| 750 | /* Allocate space for script. |
| 751 | * We need to put it in the permanent pool in case the application performs |
| 752 | * multiple compressions without changing the settings. To avoid a memory |
| 753 | * leak if jpeg_simple_progression is called repeatedly for the same JPEG |
| 754 | * object, we try to re-use previously allocated space, and we allocate |
| 755 | * enough space to handle YCbCr even if initially asked for grayscale. |
| 756 | */ |
| 757 | if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) { |
| 758 | cinfo->script_space_size = MAX(nscans, 64); |
| 759 | cinfo->script_space = (jpeg_scan_info *) |
| 760 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 761 | cinfo->script_space_size * sizeof(jpeg_scan_info)); |
| 762 | } |
| 763 | scanptr = cinfo->script_space; |
| 764 | cinfo->scan_info = scanptr; |
| 765 | cinfo->num_scans = nscans; |
| 766 |
| 767 | cinfo->master->Al_max_luma = 3; |
| 768 | cinfo->master->num_scans_luma_dc = 1; |
| 769 | cinfo->master->num_frequency_splits = 5; |
| 770 | cinfo->master->num_scans_luma = |
| 771 | cinfo->master->num_scans_luma_dc + (3 * cinfo->master->Al_max_luma + 2) + |
| 772 | (2 * cinfo->master->num_frequency_splits + 1); |
| 773 |
| 774 | /* 23 scans for luma */ |
| 775 | /* 1 scan for DC */ |
| 776 | /* 11 scans to determine successive approximation */ |
| 777 | /* 11 scans to determine frequency approximation */ |
| 778 | /* after 12 scans need to update following 11 */ |
| 779 | /* after 23 scans need to determine which to keep */ |
| 780 | /* last 4 done conditionally */ |
| 781 |
| 782 | /* luma DC by itself */ |
| 783 | if (cinfo->master->dc_scan_opt_mode == 0) |
| 784 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 0); |
| 785 | else |
| 786 | scanptr = fill_dc_scans(scanptr, 1, 0, 0); |
| 787 |
| 788 | scanptr = fill_a_scan(scanptr, 0, 1, 8, 0, 0); |
| 789 | scanptr = fill_a_scan(scanptr, 0, 9, 63, 0, 0); |
| 790 |
| 791 | for (Al = 0; Al < cinfo->master->Al_max_luma; Al++) { |
| 792 | scanptr = fill_a_scan(scanptr, 0, 1, 63, Al+1, Al); |
| 793 | scanptr = fill_a_scan(scanptr, 0, 1, 8, 0, Al+1); |
| 794 | scanptr = fill_a_scan(scanptr, 0, 9, 63, 0, Al+1); |
| 795 | } |
| 796 |
| 797 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 0, 0); |
| 798 |
| 799 | for (i = 0; i < cinfo->master->num_frequency_splits; i++) { |
| 800 | scanptr = fill_a_scan(scanptr, 0, 1, frequency_split[i], 0, 0); |
| 801 | scanptr = fill_a_scan(scanptr, 0, frequency_split[i]+1, 63, 0, 0); |
| 802 | } |
| 803 |
| 804 | if (ncomps == 1) { |
| 805 | cinfo->master->Al_max_chroma = 0; |
| 806 | cinfo->master->num_scans_chroma_dc = 0; |
| 807 | } else { |
| 808 | cinfo->master->Al_max_chroma = 2; |
| 809 | cinfo->master->num_scans_chroma_dc = 3; |
| 810 | /* 41 scans for chroma */ |
| 811 |
| 812 | /* chroma DC combined */ |
| 813 | scanptr = fill_a_scan_pair(scanptr, 1, 0, 0, 0, 0); |
| 814 | /* chroma DC separate */ |
| 815 | scanptr = fill_a_scan(scanptr, 1, 0, 0, 0, 0); |
| 816 | scanptr = fill_a_scan(scanptr, 2, 0, 0, 0, 0); |
| 817 |
| 818 | scanptr = fill_a_scan(scanptr, 1, 1, 8, 0, 0); |
| 819 | scanptr = fill_a_scan(scanptr, 1, 9, 63, 0, 0); |
| 820 | scanptr = fill_a_scan(scanptr, 2, 1, 8, 0, 0); |
| 821 | scanptr = fill_a_scan(scanptr, 2, 9, 63, 0, 0); |
| 822 |
| 823 | for (Al = 0; Al < cinfo->master->Al_max_chroma; Al++) { |
| 824 | scanptr = fill_a_scan(scanptr, 1, 1, 63, Al+1, Al); |
| 825 | scanptr = fill_a_scan(scanptr, 2, 1, 63, Al+1, Al); |
| 826 | scanptr = fill_a_scan(scanptr, 1, 1, 8, 0, Al+1); |
| 827 | scanptr = fill_a_scan(scanptr, 1, 9, 63, 0, Al+1); |
| 828 | scanptr = fill_a_scan(scanptr, 2, 1, 8, 0, Al+1); |
| 829 | scanptr = fill_a_scan(scanptr, 2, 9, 63, 0, Al+1); |
| 830 | } |
| 831 |
| 832 | scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 0); |
| 833 | scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 0); |
| 834 |
| 835 | for (i = 0; i < cinfo->master->num_frequency_splits; i++) { |
| 836 | scanptr = fill_a_scan(scanptr, 1, 1, frequency_split[i], 0, 0); |
| 837 | scanptr = fill_a_scan(scanptr, 1, frequency_split[i]+1, 63, 0, 0); |
| 838 | scanptr = fill_a_scan(scanptr, 2, 1, frequency_split[i], 0, 0); |
| 839 | scanptr = fill_a_scan(scanptr, 2, frequency_split[i]+1, 63, 0, 0); |
| 840 | } |
| 841 | } |
| 842 |
| 843 | return TRUE; |
| 844 | } |
| 845 |
| 846 | /* |
| 847 | * Create a recommended progressive-JPEG script. |
| 848 | * cinfo->num_components and cinfo->jpeg_color_space must be correct. |
| 849 | */ |
| 850 |
| 851 | GLOBAL(void) |
| 852 | jpeg_simple_progression (j_compress_ptr cinfo) |
| 853 | { |
| 854 | int ncomps; |
| 855 | int nscans; |
| 856 | jpeg_scan_info *scanptr; |
| 857 |
| 858 | if (cinfo->master->optimize_scans) { |
| 859 | if (jpeg_search_progression(cinfo) == TRUE) |
| 860 | return; |
| 861 | } |
| 862 |
| 863 | /* Safety check to ensure start_compress not called yet. */ |
| 864 | if (cinfo->global_state != CSTATE_START) |
| 865 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 866 |
| 867 | /* Figure space needed for script. Calculation must match code below! */ |
| 868 | ncomps = cinfo->num_components; |
| 869 | if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { |
| 870 | /* Custom script for YCbCr color images. */ |
| 871 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) { |
| 872 | if (cinfo->master->dc_scan_opt_mode == 0) { |
| 873 | nscans = 9; /* 1 DC scan for all components */ |
| 874 | } else if (cinfo->master->dc_scan_opt_mode == 1) { |
| 875 | nscans = 11; /* 1 DC scan for each component */ |
| 876 | } else { |
| 877 | nscans = 10; /* 1 DC scan for luminance and 1 DC scan for chroma */ |
| 878 | } |
| 879 | } else { |
| 880 | nscans = 10; /* 2 DC scans and 8 AC scans */ |
| 881 | } |
| 882 | } else { |
| 883 | /* All-purpose script for other color spaces. */ |
| 884 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) { |
| 885 | if (ncomps > MAX_COMPS_IN_SCAN) |
| 886 | nscans = 5 * ncomps; /* 2 DC + 4 AC scans per component */ |
| 887 | else |
| 888 | nscans = 1 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */ |
| 889 | } else { |
| 890 | if (ncomps > MAX_COMPS_IN_SCAN) |
| 891 | nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */ |
| 892 | else |
| 893 | nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */ |
| 894 | } |
| 895 | } |
| 896 |
| 897 | /* Allocate space for script. |
| 898 | * We need to put it in the permanent pool in case the application performs |
| 899 | * multiple compressions without changing the settings. To avoid a memory |
| 900 | * leak if jpeg_simple_progression is called repeatedly for the same JPEG |
| 901 | * object, we try to re-use previously allocated space, and we allocate |
| 902 | * enough space to handle YCbCr even if initially asked for grayscale. |
| 903 | */ |
| 904 | if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) { |
| 905 | cinfo->script_space_size = MAX(nscans, 10); |
| 906 | cinfo->script_space = (jpeg_scan_info *) |
| 907 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 908 | cinfo->script_space_size * sizeof(jpeg_scan_info)); |
| 909 | } |
| 910 | scanptr = cinfo->script_space; |
| 911 | cinfo->scan_info = scanptr; |
| 912 | cinfo->num_scans = nscans; |
| 913 |
| 914 | if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { |
| 915 | /* Custom script for YCbCr color images. */ |
| 916 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) { |
| 917 | /* scan defined in jpeg_scan_rgb.txt in jpgcrush */ |
| 918 | /* Initial DC scan */ |
| 919 | if (cinfo->master->dc_scan_opt_mode == 0) { |
| 920 | /* 1 DC scan for all components */ |
| 921 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 0); |
| 922 | } else if (cinfo->master->dc_scan_opt_mode == 1) { |
| 923 | /* 1 DC scan for each component */ |
| 924 | scanptr = fill_a_scan(scanptr, 0, 0, 0, 0, 0); |
| 925 | scanptr = fill_a_scan(scanptr, 1, 0, 0, 0, 0); |
| 926 | scanptr = fill_a_scan(scanptr, 2, 0, 0, 0, 0); |
| 927 | } else { |
| 928 | /* 1 DC scan for luminance and 1 DC scan for chroma */ |
| 929 | scanptr = fill_dc_scans(scanptr, 1, 0, 0); |
| 930 | scanptr = fill_a_scan_pair(scanptr, 1, 0, 0, 0, 0); |
| 931 | } |
| 932 | /* Low frequency AC scans */ |
| 933 | scanptr = fill_a_scan(scanptr, 0, 1, 8, 0, 2); |
| 934 | scanptr = fill_a_scan(scanptr, 1, 1, 8, 0, 0); |
| 935 | scanptr = fill_a_scan(scanptr, 2, 1, 8, 0, 0); |
| 936 | /* Complete spectral selection for luma AC */ |
| 937 | scanptr = fill_a_scan(scanptr, 0, 9, 63, 0, 2); |
| 938 | /* Finish luma AC successive approximation */ |
| 939 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1); |
| 940 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0); |
| 941 | /* Complete spectral selection for chroma AC */ |
| 942 | scanptr = fill_a_scan(scanptr, 1, 9, 63, 0, 0); |
| 943 | scanptr = fill_a_scan(scanptr, 2, 9, 63, 0, 0); |
| 944 | } else { |
| 945 | /* Initial DC scan */ |
| 946 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); |
| 947 | /* Initial AC scan: get some luma data out in a hurry */ |
| 948 | scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2); |
| 949 | /* Chroma data is too small to be worth expending many scans on */ |
| 950 | scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1); |
| 951 | scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1); |
| 952 | /* Complete spectral selection for luma AC */ |
| 953 | scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2); |
| 954 | /* Refine next bit of luma AC */ |
| 955 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1); |
| 956 | /* Finish DC successive approximation */ |
| 957 | scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); |
| 958 | /* Finish AC successive approximation */ |
| 959 | scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0); |
| 960 | scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0); |
| 961 | /* Luma bottom bit comes last since it's usually largest scan */ |
| 962 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0); |
| 963 | } |
| 964 | } else { |
| 965 | /* All-purpose script for other color spaces. */ |
| 966 | if (cinfo->master->compress_profile == JCP_MAX_COMPRESSION) { |
| 967 | /* scan defined in jpeg_scan_bw.txt in jpgcrush */ |
| 968 | /* DC component, no successive approximation */ |
| 969 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 0); |
| 970 | /* Successive approximation first pass */ |
| 971 | scanptr = fill_scans(scanptr, ncomps, 1, 8, 0, 2); |
| 972 | scanptr = fill_scans(scanptr, ncomps, 9, 63, 0, 2); |
| 973 | /* Successive approximation second pass */ |
| 974 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1); |
| 975 | /* Successive approximation final pass */ |
| 976 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0); |
| 977 | } else { |
| 978 | /* Successive approximation first pass */ |
| 979 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); |
| 980 | scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2); |
| 981 | scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2); |
| 982 | /* Successive approximation second pass */ |
| 983 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1); |
| 984 | /* Successive approximation final pass */ |
| 985 | scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); |
| 986 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 |
| 991 | #endif /* C_PROGRESSIVE_SUPPORTED */ |
| 992 |