]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdcolor.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / jpeg6 / jdcolor.cpp
1 /*\r
2 \r
3  * jdcolor.c\r
4 \r
5  *\r
6 \r
7  * Copyright (C) 1991-1995, Thomas G. Lane.\r
8 \r
9  * This file is part of the Independent JPEG Group's software.\r
10 \r
11  * For conditions of distribution and use, see the accompanying README file.\r
12 \r
13  *\r
14 \r
15  * This file contains output colorspace conversion routines.\r
16 \r
17  */\r
18 \r
19 \r
20 \r
21 #define JPEG_INTERNALS\r
22 \r
23 #include "jinclude.h"\r
24 \r
25 #include "radiant_jpeglib.h"\r
26 \r
27 \r
28 \r
29 \r
30 \r
31 /* Private subobject */\r
32 \r
33 \r
34 \r
35 typedef struct {\r
36 \r
37   struct jpeg_color_deconverter pub; /* public fields */\r
38 \r
39 \r
40 \r
41   /* Private state for YCC->RGB conversion */\r
42 \r
43   int * Cr_r_tab;               /* => table for Cr to R conversion */\r
44 \r
45   int * Cb_b_tab;               /* => table for Cb to B conversion */\r
46 \r
47   INT32 * Cr_g_tab;             /* => table for Cr to G conversion */\r
48 \r
49   INT32 * Cb_g_tab;             /* => table for Cb to G conversion */\r
50 \r
51 } my_color_deconverter;\r
52 \r
53 \r
54 \r
55 typedef my_color_deconverter * my_cconvert_ptr;\r
56 \r
57 \r
58 \r
59 \r
60 \r
61 /**************** YCbCr -> RGB conversion: most common case **************/\r
62 \r
63 \r
64 \r
65 /*\r
66 \r
67  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are\r
68 \r
69  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.\r
70 \r
71  * The conversion equations to be implemented are therefore\r
72 \r
73  *      R = Y                + 1.40200 * Cr\r
74 \r
75  *      G = Y - 0.34414 * Cb - 0.71414 * Cr\r
76 \r
77  *      B = Y + 1.77200 * Cb\r
78 \r
79  * where Cb and Cr represent the incoming values less CENTERJSAMPLE.\r
80 \r
81  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)\r
82 \r
83  *\r
84 \r
85  * To avoid floating-point arithmetic, we represent the fractional constants\r
86 \r
87  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide\r
88 \r
89  * the products by 2^16, with appropriate rounding, to get the correct answer.\r
90 \r
91  * Notice that Y, being an integral input, does not contribute any fraction\r
92 \r
93  * so it need not participate in the rounding.\r
94 \r
95  *\r
96 \r
97  * For even more speed, we avoid doing any multiplications in the inner loop\r
98 \r
99  * by precalculating the constants times Cb and Cr for all possible values.\r
100 \r
101  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);\r
102 \r
103  * for 12-bit samples it is still acceptable.  It's not very reasonable for\r
104 \r
105  * 16-bit samples, but if you want lossless storage you shouldn't be changing\r
106 \r
107  * colorspace anyway.\r
108 \r
109  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the\r
110 \r
111  * values for the G calculation are left scaled up, since we must add them\r
112 \r
113  * together before rounding.\r
114 \r
115  */\r
116 \r
117 \r
118 \r
119 #define SCALEBITS       16      /* speediest right-shift on some machines */\r
120 \r
121 #define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))\r
122 \r
123 #define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))\r
124 \r
125 \r
126 \r
127 \r
128 \r
129 /*\r
130 \r
131  * Initialize tables for YCC->RGB colorspace conversion.\r
132 \r
133  */\r
134 \r
135 \r
136 \r
137 LOCAL void\r
138 \r
139 build_ycc_rgb_table (j_decompress_ptr cinfo)\r
140 \r
141 {\r
142 \r
143   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;\r
144 \r
145   int i;\r
146 \r
147   INT32 x;\r
148 \r
149   SHIFT_TEMPS\r
150 \r
151 \r
152 \r
153   cconvert->Cr_r_tab = (int *)\r
154 \r
155     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
156 \r
157                                 (MAXJSAMPLE+1) * SIZEOF(int));\r
158 \r
159   cconvert->Cb_b_tab = (int *)\r
160 \r
161     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
162 \r
163                                 (MAXJSAMPLE+1) * SIZEOF(int));\r
164 \r
165   cconvert->Cr_g_tab = (INT32 *)\r
166 \r
167     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
168 \r
169                                 (MAXJSAMPLE+1) * SIZEOF(INT32));\r
170 \r
171   cconvert->Cb_g_tab = (INT32 *)\r
172 \r
173     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
174 \r
175                                 (MAXJSAMPLE+1) * SIZEOF(INT32));\r
176 \r
177 \r
178 \r
179   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {\r
180 \r
181     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */\r
182 \r
183     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */\r
184 \r
185     /* Cr=>R value is nearest int to 1.40200 * x */\r
186 \r
187     cconvert->Cr_r_tab[i] = (int)\r
188 \r
189                     RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);\r
190 \r
191     /* Cb=>B value is nearest int to 1.77200 * x */\r
192 \r
193     cconvert->Cb_b_tab[i] = (int)\r
194 \r
195                     RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);\r
196 \r
197     /* Cr=>G value is scaled-up -0.71414 * x */\r
198 \r
199     cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;\r
200 \r
201     /* Cb=>G value is scaled-up -0.34414 * x */\r
202 \r
203     /* We also add in ONE_HALF so that need not do it in inner loop */\r
204 \r
205     cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;\r
206 \r
207   }\r
208 \r
209 }\r
210 \r
211 \r
212 \r
213 \r
214 \r
215 /*\r
216 \r
217  * Convert some rows of samples to the output colorspace.\r
218 \r
219  *\r
220 \r
221  * Note that we change from noninterleaved, one-plane-per-component format\r
222 \r
223  * to interleaved-pixel format.  The output buffer is therefore three times\r
224 \r
225  * as wide as the input buffer.\r
226 \r
227  * A starting row offset is provided only for the input buffer.  The caller\r
228 \r
229  * can easily adjust the passed output_buf value to accommodate any row\r
230 \r
231  * offset required on that side.\r
232 \r
233  */\r
234 \r
235 \r
236 \r
237 METHODDEF void\r
238 \r
239 ycc_rgb_convert (j_decompress_ptr cinfo,\r
240 \r
241                  JSAMPIMAGE input_buf, JDIMENSION input_row,\r
242 \r
243                  JSAMPARRAY output_buf, int num_rows)\r
244 \r
245 {\r
246 \r
247   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;\r
248 \r
249   register int y, cb, cr;\r
250 \r
251   register JSAMPROW outptr;\r
252 \r
253   register JSAMPROW inptr0, inptr1, inptr2;\r
254 \r
255   register JDIMENSION col;\r
256 \r
257   JDIMENSION num_cols = cinfo->output_width;\r
258 \r
259   /* copy these pointers into registers if possible */\r
260 \r
261   register JSAMPLE * range_limit = cinfo->sample_range_limit;\r
262 \r
263   register int * Crrtab = cconvert->Cr_r_tab;\r
264 \r
265   register int * Cbbtab = cconvert->Cb_b_tab;\r
266 \r
267   register INT32 * Crgtab = cconvert->Cr_g_tab;\r
268 \r
269   register INT32 * Cbgtab = cconvert->Cb_g_tab;\r
270 \r
271   SHIFT_TEMPS\r
272 \r
273 \r
274 \r
275   while (--num_rows >= 0) {\r
276 \r
277     inptr0 = input_buf[0][input_row];\r
278 \r
279     inptr1 = input_buf[1][input_row];\r
280 \r
281     inptr2 = input_buf[2][input_row];\r
282 \r
283     input_row++;\r
284 \r
285     outptr = *output_buf++;\r
286 \r
287     for (col = 0; col < num_cols; col++) {\r
288 \r
289       y  = GETJSAMPLE(inptr0[col]);\r
290 \r
291       cb = GETJSAMPLE(inptr1[col]);\r
292 \r
293       cr = GETJSAMPLE(inptr2[col]);\r
294 \r
295       /* Range-limiting is essential due to noise introduced by DCT losses. */\r
296 \r
297       outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];\r
298 \r
299       outptr[RGB_GREEN] = range_limit[y +\r
300 \r
301                               ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],\r
302 \r
303                                                  SCALEBITS))];\r
304 \r
305       outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];\r
306 \r
307       outptr += RGB_PIXELSIZE;\r
308 \r
309     }\r
310 \r
311   }\r
312 \r
313 }\r
314 \r
315 \r
316 \r
317 \r
318 \r
319 /**************** Cases other than YCbCr -> RGB **************/\r
320 \r
321 \r
322 \r
323 \r
324 \r
325 /*\r
326 \r
327  * Color conversion for no colorspace change: just copy the data,\r
328 \r
329  * converting from separate-planes to interleaved representation.\r
330 \r
331  */\r
332 \r
333 \r
334 \r
335 METHODDEF void\r
336 \r
337 null_convert (j_decompress_ptr cinfo,\r
338 \r
339               JSAMPIMAGE input_buf, JDIMENSION input_row,\r
340 \r
341               JSAMPARRAY output_buf, int num_rows)\r
342 \r
343 {\r
344 \r
345   register JSAMPROW inptr, outptr;\r
346 \r
347   register JDIMENSION count;\r
348 \r
349   register int num_components = cinfo->num_components;\r
350 \r
351   JDIMENSION num_cols = cinfo->output_width;\r
352 \r
353   int ci;\r
354 \r
355 \r
356 \r
357   while (--num_rows >= 0) {\r
358 \r
359     for (ci = 0; ci < num_components; ci++) {\r
360 \r
361       inptr = input_buf[ci][input_row];\r
362 \r
363       outptr = output_buf[0] + ci;\r
364 \r
365       for (count = num_cols; count > 0; count--) {\r
366 \r
367         *outptr = *inptr++;     /* needn't bother with GETJSAMPLE() here */\r
368 \r
369         outptr += num_components;\r
370 \r
371       }\r
372 \r
373     }\r
374 \r
375     input_row++;\r
376 \r
377     output_buf++;\r
378 \r
379   }\r
380 \r
381 }\r
382 \r
383 \r
384 \r
385 \r
386 \r
387 /*\r
388 \r
389  * Color conversion for grayscale: just copy the data.\r
390 \r
391  * This also works for YCbCr -> grayscale conversion, in which\r
392 \r
393  * we just copy the Y (luminance) component and ignore chrominance.\r
394 \r
395  */\r
396 \r
397 \r
398 \r
399 METHODDEF void\r
400 \r
401 grayscale_convert (j_decompress_ptr cinfo,\r
402 \r
403                    JSAMPIMAGE input_buf, JDIMENSION input_row,\r
404 \r
405                    JSAMPARRAY output_buf, int num_rows)\r
406 \r
407 {\r
408 \r
409   jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,\r
410 \r
411                     num_rows, cinfo->output_width);\r
412 \r
413 }\r
414 \r
415 \r
416 \r
417 \r
418 \r
419 /*\r
420 \r
421  * Adobe-style YCCK->CMYK conversion.\r
422 \r
423  * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same\r
424 \r
425  * conversion as above, while passing K (black) unchanged.\r
426 \r
427  * We assume build_ycc_rgb_table has been called.\r
428 \r
429  */\r
430 \r
431 \r
432 \r
433 METHODDEF void\r
434 \r
435 ycck_cmyk_convert (j_decompress_ptr cinfo,\r
436 \r
437                    JSAMPIMAGE input_buf, JDIMENSION input_row,\r
438 \r
439                    JSAMPARRAY output_buf, int num_rows)\r
440 \r
441 {\r
442 \r
443   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;\r
444 \r
445   register int y, cb, cr;\r
446 \r
447   register JSAMPROW outptr;\r
448 \r
449   register JSAMPROW inptr0, inptr1, inptr2, inptr3;\r
450 \r
451   register JDIMENSION col;\r
452 \r
453   JDIMENSION num_cols = cinfo->output_width;\r
454 \r
455   /* copy these pointers into registers if possible */\r
456 \r
457   register JSAMPLE * range_limit = cinfo->sample_range_limit;\r
458 \r
459   register int * Crrtab = cconvert->Cr_r_tab;\r
460 \r
461   register int * Cbbtab = cconvert->Cb_b_tab;\r
462 \r
463   register INT32 * Crgtab = cconvert->Cr_g_tab;\r
464 \r
465   register INT32 * Cbgtab = cconvert->Cb_g_tab;\r
466 \r
467   SHIFT_TEMPS\r
468 \r
469 \r
470 \r
471   while (--num_rows >= 0) {\r
472 \r
473     inptr0 = input_buf[0][input_row];\r
474 \r
475     inptr1 = input_buf[1][input_row];\r
476 \r
477     inptr2 = input_buf[2][input_row];\r
478 \r
479     inptr3 = input_buf[3][input_row];\r
480 \r
481     input_row++;\r
482 \r
483     outptr = *output_buf++;\r
484 \r
485     for (col = 0; col < num_cols; col++) {\r
486 \r
487       y  = GETJSAMPLE(inptr0[col]);\r
488 \r
489       cb = GETJSAMPLE(inptr1[col]);\r
490 \r
491       cr = GETJSAMPLE(inptr2[col]);\r
492 \r
493       /* Range-limiting is essential due to noise introduced by DCT losses. */\r
494 \r
495       outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */\r
496 \r
497       outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */\r
498 \r
499                               ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],\r
500 \r
501                                                  SCALEBITS)))];\r
502 \r
503       outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */\r
504 \r
505       /* K passes through unchanged */\r
506 \r
507       outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */\r
508 \r
509       outptr += 4;\r
510 \r
511     }\r
512 \r
513   }\r
514 \r
515 }\r
516 \r
517 \r
518 \r
519 \r
520 \r
521 /*\r
522 \r
523  * Empty method for start_pass.\r
524 \r
525  */\r
526 \r
527 \r
528 \r
529 METHODDEF void\r
530 \r
531 start_pass_dcolor (j_decompress_ptr cinfo)\r
532 \r
533 {\r
534 \r
535   /* no work needed */\r
536 \r
537 }\r
538 \r
539 \r
540 \r
541 \r
542 \r
543 /*\r
544 \r
545  * Module initialization routine for output colorspace conversion.\r
546 \r
547  */\r
548 \r
549 \r
550 \r
551 GLOBAL void\r
552 \r
553 jinit_color_deconverter (j_decompress_ptr cinfo)\r
554 \r
555 {\r
556 \r
557   my_cconvert_ptr cconvert;\r
558 \r
559   int ci;\r
560 \r
561 \r
562 \r
563   cconvert = (my_cconvert_ptr)\r
564 \r
565     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
566 \r
567                                 SIZEOF(my_color_deconverter));\r
568 \r
569   cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;\r
570 \r
571   cconvert->pub.start_pass = start_pass_dcolor;\r
572 \r
573 \r
574 \r
575   /* Make sure num_components agrees with jpeg_color_space */\r
576 \r
577   switch (cinfo->jpeg_color_space) {\r
578 \r
579   case JCS_GRAYSCALE:\r
580 \r
581     if (cinfo->num_components != 1)\r
582 \r
583       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);\r
584 \r
585     break;\r
586 \r
587 \r
588 \r
589   case JCS_RGB:\r
590 \r
591   case JCS_YCbCr:\r
592 \r
593     if (cinfo->num_components != 3)\r
594 \r
595       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);\r
596 \r
597     break;\r
598 \r
599 \r
600 \r
601   case JCS_CMYK:\r
602 \r
603   case JCS_YCCK:\r
604 \r
605     if (cinfo->num_components != 4)\r
606 \r
607       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);\r
608 \r
609     break;\r
610 \r
611 \r
612 \r
613   default:                      /* JCS_UNKNOWN can be anything */\r
614 \r
615     if (cinfo->num_components < 1)\r
616 \r
617       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);\r
618 \r
619     break;\r
620 \r
621   }\r
622 \r
623 \r
624 \r
625   /* Set out_color_components and conversion method based on requested space.\r
626 \r
627    * Also clear the component_needed flags for any unused components,\r
628 \r
629    * so that earlier pipeline stages can avoid useless computation.\r
630 \r
631    */\r
632 \r
633 \r
634 \r
635   switch (cinfo->out_color_space) {\r
636 \r
637   case JCS_GRAYSCALE:\r
638 \r
639     cinfo->out_color_components = 1;\r
640 \r
641     if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||\r
642 \r
643         cinfo->jpeg_color_space == JCS_YCbCr) {\r
644 \r
645       cconvert->pub.color_convert = grayscale_convert;\r
646 \r
647       /* For color->grayscale conversion, only the Y (0) component is needed */\r
648 \r
649       for (ci = 1; ci < cinfo->num_components; ci++)\r
650 \r
651         cinfo->comp_info[ci].component_needed = FALSE;\r
652 \r
653     } else\r
654 \r
655       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);\r
656 \r
657     break;\r
658 \r
659 \r
660 \r
661   case JCS_RGB:\r
662 \r
663     cinfo->out_color_components = RGB_PIXELSIZE;\r
664 \r
665     if (cinfo->jpeg_color_space == JCS_YCbCr) {\r
666 \r
667       cconvert->pub.color_convert = ycc_rgb_convert;\r
668 \r
669       build_ycc_rgb_table(cinfo);\r
670 \r
671     } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {\r
672 \r
673       cconvert->pub.color_convert = null_convert;\r
674 \r
675     } else\r
676 \r
677       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);\r
678 \r
679     break;\r
680 \r
681 \r
682 \r
683   case JCS_CMYK:\r
684 \r
685     cinfo->out_color_components = 4;\r
686 \r
687     if (cinfo->jpeg_color_space == JCS_YCCK) {\r
688 \r
689       cconvert->pub.color_convert = ycck_cmyk_convert;\r
690 \r
691       build_ycc_rgb_table(cinfo);\r
692 \r
693     } else if (cinfo->jpeg_color_space == JCS_CMYK) {\r
694 \r
695       cconvert->pub.color_convert = null_convert;\r
696 \r
697     } else\r
698 \r
699       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);\r
700 \r
701     break;\r
702 \r
703 \r
704 \r
705   default:\r
706 \r
707     /* Permit null conversion to same output space */\r
708 \r
709     if (cinfo->out_color_space == cinfo->jpeg_color_space) {\r
710 \r
711       cinfo->out_color_components = cinfo->num_components;\r
712 \r
713       cconvert->pub.color_convert = null_convert;\r
714 \r
715     } else                      /* unsupported non-null conversion */\r
716 \r
717       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);\r
718 \r
719     break;\r
720 \r
721   }\r
722 \r
723 \r
724 \r
725   if (cinfo->quantize_colors)\r
726 \r
727     cinfo->output_components = 1; /* single colormapped output component */\r
728 \r
729   else\r
730 \r
731     cinfo->output_components = cinfo->out_color_components;\r
732 \r
733 }\r
734 \r