4
* Copyright (C) 1991-1996, Thomas G. Lane.
5
* This file is part of the Independent JPEG Group's software.
6
* For conditions of distribution and use, see the accompanying README file.
8
* This file contains input colorspace conversion routines.
11
#define JPEG_INTERNALS
15
/* Private subobject */
18
struct jpeg_color_converter pub; /* public fields */
20
/* Private state for RGB->YCC conversion */
21
INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
24
typedef my_color_converter * my_cconvert_ptr;
27
/**************** RGB -> YCbCr conversion: most common case **************/
30
* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
31
* normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
32
* The conversion equations to be implemented are therefore
33
* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
34
* Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
35
* Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
36
* (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
37
* Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
38
* rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
39
* negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
40
* were not represented exactly. Now we sacrifice exact representation of
41
* maximum red and maximum blue in order to get exact grayscales.
43
* To avoid floating-point arithmetic, we represent the fractional constants
44
* as integers scaled up by 2^16 (about 4 digits precision); we have to divide
45
* the products by 2^16, with appropriate rounding, to get the correct answer.
47
* For even more speed, we avoid doing any multiplications in the inner loop
48
* by precalculating the constants times R,G,B for all possible values.
49
* For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
50
* for 12-bit samples it is still acceptable. It's not very reasonable for
51
* 16-bit samples, but if you want lossless storage you shouldn't be changing
53
* The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
54
* in the tables to save adding them separately in the inner loop.
57
#define SCALEBITS 16 /* speediest right-shift on some machines */
58
#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
59
#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
60
#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
62
/* We allocate one big table and divide it up into eight parts, instead of
63
* doing eight alloc_small requests. This lets us use a single table base
64
* address, which can be held in a register in the inner loops on many
65
* machines (more than can hold all eight addresses, anyway).
68
#define R_Y_OFF 0 /* offset to R => Y section */
69
#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
70
#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
71
#define R_CB_OFF (3*(MAXJSAMPLE+1))
72
#define G_CB_OFF (4*(MAXJSAMPLE+1))
73
#define B_CB_OFF (5*(MAXJSAMPLE+1))
74
#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
75
#define G_CR_OFF (6*(MAXJSAMPLE+1))
76
#define B_CR_OFF (7*(MAXJSAMPLE+1))
77
#define TABLE_SIZE (8*(MAXJSAMPLE+1))
81
* Initialize for RGB->YCC colorspace conversion.
85
rgb_ycc_start (j_compress_ptr cinfo)
87
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
91
/* Allocate and fill in the conversion tables. */
92
cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
93
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
94
(TABLE_SIZE * SIZEOF(INT32)));
96
for (i = 0; i <= MAXJSAMPLE; i++) {
97
rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
98
rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
99
rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
100
rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
101
rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
102
/* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
103
* This ensures that the maximum output will round to MAXJSAMPLE
104
* not MAXJSAMPLE+1, and thus that we don't have to range-limit.
106
rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
107
/* B=>Cb and R=>Cr tables are the same
108
rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
110
rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
111
rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
117
* Convert some rows of samples to the JPEG colorspace.
119
* Note that we change from the application's interleaved-pixel format
120
* to our internal noninterleaved, one-plane-per-component format.
121
* The input buffer is therefore three times as wide as the output buffer.
123
* A starting row offset is provided only for the output buffer. The caller
124
* can easily adjust the passed input_buf value to accommodate any row
125
* offset required on that side.
129
rgb_ycc_convert (j_compress_ptr cinfo,
130
JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
131
JDIMENSION output_row, int num_rows)
133
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
134
register int r, g, b;
135
register INT32 * ctab = cconvert->rgb_ycc_tab;
136
register JSAMPROW inptr;
137
register JSAMPROW outptr0, outptr1, outptr2;
138
register JDIMENSION col;
139
JDIMENSION num_cols = cinfo->image_width;
141
while (--num_rows >= 0) {
142
inptr = *input_buf++;
143
outptr0 = output_buf[0][output_row];
144
outptr1 = output_buf[1][output_row];
145
outptr2 = output_buf[2][output_row];
147
for (col = 0; col < num_cols; col++) {
148
r = GETJSAMPLE(inptr[RGB_RED]);
149
g = GETJSAMPLE(inptr[RGB_GREEN]);
150
b = GETJSAMPLE(inptr[RGB_BLUE]);
151
inptr += RGB_PIXELSIZE;
152
/* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
153
* must be too; we do not need an explicit range-limiting operation.
154
* Hence the value being shifted is never negative, and we don't
155
* need the general RIGHT_SHIFT macro.
158
outptr0[col] = (JSAMPLE)
159
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
162
outptr1[col] = (JSAMPLE)
163
((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
166
outptr2[col] = (JSAMPLE)
167
((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
174
/**************** Cases other than RGB -> YCbCr **************/
178
* Convert some rows of samples to the JPEG colorspace.
179
* This version handles RGB->grayscale conversion, which is the same
180
* as the RGB->Y portion of RGB->YCbCr.
181
* We assume rgb_ycc_start has been called (we only use the Y tables).
185
rgb_gray_convert (j_compress_ptr cinfo,
186
JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
187
JDIMENSION output_row, int num_rows)
189
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
190
register int r, g, b;
191
register INT32 * ctab = cconvert->rgb_ycc_tab;
192
register JSAMPROW inptr;
193
register JSAMPROW outptr;
194
register JDIMENSION col;
195
JDIMENSION num_cols = cinfo->image_width;
197
while (--num_rows >= 0) {
198
inptr = *input_buf++;
199
outptr = output_buf[0][output_row];
201
for (col = 0; col < num_cols; col++) {
202
r = GETJSAMPLE(inptr[RGB_RED]);
203
g = GETJSAMPLE(inptr[RGB_GREEN]);
204
b = GETJSAMPLE(inptr[RGB_BLUE]);
205
inptr += RGB_PIXELSIZE;
207
outptr[col] = (JSAMPLE)
208
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
216
* Convert some rows of samples to the JPEG colorspace.
217
* This version handles Adobe-style CMYK->YCCK conversion,
218
* where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
219
* conversion as above, while passing K (black) unchanged.
220
* We assume rgb_ycc_start has been called.
224
cmyk_ycck_convert (j_compress_ptr cinfo,
225
JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
226
JDIMENSION output_row, int num_rows)
228
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
229
register int r, g, b;
230
register INT32 * ctab = cconvert->rgb_ycc_tab;
231
register JSAMPROW inptr;
232
register JSAMPROW outptr0, outptr1, outptr2, outptr3;
233
register JDIMENSION col;
234
JDIMENSION num_cols = cinfo->image_width;
236
while (--num_rows >= 0) {
237
inptr = *input_buf++;
238
outptr0 = output_buf[0][output_row];
239
outptr1 = output_buf[1][output_row];
240
outptr2 = output_buf[2][output_row];
241
outptr3 = output_buf[3][output_row];
243
for (col = 0; col < num_cols; col++) {
244
r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
245
g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
246
b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
247
/* K passes through as-is */
248
outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
250
/* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
251
* must be too; we do not need an explicit range-limiting operation.
252
* Hence the value being shifted is never negative, and we don't
253
* need the general RIGHT_SHIFT macro.
256
outptr0[col] = (JSAMPLE)
257
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
260
outptr1[col] = (JSAMPLE)
261
((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
264
outptr2[col] = (JSAMPLE)
265
((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
273
* Convert some rows of samples to the JPEG colorspace.
274
* This version handles grayscale output with no conversion.
275
* The source can be either plain grayscale or YCbCr (since Y == gray).
279
grayscale_convert (j_compress_ptr cinfo,
280
JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
281
JDIMENSION output_row, int num_rows)
283
register JSAMPROW inptr;
284
register JSAMPROW outptr;
285
register JDIMENSION col;
286
JDIMENSION num_cols = cinfo->image_width;
287
int instride = cinfo->input_components;
289
while (--num_rows >= 0) {
290
inptr = *input_buf++;
291
outptr = output_buf[0][output_row];
293
for (col = 0; col < num_cols; col++) {
294
outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
302
* Convert some rows of samples to the JPEG colorspace.
303
* This version handles multi-component colorspaces without conversion.
304
* We assume input_components == num_components.
308
null_convert (j_compress_ptr cinfo,
309
JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
310
JDIMENSION output_row, int num_rows)
312
register JSAMPROW inptr;
313
register JSAMPROW outptr;
314
register JDIMENSION col;
316
int nc = cinfo->num_components;
317
JDIMENSION num_cols = cinfo->image_width;
319
while (--num_rows >= 0) {
320
/* It seems fastest to make a separate pass for each component. */
321
for (ci = 0; ci < nc; ci++) {
323
outptr = output_buf[ci][output_row];
324
for (col = 0; col < num_cols; col++) {
325
outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
336
* Empty method for start_pass.
340
null_method (j_compress_ptr cinfo)
347
* Module initialization routine for input colorspace conversion.
351
jinit_color_converter (j_compress_ptr cinfo)
353
my_cconvert_ptr cconvert;
355
cconvert = (my_cconvert_ptr)
356
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
357
SIZEOF(my_color_converter));
358
cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
359
/* set start_pass to null method until we find out differently */
360
cconvert->pub.start_pass = null_method;
362
/* Make sure input_components agrees with in_color_space */
363
switch (cinfo->in_color_space) {
365
if (cinfo->input_components != 1)
366
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
370
#if RGB_PIXELSIZE != 3
371
if (cinfo->input_components != RGB_PIXELSIZE)
372
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
374
#endif /* else share code with YCbCr */
377
if (cinfo->input_components != 3)
378
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
383
if (cinfo->input_components != 4)
384
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
387
default: /* JCS_UNKNOWN can be anything */
388
if (cinfo->input_components < 1)
389
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
393
/* Check num_components, set conversion method based on requested space */
394
switch (cinfo->jpeg_color_space) {
396
if (cinfo->num_components != 1)
397
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
398
if (cinfo->in_color_space == JCS_GRAYSCALE)
399
cconvert->pub.color_convert = grayscale_convert;
400
else if (cinfo->in_color_space == JCS_RGB) {
401
cconvert->pub.start_pass = rgb_ycc_start;
402
cconvert->pub.color_convert = rgb_gray_convert;
403
} else if (cinfo->in_color_space == JCS_YCbCr)
404
cconvert->pub.color_convert = grayscale_convert;
406
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
410
if (cinfo->num_components != 3)
411
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
412
if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
413
cconvert->pub.color_convert = null_convert;
415
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
419
if (cinfo->num_components != 3)
420
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
421
if (cinfo->in_color_space == JCS_RGB) {
422
cconvert->pub.start_pass = rgb_ycc_start;
423
cconvert->pub.color_convert = rgb_ycc_convert;
424
} else if (cinfo->in_color_space == JCS_YCbCr)
425
cconvert->pub.color_convert = null_convert;
427
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
431
if (cinfo->num_components != 4)
432
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
433
if (cinfo->in_color_space == JCS_CMYK)
434
cconvert->pub.color_convert = null_convert;
436
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
440
if (cinfo->num_components != 4)
441
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
442
if (cinfo->in_color_space == JCS_CMYK) {
443
cconvert->pub.start_pass = rgb_ycc_start;
444
cconvert->pub.color_convert = cmyk_ycck_convert;
445
} else if (cinfo->in_color_space == JCS_YCCK)
446
cconvert->pub.color_convert = null_convert;
448
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
451
default: /* allow null conversion of JCS_UNKNOWN */
452
if (cinfo->jpeg_color_space != cinfo->in_color_space ||
453
cinfo->num_components != cinfo->input_components)
454
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
455
cconvert->pub.color_convert = null_convert;