1 /* 2 * jccolor.c 3 * 4 * This file was part of the Independent JPEG Group's software: 5 * Copyright (C) 1991-1996, Thomas G. Lane. 6 * libjpeg-turbo Modifications: 7 * Copyright 2009 Pierre Ossman <ossman (at) cendio.se> for Cendio AB 8 * Copyright (C) 2009-2012, D. R. Commander. 9 * For conditions of distribution and use, see the accompanying README file. 10 * 11 * This file contains input colorspace conversion routines. 12 */ 13 14 #define JPEG_INTERNALS 15 #include "jinclude.h" 16 #include "jpeglib.h" 17 #include "jsimd.h" 18 #include "config.h" 19 20 21 /* Private subobject */ 22 23 typedef struct { 24 struct jpeg_color_converter pub; /* public fields */ 25 26 /* Private state for RGB->YCC conversion */ 27 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ 28 } my_color_converter; 29 30 typedef my_color_converter * my_cconvert_ptr; 31 32 33 /**************** RGB -> YCbCr conversion: most common case **************/ 34 35 /* 36 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are 37 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. 38 * The conversion equations to be implemented are therefore 39 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B 40 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE 41 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE 42 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) 43 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, 44 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and 45 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) 46 * were not represented exactly. Now we sacrifice exact representation of 47 * maximum red and maximum blue in order to get exact grayscales. 48 * 49 * To avoid floating-point arithmetic, we represent the fractional constants 50 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide 51 * the products by 2^16, with appropriate rounding, to get the correct answer. 52 * 53 * For even more speed, we avoid doing any multiplications in the inner loop 54 * by precalculating the constants times R,G,B for all possible values. 55 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); 56 * for 12-bit samples it is still acceptable. It's not very reasonable for 57 * 16-bit samples, but if you want lossless storage you shouldn't be changing 58 * colorspace anyway. 59 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included 60 * in the tables to save adding them separately in the inner loop. 61 */ 62 63 #define SCALEBITS 16 /* speediest right-shift on some machines */ 64 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) 65 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) 66 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) 67 68 /* We allocate one big table and divide it up into eight parts, instead of 69 * doing eight alloc_small requests. This lets us use a single table base 70 * address, which can be held in a register in the inner loops on many 71 * machines (more than can hold all eight addresses, anyway). 72 */ 73 74 #define R_Y_OFF 0 /* offset to R => Y section */ 75 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ 76 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ 77 #define R_CB_OFF (3*(MAXJSAMPLE+1)) 78 #define G_CB_OFF (4*(MAXJSAMPLE+1)) 79 #define B_CB_OFF (5*(MAXJSAMPLE+1)) 80 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ 81 #define G_CR_OFF (6*(MAXJSAMPLE+1)) 82 #define B_CR_OFF (7*(MAXJSAMPLE+1)) 83 #define TABLE_SIZE (8*(MAXJSAMPLE+1)) 84 85 86 /* Include inline routines for colorspace extensions */ 87 88 #include "jccolext.c" 89 #undef RGB_RED 90 #undef RGB_GREEN 91 #undef RGB_BLUE 92 #undef RGB_PIXELSIZE 93 94 #define RGB_RED EXT_RGB_RED 95 #define RGB_GREEN EXT_RGB_GREEN 96 #define RGB_BLUE EXT_RGB_BLUE 97 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE 98 #define rgb_ycc_convert_internal extrgb_ycc_convert_internal 99 #define rgb_gray_convert_internal extrgb_gray_convert_internal 100 #define rgb_rgb_convert_internal extrgb_rgb_convert_internal 101 #include "jccolext.c" 102 #undef RGB_RED 103 #undef RGB_GREEN 104 #undef RGB_BLUE 105 #undef RGB_PIXELSIZE 106 #undef rgb_ycc_convert_internal 107 #undef rgb_gray_convert_internal 108 #undef rgb_rgb_convert_internal 109 110 #define RGB_RED EXT_RGBX_RED 111 #define RGB_GREEN EXT_RGBX_GREEN 112 #define RGB_BLUE EXT_RGBX_BLUE 113 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE 114 #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal 115 #define rgb_gray_convert_internal extrgbx_gray_convert_internal 116 #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal 117 #include "jccolext.c" 118 #undef RGB_RED 119 #undef RGB_GREEN 120 #undef RGB_BLUE 121 #undef RGB_PIXELSIZE 122 #undef rgb_ycc_convert_internal 123 #undef rgb_gray_convert_internal 124 #undef rgb_rgb_convert_internal 125 126 #define RGB_RED EXT_BGR_RED 127 #define RGB_GREEN EXT_BGR_GREEN 128 #define RGB_BLUE EXT_BGR_BLUE 129 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE 130 #define rgb_ycc_convert_internal extbgr_ycc_convert_internal 131 #define rgb_gray_convert_internal extbgr_gray_convert_internal 132 #define rgb_rgb_convert_internal extbgr_rgb_convert_internal 133 #include "jccolext.c" 134 #undef RGB_RED 135 #undef RGB_GREEN 136 #undef RGB_BLUE 137 #undef RGB_PIXELSIZE 138 #undef rgb_ycc_convert_internal 139 #undef rgb_gray_convert_internal 140 #undef rgb_rgb_convert_internal 141 142 #define RGB_RED EXT_BGRX_RED 143 #define RGB_GREEN EXT_BGRX_GREEN 144 #define RGB_BLUE EXT_BGRX_BLUE 145 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE 146 #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal 147 #define rgb_gray_convert_internal extbgrx_gray_convert_internal 148 #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal 149 #include "jccolext.c" 150 #undef RGB_RED 151 #undef RGB_GREEN 152 #undef RGB_BLUE 153 #undef RGB_PIXELSIZE 154 #undef rgb_ycc_convert_internal 155 #undef rgb_gray_convert_internal 156 #undef rgb_rgb_convert_internal 157 158 #define RGB_RED EXT_XBGR_RED 159 #define RGB_GREEN EXT_XBGR_GREEN 160 #define RGB_BLUE EXT_XBGR_BLUE 161 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE 162 #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal 163 #define rgb_gray_convert_internal extxbgr_gray_convert_internal 164 #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal 165 #include "jccolext.c" 166 #undef RGB_RED 167 #undef RGB_GREEN 168 #undef RGB_BLUE 169 #undef RGB_PIXELSIZE 170 #undef rgb_ycc_convert_internal 171 #undef rgb_gray_convert_internal 172 #undef rgb_rgb_convert_internal 173 174 #define RGB_RED EXT_XRGB_RED 175 #define RGB_GREEN EXT_XRGB_GREEN 176 #define RGB_BLUE EXT_XRGB_BLUE 177 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE 178 #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal 179 #define rgb_gray_convert_internal extxrgb_gray_convert_internal 180 #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal 181 #include "jccolext.c" 182 #undef RGB_RED 183 #undef RGB_GREEN 184 #undef RGB_BLUE 185 #undef RGB_PIXELSIZE 186 #undef rgb_ycc_convert_internal 187 #undef rgb_gray_convert_internal 188 #undef rgb_rgb_convert_internal 189 190 191 /* 192 * Initialize for RGB->YCC colorspace conversion. 193 */ 194 195 METHODDEF(void) 196 rgb_ycc_start (j_compress_ptr cinfo) 197 { 198 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 199 INT32 * rgb_ycc_tab; 200 INT32 i; 201 202 /* Allocate and fill in the conversion tables. */ 203 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) 204 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 205 (TABLE_SIZE * SIZEOF(INT32))); 206 207 for (i = 0; i <= MAXJSAMPLE; i++) { 208 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; 209 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; 210 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; 211 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; 212 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; 213 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. 214 * This ensures that the maximum output will round to MAXJSAMPLE 215 * not MAXJSAMPLE+1, and thus that we don't have to range-limit. 216 */ 217 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 218 /* B=>Cb and R=>Cr tables are the same 219 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 220 */ 221 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; 222 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; 223 } 224 } 225 226 227 /* 228 * Convert some rows of samples to the JPEG colorspace. 229 */ 230 231 METHODDEF(void) 232 rgb_ycc_convert (j_compress_ptr cinfo, 233 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 234 JDIMENSION output_row, int num_rows) 235 { 236 switch (cinfo->in_color_space) { 237 case JCS_EXT_RGB: 238 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 239 num_rows); 240 break; 241 case JCS_EXT_RGBX: 242 case JCS_EXT_RGBA: 243 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 244 num_rows); 245 break; 246 case JCS_EXT_BGR: 247 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 248 num_rows); 249 break; 250 case JCS_EXT_BGRX: 251 case JCS_EXT_BGRA: 252 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 253 num_rows); 254 break; 255 case JCS_EXT_XBGR: 256 case JCS_EXT_ABGR: 257 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 258 num_rows); 259 break; 260 case JCS_EXT_XRGB: 261 case JCS_EXT_ARGB: 262 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 263 num_rows); 264 break; 265 default: 266 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 267 num_rows); 268 break; 269 } 270 } 271 272 273 /**************** Cases other than RGB -> YCbCr **************/ 274 275 276 /* 277 * Convert some rows of samples to the JPEG colorspace. 278 */ 279 280 METHODDEF(void) 281 rgb_gray_convert (j_compress_ptr cinfo, 282 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 283 JDIMENSION output_row, int num_rows) 284 { 285 switch (cinfo->in_color_space) { 286 case JCS_EXT_RGB: 287 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 288 num_rows); 289 break; 290 case JCS_EXT_RGBX: 291 case JCS_EXT_RGBA: 292 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 293 num_rows); 294 break; 295 case JCS_EXT_BGR: 296 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 297 num_rows); 298 break; 299 case JCS_EXT_BGRX: 300 case JCS_EXT_BGRA: 301 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 302 num_rows); 303 break; 304 case JCS_EXT_XBGR: 305 case JCS_EXT_ABGR: 306 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 307 num_rows); 308 break; 309 case JCS_EXT_XRGB: 310 case JCS_EXT_ARGB: 311 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 312 num_rows); 313 break; 314 default: 315 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 316 num_rows); 317 break; 318 } 319 } 320 321 322 /* 323 * Extended RGB to plain RGB conversion 324 */ 325 326 METHODDEF(void) 327 rgb_rgb_convert (j_compress_ptr cinfo, 328 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 329 JDIMENSION output_row, int num_rows) 330 { 331 switch (cinfo->in_color_space) { 332 case JCS_EXT_RGB: 333 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 334 num_rows); 335 break; 336 case JCS_EXT_RGBX: 337 case JCS_EXT_RGBA: 338 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 339 num_rows); 340 break; 341 case JCS_EXT_BGR: 342 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 343 num_rows); 344 break; 345 case JCS_EXT_BGRX: 346 case JCS_EXT_BGRA: 347 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 348 num_rows); 349 break; 350 case JCS_EXT_XBGR: 351 case JCS_EXT_ABGR: 352 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 353 num_rows); 354 break; 355 case JCS_EXT_XRGB: 356 case JCS_EXT_ARGB: 357 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 358 num_rows); 359 break; 360 default: 361 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 362 num_rows); 363 break; 364 } 365 } 366 367 368 /* 369 * Convert some rows of samples to the JPEG colorspace. 370 * This version handles Adobe-style CMYK->YCCK conversion, 371 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same 372 * conversion as above, while passing K (black) unchanged. 373 * We assume rgb_ycc_start has been called. 374 */ 375 376 METHODDEF(void) 377 cmyk_ycck_convert (j_compress_ptr cinfo, 378 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 379 JDIMENSION output_row, int num_rows) 380 { 381 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 382 register int r, g, b; 383 register INT32 * ctab = cconvert->rgb_ycc_tab; 384 register JSAMPROW inptr; 385 register JSAMPROW outptr0, outptr1, outptr2, outptr3; 386 register JDIMENSION col; 387 JDIMENSION num_cols = cinfo->image_width; 388 389 while (--num_rows >= 0) { 390 inptr = *input_buf++; 391 outptr0 = output_buf[0][output_row]; 392 outptr1 = output_buf[1][output_row]; 393 outptr2 = output_buf[2][output_row]; 394 outptr3 = output_buf[3][output_row]; 395 output_row++; 396 for (col = 0; col < num_cols; col++) { 397 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); 398 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); 399 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); 400 /* K passes through as-is */ 401 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ 402 inptr += 4; 403 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 404 * must be too; we do not need an explicit range-limiting operation. 405 * Hence the value being shifted is never negative, and we don't 406 * need the general RIGHT_SHIFT macro. 407 */ 408 /* Y */ 409 outptr0[col] = (JSAMPLE) 410 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 411 >> SCALEBITS); 412 /* Cb */ 413 outptr1[col] = (JSAMPLE) 414 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 415 >> SCALEBITS); 416 /* Cr */ 417 outptr2[col] = (JSAMPLE) 418 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 419 >> SCALEBITS); 420 } 421 } 422 } 423 424 425 /* 426 * Convert some rows of samples to the JPEG colorspace. 427 * This version handles grayscale output with no conversion. 428 * The source can be either plain grayscale or YCbCr (since Y == gray). 429 */ 430 431 METHODDEF(void) 432 grayscale_convert (j_compress_ptr cinfo, 433 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 434 JDIMENSION output_row, int num_rows) 435 { 436 register JSAMPROW inptr; 437 register JSAMPROW outptr; 438 register JDIMENSION col; 439 JDIMENSION num_cols = cinfo->image_width; 440 int instride = cinfo->input_components; 441 442 while (--num_rows >= 0) { 443 inptr = *input_buf++; 444 outptr = output_buf[0][output_row]; 445 output_row++; 446 for (col = 0; col < num_cols; col++) { 447 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ 448 inptr += instride; 449 } 450 } 451 } 452 453 454 /* 455 * Convert some rows of samples to the JPEG colorspace. 456 * This version handles multi-component colorspaces without conversion. 457 * We assume input_components == num_components. 458 */ 459 460 METHODDEF(void) 461 null_convert (j_compress_ptr cinfo, 462 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 463 JDIMENSION output_row, int num_rows) 464 { 465 register JSAMPROW inptr; 466 register JSAMPROW outptr; 467 register JDIMENSION col; 468 register int ci; 469 int nc = cinfo->num_components; 470 JDIMENSION num_cols = cinfo->image_width; 471 472 while (--num_rows >= 0) { 473 /* It seems fastest to make a separate pass for each component. */ 474 for (ci = 0; ci < nc; ci++) { 475 inptr = *input_buf; 476 outptr = output_buf[ci][output_row]; 477 for (col = 0; col < num_cols; col++) { 478 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ 479 inptr += nc; 480 } 481 } 482 input_buf++; 483 output_row++; 484 } 485 } 486 487 488 /* 489 * Empty method for start_pass. 490 */ 491 492 METHODDEF(void) 493 null_method (j_compress_ptr cinfo) 494 { 495 /* no work needed */ 496 } 497 498 499 /* 500 * Module initialization routine for input colorspace conversion. 501 */ 502 503 GLOBAL(void) 504 jinit_color_converter (j_compress_ptr cinfo) 505 { 506 my_cconvert_ptr cconvert; 507 508 cconvert = (my_cconvert_ptr) 509 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 510 SIZEOF(my_color_converter)); 511 cinfo->cconvert = (struct jpeg_color_converter *) cconvert; 512 /* set start_pass to null method until we find out differently */ 513 cconvert->pub.start_pass = null_method; 514 515 /* Make sure input_components agrees with in_color_space */ 516 switch (cinfo->in_color_space) { 517 case JCS_GRAYSCALE: 518 if (cinfo->input_components != 1) 519 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 520 break; 521 522 case JCS_RGB: 523 case JCS_EXT_RGB: 524 case JCS_EXT_RGBX: 525 case JCS_EXT_BGR: 526 case JCS_EXT_BGRX: 527 case JCS_EXT_XBGR: 528 case JCS_EXT_XRGB: 529 case JCS_EXT_RGBA: 530 case JCS_EXT_BGRA: 531 case JCS_EXT_ABGR: 532 case JCS_EXT_ARGB: 533 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) 534 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 535 break; 536 537 case JCS_YCbCr: 538 if (cinfo->input_components != 3) 539 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 540 break; 541 542 case JCS_CMYK: 543 case JCS_YCCK: 544 if (cinfo->input_components != 4) 545 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 546 break; 547 548 default: /* JCS_UNKNOWN can be anything */ 549 if (cinfo->input_components < 1) 550 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 551 break; 552 } 553 554 /* Check num_components, set conversion method based on requested space */ 555 switch (cinfo->jpeg_color_space) { 556 case JCS_GRAYSCALE: 557 if (cinfo->num_components != 1) 558 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 559 if (cinfo->in_color_space == JCS_GRAYSCALE) 560 cconvert->pub.color_convert = grayscale_convert; 561 else if (cinfo->in_color_space == JCS_RGB || 562 cinfo->in_color_space == JCS_EXT_RGB || 563 cinfo->in_color_space == JCS_EXT_RGBX || 564 cinfo->in_color_space == JCS_EXT_BGR || 565 cinfo->in_color_space == JCS_EXT_BGRX || 566 cinfo->in_color_space == JCS_EXT_XBGR || 567 cinfo->in_color_space == JCS_EXT_XRGB || 568 cinfo->in_color_space == JCS_EXT_RGBA || 569 cinfo->in_color_space == JCS_EXT_BGRA || 570 cinfo->in_color_space == JCS_EXT_ABGR || 571 cinfo->in_color_space == JCS_EXT_ARGB) { 572 if (jsimd_can_rgb_gray()) 573 cconvert->pub.color_convert = jsimd_rgb_gray_convert; 574 else { 575 cconvert->pub.start_pass = rgb_ycc_start; 576 cconvert->pub.color_convert = rgb_gray_convert; 577 } 578 } else if (cinfo->in_color_space == JCS_YCbCr) 579 cconvert->pub.color_convert = grayscale_convert; 580 else 581 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 582 break; 583 584 case JCS_RGB: 585 if (cinfo->num_components != 3) 586 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 587 if (rgb_red[cinfo->in_color_space] == 0 && 588 rgb_green[cinfo->in_color_space] == 1 && 589 rgb_blue[cinfo->in_color_space] == 2 && 590 rgb_pixelsize[cinfo->in_color_space] == 3) 591 cconvert->pub.color_convert = null_convert; 592 else if (cinfo->in_color_space == JCS_RGB || 593 cinfo->in_color_space == JCS_EXT_RGB || 594 cinfo->in_color_space == JCS_EXT_RGBX || 595 cinfo->in_color_space == JCS_EXT_BGR || 596 cinfo->in_color_space == JCS_EXT_BGRX || 597 cinfo->in_color_space == JCS_EXT_XBGR || 598 cinfo->in_color_space == JCS_EXT_XRGB || 599 cinfo->in_color_space == JCS_EXT_RGBA || 600 cinfo->in_color_space == JCS_EXT_BGRA || 601 cinfo->in_color_space == JCS_EXT_ABGR || 602 cinfo->in_color_space == JCS_EXT_ARGB) 603 cconvert->pub.color_convert = rgb_rgb_convert; 604 else 605 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 606 break; 607 608 case JCS_YCbCr: 609 if (cinfo->num_components != 3) 610 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 611 if (cinfo->in_color_space == JCS_RGB || 612 cinfo->in_color_space == JCS_EXT_RGB || 613 cinfo->in_color_space == JCS_EXT_RGBX || 614 cinfo->in_color_space == JCS_EXT_BGR || 615 cinfo->in_color_space == JCS_EXT_BGRX || 616 cinfo->in_color_space == JCS_EXT_XBGR || 617 cinfo->in_color_space == JCS_EXT_XRGB || 618 cinfo->in_color_space == JCS_EXT_RGBA || 619 cinfo->in_color_space == JCS_EXT_BGRA || 620 cinfo->in_color_space == JCS_EXT_ABGR || 621 cinfo->in_color_space == JCS_EXT_ARGB) { 622 if (jsimd_can_rgb_ycc()) 623 cconvert->pub.color_convert = jsimd_rgb_ycc_convert; 624 else { 625 cconvert->pub.start_pass = rgb_ycc_start; 626 cconvert->pub.color_convert = rgb_ycc_convert; 627 } 628 } else if (cinfo->in_color_space == JCS_YCbCr) 629 cconvert->pub.color_convert = null_convert; 630 else 631 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 632 break; 633 634 case JCS_CMYK: 635 if (cinfo->num_components != 4) 636 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 637 if (cinfo->in_color_space == JCS_CMYK) 638 cconvert->pub.color_convert = null_convert; 639 else 640 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 641 break; 642 643 case JCS_YCCK: 644 if (cinfo->num_components != 4) 645 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 646 if (cinfo->in_color_space == JCS_CMYK) { 647 cconvert->pub.start_pass = rgb_ycc_start; 648 cconvert->pub.color_convert = cmyk_ycck_convert; 649 } else if (cinfo->in_color_space == JCS_YCCK) 650 cconvert->pub.color_convert = null_convert; 651 else 652 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 653 break; 654 655 default: /* allow null conversion of JCS_UNKNOWN */ 656 if (cinfo->jpeg_color_space != cinfo->in_color_space || 657 cinfo->num_components != cinfo->input_components) 658 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 659 cconvert->pub.color_convert = null_convert; 660 break; 661 } 662 } 663