Bringing jpeg-8d into the main branch
[AROS.git] / workbench / libs / jpeg / jccolor.c
blob3e2d0e927778135d3f9f13325c97c1eea9be19c8
1 /*
2 * jccolor.c
4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * Modified 2011 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains input colorspace conversion routines.
12 #define JPEG_INTERNALS
13 #include "jinclude.h"
14 #include "jpeglib.h"
17 /* Private subobject */
19 typedef struct {
20 struct jpeg_color_converter pub; /* public fields */
22 /* Private state for RGB->YCC conversion */
23 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
24 } my_color_converter;
26 typedef my_color_converter * my_cconvert_ptr;
29 /**************** RGB -> YCbCr conversion: most common case **************/
32 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
33 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
34 * The conversion equations to be implemented are therefore
35 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
36 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
37 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
38 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
39 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
40 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
41 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
42 * were not represented exactly. Now we sacrifice exact representation of
43 * maximum red and maximum blue in order to get exact grayscales.
45 * To avoid floating-point arithmetic, we represent the fractional constants
46 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
47 * the products by 2^16, with appropriate rounding, to get the correct answer.
49 * For even more speed, we avoid doing any multiplications in the inner loop
50 * by precalculating the constants times R,G,B for all possible values.
51 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
52 * for 12-bit samples it is still acceptable. It's not very reasonable for
53 * 16-bit samples, but if you want lossless storage you shouldn't be changing
54 * colorspace anyway.
55 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
56 * in the tables to save adding them separately in the inner loop.
59 #define SCALEBITS 16 /* speediest right-shift on some machines */
60 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
61 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
62 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
64 /* We allocate one big table and divide it up into eight parts, instead of
65 * doing eight alloc_small requests. This lets us use a single table base
66 * address, which can be held in a register in the inner loops on many
67 * machines (more than can hold all eight addresses, anyway).
70 #define R_Y_OFF 0 /* offset to R => Y section */
71 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
72 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
73 #define R_CB_OFF (3*(MAXJSAMPLE+1))
74 #define G_CB_OFF (4*(MAXJSAMPLE+1))
75 #define B_CB_OFF (5*(MAXJSAMPLE+1))
76 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
77 #define G_CR_OFF (6*(MAXJSAMPLE+1))
78 #define B_CR_OFF (7*(MAXJSAMPLE+1))
79 #define TABLE_SIZE (8*(MAXJSAMPLE+1))
83 * Initialize for RGB->YCC colorspace conversion.
86 METHODDEF(void)
87 rgb_ycc_start (j_compress_ptr cinfo)
89 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
90 INT32 * rgb_ycc_tab;
91 INT32 i;
93 /* Allocate and fill in the conversion tables. */
94 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
95 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
96 (TABLE_SIZE * SIZEOF(INT32)));
98 for (i = 0; i <= MAXJSAMPLE; i++) {
99 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
100 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
101 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
102 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
103 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
104 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
105 * This ensures that the maximum output will round to MAXJSAMPLE
106 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
108 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
109 /* B=>Cb and R=>Cr tables are the same
110 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
112 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
113 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
119 * Convert some rows of samples to the JPEG colorspace.
121 * Note that we change from the application's interleaved-pixel format
122 * to our internal noninterleaved, one-plane-per-component format.
123 * The input buffer is therefore three times as wide as the output buffer.
125 * A starting row offset is provided only for the output buffer. The caller
126 * can easily adjust the passed input_buf value to accommodate any row
127 * offset required on that side.
130 METHODDEF(void)
131 rgb_ycc_convert (j_compress_ptr cinfo,
132 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
133 JDIMENSION output_row, int num_rows)
135 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
136 register int r, g, b;
137 register INT32 * ctab = cconvert->rgb_ycc_tab;
138 register JSAMPROW inptr;
139 register JSAMPROW outptr0, outptr1, outptr2;
140 register JDIMENSION col;
141 JDIMENSION num_cols = cinfo->image_width;
143 while (--num_rows >= 0) {
144 inptr = *input_buf++;
145 outptr0 = output_buf[0][output_row];
146 outptr1 = output_buf[1][output_row];
147 outptr2 = output_buf[2][output_row];
148 output_row++;
149 for (col = 0; col < num_cols; col++) {
150 r = GETJSAMPLE(inptr[RGB_RED]);
151 g = GETJSAMPLE(inptr[RGB_GREEN]);
152 b = GETJSAMPLE(inptr[RGB_BLUE]);
153 inptr += RGB_PIXELSIZE;
154 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
155 * must be too; we do not need an explicit range-limiting operation.
156 * Hence the value being shifted is never negative, and we don't
157 * need the general RIGHT_SHIFT macro.
159 /* Y */
160 outptr0[col] = (JSAMPLE)
161 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
162 >> SCALEBITS);
163 /* Cb */
164 outptr1[col] = (JSAMPLE)
165 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
166 >> SCALEBITS);
167 /* Cr */
168 outptr2[col] = (JSAMPLE)
169 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
170 >> SCALEBITS);
176 /**************** Cases other than RGB -> YCbCr **************/
180 * Convert some rows of samples to the JPEG colorspace.
181 * This version handles RGB->grayscale conversion, which is the same
182 * as the RGB->Y portion of RGB->YCbCr.
183 * We assume rgb_ycc_start has been called (we only use the Y tables).
186 METHODDEF(void)
187 rgb_gray_convert (j_compress_ptr cinfo,
188 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
189 JDIMENSION output_row, int num_rows)
191 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
192 register int r, g, b;
193 register INT32 * ctab = cconvert->rgb_ycc_tab;
194 register JSAMPROW inptr;
195 register JSAMPROW outptr;
196 register JDIMENSION col;
197 JDIMENSION num_cols = cinfo->image_width;
199 while (--num_rows >= 0) {
200 inptr = *input_buf++;
201 outptr = output_buf[0][output_row];
202 output_row++;
203 for (col = 0; col < num_cols; col++) {
204 r = GETJSAMPLE(inptr[RGB_RED]);
205 g = GETJSAMPLE(inptr[RGB_GREEN]);
206 b = GETJSAMPLE(inptr[RGB_BLUE]);
207 inptr += RGB_PIXELSIZE;
208 /* Y */
209 outptr[col] = (JSAMPLE)
210 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
211 >> SCALEBITS);
218 * Convert some rows of samples to the JPEG colorspace.
219 * This version handles Adobe-style CMYK->YCCK conversion,
220 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
221 * conversion as above, while passing K (black) unchanged.
222 * We assume rgb_ycc_start has been called.
225 METHODDEF(void)
226 cmyk_ycck_convert (j_compress_ptr cinfo,
227 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
228 JDIMENSION output_row, int num_rows)
230 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
231 register int r, g, b;
232 register INT32 * ctab = cconvert->rgb_ycc_tab;
233 register JSAMPROW inptr;
234 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
235 register JDIMENSION col;
236 JDIMENSION num_cols = cinfo->image_width;
238 while (--num_rows >= 0) {
239 inptr = *input_buf++;
240 outptr0 = output_buf[0][output_row];
241 outptr1 = output_buf[1][output_row];
242 outptr2 = output_buf[2][output_row];
243 outptr3 = output_buf[3][output_row];
244 output_row++;
245 for (col = 0; col < num_cols; col++) {
246 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
247 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
248 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
249 /* K passes through as-is */
250 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
251 inptr += 4;
252 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
253 * must be too; we do not need an explicit range-limiting operation.
254 * Hence the value being shifted is never negative, and we don't
255 * need the general RIGHT_SHIFT macro.
257 /* Y */
258 outptr0[col] = (JSAMPLE)
259 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
260 >> SCALEBITS);
261 /* Cb */
262 outptr1[col] = (JSAMPLE)
263 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
264 >> SCALEBITS);
265 /* Cr */
266 outptr2[col] = (JSAMPLE)
267 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
268 >> SCALEBITS);
275 * Convert some rows of samples to the JPEG colorspace.
276 * This version handles grayscale output with no conversion.
277 * The source can be either plain grayscale or YCbCr (since Y == gray).
280 METHODDEF(void)
281 grayscale_convert (j_compress_ptr cinfo,
282 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
283 JDIMENSION output_row, int num_rows)
285 register JSAMPROW inptr;
286 register JSAMPROW outptr;
287 register JDIMENSION col;
288 JDIMENSION num_cols = cinfo->image_width;
289 int instride = cinfo->input_components;
291 while (--num_rows >= 0) {
292 inptr = *input_buf++;
293 outptr = output_buf[0][output_row];
294 output_row++;
295 for (col = 0; col < num_cols; col++) {
296 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
297 inptr += instride;
304 * Convert some rows of samples to the JPEG colorspace.
305 * No colorspace conversion, but change from interleaved
306 * to separate-planes representation.
309 METHODDEF(void)
310 rgb_convert (j_compress_ptr cinfo,
311 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
312 JDIMENSION output_row, int num_rows)
314 register JSAMPROW inptr;
315 register JSAMPROW outptr0, outptr1, outptr2;
316 register JDIMENSION col;
317 JDIMENSION num_cols = cinfo->image_width;
319 while (--num_rows >= 0) {
320 inptr = *input_buf++;
321 outptr0 = output_buf[0][output_row];
322 outptr1 = output_buf[1][output_row];
323 outptr2 = output_buf[2][output_row];
324 output_row++;
325 for (col = 0; col < num_cols; col++) {
326 /* We can dispense with GETJSAMPLE() here */
327 outptr0[col] = inptr[RGB_RED];
328 outptr1[col] = inptr[RGB_GREEN];
329 outptr2[col] = inptr[RGB_BLUE];
330 inptr += RGB_PIXELSIZE;
337 * Convert some rows of samples to the JPEG colorspace.
338 * This version handles multi-component colorspaces without conversion.
339 * We assume input_components == num_components.
342 METHODDEF(void)
343 null_convert (j_compress_ptr cinfo,
344 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
345 JDIMENSION output_row, int num_rows)
347 register JSAMPROW inptr;
348 register JSAMPROW outptr;
349 register JDIMENSION col;
350 register int ci;
351 int nc = cinfo->num_components;
352 JDIMENSION num_cols = cinfo->image_width;
354 while (--num_rows >= 0) {
355 /* It seems fastest to make a separate pass for each component. */
356 for (ci = 0; ci < nc; ci++) {
357 inptr = *input_buf;
358 outptr = output_buf[ci][output_row];
359 for (col = 0; col < num_cols; col++) {
360 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
361 inptr += nc;
364 input_buf++;
365 output_row++;
371 * Empty method for start_pass.
374 METHODDEF(void)
375 null_method (j_compress_ptr cinfo)
377 /* no work needed */
382 * Module initialization routine for input colorspace conversion.
385 GLOBAL(void)
386 jinit_color_converter (j_compress_ptr cinfo)
388 my_cconvert_ptr cconvert;
390 cconvert = (my_cconvert_ptr)
391 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
392 SIZEOF(my_color_converter));
393 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
394 /* set start_pass to null method until we find out differently */
395 cconvert->pub.start_pass = null_method;
397 /* Make sure input_components agrees with in_color_space */
398 switch (cinfo->in_color_space) {
399 case JCS_GRAYSCALE:
400 if (cinfo->input_components != 1)
401 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
402 break;
404 case JCS_RGB:
405 if (cinfo->input_components != RGB_PIXELSIZE)
406 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
407 break;
409 case JCS_YCbCr:
410 if (cinfo->input_components != 3)
411 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
412 break;
414 case JCS_CMYK:
415 case JCS_YCCK:
416 if (cinfo->input_components != 4)
417 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
418 break;
420 default: /* JCS_UNKNOWN can be anything */
421 if (cinfo->input_components < 1)
422 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
423 break;
426 /* Check num_components, set conversion method based on requested space */
427 switch (cinfo->jpeg_color_space) {
428 case JCS_GRAYSCALE:
429 if (cinfo->num_components != 1)
430 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
431 if (cinfo->in_color_space == JCS_GRAYSCALE ||
432 cinfo->in_color_space == JCS_YCbCr)
433 cconvert->pub.color_convert = grayscale_convert;
434 else if (cinfo->in_color_space == JCS_RGB) {
435 cconvert->pub.start_pass = rgb_ycc_start;
436 cconvert->pub.color_convert = rgb_gray_convert;
437 } else
438 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
439 break;
441 case JCS_RGB:
442 if (cinfo->num_components != 3)
443 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
444 if (cinfo->in_color_space == JCS_RGB)
445 cconvert->pub.color_convert = rgb_convert;
446 else
447 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
448 break;
450 case JCS_YCbCr:
451 if (cinfo->num_components != 3)
452 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
453 if (cinfo->in_color_space == JCS_RGB) {
454 cconvert->pub.start_pass = rgb_ycc_start;
455 cconvert->pub.color_convert = rgb_ycc_convert;
456 } else if (cinfo->in_color_space == JCS_YCbCr)
457 cconvert->pub.color_convert = null_convert;
458 else
459 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
460 break;
462 case JCS_CMYK:
463 if (cinfo->num_components != 4)
464 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
465 if (cinfo->in_color_space == JCS_CMYK)
466 cconvert->pub.color_convert = null_convert;
467 else
468 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
469 break;
471 case JCS_YCCK:
472 if (cinfo->num_components != 4)
473 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
474 if (cinfo->in_color_space == JCS_CMYK) {
475 cconvert->pub.start_pass = rgb_ycc_start;
476 cconvert->pub.color_convert = cmyk_ycck_convert;
477 } else if (cinfo->in_color_space == JCS_YCCK)
478 cconvert->pub.color_convert = null_convert;
479 else
480 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
481 break;
483 default: /* allow null conversion of JCS_UNKNOWN */
484 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
485 cinfo->num_components != cinfo->input_components)
486 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
487 cconvert->pub.color_convert = null_convert;
488 break;