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