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