/src/libjpeg-turbo/jdcolor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jdcolor.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
6 | | * Modified 2011 by Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
9 | | * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander. |
10 | | * Copyright (C) 2013, Linaro Limited. |
11 | | * For conditions of distribution and use, see the accompanying README.ijg |
12 | | * file. |
13 | | * |
14 | | * This file contains output colorspace conversion routines. |
15 | | */ |
16 | | |
17 | | #define JPEG_INTERNALS |
18 | | #include "jinclude.h" |
19 | | #include "jpeglib.h" |
20 | | #include "jsimd.h" |
21 | | |
22 | | |
23 | | /* Private subobject */ |
24 | | |
25 | | typedef struct { |
26 | | struct jpeg_color_deconverter pub; /* public fields */ |
27 | | |
28 | | /* Private state for YCC->RGB conversion */ |
29 | | int *Cr_r_tab; /* => table for Cr to R conversion */ |
30 | | int *Cb_b_tab; /* => table for Cb to B conversion */ |
31 | | JLONG *Cr_g_tab; /* => table for Cr to G conversion */ |
32 | | JLONG *Cb_g_tab; /* => table for Cb to G conversion */ |
33 | | |
34 | | /* Private state for RGB->Y conversion */ |
35 | | JLONG *rgb_y_tab; /* => table for RGB to Y conversion */ |
36 | | } my_color_deconverter; |
37 | | |
38 | | typedef my_color_deconverter *my_cconvert_ptr; |
39 | | |
40 | | |
41 | | /**************** YCbCr -> RGB conversion: most common case **************/ |
42 | | /**************** RGB -> Y conversion: less common case **************/ |
43 | | |
44 | | /* |
45 | | * YCbCr is defined per CCIR 601-1, except that Cb and Cr are |
46 | | * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. |
47 | | * The conversion equations to be implemented are therefore |
48 | | * |
49 | | * R = Y + 1.40200 * Cr |
50 | | * G = Y - 0.34414 * Cb - 0.71414 * Cr |
51 | | * B = Y + 1.77200 * Cb |
52 | | * |
53 | | * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B |
54 | | * |
55 | | * where Cb and Cr represent the incoming values less CENTERJSAMPLE. |
56 | | * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) |
57 | | * |
58 | | * To avoid floating-point arithmetic, we represent the fractional constants |
59 | | * as integers scaled up by 2^16 (about 4 digits precision); we have to divide |
60 | | * the products by 2^16, with appropriate rounding, to get the correct answer. |
61 | | * Notice that Y, being an integral input, does not contribute any fraction |
62 | | * so it need not participate in the rounding. |
63 | | * |
64 | | * For even more speed, we avoid doing any multiplications in the inner loop |
65 | | * by precalculating the constants times Cb and Cr for all possible values. |
66 | | * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); |
67 | | * for 12-bit samples it is still acceptable. It's not very reasonable for |
68 | | * 16-bit samples, but if you want lossless storage you shouldn't be changing |
69 | | * colorspace anyway. |
70 | | * The Cr=>R and Cb=>B values can be rounded to integers in advance; the |
71 | | * values for the G calculation are left scaled up, since we must add them |
72 | | * together before rounding. |
73 | | */ |
74 | | |
75 | 0 | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
76 | 0 | #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1)) |
77 | 0 | #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5)) |
78 | | |
79 | | /* We allocate one big table for RGB->Y conversion and divide it up into |
80 | | * three parts, instead of doing three alloc_small requests. This lets us |
81 | | * use a single table base address, which can be held in a register in the |
82 | | * inner loops on many machines (more than can hold all three addresses, |
83 | | * anyway). |
84 | | */ |
85 | | |
86 | 0 | #define R_Y_OFF 0 /* offset to R => Y section */ |
87 | 0 | #define G_Y_OFF (1 * (MAXJSAMPLE + 1)) /* offset to G => Y section */ |
88 | 0 | #define B_Y_OFF (2 * (MAXJSAMPLE + 1)) /* etc. */ |
89 | 0 | #define TABLE_SIZE (3 * (MAXJSAMPLE + 1)) |
90 | | |
91 | | |
92 | | /* Include inline routines for colorspace extensions */ |
93 | | |
94 | | #include "jdcolext.c" |
95 | | #undef RGB_RED |
96 | | #undef RGB_GREEN |
97 | | #undef RGB_BLUE |
98 | | #undef RGB_PIXELSIZE |
99 | | |
100 | 0 | #define RGB_RED EXT_RGB_RED |
101 | 0 | #define RGB_GREEN EXT_RGB_GREEN |
102 | 0 | #define RGB_BLUE EXT_RGB_BLUE |
103 | 0 | #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE |
104 | | #define ycc_rgb_convert_internal ycc_extrgb_convert_internal |
105 | | #define gray_rgb_convert_internal gray_extrgb_convert_internal |
106 | | #define rgb_rgb_convert_internal rgb_extrgb_convert_internal |
107 | | #include "jdcolext.c" |
108 | | #undef RGB_RED |
109 | | #undef RGB_GREEN |
110 | | #undef RGB_BLUE |
111 | | #undef RGB_PIXELSIZE |
112 | | #undef ycc_rgb_convert_internal |
113 | | #undef gray_rgb_convert_internal |
114 | | #undef rgb_rgb_convert_internal |
115 | | |
116 | 0 | #define RGB_RED EXT_RGBX_RED |
117 | 0 | #define RGB_GREEN EXT_RGBX_GREEN |
118 | 0 | #define RGB_BLUE EXT_RGBX_BLUE |
119 | 0 | #define RGB_ALPHA 3 |
120 | 0 | #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE |
121 | | #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal |
122 | | #define gray_rgb_convert_internal gray_extrgbx_convert_internal |
123 | | #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal |
124 | | #include "jdcolext.c" |
125 | | #undef RGB_RED |
126 | | #undef RGB_GREEN |
127 | | #undef RGB_BLUE |
128 | | #undef RGB_ALPHA |
129 | | #undef RGB_PIXELSIZE |
130 | | #undef ycc_rgb_convert_internal |
131 | | #undef gray_rgb_convert_internal |
132 | | #undef rgb_rgb_convert_internal |
133 | | |
134 | 0 | #define RGB_RED EXT_BGR_RED |
135 | 0 | #define RGB_GREEN EXT_BGR_GREEN |
136 | 0 | #define RGB_BLUE EXT_BGR_BLUE |
137 | 0 | #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE |
138 | | #define ycc_rgb_convert_internal ycc_extbgr_convert_internal |
139 | | #define gray_rgb_convert_internal gray_extbgr_convert_internal |
140 | | #define rgb_rgb_convert_internal rgb_extbgr_convert_internal |
141 | | #include "jdcolext.c" |
142 | | #undef RGB_RED |
143 | | #undef RGB_GREEN |
144 | | #undef RGB_BLUE |
145 | | #undef RGB_PIXELSIZE |
146 | | #undef ycc_rgb_convert_internal |
147 | | #undef gray_rgb_convert_internal |
148 | | #undef rgb_rgb_convert_internal |
149 | | |
150 | 0 | #define RGB_RED EXT_BGRX_RED |
151 | 0 | #define RGB_GREEN EXT_BGRX_GREEN |
152 | 0 | #define RGB_BLUE EXT_BGRX_BLUE |
153 | 0 | #define RGB_ALPHA 3 |
154 | 0 | #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE |
155 | | #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal |
156 | | #define gray_rgb_convert_internal gray_extbgrx_convert_internal |
157 | | #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal |
158 | | #include "jdcolext.c" |
159 | | #undef RGB_RED |
160 | | #undef RGB_GREEN |
161 | | #undef RGB_BLUE |
162 | | #undef RGB_ALPHA |
163 | | #undef RGB_PIXELSIZE |
164 | | #undef ycc_rgb_convert_internal |
165 | | #undef gray_rgb_convert_internal |
166 | | #undef rgb_rgb_convert_internal |
167 | | |
168 | 0 | #define RGB_RED EXT_XBGR_RED |
169 | 0 | #define RGB_GREEN EXT_XBGR_GREEN |
170 | 0 | #define RGB_BLUE EXT_XBGR_BLUE |
171 | 0 | #define RGB_ALPHA 0 |
172 | 0 | #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE |
173 | | #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal |
174 | | #define gray_rgb_convert_internal gray_extxbgr_convert_internal |
175 | | #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal |
176 | | #include "jdcolext.c" |
177 | | #undef RGB_RED |
178 | | #undef RGB_GREEN |
179 | | #undef RGB_BLUE |
180 | | #undef RGB_ALPHA |
181 | | #undef RGB_PIXELSIZE |
182 | | #undef ycc_rgb_convert_internal |
183 | | #undef gray_rgb_convert_internal |
184 | | #undef rgb_rgb_convert_internal |
185 | | |
186 | 0 | #define RGB_RED EXT_XRGB_RED |
187 | 0 | #define RGB_GREEN EXT_XRGB_GREEN |
188 | 0 | #define RGB_BLUE EXT_XRGB_BLUE |
189 | 0 | #define RGB_ALPHA 0 |
190 | 0 | #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE |
191 | | #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal |
192 | | #define gray_rgb_convert_internal gray_extxrgb_convert_internal |
193 | | #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal |
194 | | #include "jdcolext.c" |
195 | | #undef RGB_RED |
196 | | #undef RGB_GREEN |
197 | | #undef RGB_BLUE |
198 | | #undef RGB_ALPHA |
199 | | #undef RGB_PIXELSIZE |
200 | | #undef ycc_rgb_convert_internal |
201 | | #undef gray_rgb_convert_internal |
202 | | #undef rgb_rgb_convert_internal |
203 | | |
204 | | |
205 | | /* |
206 | | * Initialize tables for YCC->RGB colorspace conversion. |
207 | | */ |
208 | | |
209 | | LOCAL(void) |
210 | | build_ycc_rgb_table(j_decompress_ptr cinfo) |
211 | 0 | { |
212 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
213 | 0 | int i; |
214 | 0 | JLONG x; |
215 | 0 | SHIFT_TEMPS |
216 | |
|
217 | 0 | cconvert->Cr_r_tab = (int *) |
218 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
219 | 0 | (MAXJSAMPLE + 1) * sizeof(int)); |
220 | 0 | cconvert->Cb_b_tab = (int *) |
221 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
222 | 0 | (MAXJSAMPLE + 1) * sizeof(int)); |
223 | 0 | cconvert->Cr_g_tab = (JLONG *) |
224 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
225 | 0 | (MAXJSAMPLE + 1) * sizeof(JLONG)); |
226 | 0 | cconvert->Cb_g_tab = (JLONG *) |
227 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
228 | 0 | (MAXJSAMPLE + 1) * sizeof(JLONG)); |
229 | |
|
230 | 0 | for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
231 | | /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ |
232 | | /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ |
233 | | /* Cr=>R value is nearest int to 1.40200 * x */ |
234 | 0 | cconvert->Cr_r_tab[i] = (int) |
235 | 0 | RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
236 | | /* Cb=>B value is nearest int to 1.77200 * x */ |
237 | 0 | cconvert->Cb_b_tab[i] = (int) |
238 | 0 | RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
239 | | /* Cr=>G value is scaled-up -0.71414 * x */ |
240 | 0 | cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x; |
241 | | /* Cb=>G value is scaled-up -0.34414 * x */ |
242 | | /* We also add in ONE_HALF so that need not do it in inner loop */ |
243 | 0 | cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF; |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | | |
248 | | /* |
249 | | * Convert some rows of samples to the output colorspace. |
250 | | */ |
251 | | |
252 | | METHODDEF(void) |
253 | | ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
254 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
255 | 0 | { |
256 | 0 | switch (cinfo->out_color_space) { |
257 | 0 | case JCS_EXT_RGB: |
258 | 0 | ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
259 | 0 | num_rows); |
260 | 0 | break; |
261 | 0 | case JCS_EXT_RGBX: |
262 | 0 | case JCS_EXT_RGBA: |
263 | 0 | ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
264 | 0 | num_rows); |
265 | 0 | break; |
266 | 0 | case JCS_EXT_BGR: |
267 | 0 | ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
268 | 0 | num_rows); |
269 | 0 | break; |
270 | 0 | case JCS_EXT_BGRX: |
271 | 0 | case JCS_EXT_BGRA: |
272 | 0 | ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
273 | 0 | num_rows); |
274 | 0 | break; |
275 | 0 | case JCS_EXT_XBGR: |
276 | 0 | case JCS_EXT_ABGR: |
277 | 0 | ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
278 | 0 | num_rows); |
279 | 0 | break; |
280 | 0 | case JCS_EXT_XRGB: |
281 | 0 | case JCS_EXT_ARGB: |
282 | 0 | ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
283 | 0 | num_rows); |
284 | 0 | break; |
285 | 0 | default: |
286 | 0 | ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
287 | 0 | num_rows); |
288 | 0 | break; |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | |
293 | | /**************** Cases other than YCbCr -> RGB **************/ |
294 | | |
295 | | |
296 | | /* |
297 | | * Initialize for RGB->grayscale colorspace conversion. |
298 | | */ |
299 | | |
300 | | LOCAL(void) |
301 | | build_rgb_y_table(j_decompress_ptr cinfo) |
302 | 0 | { |
303 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
304 | 0 | JLONG *rgb_y_tab; |
305 | 0 | JLONG i; |
306 | | |
307 | | /* Allocate and fill in the conversion tables. */ |
308 | 0 | cconvert->rgb_y_tab = rgb_y_tab = (JLONG *) |
309 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
310 | 0 | (TABLE_SIZE * sizeof(JLONG))); |
311 | |
|
312 | 0 | for (i = 0; i <= MAXJSAMPLE; i++) { |
313 | 0 | rgb_y_tab[i + R_Y_OFF] = FIX(0.29900) * i; |
314 | 0 | rgb_y_tab[i + G_Y_OFF] = FIX(0.58700) * i; |
315 | 0 | rgb_y_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; |
316 | 0 | } |
317 | 0 | } |
318 | | |
319 | | |
320 | | /* |
321 | | * Convert RGB to grayscale. |
322 | | */ |
323 | | |
324 | | METHODDEF(void) |
325 | | rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
326 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
327 | 0 | { |
328 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
329 | 0 | register int r, g, b; |
330 | 0 | register JLONG *ctab = cconvert->rgb_y_tab; |
331 | 0 | register JSAMPROW outptr; |
332 | 0 | register JSAMPROW inptr0, inptr1, inptr2; |
333 | 0 | register JDIMENSION col; |
334 | 0 | JDIMENSION num_cols = cinfo->output_width; |
335 | |
|
336 | 0 | while (--num_rows >= 0) { |
337 | 0 | inptr0 = input_buf[0][input_row]; |
338 | 0 | inptr1 = input_buf[1][input_row]; |
339 | 0 | inptr2 = input_buf[2][input_row]; |
340 | 0 | input_row++; |
341 | 0 | outptr = *output_buf++; |
342 | 0 | for (col = 0; col < num_cols; col++) { |
343 | 0 | r = inptr0[col]; |
344 | 0 | g = inptr1[col]; |
345 | 0 | b = inptr2[col]; |
346 | | /* Y */ |
347 | 0 | outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] + |
348 | 0 | ctab[b + B_Y_OFF]) >> SCALEBITS); |
349 | 0 | } |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | | |
354 | | /* |
355 | | * Color conversion for no colorspace change: just copy the data, |
356 | | * converting from separate-planes to interleaved representation. |
357 | | */ |
358 | | |
359 | | METHODDEF(void) |
360 | | null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
361 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
362 | 34.1M | { |
363 | 34.1M | register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr; |
364 | 34.1M | register JDIMENSION col; |
365 | 34.1M | register int num_components = cinfo->num_components; |
366 | 34.1M | JDIMENSION num_cols = cinfo->output_width; |
367 | 34.1M | int ci; |
368 | | |
369 | 34.1M | if (num_components == 3) { |
370 | 4.75M | while (--num_rows >= 0) { |
371 | 2.37M | inptr0 = input_buf[0][input_row]; |
372 | 2.37M | inptr1 = input_buf[1][input_row]; |
373 | 2.37M | inptr2 = input_buf[2][input_row]; |
374 | 2.37M | input_row++; |
375 | 2.37M | outptr = *output_buf++; |
376 | 601M | for (col = 0; col < num_cols; col++) { |
377 | 599M | *outptr++ = inptr0[col]; |
378 | 599M | *outptr++ = inptr1[col]; |
379 | 599M | *outptr++ = inptr2[col]; |
380 | 599M | } |
381 | 2.37M | } |
382 | 31.8M | } else if (num_components == 4) { |
383 | 0 | while (--num_rows >= 0) { |
384 | 0 | inptr0 = input_buf[0][input_row]; |
385 | 0 | inptr1 = input_buf[1][input_row]; |
386 | 0 | inptr2 = input_buf[2][input_row]; |
387 | 0 | inptr3 = input_buf[3][input_row]; |
388 | 0 | input_row++; |
389 | 0 | outptr = *output_buf++; |
390 | 0 | for (col = 0; col < num_cols; col++) { |
391 | 0 | *outptr++ = inptr0[col]; |
392 | 0 | *outptr++ = inptr1[col]; |
393 | 0 | *outptr++ = inptr2[col]; |
394 | 0 | *outptr++ = inptr3[col]; |
395 | 0 | } |
396 | 0 | } |
397 | 31.8M | } else { |
398 | 63.6M | while (--num_rows >= 0) { |
399 | 63.6M | for (ci = 0; ci < num_components; ci++) { |
400 | 31.8M | inptr = input_buf[ci][input_row]; |
401 | 31.8M | outptr = *output_buf; |
402 | 9.06G | for (col = 0; col < num_cols; col++) { |
403 | 9.03G | outptr[ci] = inptr[col]; |
404 | 9.03G | outptr += num_components; |
405 | 9.03G | } |
406 | 31.8M | } |
407 | 31.8M | output_buf++; |
408 | 31.8M | input_row++; |
409 | 31.8M | } |
410 | 31.8M | } |
411 | 34.1M | } |
412 | | |
413 | | |
414 | | /* |
415 | | * Color conversion for grayscale: just copy the data. |
416 | | * This also works for YCbCr -> grayscale conversion, in which |
417 | | * we just copy the Y (luminance) component and ignore chrominance. |
418 | | */ |
419 | | |
420 | | METHODDEF(void) |
421 | | grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
422 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
423 | 0 | { |
424 | 0 | jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows, |
425 | 0 | cinfo->output_width); |
426 | 0 | } |
427 | | |
428 | | |
429 | | /* |
430 | | * Convert grayscale to RGB |
431 | | */ |
432 | | |
433 | | METHODDEF(void) |
434 | | gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
435 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
436 | 0 | { |
437 | 0 | switch (cinfo->out_color_space) { |
438 | 0 | case JCS_EXT_RGB: |
439 | 0 | gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
440 | 0 | num_rows); |
441 | 0 | break; |
442 | 0 | case JCS_EXT_RGBX: |
443 | 0 | case JCS_EXT_RGBA: |
444 | 0 | gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
445 | 0 | num_rows); |
446 | 0 | break; |
447 | 0 | case JCS_EXT_BGR: |
448 | 0 | gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
449 | 0 | num_rows); |
450 | 0 | break; |
451 | 0 | case JCS_EXT_BGRX: |
452 | 0 | case JCS_EXT_BGRA: |
453 | 0 | gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
454 | 0 | num_rows); |
455 | 0 | break; |
456 | 0 | case JCS_EXT_XBGR: |
457 | 0 | case JCS_EXT_ABGR: |
458 | 0 | gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
459 | 0 | num_rows); |
460 | 0 | break; |
461 | 0 | case JCS_EXT_XRGB: |
462 | 0 | case JCS_EXT_ARGB: |
463 | 0 | gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
464 | 0 | num_rows); |
465 | 0 | break; |
466 | 0 | default: |
467 | 0 | gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
468 | 0 | num_rows); |
469 | 0 | break; |
470 | 0 | } |
471 | 0 | } |
472 | | |
473 | | |
474 | | /* |
475 | | * Convert plain RGB to extended RGB |
476 | | */ |
477 | | |
478 | | METHODDEF(void) |
479 | | rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
480 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
481 | 0 | { |
482 | 0 | switch (cinfo->out_color_space) { |
483 | 0 | case JCS_EXT_RGB: |
484 | 0 | rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
485 | 0 | num_rows); |
486 | 0 | break; |
487 | 0 | case JCS_EXT_RGBX: |
488 | 0 | case JCS_EXT_RGBA: |
489 | 0 | rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
490 | 0 | num_rows); |
491 | 0 | break; |
492 | 0 | case JCS_EXT_BGR: |
493 | 0 | rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
494 | 0 | num_rows); |
495 | 0 | break; |
496 | 0 | case JCS_EXT_BGRX: |
497 | 0 | case JCS_EXT_BGRA: |
498 | 0 | rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
499 | 0 | num_rows); |
500 | 0 | break; |
501 | 0 | case JCS_EXT_XBGR: |
502 | 0 | case JCS_EXT_ABGR: |
503 | 0 | rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
504 | 0 | num_rows); |
505 | 0 | break; |
506 | 0 | case JCS_EXT_XRGB: |
507 | 0 | case JCS_EXT_ARGB: |
508 | 0 | rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
509 | 0 | num_rows); |
510 | 0 | break; |
511 | 0 | default: |
512 | 0 | rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
513 | 0 | num_rows); |
514 | 0 | break; |
515 | 0 | } |
516 | 0 | } |
517 | | |
518 | | |
519 | | /* |
520 | | * Adobe-style YCCK->CMYK conversion. |
521 | | * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same |
522 | | * conversion as above, while passing K (black) unchanged. |
523 | | * We assume build_ycc_rgb_table has been called. |
524 | | */ |
525 | | |
526 | | METHODDEF(void) |
527 | | ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
528 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
529 | 0 | { |
530 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
531 | 0 | register int y, cb, cr; |
532 | 0 | register JSAMPROW outptr; |
533 | 0 | register JSAMPROW inptr0, inptr1, inptr2, inptr3; |
534 | 0 | register JDIMENSION col; |
535 | 0 | JDIMENSION num_cols = cinfo->output_width; |
536 | | /* copy these pointers into registers if possible */ |
537 | 0 | register JSAMPLE *range_limit = cinfo->sample_range_limit; |
538 | 0 | register int *Crrtab = cconvert->Cr_r_tab; |
539 | 0 | register int *Cbbtab = cconvert->Cb_b_tab; |
540 | 0 | register JLONG *Crgtab = cconvert->Cr_g_tab; |
541 | 0 | register JLONG *Cbgtab = cconvert->Cb_g_tab; |
542 | 0 | SHIFT_TEMPS |
543 | |
|
544 | 0 | while (--num_rows >= 0) { |
545 | 0 | inptr0 = input_buf[0][input_row]; |
546 | 0 | inptr1 = input_buf[1][input_row]; |
547 | 0 | inptr2 = input_buf[2][input_row]; |
548 | 0 | inptr3 = input_buf[3][input_row]; |
549 | 0 | input_row++; |
550 | 0 | outptr = *output_buf++; |
551 | 0 | for (col = 0; col < num_cols; col++) { |
552 | 0 | y = inptr0[col]; |
553 | 0 | cb = inptr1[col]; |
554 | 0 | cr = inptr2[col]; |
555 | | /* Range-limiting is essential due to noise introduced by DCT losses. */ |
556 | 0 | outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ |
557 | 0 | outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ |
558 | 0 | ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], |
559 | 0 | SCALEBITS)))]; |
560 | 0 | outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ |
561 | | /* K passes through unchanged */ |
562 | 0 | outptr[3] = inptr3[col]; |
563 | 0 | outptr += 4; |
564 | 0 | } |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | | |
569 | | /* |
570 | | * RGB565 conversion |
571 | | */ |
572 | | |
573 | | #define PACK_SHORT_565_LE(r, g, b) \ |
574 | 0 | ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3)) |
575 | | #define PACK_SHORT_565_BE(r, g, b) \ |
576 | 0 | (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00)) |
577 | | |
578 | 0 | #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l) |
579 | 0 | #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r) |
580 | | |
581 | 0 | #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3) |
582 | | |
583 | 0 | #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels) |
584 | | |
585 | 0 | #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF)) |
586 | 0 | #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1)) |
587 | 0 | #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF)) |
588 | | |
589 | | |
590 | | /* Declarations for ordered dithering |
591 | | * |
592 | | * We use a 4x4 ordered dither array packed into 32 bits. This array is |
593 | | * sufficient for dithering RGB888 to RGB565. |
594 | | */ |
595 | | |
596 | 0 | #define DITHER_MASK 0x3 |
597 | 0 | #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF)) |
598 | | static const JLONG dither_matrix[4] = { |
599 | | 0x0008020A, |
600 | | 0x0C040E06, |
601 | | 0x030B0109, |
602 | | 0x0F070D05 |
603 | | }; |
604 | | |
605 | | |
606 | | static INLINE boolean is_big_endian(void) |
607 | 0 | { |
608 | 0 | int test_value = 1; |
609 | 0 | if (*(char *)&test_value != 1) |
610 | 0 | return TRUE; |
611 | 0 | return FALSE; |
612 | 0 | } |
613 | | |
614 | | |
615 | | /* Include inline routines for RGB565 conversion */ |
616 | | |
617 | 0 | #define PACK_SHORT_565 PACK_SHORT_565_LE |
618 | 0 | #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE |
619 | | #define ycc_rgb565_convert_internal ycc_rgb565_convert_le |
620 | | #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le |
621 | | #define rgb_rgb565_convert_internal rgb_rgb565_convert_le |
622 | | #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le |
623 | | #define gray_rgb565_convert_internal gray_rgb565_convert_le |
624 | | #define gray_rgb565D_convert_internal gray_rgb565D_convert_le |
625 | | #include "jdcol565.c" |
626 | | #undef PACK_SHORT_565 |
627 | | #undef PACK_TWO_PIXELS |
628 | | #undef ycc_rgb565_convert_internal |
629 | | #undef ycc_rgb565D_convert_internal |
630 | | #undef rgb_rgb565_convert_internal |
631 | | #undef rgb_rgb565D_convert_internal |
632 | | #undef gray_rgb565_convert_internal |
633 | | #undef gray_rgb565D_convert_internal |
634 | | |
635 | 0 | #define PACK_SHORT_565 PACK_SHORT_565_BE |
636 | 0 | #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE |
637 | | #define ycc_rgb565_convert_internal ycc_rgb565_convert_be |
638 | | #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be |
639 | | #define rgb_rgb565_convert_internal rgb_rgb565_convert_be |
640 | | #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be |
641 | | #define gray_rgb565_convert_internal gray_rgb565_convert_be |
642 | | #define gray_rgb565D_convert_internal gray_rgb565D_convert_be |
643 | | #include "jdcol565.c" |
644 | | #undef PACK_SHORT_565 |
645 | | #undef PACK_TWO_PIXELS |
646 | | #undef ycc_rgb565_convert_internal |
647 | | #undef ycc_rgb565D_convert_internal |
648 | | #undef rgb_rgb565_convert_internal |
649 | | #undef rgb_rgb565D_convert_internal |
650 | | #undef gray_rgb565_convert_internal |
651 | | #undef gray_rgb565D_convert_internal |
652 | | |
653 | | |
654 | | METHODDEF(void) |
655 | | ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
656 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
657 | 0 | { |
658 | 0 | if (is_big_endian()) |
659 | 0 | ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
660 | 0 | else |
661 | 0 | ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
662 | 0 | } |
663 | | |
664 | | |
665 | | METHODDEF(void) |
666 | | ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
667 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
668 | 0 | { |
669 | 0 | if (is_big_endian()) |
670 | 0 | ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
671 | 0 | else |
672 | 0 | ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
673 | 0 | } |
674 | | |
675 | | |
676 | | METHODDEF(void) |
677 | | rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
678 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
679 | 0 | { |
680 | 0 | if (is_big_endian()) |
681 | 0 | rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
682 | 0 | else |
683 | 0 | rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
684 | 0 | } |
685 | | |
686 | | |
687 | | METHODDEF(void) |
688 | | rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
689 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
690 | 0 | { |
691 | 0 | if (is_big_endian()) |
692 | 0 | rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
693 | 0 | else |
694 | 0 | rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
695 | 0 | } |
696 | | |
697 | | |
698 | | METHODDEF(void) |
699 | | gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
700 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
701 | 0 | { |
702 | 0 | if (is_big_endian()) |
703 | 0 | gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
704 | 0 | else |
705 | 0 | gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
706 | 0 | } |
707 | | |
708 | | |
709 | | METHODDEF(void) |
710 | | gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
711 | | JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) |
712 | 0 | { |
713 | 0 | if (is_big_endian()) |
714 | 0 | gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows); |
715 | 0 | else |
716 | 0 | gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows); |
717 | 0 | } |
718 | | |
719 | | |
720 | | /* |
721 | | * Empty method for start_pass. |
722 | | */ |
723 | | |
724 | | METHODDEF(void) |
725 | | start_pass_dcolor(j_decompress_ptr cinfo) |
726 | 46.5k | { |
727 | | /* no work needed */ |
728 | 46.5k | } |
729 | | |
730 | | |
731 | | /* |
732 | | * Module initialization routine for output colorspace conversion. |
733 | | */ |
734 | | |
735 | | GLOBAL(void) |
736 | | jinit_color_deconverter(j_decompress_ptr cinfo) |
737 | 54.3k | { |
738 | 54.3k | my_cconvert_ptr cconvert; |
739 | 54.3k | int ci; |
740 | | |
741 | 54.3k | cconvert = (my_cconvert_ptr) |
742 | 54.3k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
743 | 54.3k | sizeof(my_color_deconverter)); |
744 | 54.3k | cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert; |
745 | 54.3k | cconvert->pub.start_pass = start_pass_dcolor; |
746 | | |
747 | | /* Make sure num_components agrees with jpeg_color_space */ |
748 | 54.3k | switch (cinfo->jpeg_color_space) { |
749 | 0 | case JCS_GRAYSCALE: |
750 | 0 | if (cinfo->num_components != 1) |
751 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
752 | 0 | break; |
753 | | |
754 | 0 | case JCS_RGB: |
755 | 5.13k | case JCS_YCbCr: |
756 | 5.13k | if (cinfo->num_components != 3) |
757 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
758 | 5.13k | break; |
759 | | |
760 | 0 | case JCS_CMYK: |
761 | 0 | case JCS_YCCK: |
762 | 0 | if (cinfo->num_components != 4) |
763 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
764 | 0 | break; |
765 | | |
766 | 49.1k | default: /* JCS_UNKNOWN can be anything */ |
767 | 49.1k | if (cinfo->num_components < 1) |
768 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
769 | 49.1k | break; |
770 | 54.3k | } |
771 | | |
772 | | /* Set out_color_components and conversion method based on requested space. |
773 | | * Also clear the component_needed flags for any unused components, |
774 | | * so that earlier pipeline stages can avoid useless computation. |
775 | | */ |
776 | | |
777 | 54.3k | switch (cinfo->out_color_space) { |
778 | 0 | case JCS_GRAYSCALE: |
779 | 0 | cinfo->out_color_components = 1; |
780 | 0 | if (cinfo->jpeg_color_space == JCS_GRAYSCALE || |
781 | 0 | cinfo->jpeg_color_space == JCS_YCbCr) { |
782 | 0 | cconvert->pub.color_convert = grayscale_convert; |
783 | | /* For color->grayscale conversion, only the Y (0) component is needed */ |
784 | 0 | for (ci = 1; ci < cinfo->num_components; ci++) |
785 | 0 | cinfo->comp_info[ci].component_needed = FALSE; |
786 | 0 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
787 | 0 | cconvert->pub.color_convert = rgb_gray_convert; |
788 | 0 | build_rgb_y_table(cinfo); |
789 | 0 | } else |
790 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
791 | 0 | break; |
792 | | |
793 | 5.13k | case JCS_RGB: |
794 | 5.13k | case JCS_EXT_RGB: |
795 | 5.13k | case JCS_EXT_RGBX: |
796 | 5.13k | case JCS_EXT_BGR: |
797 | 5.13k | case JCS_EXT_BGRX: |
798 | 5.13k | case JCS_EXT_XBGR: |
799 | 5.13k | case JCS_EXT_XRGB: |
800 | 5.13k | case JCS_EXT_RGBA: |
801 | 5.13k | case JCS_EXT_BGRA: |
802 | 5.13k | case JCS_EXT_ABGR: |
803 | 5.13k | case JCS_EXT_ARGB: |
804 | 5.13k | cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; |
805 | 5.13k | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
806 | 5.13k | if (jsimd_can_ycc_rgb()) |
807 | 5.13k | cconvert->pub.color_convert = jsimd_ycc_rgb_convert; |
808 | 0 | else { |
809 | 0 | cconvert->pub.color_convert = ycc_rgb_convert; |
810 | 0 | build_ycc_rgb_table(cinfo); |
811 | 0 | } |
812 | 5.13k | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
813 | 0 | cconvert->pub.color_convert = gray_rgb_convert; |
814 | 0 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
815 | 0 | if (rgb_red[cinfo->out_color_space] == 0 && |
816 | 0 | rgb_green[cinfo->out_color_space] == 1 && |
817 | 0 | rgb_blue[cinfo->out_color_space] == 2 && |
818 | 0 | rgb_pixelsize[cinfo->out_color_space] == 3) |
819 | 0 | cconvert->pub.color_convert = null_convert; |
820 | 0 | else |
821 | 0 | cconvert->pub.color_convert = rgb_rgb_convert; |
822 | 0 | } else |
823 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
824 | 5.13k | break; |
825 | | |
826 | 0 | case JCS_RGB565: |
827 | 0 | cinfo->out_color_components = 3; |
828 | 0 | if (cinfo->dither_mode == JDITHER_NONE) { |
829 | 0 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
830 | 0 | if (jsimd_can_ycc_rgb565()) |
831 | 0 | cconvert->pub.color_convert = jsimd_ycc_rgb565_convert; |
832 | 0 | else { |
833 | 0 | cconvert->pub.color_convert = ycc_rgb565_convert; |
834 | 0 | build_ycc_rgb_table(cinfo); |
835 | 0 | } |
836 | 0 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
837 | 0 | cconvert->pub.color_convert = gray_rgb565_convert; |
838 | 0 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
839 | 0 | cconvert->pub.color_convert = rgb_rgb565_convert; |
840 | 0 | } else |
841 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
842 | 0 | } else { |
843 | | /* only ordered dithering is supported */ |
844 | 0 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
845 | 0 | cconvert->pub.color_convert = ycc_rgb565D_convert; |
846 | 0 | build_ycc_rgb_table(cinfo); |
847 | 0 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
848 | 0 | cconvert->pub.color_convert = gray_rgb565D_convert; |
849 | 0 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
850 | 0 | cconvert->pub.color_convert = rgb_rgb565D_convert; |
851 | 0 | } else |
852 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
853 | 0 | } |
854 | 0 | break; |
855 | | |
856 | 0 | case JCS_CMYK: |
857 | 0 | cinfo->out_color_components = 4; |
858 | 0 | if (cinfo->jpeg_color_space == JCS_YCCK) { |
859 | 0 | cconvert->pub.color_convert = ycck_cmyk_convert; |
860 | 0 | build_ycc_rgb_table(cinfo); |
861 | 0 | } else if (cinfo->jpeg_color_space == JCS_CMYK) { |
862 | 0 | cconvert->pub.color_convert = null_convert; |
863 | 0 | } else |
864 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
865 | 0 | break; |
866 | | |
867 | 49.1k | default: |
868 | | /* Permit null conversion to same output space */ |
869 | 49.1k | if (cinfo->out_color_space == cinfo->jpeg_color_space) { |
870 | 49.1k | cinfo->out_color_components = cinfo->num_components; |
871 | 49.1k | cconvert->pub.color_convert = null_convert; |
872 | 49.1k | } else /* unsupported non-null conversion */ |
873 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
874 | 49.1k | break; |
875 | 54.3k | } |
876 | | |
877 | 54.3k | if (cinfo->quantize_colors) |
878 | 0 | cinfo->output_components = 1; /* single colormapped output component */ |
879 | 54.3k | else |
880 | 54.3k | cinfo->output_components = cinfo->out_color_components; |
881 | 54.3k | } |