blob: 973055574249ad163473beae65e1be8dad95e778 [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
DRCb4570bb2011-09-07 06:31:00 +00006 * Copyright (C) 2009-2011, 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
99#include "jccolext.c"
100#undef RGB_RED
101#undef RGB_GREEN
102#undef RGB_BLUE
103#undef RGB_PIXELSIZE
104#undef rgb_ycc_convert_internal
105#undef rgb_gray_convert_internal
106
107#define RGB_RED EXT_RGBX_RED
108#define RGB_GREEN EXT_RGBX_GREEN
109#define RGB_BLUE EXT_RGBX_BLUE
110#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
111#define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
112#define rgb_gray_convert_internal extrgbx_gray_convert_internal
113#include "jccolext.c"
114#undef RGB_RED
115#undef RGB_GREEN
116#undef RGB_BLUE
117#undef RGB_PIXELSIZE
118#undef rgb_ycc_convert_internal
119#undef rgb_gray_convert_internal
120
121#define RGB_RED EXT_BGR_RED
122#define RGB_GREEN EXT_BGR_GREEN
123#define RGB_BLUE EXT_BGR_BLUE
124#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
125#define rgb_ycc_convert_internal extbgr_ycc_convert_internal
126#define rgb_gray_convert_internal extbgr_gray_convert_internal
127#include "jccolext.c"
128#undef RGB_RED
129#undef RGB_GREEN
130#undef RGB_BLUE
131#undef RGB_PIXELSIZE
132#undef rgb_ycc_convert_internal
133#undef rgb_gray_convert_internal
134
135#define RGB_RED EXT_BGRX_RED
136#define RGB_GREEN EXT_BGRX_GREEN
137#define RGB_BLUE EXT_BGRX_BLUE
138#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
139#define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
140#define rgb_gray_convert_internal extbgrx_gray_convert_internal
141#include "jccolext.c"
142#undef RGB_RED
143#undef RGB_GREEN
144#undef RGB_BLUE
145#undef RGB_PIXELSIZE
146#undef rgb_ycc_convert_internal
147#undef rgb_gray_convert_internal
148
149#define RGB_RED EXT_XBGR_RED
150#define RGB_GREEN EXT_XBGR_GREEN
151#define RGB_BLUE EXT_XBGR_BLUE
152#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
153#define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
154#define rgb_gray_convert_internal extxbgr_gray_convert_internal
155#include "jccolext.c"
156#undef RGB_RED
157#undef RGB_GREEN
158#undef RGB_BLUE
159#undef RGB_PIXELSIZE
160#undef rgb_ycc_convert_internal
161#undef rgb_gray_convert_internal
162
163#define RGB_RED EXT_XRGB_RED
164#define RGB_GREEN EXT_XRGB_GREEN
165#define RGB_BLUE EXT_XRGB_BLUE
166#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
167#define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
168#define rgb_gray_convert_internal extxrgb_gray_convert_internal
169#include "jccolext.c"
170#undef RGB_RED
171#undef RGB_GREEN
172#undef RGB_BLUE
173#undef RGB_PIXELSIZE
174#undef rgb_ycc_convert_internal
175#undef rgb_gray_convert_internal
176
177
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000178/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179 * Initialize for RGB->YCC colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000180 */
181
Thomas G. Lane489583f1996-02-07 00:00:00 +0000182METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000183rgb_ycc_start (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000184{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
186 INT32 * rgb_ycc_tab;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000187 INT32 i;
188
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000189 /* Allocate and fill in the conversion tables. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
191 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
192 (TABLE_SIZE * SIZEOF(INT32)));
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000193
194 for (i = 0; i <= MAXJSAMPLE; i++) {
195 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
196 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
197 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
198 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
199 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000200 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
201 * This ensures that the maximum output will round to MAXJSAMPLE
202 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
203 */
204 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000205/* B=>Cb and R=>Cr tables are the same
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000206 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000207*/
208 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
209 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
210 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000211}
212
213
214/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000216 */
217
Thomas G. Lane489583f1996-02-07 00:00:00 +0000218METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219rgb_ycc_convert (j_compress_ptr cinfo,
220 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
221 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000222{
DRCb4570bb2011-09-07 06:31:00 +0000223 switch (cinfo->in_color_space) {
224 case JCS_EXT_RGB:
225 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
226 num_rows);
227 break;
228 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000229 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000230 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
231 num_rows);
232 break;
233 case JCS_EXT_BGR:
234 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
235 num_rows);
236 break;
237 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000238 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000239 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
240 num_rows);
241 break;
242 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000243 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000244 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
245 num_rows);
246 break;
247 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000248 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000249 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
250 num_rows);
251 break;
252 default:
253 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
254 num_rows);
255 break;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000256 }
257}
258
259
260/**************** Cases other than RGB -> YCbCr **************/
261
262
263/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000265 */
266
Thomas G. Lane489583f1996-02-07 00:00:00 +0000267METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268rgb_gray_convert (j_compress_ptr cinfo,
269 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
270 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000271{
DRCb4570bb2011-09-07 06:31:00 +0000272 switch (cinfo->in_color_space) {
273 case JCS_EXT_RGB:
274 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
275 num_rows);
276 break;
277 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000278 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000279 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
280 num_rows);
281 break;
282 case JCS_EXT_BGR:
283 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
284 num_rows);
285 break;
286 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000287 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000288 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
289 num_rows);
290 break;
291 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000292 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000293 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
294 num_rows);
295 break;
296 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000297 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000298 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
299 num_rows);
300 break;
301 default:
302 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
303 num_rows);
304 break;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000305 }
306}
307
308
309/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310 * Convert some rows of samples to the JPEG colorspace.
311 * This version handles Adobe-style CMYK->YCCK conversion,
312 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
313 * conversion as above, while passing K (black) unchanged.
314 * We assume rgb_ycc_start has been called.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000315 */
316
Thomas G. Lane489583f1996-02-07 00:00:00 +0000317METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318cmyk_ycck_convert (j_compress_ptr cinfo,
319 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
320 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000321{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
323 register int r, g, b;
324 register INT32 * ctab = cconvert->rgb_ycc_tab;
325 register JSAMPROW inptr;
326 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
327 register JDIMENSION col;
328 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000329
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000330 while (--num_rows >= 0) {
331 inptr = *input_buf++;
332 outptr0 = output_buf[0][output_row];
333 outptr1 = output_buf[1][output_row];
334 outptr2 = output_buf[2][output_row];
335 outptr3 = output_buf[3][output_row];
336 output_row++;
337 for (col = 0; col < num_cols; col++) {
338 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
339 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
340 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
341 /* K passes through as-is */
342 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
343 inptr += 4;
344 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
345 * must be too; we do not need an explicit range-limiting operation.
346 * Hence the value being shifted is never negative, and we don't
347 * need the general RIGHT_SHIFT macro.
348 */
349 /* Y */
350 outptr0[col] = (JSAMPLE)
351 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
352 >> SCALEBITS);
353 /* Cb */
354 outptr1[col] = (JSAMPLE)
355 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
356 >> SCALEBITS);
357 /* Cr */
358 outptr2[col] = (JSAMPLE)
359 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
360 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000361 }
362 }
363}
364
365
366/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 * Convert some rows of samples to the JPEG colorspace.
368 * This version handles grayscale output with no conversion.
369 * The source can be either plain grayscale or YCbCr (since Y == gray).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000370 */
371
Thomas G. Lane489583f1996-02-07 00:00:00 +0000372METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373grayscale_convert (j_compress_ptr cinfo,
374 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
375 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000376{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000377 register JSAMPROW inptr;
378 register JSAMPROW outptr;
379 register JDIMENSION col;
380 JDIMENSION num_cols = cinfo->image_width;
381 int instride = cinfo->input_components;
382
383 while (--num_rows >= 0) {
384 inptr = *input_buf++;
385 outptr = output_buf[0][output_row];
386 output_row++;
387 for (col = 0; col < num_cols; col++) {
388 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
389 inptr += instride;
390 }
391 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000392}
393
394
395/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 * Convert some rows of samples to the JPEG colorspace.
397 * This version handles multi-component colorspaces without conversion.
398 * We assume input_components == num_components.
399 */
400
Thomas G. Lane489583f1996-02-07 00:00:00 +0000401METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000402null_convert (j_compress_ptr cinfo,
403 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
404 JDIMENSION output_row, int num_rows)
405{
406 register JSAMPROW inptr;
407 register JSAMPROW outptr;
408 register JDIMENSION col;
409 register int ci;
410 int nc = cinfo->num_components;
411 JDIMENSION num_cols = cinfo->image_width;
412
413 while (--num_rows >= 0) {
414 /* It seems fastest to make a separate pass for each component. */
415 for (ci = 0; ci < nc; ci++) {
416 inptr = *input_buf;
417 outptr = output_buf[ci][output_row];
418 for (col = 0; col < num_cols; col++) {
419 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
420 inptr += nc;
421 }
422 }
423 input_buf++;
424 output_row++;
425 }
426}
427
428
429/*
430 * Empty method for start_pass.
431 */
432
Thomas G. Lane489583f1996-02-07 00:00:00 +0000433METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434null_method (j_compress_ptr cinfo)
435{
436 /* no work needed */
437}
438
439
440/*
441 * Module initialization routine for input colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000442 */
443
Thomas G. Lane489583f1996-02-07 00:00:00 +0000444GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000445jinit_color_converter (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000446{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000447 my_cconvert_ptr cconvert;
448
449 cconvert = (my_cconvert_ptr)
450 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
451 SIZEOF(my_color_converter));
452 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
453 /* set start_pass to null method until we find out differently */
454 cconvert->pub.start_pass = null_method;
455
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000456 /* Make sure input_components agrees with in_color_space */
457 switch (cinfo->in_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000458 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000459 if (cinfo->input_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000460 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000461 break;
462
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000464 case JCS_EXT_RGB:
465 case JCS_EXT_RGBX:
466 case JCS_EXT_BGR:
467 case JCS_EXT_BGRX:
468 case JCS_EXT_XBGR:
469 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000470 case JCS_EXT_RGBA:
471 case JCS_EXT_BGRA:
472 case JCS_EXT_ABGR:
473 case JCS_EXT_ARGB:
DRCf25c0712009-04-03 12:00:51 +0000474 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
475 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
476 break;
477
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000479 if (cinfo->input_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000481 break;
482
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000483 case JCS_CMYK:
484 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000485 if (cinfo->input_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000486 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000487 break;
488
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489 default: /* JCS_UNKNOWN can be anything */
490 if (cinfo->input_components < 1)
491 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000492 break;
493 }
494
495 /* Check num_components, set conversion method based on requested space */
496 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000497 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000498 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000499 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
500 if (cinfo->in_color_space == JCS_GRAYSCALE)
501 cconvert->pub.color_convert = grayscale_convert;
DRCf25c0712009-04-03 12:00:51 +0000502 else if (cinfo->in_color_space == JCS_RGB ||
503 cinfo->in_color_space == JCS_EXT_RGB ||
504 cinfo->in_color_space == JCS_EXT_RGBX ||
505 cinfo->in_color_space == JCS_EXT_BGR ||
506 cinfo->in_color_space == JCS_EXT_BGRX ||
507 cinfo->in_color_space == JCS_EXT_XBGR ||
DRC67ce3b22011-12-19 02:21:03 +0000508 cinfo->in_color_space == JCS_EXT_XRGB ||
509 cinfo->in_color_space == JCS_EXT_RGBA ||
510 cinfo->in_color_space == JCS_EXT_BGRA ||
511 cinfo->in_color_space == JCS_EXT_ABGR ||
512 cinfo->in_color_space == JCS_EXT_ARGB) {
DRCc8666332011-02-18 11:23:45 +0000513 if (jsimd_can_rgb_gray())
514 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
515 else {
516 cconvert->pub.start_pass = rgb_ycc_start;
517 cconvert->pub.color_convert = rgb_gray_convert;
518 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000519 } else if (cinfo->in_color_space == JCS_YCbCr)
520 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000521 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000522 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000523 break;
524
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000525 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000526 case JCS_EXT_RGB:
527 case JCS_EXT_RGBX:
528 case JCS_EXT_BGR:
529 case JCS_EXT_BGRX:
530 case JCS_EXT_XBGR:
531 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000532 case JCS_EXT_RGBA:
533 case JCS_EXT_BGRA:
534 case JCS_EXT_ABGR:
535 case JCS_EXT_ARGB:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000536 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000537 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000538 if (cinfo->in_color_space == cinfo->jpeg_color_space &&
539 rgb_pixelsize[cinfo->in_color_space] == 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000540 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000541 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000542 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000543 break;
544
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000545 case JCS_YCbCr:
546 if (cinfo->num_components != 3)
547 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000548 if (cinfo->in_color_space == JCS_RGB ||
549 cinfo->in_color_space == JCS_EXT_RGB ||
550 cinfo->in_color_space == JCS_EXT_RGBX ||
551 cinfo->in_color_space == JCS_EXT_BGR ||
552 cinfo->in_color_space == JCS_EXT_BGRX ||
553 cinfo->in_color_space == JCS_EXT_XBGR ||
DRC67ce3b22011-12-19 02:21:03 +0000554 cinfo->in_color_space == JCS_EXT_XRGB ||
555 cinfo->in_color_space == JCS_EXT_RGBA ||
556 cinfo->in_color_space == JCS_EXT_BGRA ||
557 cinfo->in_color_space == JCS_EXT_ABGR ||
558 cinfo->in_color_space == JCS_EXT_ARGB) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000559 if (jsimd_can_rgb_ycc())
560 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
561 else {
562 cconvert->pub.start_pass = rgb_ycc_start;
563 cconvert->pub.color_convert = rgb_ycc_convert;
564 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000565 } else if (cinfo->in_color_space == JCS_YCbCr)
566 cconvert->pub.color_convert = null_convert;
567 else
568 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
569 break;
570
571 case JCS_CMYK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000572 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000573 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
574 if (cinfo->in_color_space == JCS_CMYK)
575 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000576 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000577 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000578 break;
579
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000580 case JCS_YCCK:
581 if (cinfo->num_components != 4)
582 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
583 if (cinfo->in_color_space == JCS_CMYK) {
584 cconvert->pub.start_pass = rgb_ycc_start;
585 cconvert->pub.color_convert = cmyk_ycck_convert;
586 } else if (cinfo->in_color_space == JCS_YCCK)
587 cconvert->pub.color_convert = null_convert;
588 else
589 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
590 break;
591
592 default: /* allow null conversion of JCS_UNKNOWN */
593 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
594 cinfo->num_components != cinfo->input_components)
595 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
596 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000597 break;
598 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000599}