blob: 94b41840b59610d78c05d5382000848d73a7ccff [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jccolor.c
3 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00006 * libjpeg-turbo Modifications:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +00008 * Copyright (C) 2009-2012, D. R. Commander.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains input colorspace conversion routines.
12 */
13
14#define JPEG_INTERNALS
15#include "jinclude.h"
16#include "jpeglib.h"
17#include "jsimd.h"
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +000018#include "config.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000019
20
21/* Private subobject */
22
23typedef struct {
24 struct jpeg_color_converter pub; /* public fields */
25
26 /* Private state for RGB->YCC conversion */
27 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
28} my_color_converter;
29
30typedef my_color_converter * my_cconvert_ptr;
31
32
33/**************** RGB -> YCbCr conversion: most common case **************/
34
35/*
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.
48 *
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.
52 *
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
58 * colorspace anyway.
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.
61 */
62
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))
67
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).
72 */
73
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))
84
85
hbono@chromium.orgc6beb742011-11-29 05:16:26 +000086/* Include inline routines for colorspace extensions */
87
88#include "jccolext.c"
89#undef RGB_RED
90#undef RGB_GREEN
91#undef RGB_BLUE
92#undef RGB_PIXELSIZE
93
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000100#define rgb_rgb_convert_internal extrgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000101#include "jccolext.c"
102#undef RGB_RED
103#undef RGB_GREEN
104#undef RGB_BLUE
105#undef RGB_PIXELSIZE
106#undef rgb_ycc_convert_internal
107#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000108#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000109
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000116#define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000117#include "jccolext.c"
118#undef RGB_RED
119#undef RGB_GREEN
120#undef RGB_BLUE
121#undef RGB_PIXELSIZE
122#undef rgb_ycc_convert_internal
123#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000124#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000125
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000132#define rgb_rgb_convert_internal extbgr_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000133#include "jccolext.c"
134#undef RGB_RED
135#undef RGB_GREEN
136#undef RGB_BLUE
137#undef RGB_PIXELSIZE
138#undef rgb_ycc_convert_internal
139#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000140#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000141
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000148#define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000149#include "jccolext.c"
150#undef RGB_RED
151#undef RGB_GREEN
152#undef RGB_BLUE
153#undef RGB_PIXELSIZE
154#undef rgb_ycc_convert_internal
155#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000156#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000157
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000164#define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000165#include "jccolext.c"
166#undef RGB_RED
167#undef RGB_GREEN
168#undef RGB_BLUE
169#undef RGB_PIXELSIZE
170#undef rgb_ycc_convert_internal
171#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000172#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000173
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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000180#define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000181#include "jccolext.c"
182#undef RGB_RED
183#undef RGB_GREEN
184#undef RGB_BLUE
185#undef RGB_PIXELSIZE
186#undef rgb_ycc_convert_internal
187#undef rgb_gray_convert_internal
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000188#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000189
190
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000191/*
192 * Initialize for RGB->YCC colorspace conversion.
193 */
194
195METHODDEF(void)
196rgb_ycc_start (j_compress_ptr cinfo)
197{
198 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
199 INT32 * rgb_ycc_tab;
200 INT32 i;
201
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)));
206
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.
216 */
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;
220*/
221 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
222 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
223 }
224}
225
226
227/*
228 * Convert some rows of samples to the JPEG colorspace.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000229 */
230
231METHODDEF(void)
232rgb_ycc_convert (j_compress_ptr cinfo,
233 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
234 JDIMENSION output_row, int num_rows)
235{
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000236 switch (cinfo->in_color_space) {
237 case JCS_EXT_RGB:
238 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
239 num_rows);
240 break;
241 case JCS_EXT_RGBX:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000242 case JCS_EXT_RGBA:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000243 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
244 num_rows);
245 break;
246 case JCS_EXT_BGR:
247 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
248 num_rows);
249 break;
250 case JCS_EXT_BGRX:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000251 case JCS_EXT_BGRA:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000252 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
253 num_rows);
254 break;
255 case JCS_EXT_XBGR:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000256 case JCS_EXT_ABGR:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000257 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
258 num_rows);
259 break;
260 case JCS_EXT_XRGB:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000261 case JCS_EXT_ARGB:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000262 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
263 num_rows);
264 break;
265 default:
266 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
267 num_rows);
268 break;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000269 }
270}
271
272
273/**************** Cases other than RGB -> YCbCr **************/
274
275
276/*
277 * Convert some rows of samples to the JPEG colorspace.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000278 */
279
280METHODDEF(void)
281rgb_gray_convert (j_compress_ptr cinfo,
282 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
283 JDIMENSION output_row, int num_rows)
284{
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000285 switch (cinfo->in_color_space) {
286 case JCS_EXT_RGB:
287 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
288 num_rows);
289 break;
290 case JCS_EXT_RGBX:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000291 case JCS_EXT_RGBA:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000292 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
293 num_rows);
294 break;
295 case JCS_EXT_BGR:
296 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
297 num_rows);
298 break;
299 case JCS_EXT_BGRX:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000300 case JCS_EXT_BGRA:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000301 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
302 num_rows);
303 break;
304 case JCS_EXT_XBGR:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000305 case JCS_EXT_ABGR:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000306 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
307 num_rows);
308 break;
309 case JCS_EXT_XRGB:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000310 case JCS_EXT_ARGB:
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000311 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
312 num_rows);
313 break;
314 default:
315 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
316 num_rows);
317 break;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000318 }
319}
320
321
322/*
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000323 * Extended RGB to plain RGB conversion
324 */
325
326METHODDEF(void)
327rgb_rgb_convert (j_compress_ptr cinfo,
328 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
329 JDIMENSION output_row, int num_rows)
330{
331 switch (cinfo->in_color_space) {
332 case JCS_EXT_RGB:
333 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
334 num_rows);
335 break;
336 case JCS_EXT_RGBX:
337 case JCS_EXT_RGBA:
338 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
339 num_rows);
340 break;
341 case JCS_EXT_BGR:
342 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
343 num_rows);
344 break;
345 case JCS_EXT_BGRX:
346 case JCS_EXT_BGRA:
347 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
348 num_rows);
349 break;
350 case JCS_EXT_XBGR:
351 case JCS_EXT_ABGR:
352 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
353 num_rows);
354 break;
355 case JCS_EXT_XRGB:
356 case JCS_EXT_ARGB:
357 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
358 num_rows);
359 break;
360 default:
361 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
362 num_rows);
363 break;
364 }
365}
366
367
368/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000369 * 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.
374 */
375
376METHODDEF(void)
377cmyk_ycck_convert (j_compress_ptr cinfo,
378 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
379 JDIMENSION output_row, int num_rows)
380{
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;
388
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];
395 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 */
402 inptr += 4;
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.
407 */
408 /* Y */
409 outptr0[col] = (JSAMPLE)
410 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
411 >> SCALEBITS);
412 /* Cb */
413 outptr1[col] = (JSAMPLE)
414 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
415 >> SCALEBITS);
416 /* Cr */
417 outptr2[col] = (JSAMPLE)
418 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
419 >> SCALEBITS);
420 }
421 }
422}
423
424
425/*
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).
429 */
430
431METHODDEF(void)
432grayscale_convert (j_compress_ptr cinfo,
433 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
434 JDIMENSION output_row, int num_rows)
435{
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;
441
442 while (--num_rows >= 0) {
443 inptr = *input_buf++;
444 outptr = output_buf[0][output_row];
445 output_row++;
446 for (col = 0; col < num_cols; col++) {
447 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
448 inptr += instride;
449 }
450 }
451}
452
453
454/*
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.
458 */
459
460METHODDEF(void)
461null_convert (j_compress_ptr cinfo,
462 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
463 JDIMENSION output_row, int num_rows)
464{
465 register JSAMPROW inptr;
466 register JSAMPROW outptr;
467 register JDIMENSION col;
468 register int ci;
469 int nc = cinfo->num_components;
470 JDIMENSION num_cols = cinfo->image_width;
471
472 while (--num_rows >= 0) {
473 /* It seems fastest to make a separate pass for each component. */
474 for (ci = 0; ci < nc; ci++) {
475 inptr = *input_buf;
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 */
479 inptr += nc;
480 }
481 }
482 input_buf++;
483 output_row++;
484 }
485}
486
487
488/*
489 * Empty method for start_pass.
490 */
491
492METHODDEF(void)
493null_method (j_compress_ptr cinfo)
494{
495 /* no work needed */
496}
497
498
499/*
500 * Module initialization routine for input colorspace conversion.
501 */
502
503GLOBAL(void)
504jinit_color_converter (j_compress_ptr cinfo)
505{
506 my_cconvert_ptr cconvert;
507
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;
514
515 /* Make sure input_components agrees with in_color_space */
516 switch (cinfo->in_color_space) {
517 case JCS_GRAYSCALE:
518 if (cinfo->input_components != 1)
519 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
520 break;
521
522 case JCS_RGB:
523 case JCS_EXT_RGB:
524 case JCS_EXT_RGBX:
525 case JCS_EXT_BGR:
526 case JCS_EXT_BGRX:
527 case JCS_EXT_XBGR:
528 case JCS_EXT_XRGB:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000529 case JCS_EXT_RGBA:
530 case JCS_EXT_BGRA:
531 case JCS_EXT_ABGR:
532 case JCS_EXT_ARGB:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000533 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
534 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
535 break;
536
537 case JCS_YCbCr:
538 if (cinfo->input_components != 3)
539 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
540 break;
541
542 case JCS_CMYK:
543 case JCS_YCCK:
544 if (cinfo->input_components != 4)
545 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
546 break;
547
548 default: /* JCS_UNKNOWN can be anything */
549 if (cinfo->input_components < 1)
550 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
551 break;
552 }
553
554 /* Check num_components, set conversion method based on requested space */
555 switch (cinfo->jpeg_color_space) {
556 case JCS_GRAYSCALE:
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 ||
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000567 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) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000572 if (jsimd_can_rgb_gray())
573 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
574 else {
575 cconvert->pub.start_pass = rgb_ycc_start;
576 cconvert->pub.color_convert = rgb_gray_convert;
577 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000578 } else if (cinfo->in_color_space == JCS_YCbCr)
579 cconvert->pub.color_convert = grayscale_convert;
580 else
581 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
582 break;
583
584 case JCS_RGB:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000585 if (cinfo->num_components != 3)
586 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000587 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)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000591 cconvert->pub.color_convert = null_convert;
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000592 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;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000604 else
605 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
606 break;
607
608 case JCS_YCbCr:
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 ||
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000617 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) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000622 if (jsimd_can_rgb_ycc())
623 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
624 else {
625 cconvert->pub.start_pass = rgb_ycc_start;
626 cconvert->pub.color_convert = rgb_ycc_convert;
627 }
628 } else if (cinfo->in_color_space == JCS_YCbCr)
629 cconvert->pub.color_convert = null_convert;
630 else
631 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
632 break;
633
634 case JCS_CMYK:
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;
639 else
640 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
641 break;
642
643 case JCS_YCCK:
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;
651 else
652 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
653 break;
654
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;
660 break;
661 }
662}