blob: 6927e5ed1d8234b3e1655bed14dad95db0c7c02a [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdcolor.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding5829cb22012-01-15 00:00:00 +00006 * Modified 2011 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRCcf763c02013-01-01 09:51:37 +00008 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCa9b646c2012-03-11 22:06:54 +00009 * Copyright (C) 2009, 2011-2012, D. R. Commander.
DRC78df2e62014-05-12 09:23:57 +000010 * Copyright (C) 2013, Linaro Limited.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000011 * For conditions of distribution and use, see the accompanying README file.
12 *
13 * This file contains output colorspace conversion routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000014 */
15
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000017#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000019#include "jsimd.h"
DRCff6961f2014-04-20 09:17:11 +000020#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000021
22
23/* Private subobject */
24
25typedef struct {
26 struct jpeg_color_deconverter pub; /* public fields */
27
28 /* Private state for YCC->RGB conversion */
DRCe5eaf372014-05-09 18:00:32 +000029 int * Cr_r_tab; /* => table for Cr to R conversion */
30 int * Cb_b_tab; /* => table for Cb to B conversion */
31 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
32 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
Guido Vollbeding5829cb22012-01-15 00:00:00 +000033
34 /* Private state for RGB->Y conversion */
DRCe5eaf372014-05-09 18:00:32 +000035 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036} my_color_deconverter;
37
38typedef my_color_deconverter * my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000039
40
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000041/**************** YCbCr -> RGB conversion: most common case **************/
Guido Vollbeding5829cb22012-01-15 00:00:00 +000042/**************** RGB -> Y conversion: less common case **************/
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000043
44/*
45 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
46 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
47 * The conversion equations to be implemented are therefore
Guido Vollbeding5829cb22012-01-15 00:00:00 +000048 *
DRCe5eaf372014-05-09 18:00:32 +000049 * R = Y + 1.40200 * Cr
50 * G = Y - 0.34414 * Cb - 0.71414 * Cr
51 * B = Y + 1.77200 * Cb
Guido Vollbeding5829cb22012-01-15 00:00:00 +000052 *
DRCe5eaf372014-05-09 18:00:32 +000053 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
Guido Vollbeding5829cb22012-01-15 00:00:00 +000054 *
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000055 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000056 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000057 *
58 * To avoid floating-point arithmetic, we represent the fractional constants
Thomas G. Lane88aeed41992-12-10 00:00:00 +000059 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
60 * the products by 2^16, with appropriate rounding, to get the correct answer.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000061 * Notice that Y, being an integral input, does not contribute any fraction
62 * so it need not participate in the rounding.
63 *
64 * For even more speed, we avoid doing any multiplications in the inner loop
65 * by precalculating the constants times Cb and Cr for all possible values.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000066 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
67 * for 12-bit samples it is still acceptable. It's not very reasonable for
68 * 16-bit samples, but if you want lossless storage you shouldn't be changing
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000069 * colorspace anyway.
70 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
71 * values for the G calculation are left scaled up, since we must add them
72 * together before rounding.
73 */
74
DRCe5eaf372014-05-09 18:00:32 +000075#define SCALEBITS 16 /* speediest right-shift on some machines */
76#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
77#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000078
Guido Vollbeding5829cb22012-01-15 00:00:00 +000079/* We allocate one big table for RGB->Y conversion and divide it up into
80 * three parts, instead of doing three alloc_small requests. This lets us
81 * use a single table base address, which can be held in a register in the
82 * inner loops on many machines (more than can hold all three addresses,
83 * anyway).
84 */
85
DRCe5eaf372014-05-09 18:00:32 +000086#define R_Y_OFF 0 /* offset to R => Y section */
87#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
88#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
89#define TABLE_SIZE (3*(MAXJSAMPLE+1))
Guido Vollbeding5829cb22012-01-15 00:00:00 +000090
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000091
DRCb4570bb2011-09-07 06:31:00 +000092/* Include inline routines for colorspace extensions */
93
94#include "jdcolext.c"
95#undef RGB_RED
96#undef RGB_GREEN
97#undef RGB_BLUE
98#undef RGB_PIXELSIZE
99
100#define RGB_RED EXT_RGB_RED
101#define RGB_GREEN EXT_RGB_GREEN
102#define RGB_BLUE EXT_RGB_BLUE
103#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
104#define ycc_rgb_convert_internal ycc_extrgb_convert_internal
105#define gray_rgb_convert_internal gray_extrgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000106#define rgb_rgb_convert_internal rgb_extrgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000107#include "jdcolext.c"
108#undef RGB_RED
109#undef RGB_GREEN
110#undef RGB_BLUE
111#undef RGB_PIXELSIZE
112#undef ycc_rgb_convert_internal
113#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000114#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000115
116#define RGB_RED EXT_RGBX_RED
117#define RGB_GREEN EXT_RGBX_GREEN
118#define RGB_BLUE EXT_RGBX_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000119#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000120#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
121#define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
122#define gray_rgb_convert_internal gray_extrgbx_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000123#define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000124#include "jdcolext.c"
125#undef RGB_RED
126#undef RGB_GREEN
127#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000128#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000129#undef RGB_PIXELSIZE
130#undef ycc_rgb_convert_internal
131#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000132#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000133
134#define RGB_RED EXT_BGR_RED
135#define RGB_GREEN EXT_BGR_GREEN
136#define RGB_BLUE EXT_BGR_BLUE
137#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
138#define ycc_rgb_convert_internal ycc_extbgr_convert_internal
139#define gray_rgb_convert_internal gray_extbgr_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000140#define rgb_rgb_convert_internal rgb_extbgr_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000141#include "jdcolext.c"
142#undef RGB_RED
143#undef RGB_GREEN
144#undef RGB_BLUE
145#undef RGB_PIXELSIZE
146#undef ycc_rgb_convert_internal
147#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000148#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000149
150#define RGB_RED EXT_BGRX_RED
151#define RGB_GREEN EXT_BGRX_GREEN
152#define RGB_BLUE EXT_BGRX_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000153#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000154#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
155#define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
156#define gray_rgb_convert_internal gray_extbgrx_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000157#define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000158#include "jdcolext.c"
159#undef RGB_RED
160#undef RGB_GREEN
161#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000162#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000163#undef RGB_PIXELSIZE
164#undef ycc_rgb_convert_internal
165#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000166#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000167
168#define RGB_RED EXT_XBGR_RED
169#define RGB_GREEN EXT_XBGR_GREEN
170#define RGB_BLUE EXT_XBGR_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000171#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000172#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
173#define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
174#define gray_rgb_convert_internal gray_extxbgr_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000175#define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000176#include "jdcolext.c"
177#undef RGB_RED
178#undef RGB_GREEN
179#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000180#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000181#undef RGB_PIXELSIZE
182#undef ycc_rgb_convert_internal
183#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000184#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000185
186#define RGB_RED EXT_XRGB_RED
187#define RGB_GREEN EXT_XRGB_GREEN
188#define RGB_BLUE EXT_XRGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000189#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000190#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
191#define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
192#define gray_rgb_convert_internal gray_extxrgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000193#define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000194#include "jdcolext.c"
195#undef RGB_RED
196#undef RGB_GREEN
197#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000198#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000199#undef RGB_PIXELSIZE
200#undef ycc_rgb_convert_internal
201#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000202#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000203
204
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000205/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000206 * Initialize tables for YCC->RGB colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000207 */
208
Thomas G. Lane489583f1996-02-07 00:00:00 +0000209LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000210build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000211{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000212 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000213 int i;
214 INT32 x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000215 SHIFT_TEMPS
216
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217 cconvert->Cr_r_tab = (int *)
218 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000219 (MAXJSAMPLE+1) * sizeof(int));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 cconvert->Cb_b_tab = (int *)
221 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000222 (MAXJSAMPLE+1) * sizeof(int));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 cconvert->Cr_g_tab = (INT32 *)
224 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000225 (MAXJSAMPLE+1) * sizeof(INT32));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 cconvert->Cb_g_tab = (INT32 *)
227 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000228 (MAXJSAMPLE+1) * sizeof(INT32));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000229
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000230 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000231 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000232 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000233 /* Cr=>R value is nearest int to 1.40200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000234 cconvert->Cr_r_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000235 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000236 /* Cb=>B value is nearest int to 1.77200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 cconvert->Cb_b_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000238 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000239 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000240 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000241 /* Cb=>G value is scaled-up -0.34414 * x */
242 /* We also add in ONE_HALF so that need not do it in inner loop */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000243 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000244 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000245}
246
247
248/*
249 * Convert some rows of samples to the output colorspace.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000250 */
251
Thomas G. Lane489583f1996-02-07 00:00:00 +0000252METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000253ycc_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000254 JSAMPIMAGE input_buf, JDIMENSION input_row,
255 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000256{
DRCb4570bb2011-09-07 06:31:00 +0000257 switch (cinfo->out_color_space) {
258 case JCS_EXT_RGB:
259 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
260 num_rows);
261 break;
262 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000263 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000264 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
265 num_rows);
266 break;
267 case JCS_EXT_BGR:
268 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
269 num_rows);
270 break;
271 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000272 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000273 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
274 num_rows);
275 break;
276 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000277 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000278 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
279 num_rows);
280 break;
281 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000282 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000283 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
284 num_rows);
285 break;
286 default:
287 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
288 num_rows);
289 break;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000290 }
291}
292
293
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000294/**************** Cases other than YCbCr -> RGB **************/
295
296
297/*
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000298 * Initialize for RGB->grayscale colorspace conversion.
299 */
300
301LOCAL(void)
302build_rgb_y_table (j_decompress_ptr cinfo)
303{
304 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
305 INT32 * rgb_y_tab;
306 INT32 i;
307
308 /* Allocate and fill in the conversion tables. */
309 cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
310 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000311 (TABLE_SIZE * sizeof(INT32)));
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000312
313 for (i = 0; i <= MAXJSAMPLE; i++) {
314 rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
315 rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
316 rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
317 }
318}
319
320
321/*
322 * Convert RGB to grayscale.
323 */
324
325METHODDEF(void)
326rgb_gray_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000327 JSAMPIMAGE input_buf, JDIMENSION input_row,
328 JSAMPARRAY output_buf, int num_rows)
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000329{
330 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
331 register int r, g, b;
332 register INT32 * ctab = cconvert->rgb_y_tab;
333 register JSAMPROW outptr;
334 register JSAMPROW inptr0, inptr1, inptr2;
335 register JDIMENSION col;
336 JDIMENSION num_cols = cinfo->output_width;
337
338 while (--num_rows >= 0) {
339 inptr0 = input_buf[0][input_row];
340 inptr1 = input_buf[1][input_row];
341 inptr2 = input_buf[2][input_row];
342 input_row++;
343 outptr = *output_buf++;
344 for (col = 0; col < num_cols; col++) {
345 r = GETJSAMPLE(inptr0[col]);
346 g = GETJSAMPLE(inptr1[col]);
347 b = GETJSAMPLE(inptr2[col]);
348 /* Y */
349 outptr[col] = (JSAMPLE)
DRCe5eaf372014-05-09 18:00:32 +0000350 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
351 >> SCALEBITS);
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000352 }
353 }
354}
355
356
357/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 * Color conversion for no colorspace change: just copy the data,
359 * converting from separate-planes to interleaved representation.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000360 */
361
Thomas G. Lane489583f1996-02-07 00:00:00 +0000362METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363null_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000364 JSAMPIMAGE input_buf, JDIMENSION input_row,
365 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000366{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 register JSAMPROW inptr, outptr;
368 register JDIMENSION count;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000369 register int num_components = cinfo->num_components;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 JDIMENSION num_cols = cinfo->output_width;
371 int ci;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000372
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 while (--num_rows >= 0) {
374 for (ci = 0; ci < num_components; ci++) {
375 inptr = input_buf[ci][input_row];
376 outptr = output_buf[0] + ci;
377 for (count = num_cols; count > 0; count--) {
DRCe5eaf372014-05-09 18:00:32 +0000378 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
379 outptr += num_components;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380 }
381 }
382 input_row++;
383 output_buf++;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000384 }
385}
386
387
388/*
389 * Color conversion for grayscale: just copy the data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390 * This also works for YCbCr -> grayscale conversion, in which
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000391 * we just copy the Y (luminance) component and ignore chrominance.
392 */
393
Thomas G. Lane489583f1996-02-07 00:00:00 +0000394METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395grayscale_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000396 JSAMPIMAGE input_buf, JDIMENSION input_row,
397 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000398{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
DRCe5eaf372014-05-09 18:00:32 +0000400 num_rows, cinfo->output_width);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000401}
402
403
404/*
DRCb4570bb2011-09-07 06:31:00 +0000405 * Convert grayscale to RGB
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000406 */
407
408METHODDEF(void)
409gray_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000410 JSAMPIMAGE input_buf, JDIMENSION input_row,
411 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000412{
DRCb4570bb2011-09-07 06:31:00 +0000413 switch (cinfo->out_color_space) {
414 case JCS_EXT_RGB:
415 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
416 num_rows);
417 break;
418 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000419 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000420 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
421 num_rows);
422 break;
423 case JCS_EXT_BGR:
424 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
425 num_rows);
426 break;
427 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000428 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000429 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
430 num_rows);
431 break;
432 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000433 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000434 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
435 num_rows);
436 break;
437 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000438 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000439 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
440 num_rows);
441 break;
442 default:
443 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
444 num_rows);
445 break;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000446 }
447}
448
449
450/*
DRCa9b646c2012-03-11 22:06:54 +0000451 * Convert plain RGB to extended RGB
452 */
453
454METHODDEF(void)
455rgb_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000456 JSAMPIMAGE input_buf, JDIMENSION input_row,
457 JSAMPARRAY output_buf, int num_rows)
DRCa9b646c2012-03-11 22:06:54 +0000458{
459 switch (cinfo->out_color_space) {
460 case JCS_EXT_RGB:
461 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
462 num_rows);
463 break;
464 case JCS_EXT_RGBX:
465 case JCS_EXT_RGBA:
466 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
467 num_rows);
468 break;
469 case JCS_EXT_BGR:
470 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
471 num_rows);
472 break;
473 case JCS_EXT_BGRX:
474 case JCS_EXT_BGRA:
475 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
476 num_rows);
477 break;
478 case JCS_EXT_XBGR:
479 case JCS_EXT_ABGR:
480 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
481 num_rows);
482 break;
483 case JCS_EXT_XRGB:
484 case JCS_EXT_ARGB:
485 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
486 num_rows);
487 break;
488 default:
489 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
490 num_rows);
491 break;
492 }
493}
494
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000495
DRCa9b646c2012-03-11 22:06:54 +0000496/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000497 * Adobe-style YCCK->CMYK conversion.
498 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
499 * conversion as above, while passing K (black) unchanged.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000500 * We assume build_ycc_rgb_table has been called.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000501 */
502
Thomas G. Lane489583f1996-02-07 00:00:00 +0000503METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000504ycck_cmyk_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000505 JSAMPIMAGE input_buf, JDIMENSION input_row,
506 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000507{
508 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
509 register int y, cb, cr;
510 register JSAMPROW outptr;
511 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
512 register JDIMENSION col;
513 JDIMENSION num_cols = cinfo->output_width;
514 /* copy these pointers into registers if possible */
515 register JSAMPLE * range_limit = cinfo->sample_range_limit;
516 register int * Crrtab = cconvert->Cr_r_tab;
517 register int * Cbbtab = cconvert->Cb_b_tab;
518 register INT32 * Crgtab = cconvert->Cr_g_tab;
519 register INT32 * Cbgtab = cconvert->Cb_g_tab;
520 SHIFT_TEMPS
521
522 while (--num_rows >= 0) {
523 inptr0 = input_buf[0][input_row];
524 inptr1 = input_buf[1][input_row];
525 inptr2 = input_buf[2][input_row];
526 inptr3 = input_buf[3][input_row];
527 input_row++;
528 outptr = *output_buf++;
529 for (col = 0; col < num_cols; col++) {
530 y = GETJSAMPLE(inptr0[col]);
531 cb = GETJSAMPLE(inptr1[col]);
532 cr = GETJSAMPLE(inptr2[col]);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000533 /* Range-limiting is essential due to noise introduced by DCT losses. */
DRCe5eaf372014-05-09 18:00:32 +0000534 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
535 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
536 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
537 SCALEBITS)))];
538 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000539 /* K passes through unchanged */
DRCe5eaf372014-05-09 18:00:32 +0000540 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000541 outptr += 4;
542 }
543 }
544}
545
546
DRC78df2e62014-05-12 09:23:57 +0000547#include "jdcol565.c"
548
549
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550/*
551 * Empty method for start_pass.
552 */
553
Thomas G. Lane489583f1996-02-07 00:00:00 +0000554METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000555start_pass_dcolor (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000556{
557 /* no work needed */
558}
559
560
561/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000562 * Module initialization routine for output colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000563 */
564
Thomas G. Lane489583f1996-02-07 00:00:00 +0000565GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000566jinit_color_deconverter (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000567{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000568 my_cconvert_ptr cconvert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000569 int ci;
570
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000571 cconvert = (my_cconvert_ptr)
572 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000573 sizeof(my_color_deconverter));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000574 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000575 cconvert->pub.start_pass = start_pass_dcolor;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000576
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000577 /* Make sure num_components agrees with jpeg_color_space */
578 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000579 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000580 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000581 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000582 break;
583
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000584 case JCS_RGB:
585 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000586 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000587 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000588 break;
589
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000590 case JCS_CMYK:
591 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000592 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000593 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000594 break;
595
DRCe5eaf372014-05-09 18:00:32 +0000596 default: /* JCS_UNKNOWN can be anything */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000597 if (cinfo->num_components < 1)
598 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000599 break;
600 }
601
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000602 /* Set out_color_components and conversion method based on requested space.
603 * Also clear the component_needed flags for any unused components,
604 * so that earlier pipeline stages can avoid useless computation.
605 */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000606
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000607 switch (cinfo->out_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000608 case JCS_GRAYSCALE:
609 cinfo->out_color_components = 1;
610 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
DRCe5eaf372014-05-09 18:00:32 +0000611 cinfo->jpeg_color_space == JCS_YCbCr) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000612 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000613 /* For color->grayscale conversion, only the Y (0) component is needed */
614 for (ci = 1; ci < cinfo->num_components; ci++)
DRCe5eaf372014-05-09 18:00:32 +0000615 cinfo->comp_info[ci].component_needed = FALSE;
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000616 } else if (cinfo->jpeg_color_space == JCS_RGB) {
617 cconvert->pub.color_convert = rgb_gray_convert;
618 build_rgb_y_table(cinfo);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000619 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000620 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000621 break;
622
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000623 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000624 case JCS_EXT_RGB:
625 case JCS_EXT_RGBX:
626 case JCS_EXT_BGR:
627 case JCS_EXT_BGRX:
628 case JCS_EXT_XBGR:
629 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000630 case JCS_EXT_RGBA:
631 case JCS_EXT_BGRA:
632 case JCS_EXT_ABGR:
633 case JCS_EXT_ARGB:
DRCf25c0712009-04-03 12:00:51 +0000634 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000635 if (cinfo->jpeg_color_space == JCS_YCbCr) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000636 if (jsimd_can_ycc_rgb())
637 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
638 else {
639 cconvert->pub.color_convert = ycc_rgb_convert;
640 build_ycc_rgb_table(cinfo);
641 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000642 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
643 cconvert->pub.color_convert = gray_rgb_convert;
DRCa9b646c2012-03-11 22:06:54 +0000644 } else if (cinfo->jpeg_color_space == JCS_RGB) {
645 if (rgb_red[cinfo->out_color_space] == 0 &&
646 rgb_green[cinfo->out_color_space] == 1 &&
647 rgb_blue[cinfo->out_color_space] == 2 &&
648 rgb_pixelsize[cinfo->out_color_space] == 3)
649 cconvert->pub.color_convert = null_convert;
650 else
651 cconvert->pub.color_convert = rgb_rgb_convert;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000652 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000653 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
654 break;
655
DRC78df2e62014-05-12 09:23:57 +0000656 case JCS_RGB565:
657 cinfo->out_color_components = 3;
658 if (cinfo->dither_mode == JDITHER_NONE) {
659 if (cinfo->jpeg_color_space == JCS_YCbCr) {
660 cconvert->pub.color_convert = ycc_rgb565_convert;
661 build_ycc_rgb_table(cinfo);
662 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
663 cconvert->pub.color_convert = gray_rgb565_convert;
664 } else if (cinfo->jpeg_color_space == JCS_RGB) {
665 cconvert->pub.color_convert = rgb_rgb565_convert;
666 } else
667 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
668 } else {
669 /* only ordered dithering is supported */
670 if (cinfo->jpeg_color_space == JCS_YCbCr) {
671 cconvert->pub.color_convert = ycc_rgb565D_convert;
672 build_ycc_rgb_table(cinfo);
673 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
674 cconvert->pub.color_convert = gray_rgb565D_convert;
675 } else if (cinfo->jpeg_color_space == JCS_RGB) {
676 cconvert->pub.color_convert = rgb_rgb565D_convert;
677 } else
678 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
679 }
680 break;
681
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000682 case JCS_CMYK:
683 cinfo->out_color_components = 4;
684 if (cinfo->jpeg_color_space == JCS_YCCK) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000685 cconvert->pub.color_convert = ycck_cmyk_convert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000686 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000687 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
688 cconvert->pub.color_convert = null_convert;
689 } else
690 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000691 break;
692
693 default:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000694 /* Permit null conversion to same output space */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000695 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000696 cinfo->out_color_components = cinfo->num_components;
697 cconvert->pub.color_convert = null_convert;
DRCe5eaf372014-05-09 18:00:32 +0000698 } else /* unsupported non-null conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000699 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000700 break;
701 }
702
703 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000704 cinfo->output_components = 1; /* single colormapped output component */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000705 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000706 cinfo->output_components = cinfo->out_color_components;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000707}