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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019