Main MRPT website > C++ reference for MRPT 1.5.9
jccolor.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 
15 /* Private subobject */
16 
17 typedef struct {
18  struct jpeg_color_converter pub; /* public fields */
19 
20  /* Private state for RGB->YCC conversion */
21  INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
23 
25 
26 
27 /**************** RGB -> YCbCr conversion: most common case **************/
28 
29 /*
30  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
31  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
32  * The conversion equations to be implemented are therefore
33  * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
34  * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
35  * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
36  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
37  * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
38  * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
39  * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
40  * were not represented exactly. Now we sacrifice exact representation of
41  * maximum red and maximum blue in order to get exact grayscales.
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  *
47  * For even more speed, we avoid doing any multiplications in the inner loop
48  * by precalculating the constants times R,G,B 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 CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
54  * in the tables to save adding them separately in the inner loop.
55  */
56 
57 #define SCALEBITS 16 /* speediest right-shift on some machines */
58 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
59 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
60 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
61 
62 /* We allocate one big table and divide it up into eight parts, instead of
63  * doing eight alloc_small requests. This lets us use a single table base
64  * address, which can be held in a in the inner loops on many
65  * machines (more than can hold all eight addresses, anyway).
66  */
67 
68 #define R_Y_OFF 0 /* offset to R => Y section */
69 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
70 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
71 #define R_CB_OFF (3*(MAXJSAMPLE+1))
72 #define G_CB_OFF (4*(MAXJSAMPLE+1))
73 #define B_CB_OFF (5*(MAXJSAMPLE+1))
74 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
75 #define G_CR_OFF (6*(MAXJSAMPLE+1))
76 #define B_CR_OFF (7*(MAXJSAMPLE+1))
77 #define TABLE_SIZE (8*(MAXJSAMPLE+1))
78 
79 
80 /*
81  * Initialize for RGB->YCC colorspace conversion.
82  */
83 
84 METHODDEF(void)
86 {
87  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
88  INT32 * rgb_ycc_tab;
89  INT32 i;
90 
91  /* Allocate and fill in the conversion tables. */
92  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
93  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
94  (TABLE_SIZE * SIZEOF(INT32)));
95 
96  for (i = 0; i <= MAXJSAMPLE; i++) {
97  rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
98  rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
99  rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
100  rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
101  rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
102  /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
103  * This ensures that the maximum output will round to MAXJSAMPLE
104  * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
105  */
106  rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
107 /* B=>Cb and R=>Cr tables are the same
108  rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
109 */
110  rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
111  rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
112  }
113 }
114 
115 
116 /*
117  * Convert some rows of samples to the JPEG colorspace.
118  *
119  * Note that we change from the application's interleaved-pixel format
120  * to our internal noninterleaved, one-plane-per-component format.
121  * The input buffer is therefore three times as wide as the output buffer.
122  *
123  * A starting row offset is provided only for the output buffer. The caller
124  * can easily adjust the passed input_buf value to accommodate any row
125  * offset required on that side.
126  */
127 
128 METHODDEF(void)
132 {
133  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
134  int r, g, b;
135  INT32 * ctab = cconvert->rgb_ycc_tab;
136  JSAMPROW inptr;
137  JSAMPROW outptr0, outptr1, outptr2;
138  JDIMENSION col;
139  JDIMENSION num_cols = cinfo->image_width;
140 
141  while (--num_rows >= 0) {
142  inptr = *input_buf++;
143  outptr0 = output_buf[0][output_row];
144  outptr1 = output_buf[1][output_row];
145  outptr2 = output_buf[2][output_row];
146  output_row++;
147  for (col = 0; col < num_cols; col++) {
148  r = GETJSAMPLE(inptr[RGB_RED]);
149  g = GETJSAMPLE(inptr[RGB_GREEN]);
150  b = GETJSAMPLE(inptr[RGB_BLUE]);
151  inptr += RGB_PIXELSIZE;
152  /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
153  * must be too; we do not need an explicit range-limiting operation.
154  * Hence the value being shifted is never negative, and we don't
155  * need the general RIGHT_SHIFT macro.
156  */
157  /* Y */
158  outptr0[col] = (JSAMPLE)
159  ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
160  >> SCALEBITS);
161  /* Cb */
162  outptr1[col] = (JSAMPLE)
163  ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
164  >> SCALEBITS);
165  /* Cr */
166  outptr2[col] = (JSAMPLE)
167  ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
168  >> SCALEBITS);
169  }
170  }
171 }
172 
173 
174 /**************** Cases other than RGB -> YCbCr **************/
175 
176 
177 /*
178  * Convert some rows of samples to the JPEG colorspace.
179  * This version handles RGB->grayscale conversion, which is the same
180  * as the RGB->Y portion of RGB->YCbCr.
181  * We assume rgb_ycc_start has been called (we only use the Y tables).
182  */
183 
184 METHODDEF(void)
188 {
189  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
190  int r, g, b;
191  INT32 * ctab = cconvert->rgb_ycc_tab;
192  JSAMPROW inptr;
193  JSAMPROW outptr;
194  JDIMENSION col;
195  JDIMENSION num_cols = cinfo->image_width;
196 
197  while (--num_rows >= 0) {
198  inptr = *input_buf++;
199  outptr = output_buf[0][output_row];
200  output_row++;
201  for (col = 0; col < num_cols; col++) {
202  r = GETJSAMPLE(inptr[RGB_RED]);
203  g = GETJSAMPLE(inptr[RGB_GREEN]);
204  b = GETJSAMPLE(inptr[RGB_BLUE]);
205  inptr += RGB_PIXELSIZE;
206  /* Y */
207  outptr[col] = (JSAMPLE)
208  ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
209  >> SCALEBITS);
210  }
211  }
212 }
213 
214 
215 /*
216  * Convert some rows of samples to the JPEG colorspace.
217  * This version handles Adobe-style CMYK->YCCK conversion,
218  * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
219  * conversion as above, while passing K (black) unchanged.
220  * We assume rgb_ycc_start has been called.
221  */
222 
223 METHODDEF(void)
227 {
228  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
229  int r, g, b;
230  INT32 * ctab = cconvert->rgb_ycc_tab;
231  JSAMPROW inptr;
232  JSAMPROW outptr0, outptr1, outptr2, outptr3;
233  JDIMENSION col;
234  JDIMENSION num_cols = cinfo->image_width;
235 
236  while (--num_rows >= 0) {
237  inptr = *input_buf++;
238  outptr0 = output_buf[0][output_row];
239  outptr1 = output_buf[1][output_row];
240  outptr2 = output_buf[2][output_row];
241  outptr3 = output_buf[3][output_row];
242  output_row++;
243  for (col = 0; col < num_cols; col++) {
244  r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
245  g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
246  b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
247  /* K passes through as-is */
248  outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
249  inptr += 4;
250  /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
251  * must be too; we do not need an explicit range-limiting operation.
252  * Hence the value being shifted is never negative, and we don't
253  * need the general RIGHT_SHIFT macro.
254  */
255  /* Y */
256  outptr0[col] = (JSAMPLE)
257  ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
258  >> SCALEBITS);
259  /* Cb */
260  outptr1[col] = (JSAMPLE)
261  ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
262  >> SCALEBITS);
263  /* Cr */
264  outptr2[col] = (JSAMPLE)
265  ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
266  >> SCALEBITS);
267  }
268  }
269 }
270 
271 
272 /*
273  * Convert some rows of samples to the JPEG colorspace.
274  * This version handles grayscale output with no conversion.
275  * The source can be either plain grayscale or YCbCr (since Y == gray).
276  */
277 
278 METHODDEF(void)
282 {
283  JSAMPROW inptr;
284  JSAMPROW outptr;
285  JDIMENSION col;
286  JDIMENSION num_cols = cinfo->image_width;
287  int instride = cinfo->input_components;
288 
289  while (--num_rows >= 0) {
290  inptr = *input_buf++;
291  outptr = output_buf[0][output_row];
292  output_row++;
293  for (col = 0; col < num_cols; col++) {
294  outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
295  inptr += instride;
296  }
297  }
298 }
299 
300 
301 /*
302  * Convert some rows of samples to the JPEG colorspace.
303  * This version handles multi-component colorspaces without conversion.
304  * We assume input_components == num_components.
305  */
306 
307 METHODDEF(void)
311 {
312  JSAMPROW inptr;
313  JSAMPROW outptr;
314  JDIMENSION col;
315  int ci;
316  int nc = cinfo->num_components;
317  JDIMENSION num_cols = cinfo->image_width;
318 
319  while (--num_rows >= 0) {
320  /* It seems fastest to make a separate pass for each component. */
321  for (ci = 0; ci < nc; ci++) {
322  inptr = *input_buf;
323  outptr = output_buf[ci][output_row];
324  for (col = 0; col < num_cols; col++) {
325  outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
326  inptr += nc;
327  }
328  }
329  input_buf++;
330  output_row++;
331  }
332 }
333 
334 
335 /*
336  * Empty method for start_pass.
337  */
338 
339 METHODDEF(void)
341 {
342  /* no work needed */
343 }
344 
345 
346 /*
347  * Module initialization routine for input colorspace conversion.
348  */
349 
350 GLOBAL(void)
352 {
353  my_cconvert_ptr cconvert;
354 
355  cconvert = (my_cconvert_ptr)
356  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
358  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
359  /* set start_pass to null method until we find out differently */
360  cconvert->pub.start_pass = null_method;
361 
362  /* Make sure input_components agrees with in_color_space */
363  switch (cinfo->in_color_space) {
364  case JCS_GRAYSCALE:
365  if (cinfo->input_components != 1)
366  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
367  break;
368 
369  case JCS_RGB:
370 #if RGB_PIXELSIZE != 3
371  if (cinfo->input_components != RGB_PIXELSIZE)
372  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
373  break;
374 #endif /* else share code with YCbCr */
375 
376  case JCS_YCbCr:
377  if (cinfo->input_components != 3)
378  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
379  break;
380 
381  case JCS_CMYK:
382  case JCS_YCCK:
383  if (cinfo->input_components != 4)
384  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
385  break;
386 
387  default: /* JCS_UNKNOWN can be anything */
388  if (cinfo->input_components < 1)
389  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
390  break;
391  }
392 
393  /* Check num_components, set conversion method based on requested space */
394  switch (cinfo->jpeg_color_space) {
395  case JCS_GRAYSCALE:
396  if (cinfo->num_components != 1)
397  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
398  if (cinfo->in_color_space == JCS_GRAYSCALE)
399  cconvert->pub.color_convert = grayscale_convert;
400  else if (cinfo->in_color_space == JCS_RGB) {
401  cconvert->pub.start_pass = rgb_ycc_start;
402  cconvert->pub.color_convert = rgb_gray_convert;
403  } else if (cinfo->in_color_space == JCS_YCbCr)
404  cconvert->pub.color_convert = grayscale_convert;
405  else
406  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
407  break;
408 
409  case JCS_RGB:
410  if (cinfo->num_components != 3)
411  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
412  if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
413  cconvert->pub.color_convert = null_convert;
414  else
415  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
416  break;
417 
418  case JCS_YCbCr:
419  if (cinfo->num_components != 3)
420  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
421  if (cinfo->in_color_space == JCS_RGB) {
422  cconvert->pub.start_pass = rgb_ycc_start;
423  cconvert->pub.color_convert = rgb_ycc_convert;
424  } else if (cinfo->in_color_space == JCS_YCbCr)
425  cconvert->pub.color_convert = null_convert;
426  else
427  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
428  break;
429 
430  case JCS_CMYK:
431  if (cinfo->num_components != 4)
432  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
433  if (cinfo->in_color_space == JCS_CMYK)
434  cconvert->pub.color_convert = null_convert;
435  else
436  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
437  break;
438 
439  case JCS_YCCK:
440  if (cinfo->num_components != 4)
441  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
442  if (cinfo->in_color_space == JCS_CMYK) {
443  cconvert->pub.start_pass = rgb_ycc_start;
444  cconvert->pub.color_convert = cmyk_ycck_convert;
445  } else if (cinfo->in_color_space == JCS_YCCK)
446  cconvert->pub.color_convert = null_convert;
447  else
448  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
449  break;
450 
451  default: /* allow null conversion of JCS_UNKNOWN */
452  if (cinfo->jpeg_color_space != cinfo->in_color_space ||
453  cinfo->num_components != cinfo->input_components)
454  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
455  cconvert->pub.color_convert = null_convert;
456  break;
457  }
458 }
char JSAMPLE
Definition: jmorecfg.h:61
null_method(j_compress_ptr)
Definition: jccolor.cpp:340
#define SCALEBITS
Definition: jccolor.cpp:57
INT32 * rgb_ycc_tab
Definition: jccolor.cpp:21
rgb_ycc_start(j_compress_ptr cinfo)
Definition: jccolor.cpp:85
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define GETJSAMPLE(value)
Definition: jmorecfg.h:65
#define ERREXIT(cinfo, code)
Definition: jerror.h:199
#define SIZEOF(object)
Definition: jinclude.h:73
#define MAXJSAMPLE
Definition: jmorecfg.h:70
null_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
Definition: jccolor.cpp:308
#define R_Y_OFF
Definition: jccolor.cpp:68
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:63
long INT32
Definition: jmorecfg.h:158
#define G_CB_OFF
Definition: jccolor.cpp:72
JSAMPIMAGE input_buf
Definition: jccoefct.cpp:59
grayscale_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
Definition: jccolor.cpp:279
rgb_ycc_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
Definition: jccolor.cpp:129
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:746
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:64
GLubyte g
Definition: glext.h:5575
#define G_Y_OFF
Definition: jccolor.cpp:69
GLubyte GLubyte b
Definition: glext.h:5575
#define B_CR_OFF
Definition: jccolor.cpp:76
#define FIX(x)
Definition: jccolor.cpp:60
JBLOCKROW output_row
Definition: jpegint.h:373
struct jpeg_color_converter pub
Definition: jccolor.cpp:18
int JSAMPARRAY int int num_rows
Definition: jpegint.h:370
int JSAMPARRAY int int JDIMENSION num_cols
Definition: jpegint.h:370
rgb_gray_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
Definition: jccolor.cpp:185
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:65
#define ONE_HALF
Definition: jccolor.cpp:59
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
JSAMPIMAGE output_buf
Definition: jdcoefct.cpp:59
#define GLOBAL(type)
Definition: jmorecfg.h:185
cmyk_ycck_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
Definition: jccolor.cpp:224
#define METHODDEF(type)
Definition: jmorecfg.h:181
jinit_color_converter(j_compress_ptr cinfo)
Definition: jccolor.cpp:351
#define R_CB_OFF
Definition: jccolor.cpp:71
#define B_Y_OFF
Definition: jccolor.cpp:70
unsigned int JDIMENSION
Definition: jmorecfg.h:168
my_color_converter * my_cconvert_ptr
Definition: jccolor.cpp:24
#define G_CR_OFF
Definition: jccolor.cpp:75
#define CBCR_OFFSET
Definition: jccolor.cpp:58
#define R_CR_OFF
Definition: jccolor.cpp:74
#define B_CB_OFF
Definition: jccolor.cpp:73
#define TABLE_SIZE
Definition: jccolor.cpp:77



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020