blob: 3a0772bb5a77223e99930d642942a7857f074d8c [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jccolor.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1991-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCa9b646c2012-03-11 22:06:54 +00006 * Copyright (C) 2009-2012, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains input colorspace conversion routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000011 */
12
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000013#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000014#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000016#include "jsimd.h"
DRCa7466c92012-01-26 22:20:31 +000017#include "config.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000018
19
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000020/* Private subobject */
21
22typedef struct {
23 struct jpeg_color_converter pub; /* public fields */
24
25 /* Private state for RGB->YCC conversion */
26 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
27} my_color_converter;
28
29typedef my_color_converter * my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000030
31
Thomas G. Lane88aeed41992-12-10 00:00:00 +000032/**************** RGB -> YCbCr conversion: most common case **************/
33
34/*
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
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000039 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
40 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
Thomas G. Lane88aeed41992-12-10 00:00:00 +000041 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000042 * 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.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000047 *
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.
51 *
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
57 * colorspace anyway.
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000058 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
Thomas G. Lane88aeed41992-12-10 00:00:00 +000059 * in the tables to save adding them separately in the inner loop.
60 */
61
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062#define SCALEBITS 16 /* speediest right-shift on some machines */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000063#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
Thomas G. Lane88aeed41992-12-10 00:00:00 +000064#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
65#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
66
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).
71 */
72
Thomas G. Lane88aeed41992-12-10 00:00:00 +000073#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))
83
84
DRCb4570bb2011-09-07 06:31:00 +000085/* Include inline routines for colorspace extensions */
86
87#include "jccolext.c"
88#undef RGB_RED
89#undef RGB_GREEN
90#undef RGB_BLUE
91#undef RGB_PIXELSIZE
92
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
DRCa9b646c2012-03-11 22:06:54 +000099#define rgb_rgb_convert_internal extrgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000100#include "jccolext.c"
101#undef RGB_RED
102#undef RGB_GREEN
103#undef RGB_BLUE
104#undef RGB_PIXELSIZE
105#undef rgb_ycc_convert_internal
106#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000107#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000108
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
DRCa9b646c2012-03-11 22:06:54 +0000115#define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000116#include "jccolext.c"
117#undef RGB_RED
118#undef RGB_GREEN
119#undef RGB_BLUE
120#undef RGB_PIXELSIZE
121#undef rgb_ycc_convert_internal
122#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000123#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000124
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
DRCa9b646c2012-03-11 22:06:54 +0000131#define rgb_rgb_convert_internal extbgr_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000132#include "jccolext.c"
133#undef RGB_RED
134#undef RGB_GREEN
135#undef RGB_BLUE
136#undef RGB_PIXELSIZE
137#undef rgb_ycc_convert_internal
138#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000139#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000140
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
DRCa9b646c2012-03-11 22:06:54 +0000147#define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000148#include "jccolext.c"
149#undef RGB_RED
150#undef RGB_GREEN
151#undef RGB_BLUE
152#undef RGB_PIXELSIZE
153#undef rgb_ycc_convert_internal
154#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000155#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000156
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
DRCa9b646c2012-03-11 22:06:54 +0000163#define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000164#include "jccolext.c"
165#undef RGB_RED
166#undef RGB_GREEN
167#undef RGB_BLUE
168#undef RGB_PIXELSIZE
169#undef rgb_ycc_convert_internal
170#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000171#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000172
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
DRCa9b646c2012-03-11 22:06:54 +0000179#define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000180#include "jccolext.c"
181#undef RGB_RED
182#undef RGB_GREEN
183#undef RGB_BLUE
184#undef RGB_PIXELSIZE
185#undef rgb_ycc_convert_internal
186#undef rgb_gray_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000187#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000188
189
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000190/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191 * Initialize for RGB->YCC colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000192 */
193
Thomas G. Lane489583f1996-02-07 00:00:00 +0000194METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195rgb_ycc_start (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000196{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
198 INT32 * rgb_ycc_tab;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000199 INT32 i;
200
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000201 /* Allocate and fill in the conversion tables. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
203 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
204 (TABLE_SIZE * SIZEOF(INT32)));
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000205
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;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000212 /* 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.
215 */
216 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000217/* B=>Cb and R=>Cr tables are the same
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000218 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000219*/
220 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
221 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
222 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000223}
224
225
226/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000228 */
229
Thomas G. Lane489583f1996-02-07 00:00:00 +0000230METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231rgb_ycc_convert (j_compress_ptr cinfo,
232 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
233 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000234{
DRCb4570bb2011-09-07 06:31:00 +0000235 switch (cinfo->in_color_space) {
236 case JCS_EXT_RGB:
237 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
238 num_rows);
239 break;
240 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000241 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000242 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
243 num_rows);
244 break;
245 case JCS_EXT_BGR:
246 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
247 num_rows);
248 break;
249 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000250 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000251 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
252 num_rows);
253 break;
254 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000255 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000256 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
257 num_rows);
258 break;
259 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000260 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000261 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
262 num_rows);
263 break;
264 default:
265 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
266 num_rows);
267 break;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000268 }
269}
270
271
272/**************** Cases other than RGB -> YCbCr **************/
273
274
275/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000277 */
278
Thomas G. Lane489583f1996-02-07 00:00:00 +0000279METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280rgb_gray_convert (j_compress_ptr cinfo,
281 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
282 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000283{
DRCb4570bb2011-09-07 06:31:00 +0000284 switch (cinfo->in_color_space) {
285 case JCS_EXT_RGB:
286 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
287 num_rows);
288 break;
289 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000290 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000291 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
292 num_rows);
293 break;
294 case JCS_EXT_BGR:
295 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
296 num_rows);
297 break;
298 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000299 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000300 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
301 num_rows);
302 break;
303 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000304 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000305 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
306 num_rows);
307 break;
308 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000309 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000310 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
311 num_rows);
312 break;
313 default:
314 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
315 num_rows);
316 break;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000317 }
318}
319
320
321/*
DRCa9b646c2012-03-11 22:06:54 +0000322 * Extended RGB to plain RGB conversion
323 */
324
325METHODDEF(void)
326rgb_rgb_convert (j_compress_ptr cinfo,
327 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
328 JDIMENSION output_row, int num_rows)
329{
330 switch (cinfo->in_color_space) {
331 case JCS_EXT_RGB:
332 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
333 num_rows);
334 break;
335 case JCS_EXT_RGBX:
336 case JCS_EXT_RGBA:
337 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
338 num_rows);
339 break;
340 case JCS_EXT_BGR:
341 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
342 num_rows);
343 break;
344 case JCS_EXT_BGRX:
345 case JCS_EXT_BGRA:
346 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
347 num_rows);
348 break;
349 case JCS_EXT_XBGR:
350 case JCS_EXT_ABGR:
351 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
352 num_rows);
353 break;
354 case JCS_EXT_XRGB:
355 case JCS_EXT_ARGB:
356 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
357 num_rows);
358 break;
359 default:
360 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
361 num_rows);
362 break;
363 }
364}
365
366
367/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368 * 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.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000373 */
374
Thomas G. Lane489583f1996-02-07 00:00:00 +0000375METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376cmyk_ycck_convert (j_compress_ptr cinfo,
377 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
378 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000379{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380 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;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000387
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 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];
394 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 */
401 inptr += 4;
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.
406 */
407 /* Y */
408 outptr0[col] = (JSAMPLE)
409 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
410 >> SCALEBITS);
411 /* Cb */
412 outptr1[col] = (JSAMPLE)
413 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
414 >> SCALEBITS);
415 /* Cr */
416 outptr2[col] = (JSAMPLE)
417 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
418 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000419 }
420 }
421}
422
423
424/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425 * 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).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000428 */
429
Thomas G. Lane489583f1996-02-07 00:00:00 +0000430METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431grayscale_convert (j_compress_ptr cinfo,
432 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
433 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000434{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435 register JSAMPROW inptr;
436 register JSAMPROW outptr;
437 register JDIMENSION col;
438 JDIMENSION num_cols = cinfo->image_width;
439 int instride = cinfo->input_components;
440
441 while (--num_rows >= 0) {
442 inptr = *input_buf++;
443 outptr = output_buf[0][output_row];
444 output_row++;
445 for (col = 0; col < num_cols; col++) {
446 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
447 inptr += instride;
448 }
449 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000450}
451
452
453/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454 * 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.
457 */
458
Thomas G. Lane489583f1996-02-07 00:00:00 +0000459METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000460null_convert (j_compress_ptr cinfo,
461 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
462 JDIMENSION output_row, int num_rows)
463{
464 register JSAMPROW inptr;
465 register JSAMPROW outptr;
466 register JDIMENSION col;
467 register int ci;
468 int nc = cinfo->num_components;
469 JDIMENSION num_cols = cinfo->image_width;
470
471 while (--num_rows >= 0) {
472 /* It seems fastest to make a separate pass for each component. */
473 for (ci = 0; ci < nc; ci++) {
474 inptr = *input_buf;
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 */
478 inptr += nc;
479 }
480 }
481 input_buf++;
482 output_row++;
483 }
484}
485
486
487/*
488 * Empty method for start_pass.
489 */
490
Thomas G. Lane489583f1996-02-07 00:00:00 +0000491METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000492null_method (j_compress_ptr cinfo)
493{
494 /* no work needed */
495}
496
497
498/*
499 * Module initialization routine for input colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000500 */
501
Thomas G. Lane489583f1996-02-07 00:00:00 +0000502GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000503jinit_color_converter (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000504{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000505 my_cconvert_ptr cconvert;
506
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;
513
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000514 /* Make sure input_components agrees with in_color_space */
515 switch (cinfo->in_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000516 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000517 if (cinfo->input_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000518 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000519 break;
520
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000522 case JCS_EXT_RGB:
523 case JCS_EXT_RGBX:
524 case JCS_EXT_BGR:
525 case JCS_EXT_BGRX:
526 case JCS_EXT_XBGR:
527 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000528 case JCS_EXT_RGBA:
529 case JCS_EXT_BGRA:
530 case JCS_EXT_ABGR:
531 case JCS_EXT_ARGB:
DRCf25c0712009-04-03 12:00:51 +0000532 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
533 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
534 break;
535
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000536 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000537 if (cinfo->input_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000538 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000539 break;
540
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000541 case JCS_CMYK:
542 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000543 if (cinfo->input_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000544 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000545 break;
546
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000547 default: /* JCS_UNKNOWN can be anything */
548 if (cinfo->input_components < 1)
549 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000550 break;
551 }
552
553 /* Check num_components, set conversion method based on requested space */
554 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000555 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000556 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000557 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
558 if (cinfo->in_color_space == JCS_GRAYSCALE)
559 cconvert->pub.color_convert = grayscale_convert;
DRCf25c0712009-04-03 12:00:51 +0000560 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 ||
DRC67ce3b22011-12-19 02:21:03 +0000566 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) {
DRCc8666332011-02-18 11:23:45 +0000571 if (jsimd_can_rgb_gray())
572 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
573 else {
574 cconvert->pub.start_pass = rgb_ycc_start;
575 cconvert->pub.color_convert = rgb_gray_convert;
576 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000577 } else if (cinfo->in_color_space == JCS_YCbCr)
578 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000579 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000580 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000581 break;
582
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 case JCS_RGB:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000584 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCa9b646c2012-03-11 22:06:54 +0000586 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)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000590 cconvert->pub.color_convert = null_convert;
DRCa9b646c2012-03-11 22:06:54 +0000591 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;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000603 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000604 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000605 break;
606
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000607 case JCS_YCbCr:
608 if (cinfo->num_components != 3)
609 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000610 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 ||
DRC67ce3b22011-12-19 02:21:03 +0000616 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) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000621 if (jsimd_can_rgb_ycc())
622 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
623 else {
624 cconvert->pub.start_pass = rgb_ycc_start;
625 cconvert->pub.color_convert = rgb_ycc_convert;
626 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000627 } else if (cinfo->in_color_space == JCS_YCbCr)
628 cconvert->pub.color_convert = null_convert;
629 else
630 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631 break;
632
633 case JCS_CMYK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000634 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000635 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
636 if (cinfo->in_color_space == JCS_CMYK)
637 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000638 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000639 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000640 break;
641
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000642 case JCS_YCCK:
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;
650 else
651 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652 break;
653
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;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000659 break;
660 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000661}