1 /* 2 * jccolor.c 3 * 4 * Copyright (C) 1991-1996, Thomas G. Lane. 5 * Modified 2011-2012 by Guido Vollbeding. 6 * This file is part of the Independent JPEG Group's software. 7 * For conditions of distribution and use, see the accompanying README file. 8 * 9 * This file contains input colorspace conversion routines. 10 */ 11 12 #define JPEG_INTERNALS 13 #include "jinclude.h" 14 #include "jpeglib.h" 15 16 17 /* Private subobject */ 18 19 typedef struct { 20 struct jpeg_color_converter pub; /* public fields */ 21 22 /* Private state for RGB->YCC conversion */ 23 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ 24 } my_color_converter; 25 26 typedef my_color_converter * my_cconvert_ptr; 27 28 29 /**************** RGB -> YCbCr conversion: most common case **************/ 30 31 /* 32 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are 33 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. 34 * The conversion equations to be implemented are therefore 35 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B 36 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE 37 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE 38 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) 39 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, 40 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and 41 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) 42 * were not represented exactly. Now we sacrifice exact representation of 43 * maximum red and maximum blue in order to get exact grayscales. 44 * 45 * To avoid floating-point arithmetic, we represent the fractional constants 46 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide 47 * the products by 2^16, with appropriate rounding, to get the correct answer. 48 * 49 * For even more speed, we avoid doing any multiplications in the inner loop 50 * by precalculating the constants times R,G,B for all possible values. 51 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); 52 * for 12-bit samples it is still acceptable. It's not very reasonable for 53 * 16-bit samples, but if you want lossless storage you shouldn't be changing 54 * colorspace anyway. 55 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included 56 * in the tables to save adding them separately in the inner loop. 57 */ 58 59 #define SCALEBITS 16 /* speediest right-shift on some machines */ 60 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) 61 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) 62 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) 63 64 /* We allocate one big table and divide it up into eight parts, instead of 65 * doing eight alloc_small requests. This lets us use a single table base 66 * address, which can be held in a register in the inner loops on many 67 * machines (more than can hold all eight addresses, anyway). 68 */ 69 70 #define R_Y_OFF 0 /* offset to R => Y section */ 71 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ 72 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ 73 #define R_CB_OFF (3*(MAXJSAMPLE+1)) 74 #define G_CB_OFF (4*(MAXJSAMPLE+1)) 75 #define B_CB_OFF (5*(MAXJSAMPLE+1)) 76 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ 77 #define G_CR_OFF (6*(MAXJSAMPLE+1)) 78 #define B_CR_OFF (7*(MAXJSAMPLE+1)) 79 #define TABLE_SIZE (8*(MAXJSAMPLE+1)) 80 81 82 /* 83 * Initialize for RGB->YCC colorspace conversion. 84 */ 85 86 METHODDEF(void) 87 rgb_ycc_start (j_compress_ptr cinfo) 88 { 89 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 90 INT32 * rgb_ycc_tab; 91 INT32 i; 92 93 /* Allocate and fill in the conversion tables. */ 94 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) 95 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 96 (TABLE_SIZE * SIZEOF(INT32))); 97 98 for (i = 0; i <= MAXJSAMPLE; i++) { 99 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; 100 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; 101 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; 102 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; 103 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; 104 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. 105 * This ensures that the maximum output will round to MAXJSAMPLE 106 * not MAXJSAMPLE+1, and thus that we don't have to range-limit. 107 */ 108 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 109 /* B=>Cb and R=>Cr tables are the same 110 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 111 */ 112 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; 113 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; 114 } 115 } 116 117 118 /* 119 * Convert some rows of samples to the JPEG colorspace. 120 * 121 * Note that we change from the application's interleaved-pixel format 122 * to our internal noninterleaved, one-plane-per-component format. 123 * The input buffer is therefore three times as wide as the output buffer. 124 * 125 * A starting row offset is provided only for the output buffer. The caller 126 * can easily adjust the passed input_buf value to accommodate any row 127 * offset required on that side. 128 */ 129 130 METHODDEF(void) 131 rgb_ycc_convert (j_compress_ptr cinfo, 132 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 133 JDIMENSION output_row, int num_rows) 134 { 135 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 136 register INT32 * ctab = cconvert->rgb_ycc_tab; 137 register int r, g, b; 138 register JSAMPROW inptr; 139 register JSAMPROW outptr0, outptr1, outptr2; 140 register JDIMENSION col; 141 JDIMENSION num_cols = cinfo->image_width; 142 143 while (--num_rows >= 0) { 144 inptr = *input_buf++; 145 outptr0 = output_buf[0][output_row]; 146 outptr1 = output_buf[1][output_row]; 147 outptr2 = output_buf[2][output_row]; 148 output_row++; 149 for (col = 0; col < num_cols; col++) { 150 r = GETJSAMPLE(inptr[RGB_RED]); 151 g = GETJSAMPLE(inptr[RGB_GREEN]); 152 b = GETJSAMPLE(inptr[RGB_BLUE]); 153 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 154 * must be too; we do not need an explicit range-limiting operation. 155 * Hence the value being shifted is never negative, and we don't 156 * need the general RIGHT_SHIFT macro. 157 */ 158 /* Y */ 159 outptr0[col] = (JSAMPLE) 160 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 161 >> SCALEBITS); 162 /* Cb */ 163 outptr1[col] = (JSAMPLE) 164 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 165 >> SCALEBITS); 166 /* Cr */ 167 outptr2[col] = (JSAMPLE) 168 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 169 >> SCALEBITS); 170 inptr += RGB_PIXELSIZE; 171 } 172 } 173 } 174 175 176 /**************** Cases other than RGB -> YCbCr **************/ 177 178 179 /* 180 * Convert some rows of samples to the JPEG colorspace. 181 * This version handles RGB->grayscale conversion, which is the same 182 * as the RGB->Y portion of RGB->YCbCr. 183 * We assume rgb_ycc_start has been called (we only use the Y tables). 184 */ 185 186 METHODDEF(void) 187 rgb_gray_convert (j_compress_ptr cinfo, 188 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 189 JDIMENSION output_row, int num_rows) 190 { 191 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 192 register INT32 * ctab = cconvert->rgb_ycc_tab; 193 register int r, g, b; 194 register JSAMPROW inptr; 195 register JSAMPROW outptr; 196 register JDIMENSION col; 197 JDIMENSION num_cols = cinfo->image_width; 198 199 while (--num_rows >= 0) { 200 inptr = *input_buf++; 201 outptr = output_buf[0][output_row++]; 202 for (col = 0; col < num_cols; col++) { 203 r = GETJSAMPLE(inptr[RGB_RED]); 204 g = GETJSAMPLE(inptr[RGB_GREEN]); 205 b = GETJSAMPLE(inptr[RGB_BLUE]); 206 /* Y */ 207 outptr[col] = (JSAMPLE) 208 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 209 >> SCALEBITS); 210 inptr += RGB_PIXELSIZE; 211 } 212 } 213 } 214 215 216 /* 217 * Convert some rows of samples to the JPEG colorspace. 218 * This version handles Adobe-style CMYK->YCCK conversion, 219 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same 220 * conversion as above, while passing K (black) unchanged. 221 * We assume rgb_ycc_start has been called. 222 */ 223 224 METHODDEF(void) 225 cmyk_ycck_convert (j_compress_ptr cinfo, 226 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 227 JDIMENSION output_row, int num_rows) 228 { 229 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 230 register INT32 * ctab = cconvert->rgb_ycc_tab; 231 register int r, g, b; 232 register JSAMPROW inptr; 233 register JSAMPROW outptr0, outptr1, outptr2, outptr3; 234 register JDIMENSION col; 235 JDIMENSION num_cols = cinfo->image_width; 236 237 while (--num_rows >= 0) { 238 inptr = *input_buf++; 239 outptr0 = output_buf[0][output_row]; 240 outptr1 = output_buf[1][output_row]; 241 outptr2 = output_buf[2][output_row]; 242 outptr3 = output_buf[3][output_row]; 243 output_row++; 244 for (col = 0; col < num_cols; col++) { 245 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); 246 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); 247 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); 248 /* K passes through as-is */ 249 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ 250 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 251 * must be too; we do not need an explicit range-limiting operation. 252 * Hence the value being shifted is never negative, and we don't 253 * need the general RIGHT_SHIFT macro. 254 */ 255 /* Y */ 256 outptr0[col] = (JSAMPLE) 257 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 258 >> SCALEBITS); 259 /* Cb */ 260 outptr1[col] = (JSAMPLE) 261 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 262 >> SCALEBITS); 263 /* Cr */ 264 outptr2[col] = (JSAMPLE) 265 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 266 >> SCALEBITS); 267 inptr += 4; 268 } 269 } 270 } 271 272 273 /* 274 * Convert some rows of samples to the JPEG colorspace. 275 * [R,G,B] to [R-G,G,B-G] conversion with modulo calculation 276 * (forward reversible color transform). 277 */ 278 279 METHODDEF(void) 280 rgb_rgb1_convert (j_compress_ptr cinfo, 281 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 282 JDIMENSION output_row, int num_rows) 283 { 284 register int r, g, b; 285 register JSAMPROW inptr; 286 register JSAMPROW outptr0, outptr1, outptr2; 287 register JDIMENSION col; 288 JDIMENSION num_cols = cinfo->image_width; 289 290 while (--num_rows >= 0) { 291 inptr = *input_buf++; 292 outptr0 = output_buf[0][output_row]; 293 outptr1 = output_buf[1][output_row]; 294 outptr2 = output_buf[2][output_row]; 295 output_row++; 296 for (col = 0; col < num_cols; col++) { 297 r = GETJSAMPLE(inptr[RGB_RED]); 298 g = GETJSAMPLE(inptr[RGB_GREEN]); 299 b = GETJSAMPLE(inptr[RGB_BLUE]); 300 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD 301 * (modulo) operator is equivalent to the bitmask operator AND. 302 */ 303 outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE); 304 outptr1[col] = (JSAMPLE) g; 305 outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE); 306 inptr += RGB_PIXELSIZE; 307 } 308 } 309 } 310 311 312 /* 313 * Convert some rows of samples to the JPEG colorspace. 314 * This version handles grayscale output with no conversion. 315 * The source can be either plain grayscale or YCbCr (since Y == gray). 316 */ 317 318 METHODDEF(void) 319 grayscale_convert (j_compress_ptr cinfo, 320 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 321 JDIMENSION output_row, int num_rows) 322 { 323 int instride = cinfo->input_components; 324 register JSAMPROW inptr; 325 register JSAMPROW outptr; 326 register JDIMENSION col; 327 JDIMENSION num_cols = cinfo->image_width; 328 329 while (--num_rows >= 0) { 330 inptr = *input_buf++; 331 outptr = output_buf[0][output_row++]; 332 for (col = 0; col < num_cols; col++) { 333 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ 334 inptr += instride; 335 } 336 } 337 } 338 339 340 /* 341 * Convert some rows of samples to the JPEG colorspace. 342 * No colorspace conversion, but change from interleaved 343 * to separate-planes representation. 344 */ 345 346 METHODDEF(void) 347 rgb_convert (j_compress_ptr cinfo, 348 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 349 JDIMENSION output_row, int num_rows) 350 { 351 register JSAMPROW inptr; 352 register JSAMPROW outptr0, outptr1, outptr2; 353 register JDIMENSION col; 354 JDIMENSION num_cols = cinfo->image_width; 355 356 while (--num_rows >= 0) { 357 inptr = *input_buf++; 358 outptr0 = output_buf[0][output_row]; 359 outptr1 = output_buf[1][output_row]; 360 outptr2 = output_buf[2][output_row]; 361 output_row++; 362 for (col = 0; col < num_cols; col++) { 363 /* We can dispense with GETJSAMPLE() here */ 364 outptr0[col] = inptr[RGB_RED]; 365 outptr1[col] = inptr[RGB_GREEN]; 366 outptr2[col] = inptr[RGB_BLUE]; 367 inptr += RGB_PIXELSIZE; 368 } 369 } 370 } 371 372 373 /* 374 * Convert some rows of samples to the JPEG colorspace. 375 * This version handles multi-component colorspaces without conversion. 376 * We assume input_components == num_components. 377 */ 378 379 METHODDEF(void) 380 null_convert (j_compress_ptr cinfo, 381 JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 382 JDIMENSION output_row, int num_rows) 383 { 384 int ci; 385 register int nc = cinfo->num_components; 386 register JSAMPROW inptr; 387 register JSAMPROW outptr; 388 register JDIMENSION col; 389 JDIMENSION num_cols = cinfo->image_width; 390 391 while (--num_rows >= 0) { 392 /* It seems fastest to make a separate pass for each component. */ 393 for (ci = 0; ci < nc; ci++) { 394 inptr = input_buf[0] + ci; 395 outptr = output_buf[ci][output_row]; 396 for (col = 0; col < num_cols; col++) { 397 *outptr++ = *inptr; /* don't need GETJSAMPLE() here */ 398 inptr += nc; 399 } 400 } 401 input_buf++; 402 output_row++; 403 } 404 } 405 406 407 /* 408 * Empty method for start_pass. 409 */ 410 411 METHODDEF(void) 412 null_method (j_compress_ptr cinfo) 413 { 414 /* no work needed */ 415 } 416 417 418 /* 419 * Module initialization routine for input colorspace conversion. 420 */ 421 422 GLOBAL(void) 423 jinit_color_converter (j_compress_ptr cinfo) 424 { 425 my_cconvert_ptr cconvert; 426 427 cconvert = (my_cconvert_ptr) 428 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 429 SIZEOF(my_color_converter)); 430 cinfo->cconvert = &cconvert->pub; 431 /* set start_pass to null method until we find out differently */ 432 cconvert->pub.start_pass = null_method; 433 434 /* Make sure input_components agrees with in_color_space */ 435 switch (cinfo->in_color_space) { 436 case JCS_GRAYSCALE: 437 if (cinfo->input_components != 1) 438 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 439 break; 440 441 case JCS_RGB: 442 if (cinfo->input_components != RGB_PIXELSIZE) 443 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 444 break; 445 446 case JCS_YCbCr: 447 if (cinfo->input_components != 3) 448 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 449 break; 450 451 case JCS_CMYK: 452 case JCS_YCCK: 453 if (cinfo->input_components != 4) 454 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 455 break; 456 457 default: /* JCS_UNKNOWN can be anything */ 458 if (cinfo->input_components < 1) 459 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 460 break; 461 } 462 463 /* Support color transform only for RGB colorspace */ 464 if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB) 465 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 466 467 /* Check num_components, set conversion method based on requested space */ 468 switch (cinfo->jpeg_color_space) { 469 case JCS_GRAYSCALE: 470 if (cinfo->num_components != 1) 471 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 472 if (cinfo->in_color_space == JCS_GRAYSCALE || 473 cinfo->in_color_space == JCS_YCbCr) 474 cconvert->pub.color_convert = grayscale_convert; 475 else if (cinfo->in_color_space == JCS_RGB) { 476 cconvert->pub.start_pass = rgb_ycc_start; 477 cconvert->pub.color_convert = rgb_gray_convert; 478 } else 479 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 480 break; 481 482 case JCS_RGB: 483 if (cinfo->num_components != 3) 484 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 485 if (cinfo->in_color_space == JCS_RGB) { 486 switch (cinfo->color_transform) { 487 case JCT_NONE: 488 cconvert->pub.color_convert = rgb_convert; 489 break; 490 case JCT_SUBTRACT_GREEN: 491 cconvert->pub.color_convert = rgb_rgb1_convert; 492 break; 493 default: 494 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 495 break; 496 } 497 } else 498 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 499 break; 500 501 case JCS_YCbCr: 502 if (cinfo->num_components != 3) 503 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 504 if (cinfo->in_color_space == JCS_RGB) { 505 cconvert->pub.start_pass = rgb_ycc_start; 506 cconvert->pub.color_convert = rgb_ycc_convert; 507 } else if (cinfo->in_color_space == JCS_YCbCr) 508 cconvert->pub.color_convert = null_convert; 509 else 510 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 511 break; 512 513 case JCS_CMYK: 514 if (cinfo->num_components != 4) 515 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 516 if (cinfo->in_color_space == JCS_CMYK) 517 cconvert->pub.color_convert = null_convert; 518 else 519 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 520 break; 521 522 case JCS_YCCK: 523 if (cinfo->num_components != 4) 524 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 525 if (cinfo->in_color_space == JCS_CMYK) { 526 cconvert->pub.start_pass = rgb_ycc_start; 527 cconvert->pub.color_convert = cmyk_ycck_convert; 528 } else if (cinfo->in_color_space == JCS_YCCK) 529 cconvert->pub.color_convert = null_convert; 530 else 531 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 532 break; 533 534 default: /* allow null conversion of JCS_UNKNOWN */ 535 if (cinfo->jpeg_color_space != cinfo->in_color_space || 536 cinfo->num_components != cinfo->input_components) 537 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 538 cconvert->pub.color_convert = null_convert; 539 break; 540 } 541 } 542