4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
6 * Copyright (C) 2009-2012, D. R. Commander.
7 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains input colorspace conversion routines.
13 #define JPEG_INTERNALS
20 /* Private subobject */
23 struct jpeg_color_converter pub; /* public fields */
25 /* Private state for RGB->YCC conversion */
26 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
29 typedef my_color_converter * my_cconvert_ptr;
32 /**************** RGB -> YCbCr conversion: most common case **************/
35 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
36 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
37 * The conversion equations to be implemented are therefore
38 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
39 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
40 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
41 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
42 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
43 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
44 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
45 * were not represented exactly. Now we sacrifice exact representation of
46 * maximum red and maximum blue in order to get exact grayscales.
48 * To avoid floating-point arithmetic, we represent the fractional constants
49 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
50 * the products by 2^16, with appropriate rounding, to get the correct answer.
52 * For even more speed, we avoid doing any multiplications in the inner loop
53 * by precalculating the constants times R,G,B for all possible values.
54 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
55 * for 12-bit samples it is still acceptable. It's not very reasonable for
56 * 16-bit samples, but if you want lossless storage you shouldn't be changing
58 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
59 * in the tables to save adding them separately in the inner loop.
62 #define SCALEBITS 16 /* speediest right-shift on some machines */
63 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
64 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
65 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
67 /* We allocate one big table and divide it up into eight parts, instead of
68 * doing eight alloc_small requests. This lets us use a single table base
69 * address, which can be held in a register in the inner loops on many
70 * machines (more than can hold all eight addresses, anyway).
73 #define R_Y_OFF 0 /* offset to R => Y section */
74 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
75 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
76 #define R_CB_OFF (3*(MAXJSAMPLE+1))
77 #define G_CB_OFF (4*(MAXJSAMPLE+1))
78 #define B_CB_OFF (5*(MAXJSAMPLE+1))
79 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
80 #define G_CR_OFF (6*(MAXJSAMPLE+1))
81 #define B_CR_OFF (7*(MAXJSAMPLE+1))
82 #define TABLE_SIZE (8*(MAXJSAMPLE+1))
85 /* Include inline routines for colorspace extensions */
93 #define RGB_RED EXT_RGB_RED
94 #define RGB_GREEN EXT_RGB_GREEN
95 #define RGB_BLUE EXT_RGB_BLUE
96 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
97 #define rgb_ycc_convert_internal extrgb_ycc_convert_internal
98 #define rgb_gray_convert_internal extrgb_gray_convert_internal
99 #define rgb_rgb_convert_internal extrgb_rgb_convert_internal
100 #include "jccolext.c"
105 #undef rgb_ycc_convert_internal
106 #undef rgb_gray_convert_internal
107 #undef rgb_rgb_convert_internal
109 #define RGB_RED EXT_RGBX_RED
110 #define RGB_GREEN EXT_RGBX_GREEN
111 #define RGB_BLUE EXT_RGBX_BLUE
112 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
113 #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
114 #define rgb_gray_convert_internal extrgbx_gray_convert_internal
115 #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
116 #include "jccolext.c"
121 #undef rgb_ycc_convert_internal
122 #undef rgb_gray_convert_internal
123 #undef rgb_rgb_convert_internal
125 #define RGB_RED EXT_BGR_RED
126 #define RGB_GREEN EXT_BGR_GREEN
127 #define RGB_BLUE EXT_BGR_BLUE
128 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
129 #define rgb_ycc_convert_internal extbgr_ycc_convert_internal
130 #define rgb_gray_convert_internal extbgr_gray_convert_internal
131 #define rgb_rgb_convert_internal extbgr_rgb_convert_internal
132 #include "jccolext.c"
137 #undef rgb_ycc_convert_internal
138 #undef rgb_gray_convert_internal
139 #undef rgb_rgb_convert_internal
141 #define RGB_RED EXT_BGRX_RED
142 #define RGB_GREEN EXT_BGRX_GREEN
143 #define RGB_BLUE EXT_BGRX_BLUE
144 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
145 #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
146 #define rgb_gray_convert_internal extbgrx_gray_convert_internal
147 #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
148 #include "jccolext.c"
153 #undef rgb_ycc_convert_internal
154 #undef rgb_gray_convert_internal
155 #undef rgb_rgb_convert_internal
157 #define RGB_RED EXT_XBGR_RED
158 #define RGB_GREEN EXT_XBGR_GREEN
159 #define RGB_BLUE EXT_XBGR_BLUE
160 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
161 #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
162 #define rgb_gray_convert_internal extxbgr_gray_convert_internal
163 #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
164 #include "jccolext.c"
169 #undef rgb_ycc_convert_internal
170 #undef rgb_gray_convert_internal
171 #undef rgb_rgb_convert_internal
173 #define RGB_RED EXT_XRGB_RED
174 #define RGB_GREEN EXT_XRGB_GREEN
175 #define RGB_BLUE EXT_XRGB_BLUE
176 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
177 #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
178 #define rgb_gray_convert_internal extxrgb_gray_convert_internal
179 #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
180 #include "jccolext.c"
185 #undef rgb_ycc_convert_internal
186 #undef rgb_gray_convert_internal
187 #undef rgb_rgb_convert_internal
191 * Initialize for RGB->YCC colorspace conversion.
195 rgb_ycc_start (j_compress_ptr cinfo)
197 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
201 /* Allocate and fill in the conversion tables. */
202 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
203 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
204 (TABLE_SIZE * SIZEOF(INT32)));
206 for (i = 0; i <= MAXJSAMPLE; i++) {
207 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
208 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
209 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
210 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
211 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
212 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
213 * This ensures that the maximum output will round to MAXJSAMPLE
214 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
216 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
217 /* B=>Cb and R=>Cr tables are the same
218 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
220 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
221 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
227 * Convert some rows of samples to the JPEG colorspace.
231 rgb_ycc_convert (j_compress_ptr cinfo,
232 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
233 JDIMENSION output_row, int num_rows)
235 switch (cinfo->in_color_space) {
237 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
242 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
246 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
251 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
256 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
261 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
265 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
272 /**************** Cases other than RGB -> YCbCr **************/
276 * Convert some rows of samples to the JPEG colorspace.
280 rgb_gray_convert (j_compress_ptr cinfo,
281 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
282 JDIMENSION output_row, int num_rows)
284 switch (cinfo->in_color_space) {
286 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
291 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
295 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
300 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
305 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
310 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
314 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
322 * Extended RGB to plain RGB conversion
326 rgb_rgb_convert (j_compress_ptr cinfo,
327 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
328 JDIMENSION output_row, int num_rows)
330 switch (cinfo->in_color_space) {
332 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
337 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
341 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
346 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
351 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
356 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
360 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
368 * Convert some rows of samples to the JPEG colorspace.
369 * This version handles Adobe-style CMYK->YCCK conversion,
370 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
371 * conversion as above, while passing K (black) unchanged.
372 * We assume rgb_ycc_start has been called.
376 cmyk_ycck_convert (j_compress_ptr cinfo,
377 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
378 JDIMENSION output_row, int num_rows)
380 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
381 register int r, g, b;
382 register INT32 * ctab = cconvert->rgb_ycc_tab;
383 register JSAMPROW inptr;
384 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
385 register JDIMENSION col;
386 JDIMENSION num_cols = cinfo->image_width;
388 while (--num_rows >= 0) {
389 inptr = *input_buf++;
390 outptr0 = output_buf[0][output_row];
391 outptr1 = output_buf[1][output_row];
392 outptr2 = output_buf[2][output_row];
393 outptr3 = output_buf[3][output_row];
395 for (col = 0; col < num_cols; col++) {
396 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
397 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
398 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
399 /* K passes through as-is */
400 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
402 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
403 * must be too; we do not need an explicit range-limiting operation.
404 * Hence the value being shifted is never negative, and we don't
405 * need the general RIGHT_SHIFT macro.
408 outptr0[col] = (JSAMPLE)
409 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
412 outptr1[col] = (JSAMPLE)
413 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
416 outptr2[col] = (JSAMPLE)
417 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
425 * Convert some rows of samples to the JPEG colorspace.
426 * This version handles grayscale output with no conversion.
427 * The source can be either plain grayscale or YCbCr (since Y == gray).
431 grayscale_convert (j_compress_ptr cinfo,
432 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
433 JDIMENSION output_row, int num_rows)
435 register JSAMPROW inptr;
436 register JSAMPROW outptr;
437 register JDIMENSION col;
438 JDIMENSION num_cols = cinfo->image_width;
439 int instride = cinfo->input_components;
441 while (--num_rows >= 0) {
442 inptr = *input_buf++;
443 outptr = output_buf[0][output_row];
445 for (col = 0; col < num_cols; col++) {
446 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
454 * Convert some rows of samples to the JPEG colorspace.
455 * This version handles multi-component colorspaces without conversion.
456 * We assume input_components == num_components.
460 null_convert (j_compress_ptr cinfo,
461 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
462 JDIMENSION output_row, int num_rows)
464 register JSAMPROW inptr;
465 register JSAMPROW outptr;
466 register JDIMENSION col;
468 int nc = cinfo->num_components;
469 JDIMENSION num_cols = cinfo->image_width;
471 while (--num_rows >= 0) {
472 /* It seems fastest to make a separate pass for each component. */
473 for (ci = 0; ci < nc; ci++) {
475 outptr = output_buf[ci][output_row];
476 for (col = 0; col < num_cols; col++) {
477 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
488 * Empty method for start_pass.
492 null_method (j_compress_ptr cinfo)
499 * Module initialization routine for input colorspace conversion.
503 jinit_color_converter (j_compress_ptr cinfo)
505 my_cconvert_ptr cconvert;
507 cconvert = (my_cconvert_ptr)
508 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
509 SIZEOF(my_color_converter));
510 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
511 /* set start_pass to null method until we find out differently */
512 cconvert->pub.start_pass = null_method;
514 /* Make sure input_components agrees with in_color_space */
515 switch (cinfo->in_color_space) {
517 if (cinfo->input_components != 1)
518 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
533 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
537 if (cinfo->input_components != 3)
538 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
543 if (cinfo->input_components != 4)
544 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
547 default: /* JCS_UNKNOWN can be anything */
548 if (cinfo->input_components < 1)
549 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
553 /* Check num_components, set conversion method based on requested space */
554 switch (cinfo->jpeg_color_space) {
556 if (cinfo->num_components != 1)
557 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
558 if (cinfo->in_color_space == JCS_GRAYSCALE)
559 cconvert->pub.color_convert = grayscale_convert;
560 else if (cinfo->in_color_space == JCS_RGB ||
561 cinfo->in_color_space == JCS_EXT_RGB ||
562 cinfo->in_color_space == JCS_EXT_RGBX ||
563 cinfo->in_color_space == JCS_EXT_BGR ||
564 cinfo->in_color_space == JCS_EXT_BGRX ||
565 cinfo->in_color_space == JCS_EXT_XBGR ||
566 cinfo->in_color_space == JCS_EXT_XRGB ||
567 cinfo->in_color_space == JCS_EXT_RGBA ||
568 cinfo->in_color_space == JCS_EXT_BGRA ||
569 cinfo->in_color_space == JCS_EXT_ABGR ||
570 cinfo->in_color_space == JCS_EXT_ARGB) {
571 if (jsimd_can_rgb_gray())
572 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
574 cconvert->pub.start_pass = rgb_ycc_start;
575 cconvert->pub.color_convert = rgb_gray_convert;
577 } else if (cinfo->in_color_space == JCS_YCbCr)
578 cconvert->pub.color_convert = grayscale_convert;
580 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
584 if (cinfo->num_components != 3)
585 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
586 if (rgb_red[cinfo->in_color_space] == 0 &&
587 rgb_green[cinfo->in_color_space] == 1 &&
588 rgb_blue[cinfo->in_color_space] == 2 &&
589 rgb_pixelsize[cinfo->in_color_space] == 3)
590 cconvert->pub.color_convert = null_convert;
591 else if (cinfo->in_color_space == JCS_RGB ||
592 cinfo->in_color_space == JCS_EXT_RGB ||
593 cinfo->in_color_space == JCS_EXT_RGBX ||
594 cinfo->in_color_space == JCS_EXT_BGR ||
595 cinfo->in_color_space == JCS_EXT_BGRX ||
596 cinfo->in_color_space == JCS_EXT_XBGR ||
597 cinfo->in_color_space == JCS_EXT_XRGB ||
598 cinfo->in_color_space == JCS_EXT_RGBA ||
599 cinfo->in_color_space == JCS_EXT_BGRA ||
600 cinfo->in_color_space == JCS_EXT_ABGR ||
601 cinfo->in_color_space == JCS_EXT_ARGB)
602 cconvert->pub.color_convert = rgb_rgb_convert;
604 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
608 if (cinfo->num_components != 3)
609 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
610 if (cinfo->in_color_space == JCS_RGB ||
611 cinfo->in_color_space == JCS_EXT_RGB ||
612 cinfo->in_color_space == JCS_EXT_RGBX ||
613 cinfo->in_color_space == JCS_EXT_BGR ||
614 cinfo->in_color_space == JCS_EXT_BGRX ||
615 cinfo->in_color_space == JCS_EXT_XBGR ||
616 cinfo->in_color_space == JCS_EXT_XRGB ||
617 cinfo->in_color_space == JCS_EXT_RGBA ||
618 cinfo->in_color_space == JCS_EXT_BGRA ||
619 cinfo->in_color_space == JCS_EXT_ABGR ||
620 cinfo->in_color_space == JCS_EXT_ARGB) {
621 if (jsimd_can_rgb_ycc())
622 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
624 cconvert->pub.start_pass = rgb_ycc_start;
625 cconvert->pub.color_convert = rgb_ycc_convert;
627 } else if (cinfo->in_color_space == JCS_YCbCr)
628 cconvert->pub.color_convert = null_convert;
630 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
634 if (cinfo->num_components != 4)
635 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
636 if (cinfo->in_color_space == JCS_CMYK)
637 cconvert->pub.color_convert = null_convert;
639 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
643 if (cinfo->num_components != 4)
644 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
645 if (cinfo->in_color_space == JCS_CMYK) {
646 cconvert->pub.start_pass = rgb_ycc_start;
647 cconvert->pub.color_convert = cmyk_ycck_convert;
648 } else if (cinfo->in_color_space == JCS_YCCK)
649 cconvert->pub.color_convert = null_convert;
651 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
654 default: /* allow null conversion of JCS_UNKNOWN */
655 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
656 cinfo->num_components != cinfo->input_components)
657 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658 cconvert->pub.color_convert = null_convert;