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