source: rtems-graphics-toolkit/jpeg-8d/jdcolor.c @ 86b99f7

Last change on this file since 86b99f7 was 86b99f7, checked in by Alexandru-Sever Horin <alex.sever.h@…>, on 08/01/12 at 22:40:32

Added jpeg-8d version. Made modifications to compile for RTEMS, without man or binaries

  • Property mode set to 100644
File size: 15.9 KB
Line 
1/*
2 * jdcolor.c
3 *
4 * Copyright (C) 1991-1997, 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.
8 *
9 * This file contains output colorspace conversion routines.
10 */
11
12#define JPEG_INTERNALS
13#include "jinclude.h"
14#include "jpeglib.h"
15
16
17/* Private subobject */
18
19typedef struct {
20  struct jpeg_color_deconverter pub; /* public fields */
21
22  /* Private state for YCC->RGB conversion */
23  int * Cr_r_tab;               /* => table for Cr to R conversion */
24  int * Cb_b_tab;               /* => table for Cb to B conversion */
25  INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
26  INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
27
28  /* Private state for RGB->Y conversion */
29  INT32 * rgb_y_tab;            /* => table for RGB to Y conversion */
30} my_color_deconverter;
31
32typedef my_color_deconverter * my_cconvert_ptr;
33
34
35/**************** YCbCr -> RGB conversion: most common case **************/
36/****************   RGB -> Y   conversion: less common case **************/
37
38/*
39 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
40 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
41 * The conversion equations to be implemented are therefore
42 *
43 *      R = Y                + 1.40200 * Cr
44 *      G = Y - 0.34414 * Cb - 0.71414 * Cr
45 *      B = Y + 1.77200 * Cb
46 *
47 *      Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
48 *
49 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
50 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
51 *
52 * To avoid floating-point arithmetic, we represent the fractional constants
53 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
54 * the products by 2^16, with appropriate rounding, to get the correct answer.
55 * Notice that Y, being an integral input, does not contribute any fraction
56 * so it need not participate in the rounding.
57 *
58 * For even more speed, we avoid doing any multiplications in the inner loop
59 * by precalculating the constants times Cb and Cr for all possible values.
60 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
61 * for 12-bit samples it is still acceptable.  It's not very reasonable for
62 * 16-bit samples, but if you want lossless storage you shouldn't be changing
63 * colorspace anyway.
64 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
65 * values for the G calculation are left scaled up, since we must add them
66 * together before rounding.
67 */
68
69#define SCALEBITS       16      /* speediest right-shift on some machines */
70#define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
71#define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
72
73/* We allocate one big table for RGB->Y conversion and divide it up into
74 * three parts, instead of doing three alloc_small requests.  This lets us
75 * use a single table base address, which can be held in a register in the
76 * inner loops on many machines (more than can hold all three addresses,
77 * anyway).
78 */
79
80#define R_Y_OFF         0                       /* offset to R => Y section */
81#define G_Y_OFF         (1*(MAXJSAMPLE+1))      /* offset to G => Y section */
82#define B_Y_OFF         (2*(MAXJSAMPLE+1))      /* etc. */
83#define TABLE_SIZE      (3*(MAXJSAMPLE+1))
84
85
86/*
87 * Initialize tables for YCC->RGB colorspace conversion.
88 */
89
90LOCAL(void)
91build_ycc_rgb_table (j_decompress_ptr cinfo)
92{
93  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
94  int i;
95  INT32 x;
96  SHIFT_TEMPS
97
98  cconvert->Cr_r_tab = (int *)
99    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
100                                (MAXJSAMPLE+1) * SIZEOF(int));
101  cconvert->Cb_b_tab = (int *)
102    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
103                                (MAXJSAMPLE+1) * SIZEOF(int));
104  cconvert->Cr_g_tab = (INT32 *)
105    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
106                                (MAXJSAMPLE+1) * SIZEOF(INT32));
107  cconvert->Cb_g_tab = (INT32 *)
108    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
109                                (MAXJSAMPLE+1) * SIZEOF(INT32));
110
111  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
112    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
113    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
114    /* Cr=>R value is nearest int to 1.40200 * x */
115    cconvert->Cr_r_tab[i] = (int)
116                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
117    /* Cb=>B value is nearest int to 1.77200 * x */
118    cconvert->Cb_b_tab[i] = (int)
119                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
120    /* Cr=>G value is scaled-up -0.71414 * x */
121    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
122    /* Cb=>G value is scaled-up -0.34414 * x */
123    /* We also add in ONE_HALF so that need not do it in inner loop */
124    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
125  }
126}
127
128
129/*
130 * Convert some rows of samples to the output colorspace.
131 *
132 * Note that we change from noninterleaved, one-plane-per-component format
133 * to interleaved-pixel format.  The output buffer is therefore three times
134 * as wide as the input buffer.
135 * A starting row offset is provided only for the input buffer.  The caller
136 * can easily adjust the passed output_buf value to accommodate any row
137 * offset required on that side.
138 */
139
140METHODDEF(void)
141ycc_rgb_convert (j_decompress_ptr cinfo,
142                 JSAMPIMAGE input_buf, JDIMENSION input_row,
143                 JSAMPARRAY output_buf, int num_rows)
144{
145  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
146  register int y, cb, cr;
147  register JSAMPROW outptr;
148  register JSAMPROW inptr0, inptr1, inptr2;
149  register JDIMENSION col;
150  JDIMENSION num_cols = cinfo->output_width;
151  /* copy these pointers into registers if possible */
152  register JSAMPLE * range_limit = cinfo->sample_range_limit;
153  register int * Crrtab = cconvert->Cr_r_tab;
154  register int * Cbbtab = cconvert->Cb_b_tab;
155  register INT32 * Crgtab = cconvert->Cr_g_tab;
156  register INT32 * Cbgtab = cconvert->Cb_g_tab;
157  SHIFT_TEMPS
158
159  while (--num_rows >= 0) {
160    inptr0 = input_buf[0][input_row];
161    inptr1 = input_buf[1][input_row];
162    inptr2 = input_buf[2][input_row];
163    input_row++;
164    outptr = *output_buf++;
165    for (col = 0; col < num_cols; col++) {
166      y  = GETJSAMPLE(inptr0[col]);
167      cb = GETJSAMPLE(inptr1[col]);
168      cr = GETJSAMPLE(inptr2[col]);
169      /* Range-limiting is essential due to noise introduced by DCT losses. */
170      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
171      outptr[RGB_GREEN] = range_limit[y +
172                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
173                                                 SCALEBITS))];
174      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
175      outptr += RGB_PIXELSIZE;
176    }
177  }
178}
179
180
181/**************** Cases other than YCbCr -> RGB **************/
182
183
184/*
185 * Initialize for RGB->grayscale colorspace conversion.
186 */
187
188LOCAL(void)
189build_rgb_y_table (j_decompress_ptr cinfo)
190{
191  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
192  INT32 * rgb_y_tab;
193  INT32 i;
194
195  /* Allocate and fill in the conversion tables. */
196  cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
197    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
198                                (TABLE_SIZE * SIZEOF(INT32)));
199
200  for (i = 0; i <= MAXJSAMPLE; i++) {
201    rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
202    rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
203    rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
204  }
205}
206
207
208/*
209 * Convert RGB to grayscale.
210 */
211
212METHODDEF(void)
213rgb_gray_convert (j_decompress_ptr cinfo,
214                  JSAMPIMAGE input_buf, JDIMENSION input_row,
215                  JSAMPARRAY output_buf, int num_rows)
216{
217  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
218  register int r, g, b;
219  register INT32 * ctab = cconvert->rgb_y_tab;
220  register JSAMPROW outptr;
221  register JSAMPROW inptr0, inptr1, inptr2;
222  register JDIMENSION col;
223  JDIMENSION num_cols = cinfo->output_width;
224
225  while (--num_rows >= 0) {
226    inptr0 = input_buf[0][input_row];
227    inptr1 = input_buf[1][input_row];
228    inptr2 = input_buf[2][input_row];
229    input_row++;
230    outptr = *output_buf++;
231    for (col = 0; col < num_cols; col++) {
232      r = GETJSAMPLE(inptr0[col]);
233      g = GETJSAMPLE(inptr1[col]);
234      b = GETJSAMPLE(inptr2[col]);
235      /* Y */
236      outptr[col] = (JSAMPLE)
237                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
238                 >> SCALEBITS);
239    }
240  }
241}
242
243
244/*
245 * No colorspace change, but conversion from separate-planes
246 * to interleaved representation.
247 */
248
249METHODDEF(void)
250rgb_convert (j_decompress_ptr cinfo,
251             JSAMPIMAGE input_buf, JDIMENSION input_row,
252             JSAMPARRAY output_buf, int num_rows)
253{
254  register JSAMPROW outptr;
255  register JSAMPROW inptr0, inptr1, inptr2;
256  register JDIMENSION col;
257  JDIMENSION num_cols = cinfo->output_width;
258
259  while (--num_rows >= 0) {
260    inptr0 = input_buf[0][input_row];
261    inptr1 = input_buf[1][input_row];
262    inptr2 = input_buf[2][input_row];
263    input_row++;
264    outptr = *output_buf++;
265    for (col = 0; col < num_cols; col++) {
266      /* We can dispense with GETJSAMPLE() here */
267      outptr[RGB_RED]   = inptr0[col];
268      outptr[RGB_GREEN] = inptr1[col];
269      outptr[RGB_BLUE]  = inptr2[col];
270      outptr += RGB_PIXELSIZE;
271    }
272  }
273}
274
275
276/*
277 * Color conversion for no colorspace change: just copy the data,
278 * converting from separate-planes to interleaved representation.
279 */
280
281METHODDEF(void)
282null_convert (j_decompress_ptr cinfo,
283              JSAMPIMAGE input_buf, JDIMENSION input_row,
284              JSAMPARRAY output_buf, int num_rows)
285{
286  register JSAMPROW inptr, outptr;
287  register JDIMENSION count;
288  register int num_components = cinfo->num_components;
289  JDIMENSION num_cols = cinfo->output_width;
290  int ci;
291
292  while (--num_rows >= 0) {
293    for (ci = 0; ci < num_components; ci++) {
294      inptr = input_buf[ci][input_row];
295      outptr = output_buf[0] + ci;
296      for (count = num_cols; count > 0; count--) {
297        *outptr = *inptr++;     /* needn't bother with GETJSAMPLE() here */
298        outptr += num_components;
299      }
300    }
301    input_row++;
302    output_buf++;
303  }
304}
305
306
307/*
308 * Color conversion for grayscale: just copy the data.
309 * This also works for YCbCr -> grayscale conversion, in which
310 * we just copy the Y (luminance) component and ignore chrominance.
311 */
312
313METHODDEF(void)
314grayscale_convert (j_decompress_ptr cinfo,
315                   JSAMPIMAGE input_buf, JDIMENSION input_row,
316                   JSAMPARRAY output_buf, int num_rows)
317{
318  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
319                    num_rows, cinfo->output_width);
320}
321
322
323/*
324 * Convert grayscale to RGB: just duplicate the graylevel three times.
325 * This is provided to support applications that don't want to cope
326 * with grayscale as a separate case.
327 */
328
329METHODDEF(void)
330gray_rgb_convert (j_decompress_ptr cinfo,
331                  JSAMPIMAGE input_buf, JDIMENSION input_row,
332                  JSAMPARRAY output_buf, int num_rows)
333{
334  register JSAMPROW inptr, outptr;
335  register JDIMENSION col;
336  JDIMENSION num_cols = cinfo->output_width;
337
338  while (--num_rows >= 0) {
339    inptr = input_buf[0][input_row++];
340    outptr = *output_buf++;
341    for (col = 0; col < num_cols; col++) {
342      /* We can dispense with GETJSAMPLE() here */
343      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
344      outptr += RGB_PIXELSIZE;
345    }
346  }
347}
348
349
350/*
351 * Adobe-style YCCK->CMYK conversion.
352 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
353 * conversion as above, while passing K (black) unchanged.
354 * We assume build_ycc_rgb_table has been called.
355 */
356
357METHODDEF(void)
358ycck_cmyk_convert (j_decompress_ptr cinfo,
359                   JSAMPIMAGE input_buf, JDIMENSION input_row,
360                   JSAMPARRAY output_buf, int num_rows)
361{
362  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
363  register int y, cb, cr;
364  register JSAMPROW outptr;
365  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
366  register JDIMENSION col;
367  JDIMENSION num_cols = cinfo->output_width;
368  /* copy these pointers into registers if possible */
369  register JSAMPLE * range_limit = cinfo->sample_range_limit;
370  register int * Crrtab = cconvert->Cr_r_tab;
371  register int * Cbbtab = cconvert->Cb_b_tab;
372  register INT32 * Crgtab = cconvert->Cr_g_tab;
373  register INT32 * Cbgtab = cconvert->Cb_g_tab;
374  SHIFT_TEMPS
375
376  while (--num_rows >= 0) {
377    inptr0 = input_buf[0][input_row];
378    inptr1 = input_buf[1][input_row];
379    inptr2 = input_buf[2][input_row];
380    inptr3 = input_buf[3][input_row];
381    input_row++;
382    outptr = *output_buf++;
383    for (col = 0; col < num_cols; col++) {
384      y  = GETJSAMPLE(inptr0[col]);
385      cb = GETJSAMPLE(inptr1[col]);
386      cr = GETJSAMPLE(inptr2[col]);
387      /* Range-limiting is essential due to noise introduced by DCT losses. */
388      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
389      outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
390                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
391                                                 SCALEBITS)))];
392      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
393      /* K passes through unchanged */
394      outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
395      outptr += 4;
396    }
397  }
398}
399
400
401/*
402 * Empty method for start_pass.
403 */
404
405METHODDEF(void)
406start_pass_dcolor (j_decompress_ptr cinfo)
407{
408  /* no work needed */
409}
410
411
412/*
413 * Module initialization routine for output colorspace conversion.
414 */
415
416GLOBAL(void)
417jinit_color_deconverter (j_decompress_ptr cinfo)
418{
419  my_cconvert_ptr cconvert;
420  int ci;
421
422  cconvert = (my_cconvert_ptr)
423    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
424                                SIZEOF(my_color_deconverter));
425  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
426  cconvert->pub.start_pass = start_pass_dcolor;
427
428  /* Make sure num_components agrees with jpeg_color_space */
429  switch (cinfo->jpeg_color_space) {
430  case JCS_GRAYSCALE:
431    if (cinfo->num_components != 1)
432      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
433    break;
434
435  case JCS_RGB:
436  case JCS_YCbCr:
437    if (cinfo->num_components != 3)
438      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
439    break;
440
441  case JCS_CMYK:
442  case JCS_YCCK:
443    if (cinfo->num_components != 4)
444      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
445    break;
446
447  default:                      /* JCS_UNKNOWN can be anything */
448    if (cinfo->num_components < 1)
449      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
450    break;
451  }
452
453  /* Set out_color_components and conversion method based on requested space.
454   * Also clear the component_needed flags for any unused components,
455   * so that earlier pipeline stages can avoid useless computation.
456   */
457
458  switch (cinfo->out_color_space) {
459  case JCS_GRAYSCALE:
460    cinfo->out_color_components = 1;
461    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
462        cinfo->jpeg_color_space == JCS_YCbCr) {
463      cconvert->pub.color_convert = grayscale_convert;
464      /* For color->grayscale conversion, only the Y (0) component is needed */
465      for (ci = 1; ci < cinfo->num_components; ci++)
466        cinfo->comp_info[ci].component_needed = FALSE;
467    } else if (cinfo->jpeg_color_space == JCS_RGB) {
468      cconvert->pub.color_convert = rgb_gray_convert;
469      build_rgb_y_table(cinfo);
470    } else
471      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
472    break;
473
474  case JCS_RGB:
475    cinfo->out_color_components = RGB_PIXELSIZE;
476    if (cinfo->jpeg_color_space == JCS_YCbCr) {
477      cconvert->pub.color_convert = ycc_rgb_convert;
478      build_ycc_rgb_table(cinfo);
479    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
480      cconvert->pub.color_convert = gray_rgb_convert;
481    } else if (cinfo->jpeg_color_space == JCS_RGB) {
482      cconvert->pub.color_convert = rgb_convert;
483    } else
484      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
485    break;
486
487  case JCS_CMYK:
488    cinfo->out_color_components = 4;
489    if (cinfo->jpeg_color_space == JCS_YCCK) {
490      cconvert->pub.color_convert = ycck_cmyk_convert;
491      build_ycc_rgb_table(cinfo);
492    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
493      cconvert->pub.color_convert = null_convert;
494    } else
495      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
496    break;
497
498  default:
499    /* Permit null conversion to same output space */
500    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
501      cinfo->out_color_components = cinfo->num_components;
502      cconvert->pub.color_convert = null_convert;
503    } else                      /* unsupported non-null conversion */
504      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
505    break;
506  }
507
508  if (cinfo->quantize_colors)
509    cinfo->output_components = 1; /* single colormapped output component */
510  else
511    cinfo->output_components = cinfo->out_color_components;
512}
Note: See TracBrowser for help on using the repository browser.