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