1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * jccolor.c
7 *
8 * Copyright (C) 1991-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This file contains input colorspace conversion routines.
13 */
14
15#define JPEG_INTERNALS
16#include "jinclude.h"
17#include "jpeglib.h"
18
19
20/* 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;
30
31
32/**************** 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
39 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
40 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
41 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
42 * 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.
47 *
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.
58 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
59 * in the tables to save adding them separately in the inner loop.
60 */
61
62#define SCALEBITS 16 /* speediest right-shift on some machines */
63#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
64#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
73#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
85/*
86 * Initialize for RGB->YCC colorspace conversion.
87 */
88
89METHODDEF(void)
90rgb_ycc_start (j_compress_ptr cinfo)
91{
92 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
93 INT32 * rgb_ycc_tab;
94 INT32 i;
95
96 /* Allocate and fill in the conversion tables. */
97 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
98 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
99 (TABLE_SIZE * SIZEOF(INT32)));
100
101 for (i = 0; i <= MAXJSAMPLE; i++) {
102 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
103 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
104 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
105 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
106 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
107 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
108 * This ensures that the maximum output will round to MAXJSAMPLE
109 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
110 */
111 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
112/* B=>Cb and R=>Cr tables are the same
113 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
114*/
115 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
116 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
117 }
118}
119
120
121/*
122 * Convert some rows of samples to the JPEG colorspace.
123 *
124 * Note that we change from the application's interleaved-pixel format
125 * to our internal noninterleaved, one-plane-per-component format.
126 * The input buffer is therefore three times as wide as the output buffer.
127 *
128 * A starting row offset is provided only for the output buffer. The caller
129 * can easily adjust the passed input_buf value to accommodate any row
130 * offset required on that side.
131 */
132
133METHODDEF(void)
134rgb_ycc_convert (j_compress_ptr cinfo,
135 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
136 JDIMENSION output_row, int num_rows)
137{
138 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
139 register int r, g, b;
140 register INT32 * ctab = cconvert->rgb_ycc_tab;
141 register JSAMPROW inptr;
142 register JSAMPROW outptr0, outptr1, outptr2;
143 register JDIMENSION col;
144 JDIMENSION num_cols = cinfo->image_width;
145
146 while (--num_rows >= 0) {
147 inptr = *input_buf++;
148 outptr0 = output_buf[0][output_row];
149 outptr1 = output_buf[1][output_row];
150 outptr2 = output_buf[2][output_row];
151 output_row++;
152 for (col = 0; col < num_cols; col++) {
153 r = GETJSAMPLE(inptr[RGB_RED]);
154 g = GETJSAMPLE(inptr[RGB_GREEN]);
155 b = GETJSAMPLE(inptr[RGB_BLUE]);
156 inptr += RGB_PIXELSIZE;
157 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
158 * must be too; we do not need an explicit range-limiting operation.
159 * Hence the value being shifted is never negative, and we don't
160 * need the general RIGHT_SHIFT macro.
161 */
162 /* Y */
163 outptr0[col] = (JSAMPLE)
164 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
165 >> SCALEBITS);
166 /* Cb */
167 outptr1[col] = (JSAMPLE)
168 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
169 >> SCALEBITS);
170 /* Cr */
171 outptr2[col] = (JSAMPLE)
172 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
173 >> SCALEBITS);
174 }
175 }
176}
177
178
179/**************** Cases other than RGB -> YCbCr **************/
180
181
182/*
183 * Convert some rows of samples to the JPEG colorspace.
184 * This version handles RGB->grayscale conversion, which is the same
185 * as the RGB->Y portion of RGB->YCbCr.
186 * We assume rgb_ycc_start has been called (we only use the Y tables).
187 */
188
189METHODDEF(void)
190rgb_gray_convert (j_compress_ptr cinfo,
191 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
192 JDIMENSION output_row, int num_rows)
193{
194 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
195 register int r, g, b;
196 register INT32 * ctab = cconvert->rgb_ycc_tab;
197 register JSAMPROW inptr;
198 register JSAMPROW outptr;
199 register JDIMENSION col;
200 JDIMENSION num_cols = cinfo->image_width;
201
202 while (--num_rows >= 0) {
203 inptr = *input_buf++;
204 outptr = output_buf[0][output_row];
205 output_row++;
206 for (col = 0; col < num_cols; col++) {
207 r = GETJSAMPLE(inptr[RGB_RED]);
208 g = GETJSAMPLE(inptr[RGB_GREEN]);
209 b = GETJSAMPLE(inptr[RGB_BLUE]);
210 inptr += RGB_PIXELSIZE;
211 /* Y */
212 outptr[col] = (JSAMPLE)
213 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
214 >> SCALEBITS);
215 }
216 }
217}
218
219/*
220 * Convert some rows of samples to the JPEG colorspace.
221 * This version handles Adobe-style CMYK->YCCK conversion,
222 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
223 * conversion as above, while passing K (black) unchanged.
224 * We assume rgb_ycc_start has been called.
225 */
226
227METHODDEF(void)
228cmyk_ycck_convert (j_compress_ptr cinfo,
229 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
230 JDIMENSION output_row, int num_rows)
231{
232 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
233 register int r, g, b;
234 register INT32 * ctab = cconvert->rgb_ycc_tab;
235 register JSAMPROW inptr;
236 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
237 register JDIMENSION col;
238 JDIMENSION num_cols = cinfo->image_width;
239
240 while (--num_rows >= 0) {
241 inptr = *input_buf++;
242 outptr0 = output_buf[0][output_row];
243 outptr1 = output_buf[1][output_row];
244 outptr2 = output_buf[2][output_row];
245 outptr3 = output_buf[3][output_row];
246 output_row++;
247 for (col = 0; col < num_cols; col++) {
248 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
249 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
250 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
251 /* K passes through as-is */
252 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
253 inptr += 4;
254 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
255 * must be too; we do not need an explicit range-limiting operation.
256 * Hence the value being shifted is never negative, and we don't
257 * need the general RIGHT_SHIFT macro.
258 */
259 /* Y */
260 outptr0[col] = (JSAMPLE)
261 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
262 >> SCALEBITS);
263 /* Cb */
264 outptr1[col] = (JSAMPLE)
265 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
266 >> SCALEBITS);
267 /* Cr */
268 outptr2[col] = (JSAMPLE)
269 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
270 >> SCALEBITS);
271 }
272 }
273}
274
275
276/*
277 * Convert some rows of samples to the JPEG colorspace.
278 * This version handles grayscale output with no conversion.
279 * The source can be either plain grayscale or YCbCr (since Y == gray).
280 */
281
282METHODDEF(void)
283grayscale_convert (j_compress_ptr cinfo,
284 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
285 JDIMENSION output_row, int num_rows)
286{
287 register JSAMPROW inptr;
288 register JSAMPROW outptr;
289 register JDIMENSION col;
290 JDIMENSION num_cols = cinfo->image_width;
291 int instride = cinfo->input_components;
292
293 while (--num_rows >= 0) {
294 inptr = *input_buf++;
295 outptr = output_buf[0][output_row];
296 output_row++;
297 for (col = 0; col < num_cols; col++) {
298 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
299 inptr += instride;
300 }
301 }
302}
303
304
305/*
306 * Convert some rows of samples to the JPEG colorspace.
307 * This version handles multi-component colorspaces without conversion.
308 * We assume input_components == num_components.
309 */
310
311METHODDEF(void)
312null_convert (j_compress_ptr cinfo,
313 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
314 JDIMENSION output_row, int num_rows)
315{
316 register JSAMPROW inptr;
317 register JSAMPROW outptr;
318 register JDIMENSION col;
319 register int ci;
320 int nc = cinfo->num_components;
321 JDIMENSION num_cols = cinfo->image_width;
322
323 while (--num_rows >= 0) {
324 /* It seems fastest to make a separate pass for each component. */
325 for (ci = 0; ci < nc; ci++) {
326 inptr = *input_buf;
327 outptr = output_buf[ci][output_row];
328 for (col = 0; col < num_cols; col++) {
329 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
330 inptr += nc;
331 }
332 }
333 input_buf++;
334 output_row++;
335 }
336}
337
338
339/*
340 * Empty method for start_pass.
341 */
342
343METHODDEF(void)
344null_method (j_compress_ptr cinfo)
345{
346 /* no work needed */
347}
348
349
350/*
351 * Module initialization routine for input colorspace conversion.
352 */
353
354GLOBAL(void)
355jinit_color_converter (j_compress_ptr cinfo)
356{
357 my_cconvert_ptr cconvert;
358
359 cconvert = (my_cconvert_ptr)
360 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
361 SIZEOF(my_color_converter));
362 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
363 /* set start_pass to null method until we find out differently */
364 cconvert->pub.start_pass = null_method;
365
366 /* Make sure input_components agrees with in_color_space */
367 switch (cinfo->in_color_space) {
368 case JCS_GRAYSCALE:
369 if (cinfo->input_components != 1)
370 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
371 break;
372
373 case JCS_RGB:
374#if RGB_PIXELSIZE != 3
375 if (cinfo->input_components != RGB_PIXELSIZE)
376 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
377 break;
378#endif /* else share code with YCbCr */
379
380 case JCS_YCbCr:
381 if (cinfo->input_components != 3)
382 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
383 break;
384
385 case JCS_CMYK:
386 case JCS_YCCK:
387 if (cinfo->input_components != 4)
388 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
389 break;
390
391 default: /* JCS_UNKNOWN can be anything */
392 if (cinfo->input_components < 1)
393 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
394 break;
395 }
396
397 /* Check num_components, set conversion method based on requested space */
398 switch (cinfo->jpeg_color_space) {
399 case JCS_GRAYSCALE:
400 if (cinfo->num_components != 1)
401 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
402 if (cinfo->in_color_space == JCS_GRAYSCALE)
403 cconvert->pub.color_convert = grayscale_convert;
404 else if (cinfo->in_color_space == JCS_RGB) {
405 cconvert->pub.start_pass = rgb_ycc_start;
406 cconvert->pub.color_convert = rgb_gray_convert;
407 } else if (cinfo->in_color_space == JCS_YCbCr)
408 cconvert->pub.color_convert = grayscale_convert;
409 else
410 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
411 break;
412
413 case JCS_RGB:
414 if (cinfo->num_components != 3)
415 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
416 if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
417 cconvert->pub.color_convert = null_convert;
418 else
419 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
420 break;
421
422 case JCS_YCbCr:
423 if (cinfo->num_components != 3)
424 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
425 if (cinfo->in_color_space == JCS_RGB) {
426 cconvert->pub.start_pass = rgb_ycc_start;
427 cconvert->pub.color_convert = rgb_ycc_convert;
428 } else if (cinfo->in_color_space == JCS_YCbCr)
429 cconvert->pub.color_convert = null_convert;
430 else
431 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
432 break;
433
434 case JCS_CMYK:
435 if (cinfo->num_components != 4)
436 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
437 if (cinfo->in_color_space == JCS_CMYK)
438 cconvert->pub.color_convert = null_convert;
439 else
440 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
441 break;
442
443 case JCS_YCCK:
444 if (cinfo->num_components != 4)
445 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
446 if (cinfo->in_color_space == JCS_CMYK) {
447 cconvert->pub.start_pass = rgb_ycc_start;
448 cconvert->pub.color_convert = cmyk_ycck_convert;
449 } else if (cinfo->in_color_space == JCS_YCCK)
450 cconvert->pub.color_convert = null_convert;
451 else
452 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
453 break;
454
455 default: /* allow null conversion of JCS_UNKNOWN */
456 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
457 cinfo->num_components != cinfo->input_components)
458 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
459 cconvert->pub.color_convert = null_convert;
460 break;
461 }
462}
463