]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdcolor.c
Fix mistypings
[gdcm.git] / src / gdcmjpeg / jdcolor.c
1 /*
2  * jdcolor.c
3  *
4  * Copyright (C) 1991-1997, 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.
7  *
8  * This file contains output colorspace conversion routines.
9  */
10
11 #define JPEG_INTERNALS
12 #include "jinclude.h"
13 #include "jpeglib.h"
14
15
16 /* Private subobject */
17
18 typedef struct {
19   struct jpeg_color_deconverter pub; /* public fields */
20
21   /* Private state for YCC->RGB conversion */
22   int * Cr_r_tab;    /* => table for Cr to R conversion */
23   int * Cb_b_tab;    /* => table for Cb to B conversion */
24   INT32 * Cr_g_tab;    /* => table for Cr to G conversion */
25   INT32 * Cb_g_tab;    /* => table for Cb to G conversion */
26 } my_color_deconverter;
27
28 typedef my_color_deconverter * my_cconvert_ptr;
29
30
31 /**************** YCbCr -> RGB conversion: most common case **************/
32
33 /*
34  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
35  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
36  * The conversion equations to be implemented are therefore
37  *  R = Y                + 1.40200 * Cr
38  *  G = Y - 0.34414 * Cb - 0.71414 * Cr
39  *  B = Y + 1.77200 * Cb
40  * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
41  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
42  *
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.
46  * Notice that Y, being an integral input, does not contribute any fraction
47  * so it need not participate in the rounding.
48  *
49  * For even more speed, we avoid doing any multiplications in the inner loop
50  * by precalculating the constants times Cb and Cr 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 Cr=>R and Cb=>B values can be rounded to integers in advance; the
56  * values for the G calculation are left scaled up, since we must add them
57  * together before rounding.
58  */
59
60 #define SCALEBITS  16  /* speediest right-shift on some machines */
61 #define ONE_HALF  ((INT32) 1 << (SCALEBITS-1))
62 #define FIX(x)    ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
63
64
65 /*
66  * Initialize tables for YCC->RGB colorspace conversion.
67  */
68
69 LOCAL(void)
70 build_ycc_rgb_table (j_decompress_ptr cinfo)
71 {
72   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
73   int i;
74   INT32 x;
75 #if BITS_IN_JSAMPLE == 16
76   /* no need for temporaries */
77 #else
78   SHIFT_TEMPS
79 #endif
80
81   cconvert->Cr_r_tab = (int *)
82     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
83         (MAXJSAMPLE+1) * SIZEOF(int));
84   cconvert->Cb_b_tab = (int *)
85     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
86         (MAXJSAMPLE+1) * SIZEOF(int));
87   cconvert->Cr_g_tab = (INT32 *)
88     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
89         (MAXJSAMPLE+1) * SIZEOF(INT32));
90   cconvert->Cb_g_tab = (INT32 *)
91     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
92         (MAXJSAMPLE+1) * SIZEOF(INT32));
93
94   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
95     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
96     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
97
98 #if BITS_IN_JSAMPLE == 16
99     /* Bug fix 2001-11-06 by Eichelberg: The integer routines below
100        produce an overflow when used with MAXJSAMPLE == 65535.
101        Use floating point calculation instead. */
102
103     /* Cr=>R value is nearest int to 1.40200 * x */
104     cconvert->Cr_r_tab[i] = (int)(1.40200 * (double)x + 0.5);
105     /* Cb=>B value is nearest int to 1.77200 * x */
106     cconvert->Cb_b_tab[i] = (int)(1.77200 * (double)x + 0.5);
107 #else
108     /* Cr=>R value is nearest int to 1.40200 * x */
109     cconvert->Cr_r_tab[i] = (int)
110         RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
111     /* Cb=>B value is nearest int to 1.77200 * x */
112     cconvert->Cb_b_tab[i] = (int)
113         RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
114 #endif
115
116     /* Cr=>G value is scaled-up -0.71414 * x */
117     cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
118     /* Cb=>G value is scaled-up -0.34414 * x */
119     /* We also add in ONE_HALF so that need not do it in inner loop */
120     cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
121   }
122 }
123
124
125 /*
126  * Convert some rows of samples to the output colorspace.
127  *
128  * Note that we change from noninterleaved, one-plane-per-component format
129  * to interleaved-pixel format.  The output buffer is therefore three times
130  * as wide as the input buffer.
131  * A starting row offset is provided only for the input buffer.  The caller
132  * can easily adjust the passed output_buf value to accommodate any row
133  * offset required on that side.
134  */
135
136 METHODDEF(void)
137 ycc_rgb_convert (j_decompress_ptr cinfo,
138      JSAMPIMAGE input_buf, JDIMENSION input_row,
139      JSAMPARRAY output_buf, int num_rows)
140 {
141   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
142   register int y, cb, cr;
143   register JSAMPROW outptr;
144   register JSAMPROW inptr0, inptr1, inptr2;
145   register JDIMENSION col;
146   JDIMENSION num_cols = cinfo->output_width;
147   /* copy these pointers into registers if possible */
148   register JSAMPLE * range_limit = cinfo->sample_range_limit;
149   register int * Crrtab = cconvert->Cr_r_tab;
150   register int * Cbbtab = cconvert->Cb_b_tab;
151   register INT32 * Crgtab = cconvert->Cr_g_tab;
152   register INT32 * Cbgtab = cconvert->Cb_g_tab;
153   SHIFT_TEMPS
154
155   while (--num_rows >= 0) {
156     inptr0 = input_buf[0][input_row];
157     inptr1 = input_buf[1][input_row];
158     inptr2 = input_buf[2][input_row];
159     input_row++;
160     outptr = *output_buf++;
161     for (col = 0; col < num_cols; col++) {
162       y  = GETJSAMPLE(inptr0[col]);
163       cb = GETJSAMPLE(inptr1[col]);
164       cr = GETJSAMPLE(inptr2[col]);
165       /* Range-limiting is essential due to noise introduced by DCT losses. */
166       outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
167       outptr[RGB_GREEN] = range_limit[y +
168             ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
169              SCALEBITS))];
170       outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
171       outptr += RGB_PIXELSIZE;
172     }
173   }
174 }
175
176
177 /**************** Cases other than YCbCr -> RGB **************/
178
179
180 /*
181  * Color conversion for no colorspace change: just copy the data,
182  * converting from separate-planes to interleaved representation.
183  */
184
185 METHODDEF(void)
186 null_convert (j_decompress_ptr cinfo,
187         JSAMPIMAGE input_buf, JDIMENSION input_row,
188         JSAMPARRAY output_buf, int num_rows)
189 {
190   register JSAMPROW inptr, outptr;
191   register JDIMENSION count;
192   register int num_components = cinfo->num_components;
193   JDIMENSION num_cols = cinfo->output_width;
194   int ci;
195
196   while (--num_rows >= 0) {
197     for (ci = 0; ci < num_components; ci++) {
198       inptr = input_buf[ci][input_row];
199       outptr = output_buf[0] + ci;
200       for (count = num_cols; count > 0; count--) {
201   *outptr = *inptr++;  /* needn't bother with GETJSAMPLE() here */
202   outptr += num_components;
203       }
204     }
205     input_row++;
206     output_buf++;
207   }
208 }
209
210
211 /*
212  * Color conversion for grayscale: just copy the data.
213  * This also works for YCbCr -> grayscale conversion, in which
214  * we just copy the Y (luminance) component and ignore chrominance.
215  */
216
217 METHODDEF(void)
218 grayscale_convert (j_decompress_ptr cinfo,
219        JSAMPIMAGE input_buf, JDIMENSION input_row,
220        JSAMPARRAY output_buf, int num_rows)
221 {
222   jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
223         num_rows, cinfo->output_width);
224 }
225
226
227 /*
228  * Convert grayscale to RGB: just duplicate the graylevel three times.
229  * This is provided to support applications that don't want to cope
230  * with grayscale as a separate case.
231  */
232
233 METHODDEF(void)
234 gray_rgb_convert (j_decompress_ptr cinfo,
235       JSAMPIMAGE input_buf, JDIMENSION input_row,
236       JSAMPARRAY output_buf, int num_rows)
237 {
238   register JSAMPROW inptr, outptr;
239   register JDIMENSION col;
240   JDIMENSION num_cols = cinfo->output_width;
241
242   while (--num_rows >= 0) {
243     inptr = input_buf[0][input_row++];
244     outptr = *output_buf++;
245     for (col = 0; col < num_cols; col++) {
246       /* We can dispense with GETJSAMPLE() here */
247       outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
248       outptr += RGB_PIXELSIZE;
249     }
250   }
251 }
252
253
254 /*
255  * Adobe-style YCCK->CMYK conversion.
256  * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
257  * conversion as above, while passing K (black) unchanged.
258  * We assume build_ycc_rgb_table has been called.
259  */
260
261 METHODDEF(void)
262 ycck_cmyk_convert (j_decompress_ptr cinfo,
263        JSAMPIMAGE input_buf, JDIMENSION input_row,
264        JSAMPARRAY output_buf, int num_rows)
265 {
266   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
267   register int y, cb, cr;
268   register JSAMPROW outptr;
269   register JSAMPROW inptr0, inptr1, inptr2, inptr3;
270   register JDIMENSION col;
271   JDIMENSION num_cols = cinfo->output_width;
272   /* copy these pointers into registers if possible */
273   register JSAMPLE * range_limit = cinfo->sample_range_limit;
274   register int * Crrtab = cconvert->Cr_r_tab;
275   register int * Cbbtab = cconvert->Cb_b_tab;
276   register INT32 * Crgtab = cconvert->Cr_g_tab;
277   register INT32 * Cbgtab = cconvert->Cb_g_tab;
278   SHIFT_TEMPS
279
280   while (--num_rows >= 0) {
281     inptr0 = input_buf[0][input_row];
282     inptr1 = input_buf[1][input_row];
283     inptr2 = input_buf[2][input_row];
284     inptr3 = input_buf[3][input_row];
285     input_row++;
286     outptr = *output_buf++;
287     for (col = 0; col < num_cols; col++) {
288       y  = GETJSAMPLE(inptr0[col]);
289       cb = GETJSAMPLE(inptr1[col]);
290       cr = GETJSAMPLE(inptr2[col]);
291       /* Range-limiting is essential due to noise introduced by DCT losses. */
292       outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];  /* red */
293       outptr[1] = range_limit[MAXJSAMPLE - (y +      /* green */
294             ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
295              SCALEBITS)))];
296       outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];  /* blue */
297       /* K passes through unchanged */
298       outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
299       outptr += 4;
300     }
301   }
302 }
303
304
305 /*
306  * Empty method for start_pass.
307  */
308
309 METHODDEF(void)
310 start_pass_dcolor (j_decompress_ptr cinfo)
311 {
312   (void)cinfo;
313   /* no work needed */
314 }
315
316
317 /*
318  * Module initialization routine for output colorspace conversion.
319  */
320
321 GLOBAL(void)
322 jinit_color_deconverter (j_decompress_ptr cinfo)
323 {
324   my_cconvert_ptr cconvert;
325   int ci;
326
327   cconvert = (my_cconvert_ptr)
328     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
329         SIZEOF(my_color_deconverter));
330   cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
331   cconvert->pub.start_pass = start_pass_dcolor;
332
333   /* Make sure num_components agrees with jpeg_color_space */
334   switch (cinfo->jpeg_color_space) {
335   case JCS_GRAYSCALE:
336     if (cinfo->num_components != 1)
337       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
338     break;
339
340   case JCS_RGB:
341   case JCS_YCbCr:
342     if (cinfo->num_components != 3)
343       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
344     break;
345
346   case JCS_CMYK:
347   case JCS_YCCK:
348     if (cinfo->num_components != 4)
349       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
350     break;
351
352   default:      /* JCS_UNKNOWN can be anything */
353     if (cinfo->num_components < 1)
354       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
355     break;
356   }
357
358   /* Set out_color_components and conversion method based on requested space.
359    * Also clear the component_needed flags for any unused components,
360    * so that earlier pipeline stages can avoid useless computation.
361    */
362
363   switch (cinfo->out_color_space) {
364   case JCS_GRAYSCALE:
365     cinfo->out_color_components = 1;
366     if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
367   cinfo->jpeg_color_space == JCS_YCbCr) {
368       cconvert->pub.color_convert = grayscale_convert;
369       /* For color->grayscale conversion, only the Y (0) component is needed */
370       for (ci = 1; ci < cinfo->num_components; ci++)
371   cinfo->comp_info[ci].component_needed = FALSE;
372     } else
373       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
374     break;
375
376   case JCS_RGB:
377     cinfo->out_color_components = RGB_PIXELSIZE;
378     if (cinfo->jpeg_color_space == JCS_YCbCr) {
379       cconvert->pub.color_convert = ycc_rgb_convert;
380       build_ycc_rgb_table(cinfo);
381     } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
382       cconvert->pub.color_convert = gray_rgb_convert;
383     } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
384       cconvert->pub.color_convert = null_convert;
385     } else
386       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
387     break;
388
389   case JCS_CMYK:
390     cinfo->out_color_components = 4;
391     if (cinfo->jpeg_color_space == JCS_YCCK) {
392       cconvert->pub.color_convert = ycck_cmyk_convert;
393       build_ycc_rgb_table(cinfo);
394     } else if (cinfo->jpeg_color_space == JCS_CMYK) {
395       cconvert->pub.color_convert = null_convert;
396     } else
397       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
398     break;
399
400   default:
401     /* Permit null conversion to same output space */
402     if (cinfo->out_color_space == cinfo->jpeg_color_space) {
403       cinfo->out_color_components = cinfo->num_components;
404       cconvert->pub.color_convert = null_convert;
405     } else      /* unsupported non-null conversion */
406       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
407     break;
408   }
409
410   if (cinfo->quantize_colors)
411     cinfo->output_components = 1; /* single colormapped output component */
412   else
413     cinfo->output_components = cinfo->out_color_components;
414 }