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@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.
11 * This file contains input colorspace conversion routines.
14 #define JPEG_INTERNALS
21 /* Private subobject */
24 struct jpeg_color_converter pub; /* public fields */
26 /* Private state for RGB->YCC conversion */
27 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
30 typedef my_color_converter * my_cconvert_ptr;
33 /**************** RGB -> YCbCr conversion: most common case **************/
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.
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.
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
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.
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))
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).
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))
86 /* Include inline routines for colorspace extensions */
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"
106 #undef rgb_ycc_convert_internal
107 #undef rgb_gray_convert_internal
108 #undef rgb_rgb_convert_internal
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"
122 #undef rgb_ycc_convert_internal
123 #undef rgb_gray_convert_internal
124 #undef rgb_rgb_convert_internal
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"
138 #undef rgb_ycc_convert_internal
139 #undef rgb_gray_convert_internal
140 #undef rgb_rgb_convert_internal
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"
154 #undef rgb_ycc_convert_internal
155 #undef rgb_gray_convert_internal
156 #undef rgb_rgb_convert_internal
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"
170 #undef rgb_ycc_convert_internal
171 #undef rgb_gray_convert_internal
172 #undef rgb_rgb_convert_internal
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"
186 #undef rgb_ycc_convert_internal
187 #undef rgb_gray_convert_internal
188 #undef rgb_rgb_convert_internal
192 * Initialize for RGB->YCC colorspace conversion.
196 rgb_ycc_start (j_compress_ptr cinfo)
198 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
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)));
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.
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;
221 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
222 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
228 * Convert some rows of samples to the JPEG colorspace.
232 rgb_ycc_convert (j_compress_ptr cinfo,
233 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
234 JDIMENSION output_row, int num_rows)
236 switch (cinfo->in_color_space) {
238 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
243 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
247 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
252 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
257 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
262 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
266 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
273 /**************** Cases other than RGB -> YCbCr **************/
277 * Convert some rows of samples to the JPEG colorspace.
281 rgb_gray_convert (j_compress_ptr cinfo,
282 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
283 JDIMENSION output_row, int num_rows)
285 switch (cinfo->in_color_space) {
287 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
292 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
296 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
301 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
306 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
311 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
315 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
323 * Extended RGB to plain RGB conversion
327 rgb_rgb_convert (j_compress_ptr cinfo,
328 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
329 JDIMENSION output_row, int num_rows)
331 switch (cinfo->in_color_space) {
333 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
338 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
342 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
347 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
352 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
357 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
361 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
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.
377 cmyk_ycck_convert (j_compress_ptr cinfo,
378 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
379 JDIMENSION output_row, int num_rows)
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;
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];
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 */
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.
409 outptr0[col] = (JSAMPLE)
410 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
413 outptr1[col] = (JSAMPLE)
414 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
417 outptr2[col] = (JSAMPLE)
418 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
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).
432 grayscale_convert (j_compress_ptr cinfo,
433 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
434 JDIMENSION output_row, int num_rows)
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;
442 while (--num_rows >= 0) {
443 inptr = *input_buf++;
444 outptr = output_buf[0][output_row];
446 for (col = 0; col < num_cols; col++) {
447 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
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.
461 null_convert (j_compress_ptr cinfo,
462 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
463 JDIMENSION output_row, int num_rows)
465 register JSAMPROW inptr;
466 register JSAMPROW outptr;
467 register JDIMENSION col;
469 int nc = cinfo->num_components;
470 JDIMENSION num_cols = cinfo->image_width;
472 while (--num_rows >= 0) {
473 /* It seems fastest to make a separate pass for each component. */
474 for (ci = 0; ci < nc; ci++) {
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 */
489 * Empty method for start_pass.
493 null_method (j_compress_ptr cinfo)
500 * Module initialization routine for input colorspace conversion.
504 jinit_color_converter (j_compress_ptr cinfo)
506 my_cconvert_ptr cconvert;
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;
515 /* Make sure input_components agrees with in_color_space */
516 switch (cinfo->in_color_space) {
518 if (cinfo->input_components != 1)
519 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
533 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
534 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
538 if (cinfo->input_components != 3)
539 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
544 if (cinfo->input_components != 4)
545 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
548 default: /* JCS_UNKNOWN can be anything */
549 if (cinfo->input_components < 1)
550 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
554 /* Check num_components, set conversion method based on requested space */
555 switch (cinfo->jpeg_color_space) {
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;
575 cconvert->pub.start_pass = rgb_ycc_start;
576 cconvert->pub.color_convert = rgb_gray_convert;
578 } else if (cinfo->in_color_space == JCS_YCbCr)
579 cconvert->pub.color_convert = grayscale_convert;
581 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
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;
605 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
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;
625 cconvert->pub.start_pass = rgb_ycc_start;
626 cconvert->pub.color_convert = rgb_ycc_convert;
628 } else if (cinfo->in_color_space == JCS_YCbCr)
629 cconvert->pub.color_convert = null_convert;
631 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
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;
640 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
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;
652 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
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;