1 /*
2  * jccolext.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1991-1996, Thomas G. Lane.
6  * libjpeg-turbo Modifications:
7  * Copyright (C) 2009-2012, D. R. Commander.
8  * For conditions of distribution and use, see the accompanying README file.
9  *
10  * This file contains input colorspace conversion routines.
11  */
12 
13 
14 /* This file is included by jccolor.c */
15 
16 
17 /*
18  * Convert some rows of samples to the JPEG colorspace.
19  *
20  * Note that we change from the application's interleaved-pixel format
21  * to our internal noninterleaved, one-plane-per-component format.
22  * The input buffer is therefore three times as wide as the output buffer.
23  *
24  * A starting row offset is provided only for the output buffer.  The caller
25  * can easily adjust the passed input_buf value to accommodate any row
26  * offset required on that side.
27  */
28 
29 INLINE
LOCAL(void)30 LOCAL(void)
31 rgb_ycc_convert_internal (j_compress_ptr cinfo,
32                           JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
33                           JDIMENSION output_row, int num_rows)
34 {
35   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
36   register int r, g, b;
37   register INT32 * ctab = cconvert->rgb_ycc_tab;
38   register JSAMPROW inptr;
39   register JSAMPROW outptr0, outptr1, outptr2;
40   register JDIMENSION col;
41   JDIMENSION num_cols = cinfo->image_width;
42 
43   while (--num_rows >= 0) {
44     inptr = *input_buf++;
45     outptr0 = output_buf[0][output_row];
46     outptr1 = output_buf[1][output_row];
47     outptr2 = output_buf[2][output_row];
48     output_row++;
49     for (col = 0; col < num_cols; col++) {
50       r = GETJSAMPLE(inptr[RGB_RED]);
51       g = GETJSAMPLE(inptr[RGB_GREEN]);
52       b = GETJSAMPLE(inptr[RGB_BLUE]);
53       inptr += RGB_PIXELSIZE;
54       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
55        * must be too; we do not need an explicit range-limiting operation.
56        * Hence the value being shifted is never negative, and we don't
57        * need the general RIGHT_SHIFT macro.
58        */
59       /* Y */
60       outptr0[col] = (JSAMPLE)
61                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
62                  >> SCALEBITS);
63       /* Cb */
64       outptr1[col] = (JSAMPLE)
65                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
66                  >> SCALEBITS);
67       /* Cr */
68       outptr2[col] = (JSAMPLE)
69                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
70                  >> SCALEBITS);
71     }
72   }
73 }
74 
75 
76 /**************** Cases other than RGB -> YCbCr **************/
77 
78 
79 /*
80  * Convert some rows of samples to the JPEG colorspace.
81  * This version handles RGB->grayscale conversion, which is the same
82  * as the RGB->Y portion of RGB->YCbCr.
83  * We assume rgb_ycc_start has been called (we only use the Y tables).
84  */
85 
86 INLINE
LOCAL(void)87 LOCAL(void)
88 rgb_gray_convert_internal (j_compress_ptr cinfo,
89                            JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
90                            JDIMENSION output_row, int num_rows)
91 {
92   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
93   register int r, g, b;
94   register INT32 * ctab = cconvert->rgb_ycc_tab;
95   register JSAMPROW inptr;
96   register JSAMPROW outptr;
97   register JDIMENSION col;
98   JDIMENSION num_cols = cinfo->image_width;
99 
100   while (--num_rows >= 0) {
101     inptr = *input_buf++;
102     outptr = output_buf[0][output_row];
103     output_row++;
104     for (col = 0; col < num_cols; col++) {
105       r = GETJSAMPLE(inptr[RGB_RED]);
106       g = GETJSAMPLE(inptr[RGB_GREEN]);
107       b = GETJSAMPLE(inptr[RGB_BLUE]);
108       inptr += RGB_PIXELSIZE;
109       /* Y */
110       outptr[col] = (JSAMPLE)
111                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
112                  >> SCALEBITS);
113     }
114   }
115 }
116 
117 
118 /*
119  * Convert some rows of samples to the JPEG colorspace.
120  * This version handles extended RGB->plain RGB conversion
121  */
122 
123 INLINE
LOCAL(void)124 LOCAL(void)
125 rgb_rgb_convert_internal (j_compress_ptr cinfo,
126                           JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
127                           JDIMENSION output_row, int num_rows)
128 {
129   register JSAMPROW inptr;
130   register JSAMPROW outptr0, outptr1, outptr2;
131   register JDIMENSION col;
132   JDIMENSION num_cols = cinfo->image_width;
133 
134   while (--num_rows >= 0) {
135     inptr = *input_buf++;
136     outptr0 = output_buf[0][output_row];
137     outptr1 = output_buf[1][output_row];
138     outptr2 = output_buf[2][output_row];
139     output_row++;
140     for (col = 0; col < num_cols; col++) {
141       outptr0[col] = GETJSAMPLE(inptr[RGB_RED]);
142       outptr1[col] = GETJSAMPLE(inptr[RGB_GREEN]);
143       outptr2[col] = GETJSAMPLE(inptr[RGB_BLUE]);
144       inptr += RGB_PIXELSIZE;
145     }
146   }
147 }
148