blob: 72a530107036783cbc768869e9a45fe702dfd0d9 [file] [log] [blame]
DRCb4570bb2011-09-07 06:31:00 +00001/*
2 * jdcolext.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
DRCb4570bb2011-09-07 06:31:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
Alex Naidis6eb7d372016-10-16 23:10:08 +02007 * Copyright (C) 2009, 2011, 2015, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
DRCb4570bb2011-09-07 06:31:00 +000010 *
11 * This file contains output colorspace conversion routines.
12 */
13
14
15/* This file is included by jdcolor.c */
16
17
18/*
19 * Convert some rows of samples to the output colorspace.
20 *
21 * Note that we change from noninterleaved, one-plane-per-component format
22 * to interleaved-pixel format. The output buffer is therefore three times
23 * as wide as the input buffer.
24 * A starting row offset is provided only for the input buffer. The caller
25 * can easily adjust the passed output_buf value to accommodate any row
26 * offset required on that side.
27 */
28
29INLINE
30LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040031ycc_rgb_convert_internal(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
32 JDIMENSION input_row, JSAMPARRAY output_buf,
33 int num_rows)
DRCb4570bb2011-09-07 06:31:00 +000034{
Leon Scroggins III3993b372018-07-16 10:43:45 -040035 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
DRCb4570bb2011-09-07 06:31:00 +000036 register int y, cb, cr;
37 register JSAMPROW outptr;
38 register JSAMPROW inptr0, inptr1, inptr2;
39 register JDIMENSION col;
40 JDIMENSION num_cols = cinfo->output_width;
41 /* copy these pointers into registers if possible */
Leon Scroggins III3993b372018-07-16 10:43:45 -040042 register JSAMPLE *range_limit = cinfo->sample_range_limit;
43 register int *Crrtab = cconvert->Cr_r_tab;
44 register int *Cbbtab = cconvert->Cb_b_tab;
45 register JLONG *Crgtab = cconvert->Cr_g_tab;
46 register JLONG *Cbgtab = cconvert->Cb_g_tab;
DRCb4570bb2011-09-07 06:31:00 +000047 SHIFT_TEMPS
48
49 while (--num_rows >= 0) {
50 inptr0 = input_buf[0][input_row];
51 inptr1 = input_buf[1][input_row];
52 inptr2 = input_buf[2][input_row];
53 input_row++;
54 outptr = *output_buf++;
55 for (col = 0; col < num_cols; col++) {
56 y = GETJSAMPLE(inptr0[col]);
57 cb = GETJSAMPLE(inptr1[col]);
58 cr = GETJSAMPLE(inptr2[col]);
59 /* Range-limiting is essential due to noise introduced by DCT losses. */
60 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
61 outptr[RGB_GREEN] = range_limit[y +
Leon Scroggins III3993b372018-07-16 10:43:45 -040062 ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
63 SCALEBITS))];
DRCb4570bb2011-09-07 06:31:00 +000064 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
DRCd89e01f2011-09-09 18:15:53 +000065 /* Set unused byte to 0xFF so it can be interpreted as an opaque */
66 /* alpha channel value */
67#ifdef RGB_ALPHA
68 outptr[RGB_ALPHA] = 0xFF;
69#endif
DRCb4570bb2011-09-07 06:31:00 +000070 outptr += RGB_PIXELSIZE;
71 }
72 }
73}
74
75
76/*
77 * Convert grayscale to RGB: just duplicate the graylevel three times.
78 * This is provided to support applications that don't want to cope
79 * with grayscale as a separate case.
80 */
81
82INLINE
83LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040084gray_rgb_convert_internal(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
85 JDIMENSION input_row, JSAMPARRAY output_buf,
86 int num_rows)
DRCb4570bb2011-09-07 06:31:00 +000087{
88 register JSAMPROW inptr, outptr;
89 register JDIMENSION col;
90 JDIMENSION num_cols = cinfo->output_width;
91
92 while (--num_rows >= 0) {
93 inptr = input_buf[0][input_row++];
94 outptr = *output_buf++;
95 for (col = 0; col < num_cols; col++) {
DRCb4570bb2011-09-07 06:31:00 +000096 /* We can dispense with GETJSAMPLE() here */
97 outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
DRCd89e01f2011-09-09 18:15:53 +000098 /* Set unused byte to 0xFF so it can be interpreted as an opaque */
99 /* alpha channel value */
100#ifdef RGB_ALPHA
101 outptr[RGB_ALPHA] = 0xFF;
102#endif
DRCb4570bb2011-09-07 06:31:00 +0000103 outptr += RGB_PIXELSIZE;
104 }
105 }
106}
DRCa9b646c2012-03-11 22:06:54 +0000107
108
109/*
110 * Convert RGB to extended RGB: just swap the order of source pixels
111 */
112
113INLINE
114LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400115rgb_rgb_convert_internal(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
116 JDIMENSION input_row, JSAMPARRAY output_buf,
117 int num_rows)
DRCa9b646c2012-03-11 22:06:54 +0000118{
119 register JSAMPROW inptr0, inptr1, inptr2;
120 register JSAMPROW outptr;
121 register JDIMENSION col;
122 JDIMENSION num_cols = cinfo->output_width;
123
124 while (--num_rows >= 0) {
125 inptr0 = input_buf[0][input_row];
126 inptr1 = input_buf[1][input_row];
127 inptr2 = input_buf[2][input_row];
128 input_row++;
129 outptr = *output_buf++;
130 for (col = 0; col < num_cols; col++) {
131 /* We can dispense with GETJSAMPLE() here */
132 outptr[RGB_RED] = inptr0[col];
133 outptr[RGB_GREEN] = inptr1[col];
134 outptr[RGB_BLUE] = inptr2[col];
135 /* Set unused byte to 0xFF so it can be interpreted as an opaque */
136 /* alpha channel value */
137#ifdef RGB_ALPHA
138 outptr[RGB_ALPHA] = 0xFF;
139#endif
140 outptr += RGB_PIXELSIZE;
141 }
142 }
143}