/src/libjpeg-turbo/jccolor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jccolor.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 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
8 | | * Copyright (C) 2009-2012, 2015, 2022, D. R. Commander. |
9 | | * Copyright (C) 2014, MIPS Technologies, Inc., California. |
10 | | * For conditions of distribution and use, see the accompanying README.ijg |
11 | | * file. |
12 | | * |
13 | | * This file contains input colorspace conversion routines. |
14 | | */ |
15 | | |
16 | | #define JPEG_INTERNALS |
17 | | #include "jinclude.h" |
18 | | #include "jpeglib.h" |
19 | | #include "jsimd.h" |
20 | | #include "jsamplecomp.h" |
21 | | |
22 | | |
23 | | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
24 | | |
25 | | /* Private subobject */ |
26 | | |
27 | | typedef struct { |
28 | | struct jpeg_color_converter pub; /* public fields */ |
29 | | |
30 | | #if BITS_IN_JSAMPLE != 16 |
31 | | /* Private state for RGB->YCC conversion */ |
32 | | JLONG *rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ |
33 | | #endif |
34 | | } my_color_converter; |
35 | | |
36 | | typedef my_color_converter *my_cconvert_ptr; |
37 | | |
38 | | |
39 | | /**************** RGB -> YCbCr conversion: most common case **************/ |
40 | | |
41 | | /* |
42 | | * YCbCr is defined per CCIR 601-1, except that Cb and Cr are |
43 | | * normalized to the range 0.._MAXJSAMPLE rather than -0.5 .. 0.5. |
44 | | * The conversion equations to be implemented are therefore |
45 | | * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B |
46 | | * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + _CENTERJSAMPLE |
47 | | * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + _CENTERJSAMPLE |
48 | | * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) |
49 | | * Note: older versions of the IJG code used a zero offset of _MAXJSAMPLE/2, |
50 | | * rather than _CENTERJSAMPLE, for Cb and Cr. This gave equal positive and |
51 | | * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) |
52 | | * were not represented exactly. Now we sacrifice exact representation of |
53 | | * maximum red and maximum blue in order to get exact grayscales. |
54 | | * |
55 | | * To avoid floating-point arithmetic, we represent the fractional constants |
56 | | * as integers scaled up by 2^16 (about 4 digits precision); we have to divide |
57 | | * the products by 2^16, with appropriate rounding, to get the correct answer. |
58 | | * |
59 | | * For even more speed, we avoid doing any multiplications in the inner loop |
60 | | * by precalculating the constants times R,G,B for all possible values. |
61 | | * For 8-bit samples this is very reasonable (only 256 entries per table); |
62 | | * for 12-bit samples it is still acceptable. It's not very reasonable for |
63 | | * 16-bit samples, but if you want lossless storage you shouldn't be changing |
64 | | * colorspace anyway. |
65 | | * The _CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included |
66 | | * in the tables to save adding them separately in the inner loop. |
67 | | */ |
68 | | |
69 | 0 | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
70 | 0 | #define CBCR_OFFSET ((JLONG)_CENTERJSAMPLE << SCALEBITS) |
71 | 0 | #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1)) |
72 | 0 | #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5)) |
73 | | |
74 | | /* We allocate one big table and divide it up into eight parts, instead of |
75 | | * doing eight alloc_small requests. This lets us use a single table base |
76 | | * address, which can be held in a register in the inner loops on many |
77 | | * machines (more than can hold all eight addresses, anyway). |
78 | | */ |
79 | | |
80 | 0 | #define R_Y_OFF 0 /* offset to R => Y section */ |
81 | 0 | #define G_Y_OFF (1 * (_MAXJSAMPLE + 1)) /* offset to G => Y section */ |
82 | 0 | #define B_Y_OFF (2 * (_MAXJSAMPLE + 1)) /* etc. */ |
83 | 0 | #define R_CB_OFF (3 * (_MAXJSAMPLE + 1)) |
84 | 0 | #define G_CB_OFF (4 * (_MAXJSAMPLE + 1)) |
85 | 0 | #define B_CB_OFF (5 * (_MAXJSAMPLE + 1)) |
86 | 0 | #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ |
87 | 0 | #define G_CR_OFF (6 * (_MAXJSAMPLE + 1)) |
88 | 0 | #define B_CR_OFF (7 * (_MAXJSAMPLE + 1)) |
89 | 0 | #define TABLE_SIZE (8 * (_MAXJSAMPLE + 1)) |
90 | | |
91 | | /* 12-bit samples use a 16-bit data type, so it is possible to pass |
92 | | * out-of-range sample values (< 0 or > 4095) to jpeg_write_scanlines(). |
93 | | * Thus, we mask the incoming 12-bit samples to guard against overrunning |
94 | | * or underrunning the conversion tables. |
95 | | */ |
96 | | |
97 | | #if BITS_IN_JSAMPLE == 12 |
98 | 0 | #define RANGE_LIMIT(value) ((value) & 0xFFF) |
99 | | #else |
100 | | #define RANGE_LIMIT(value) (value) |
101 | | #endif |
102 | | |
103 | | |
104 | | /* Include inline routines for colorspace extensions */ |
105 | | |
106 | | #include "jccolext.c" |
107 | | #undef RGB_RED |
108 | | #undef RGB_GREEN |
109 | | #undef RGB_BLUE |
110 | | #undef RGB_PIXELSIZE |
111 | | |
112 | 0 | #define RGB_RED EXT_RGB_RED |
113 | 0 | #define RGB_GREEN EXT_RGB_GREEN |
114 | 0 | #define RGB_BLUE EXT_RGB_BLUE |
115 | 0 | #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE |
116 | | #define rgb_ycc_convert_internal extrgb_ycc_convert_internal |
117 | | #define rgb_gray_convert_internal extrgb_gray_convert_internal |
118 | | #define rgb_rgb_convert_internal extrgb_rgb_convert_internal |
119 | | #include "jccolext.c" |
120 | | #undef RGB_RED |
121 | | #undef RGB_GREEN |
122 | | #undef RGB_BLUE |
123 | | #undef RGB_PIXELSIZE |
124 | | #undef rgb_ycc_convert_internal |
125 | | #undef rgb_gray_convert_internal |
126 | | #undef rgb_rgb_convert_internal |
127 | | |
128 | 0 | #define RGB_RED EXT_RGBX_RED |
129 | 0 | #define RGB_GREEN EXT_RGBX_GREEN |
130 | 0 | #define RGB_BLUE EXT_RGBX_BLUE |
131 | 0 | #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE |
132 | | #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal |
133 | | #define rgb_gray_convert_internal extrgbx_gray_convert_internal |
134 | | #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal |
135 | | #include "jccolext.c" |
136 | | #undef RGB_RED |
137 | | #undef RGB_GREEN |
138 | | #undef RGB_BLUE |
139 | | #undef RGB_PIXELSIZE |
140 | | #undef rgb_ycc_convert_internal |
141 | | #undef rgb_gray_convert_internal |
142 | | #undef rgb_rgb_convert_internal |
143 | | |
144 | 0 | #define RGB_RED EXT_BGR_RED |
145 | 0 | #define RGB_GREEN EXT_BGR_GREEN |
146 | 0 | #define RGB_BLUE EXT_BGR_BLUE |
147 | 0 | #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE |
148 | | #define rgb_ycc_convert_internal extbgr_ycc_convert_internal |
149 | | #define rgb_gray_convert_internal extbgr_gray_convert_internal |
150 | | #define rgb_rgb_convert_internal extbgr_rgb_convert_internal |
151 | | #include "jccolext.c" |
152 | | #undef RGB_RED |
153 | | #undef RGB_GREEN |
154 | | #undef RGB_BLUE |
155 | | #undef RGB_PIXELSIZE |
156 | | #undef rgb_ycc_convert_internal |
157 | | #undef rgb_gray_convert_internal |
158 | | #undef rgb_rgb_convert_internal |
159 | | |
160 | 0 | #define RGB_RED EXT_BGRX_RED |
161 | 0 | #define RGB_GREEN EXT_BGRX_GREEN |
162 | 0 | #define RGB_BLUE EXT_BGRX_BLUE |
163 | 0 | #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE |
164 | | #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal |
165 | | #define rgb_gray_convert_internal extbgrx_gray_convert_internal |
166 | | #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal |
167 | | #include "jccolext.c" |
168 | | #undef RGB_RED |
169 | | #undef RGB_GREEN |
170 | | #undef RGB_BLUE |
171 | | #undef RGB_PIXELSIZE |
172 | | #undef rgb_ycc_convert_internal |
173 | | #undef rgb_gray_convert_internal |
174 | | #undef rgb_rgb_convert_internal |
175 | | |
176 | 0 | #define RGB_RED EXT_XBGR_RED |
177 | 0 | #define RGB_GREEN EXT_XBGR_GREEN |
178 | 0 | #define RGB_BLUE EXT_XBGR_BLUE |
179 | 0 | #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE |
180 | | #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal |
181 | | #define rgb_gray_convert_internal extxbgr_gray_convert_internal |
182 | | #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal |
183 | | #include "jccolext.c" |
184 | | #undef RGB_RED |
185 | | #undef RGB_GREEN |
186 | | #undef RGB_BLUE |
187 | | #undef RGB_PIXELSIZE |
188 | | #undef rgb_ycc_convert_internal |
189 | | #undef rgb_gray_convert_internal |
190 | | #undef rgb_rgb_convert_internal |
191 | | |
192 | 0 | #define RGB_RED EXT_XRGB_RED |
193 | 0 | #define RGB_GREEN EXT_XRGB_GREEN |
194 | 0 | #define RGB_BLUE EXT_XRGB_BLUE |
195 | 0 | #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE |
196 | | #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal |
197 | | #define rgb_gray_convert_internal extxrgb_gray_convert_internal |
198 | | #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal |
199 | | #include "jccolext.c" |
200 | | #undef RGB_RED |
201 | | #undef RGB_GREEN |
202 | | #undef RGB_BLUE |
203 | | #undef RGB_PIXELSIZE |
204 | | #undef rgb_ycc_convert_internal |
205 | | #undef rgb_gray_convert_internal |
206 | | #undef rgb_rgb_convert_internal |
207 | | |
208 | | |
209 | | /* |
210 | | * Initialize for RGB->YCC colorspace conversion. |
211 | | */ |
212 | | |
213 | | METHODDEF(void) |
214 | | rgb_ycc_start(j_compress_ptr cinfo) |
215 | 0 | { |
216 | 0 | #if BITS_IN_JSAMPLE != 16 |
217 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
218 | 0 | JLONG *rgb_ycc_tab; |
219 | 0 | JLONG i; |
220 | | |
221 | | /* Allocate and fill in the conversion tables. */ |
222 | 0 | cconvert->rgb_ycc_tab = rgb_ycc_tab = (JLONG *) |
223 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
224 | 0 | (TABLE_SIZE * sizeof(JLONG))); |
225 | |
|
226 | 0 | for (i = 0; i <= _MAXJSAMPLE; i++) { |
227 | 0 | rgb_ycc_tab[i + R_Y_OFF] = FIX(0.29900) * i; |
228 | 0 | rgb_ycc_tab[i + G_Y_OFF] = FIX(0.58700) * i; |
229 | 0 | rgb_ycc_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; |
230 | 0 | rgb_ycc_tab[i + R_CB_OFF] = (-FIX(0.16874)) * i; |
231 | 0 | rgb_ycc_tab[i + G_CB_OFF] = (-FIX(0.33126)) * i; |
232 | | /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. |
233 | | * This ensures that the maximum output will round to _MAXJSAMPLE |
234 | | * not _MAXJSAMPLE+1, and thus that we don't have to range-limit. |
235 | | */ |
236 | 0 | rgb_ycc_tab[i + B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF - 1; |
237 | | /* B=>Cb and R=>Cr tables are the same |
238 | | rgb_ycc_tab[i + R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF - 1; |
239 | | */ |
240 | 0 | rgb_ycc_tab[i + G_CR_OFF] = (-FIX(0.41869)) * i; |
241 | 0 | rgb_ycc_tab[i + B_CR_OFF] = (-FIX(0.08131)) * i; |
242 | 0 | } |
243 | | #else |
244 | | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
245 | | #endif |
246 | 0 | } |
247 | | |
248 | | |
249 | | /* |
250 | | * Convert some rows of samples to the JPEG colorspace. |
251 | | */ |
252 | | |
253 | | METHODDEF(void) |
254 | | rgb_ycc_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
255 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
256 | 0 | { |
257 | 0 | switch (cinfo->in_color_space) { |
258 | 0 | case JCS_EXT_RGB: |
259 | 0 | extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
260 | 0 | num_rows); |
261 | 0 | break; |
262 | 0 | case JCS_EXT_RGBX: |
263 | 0 | case JCS_EXT_RGBA: |
264 | 0 | extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
265 | 0 | num_rows); |
266 | 0 | break; |
267 | 0 | case JCS_EXT_BGR: |
268 | 0 | extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
269 | 0 | num_rows); |
270 | 0 | break; |
271 | 0 | case JCS_EXT_BGRX: |
272 | 0 | case JCS_EXT_BGRA: |
273 | 0 | extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
274 | 0 | num_rows); |
275 | 0 | break; |
276 | 0 | case JCS_EXT_XBGR: |
277 | 0 | case JCS_EXT_ABGR: |
278 | 0 | extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
279 | 0 | num_rows); |
280 | 0 | break; |
281 | 0 | case JCS_EXT_XRGB: |
282 | 0 | case JCS_EXT_ARGB: |
283 | 0 | extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
284 | 0 | num_rows); |
285 | 0 | break; |
286 | 0 | default: |
287 | 0 | rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, |
288 | 0 | num_rows); |
289 | 0 | break; |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | | |
294 | | /**************** Cases other than RGB -> YCbCr **************/ |
295 | | |
296 | | |
297 | | /* |
298 | | * Convert some rows of samples to the JPEG colorspace. |
299 | | */ |
300 | | |
301 | | METHODDEF(void) |
302 | | rgb_gray_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
303 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
304 | 0 | { |
305 | 0 | switch (cinfo->in_color_space) { |
306 | 0 | case JCS_EXT_RGB: |
307 | 0 | extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
308 | 0 | num_rows); |
309 | 0 | break; |
310 | 0 | case JCS_EXT_RGBX: |
311 | 0 | case JCS_EXT_RGBA: |
312 | 0 | extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
313 | 0 | num_rows); |
314 | 0 | break; |
315 | 0 | case JCS_EXT_BGR: |
316 | 0 | extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
317 | 0 | num_rows); |
318 | 0 | break; |
319 | 0 | case JCS_EXT_BGRX: |
320 | 0 | case JCS_EXT_BGRA: |
321 | 0 | extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
322 | 0 | num_rows); |
323 | 0 | break; |
324 | 0 | case JCS_EXT_XBGR: |
325 | 0 | case JCS_EXT_ABGR: |
326 | 0 | extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
327 | 0 | num_rows); |
328 | 0 | break; |
329 | 0 | case JCS_EXT_XRGB: |
330 | 0 | case JCS_EXT_ARGB: |
331 | 0 | extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
332 | 0 | num_rows); |
333 | 0 | break; |
334 | 0 | default: |
335 | 0 | rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, |
336 | 0 | num_rows); |
337 | 0 | break; |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | | |
342 | | /* |
343 | | * Extended RGB to plain RGB conversion |
344 | | */ |
345 | | |
346 | | METHODDEF(void) |
347 | | rgb_rgb_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
348 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
349 | 0 | { |
350 | 0 | switch (cinfo->in_color_space) { |
351 | 0 | case JCS_EXT_RGB: |
352 | 0 | extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
353 | 0 | num_rows); |
354 | 0 | break; |
355 | 0 | case JCS_EXT_RGBX: |
356 | 0 | case JCS_EXT_RGBA: |
357 | 0 | extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
358 | 0 | num_rows); |
359 | 0 | break; |
360 | 0 | case JCS_EXT_BGR: |
361 | 0 | extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
362 | 0 | num_rows); |
363 | 0 | break; |
364 | 0 | case JCS_EXT_BGRX: |
365 | 0 | case JCS_EXT_BGRA: |
366 | 0 | extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
367 | 0 | num_rows); |
368 | 0 | break; |
369 | 0 | case JCS_EXT_XBGR: |
370 | 0 | case JCS_EXT_ABGR: |
371 | 0 | extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
372 | 0 | num_rows); |
373 | 0 | break; |
374 | 0 | case JCS_EXT_XRGB: |
375 | 0 | case JCS_EXT_ARGB: |
376 | 0 | extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
377 | 0 | num_rows); |
378 | 0 | break; |
379 | 0 | default: |
380 | 0 | rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, |
381 | 0 | num_rows); |
382 | 0 | break; |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | | |
387 | | /* |
388 | | * Convert some rows of samples to the JPEG colorspace. |
389 | | * This version handles Adobe-style CMYK->YCCK conversion, |
390 | | * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same |
391 | | * conversion as above, while passing K (black) unchanged. |
392 | | * We assume rgb_ycc_start has been called. |
393 | | */ |
394 | | |
395 | | METHODDEF(void) |
396 | | cmyk_ycck_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
397 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
398 | 0 | { |
399 | 0 | #if BITS_IN_JSAMPLE != 16 |
400 | 0 | my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert; |
401 | 0 | register int r, g, b; |
402 | 0 | register JLONG *ctab = cconvert->rgb_ycc_tab; |
403 | 0 | register _JSAMPROW inptr; |
404 | 0 | register _JSAMPROW outptr0, outptr1, outptr2, outptr3; |
405 | 0 | register JDIMENSION col; |
406 | 0 | JDIMENSION num_cols = cinfo->image_width; |
407 | |
|
408 | 0 | while (--num_rows >= 0) { |
409 | 0 | inptr = *input_buf++; |
410 | 0 | outptr0 = output_buf[0][output_row]; |
411 | 0 | outptr1 = output_buf[1][output_row]; |
412 | 0 | outptr2 = output_buf[2][output_row]; |
413 | 0 | outptr3 = output_buf[3][output_row]; |
414 | 0 | output_row++; |
415 | 0 | for (col = 0; col < num_cols; col++) { |
416 | 0 | r = _MAXJSAMPLE - RANGE_LIMIT(inptr[0]); |
417 | 0 | g = _MAXJSAMPLE - RANGE_LIMIT(inptr[1]); |
418 | 0 | b = _MAXJSAMPLE - RANGE_LIMIT(inptr[2]); |
419 | | /* K passes through as-is */ |
420 | 0 | outptr3[col] = inptr[3]; |
421 | 0 | inptr += 4; |
422 | | /* If the inputs are 0.._MAXJSAMPLE, the outputs of these equations |
423 | | * must be too; we do not need an explicit range-limiting operation. |
424 | | * Hence the value being shifted is never negative, and we don't |
425 | | * need the general RIGHT_SHIFT macro. |
426 | | */ |
427 | | /* Y */ |
428 | 0 | outptr0[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] + |
429 | 0 | ctab[b + B_Y_OFF]) >> SCALEBITS); |
430 | | /* Cb */ |
431 | 0 | outptr1[col] = (_JSAMPLE)((ctab[r + R_CB_OFF] + ctab[g + G_CB_OFF] + |
432 | 0 | ctab[b + B_CB_OFF]) >> SCALEBITS); |
433 | | /* Cr */ |
434 | 0 | outptr2[col] = (_JSAMPLE)((ctab[r + R_CR_OFF] + ctab[g + G_CR_OFF] + |
435 | 0 | ctab[b + B_CR_OFF]) >> SCALEBITS); |
436 | 0 | } |
437 | 0 | } |
438 | | #else |
439 | | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
440 | | #endif |
441 | 0 | } |
442 | | |
443 | | |
444 | | /* |
445 | | * Convert some rows of samples to the JPEG colorspace. |
446 | | * This version handles grayscale output with no conversion. |
447 | | * The source can be either plain grayscale or YCbCr (since Y == gray). |
448 | | */ |
449 | | |
450 | | METHODDEF(void) |
451 | | grayscale_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
452 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
453 | 0 | { |
454 | 0 | register _JSAMPROW inptr; |
455 | 0 | register _JSAMPROW outptr; |
456 | 0 | register JDIMENSION col; |
457 | 0 | JDIMENSION num_cols = cinfo->image_width; |
458 | 0 | int instride = cinfo->input_components; |
459 | |
|
460 | 0 | while (--num_rows >= 0) { |
461 | 0 | inptr = *input_buf++; |
462 | 0 | outptr = output_buf[0][output_row]; |
463 | 0 | output_row++; |
464 | 0 | for (col = 0; col < num_cols; col++) { |
465 | 0 | outptr[col] = inptr[0]; |
466 | 0 | inptr += instride; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | | |
472 | | /* |
473 | | * Convert some rows of samples to the JPEG colorspace. |
474 | | * This version handles multi-component colorspaces without conversion. |
475 | | * We assume input_components == num_components. |
476 | | */ |
477 | | |
478 | | METHODDEF(void) |
479 | | null_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf, |
480 | | _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) |
481 | 0 | { |
482 | 0 | register _JSAMPROW inptr; |
483 | 0 | register _JSAMPROW outptr, outptr0, outptr1, outptr2, outptr3; |
484 | 0 | register JDIMENSION col; |
485 | 0 | register int ci; |
486 | 0 | int nc = cinfo->num_components; |
487 | 0 | JDIMENSION num_cols = cinfo->image_width; |
488 | |
|
489 | 0 | if (nc == 3) { |
490 | 0 | while (--num_rows >= 0) { |
491 | 0 | inptr = *input_buf++; |
492 | 0 | outptr0 = output_buf[0][output_row]; |
493 | 0 | outptr1 = output_buf[1][output_row]; |
494 | 0 | outptr2 = output_buf[2][output_row]; |
495 | 0 | output_row++; |
496 | 0 | for (col = 0; col < num_cols; col++) { |
497 | 0 | outptr0[col] = *inptr++; |
498 | 0 | outptr1[col] = *inptr++; |
499 | 0 | outptr2[col] = *inptr++; |
500 | 0 | } |
501 | 0 | } |
502 | 0 | } else if (nc == 4) { |
503 | 0 | while (--num_rows >= 0) { |
504 | 0 | inptr = *input_buf++; |
505 | 0 | outptr0 = output_buf[0][output_row]; |
506 | 0 | outptr1 = output_buf[1][output_row]; |
507 | 0 | outptr2 = output_buf[2][output_row]; |
508 | 0 | outptr3 = output_buf[3][output_row]; |
509 | 0 | output_row++; |
510 | 0 | for (col = 0; col < num_cols; col++) { |
511 | 0 | outptr0[col] = *inptr++; |
512 | 0 | outptr1[col] = *inptr++; |
513 | 0 | outptr2[col] = *inptr++; |
514 | 0 | outptr3[col] = *inptr++; |
515 | 0 | } |
516 | 0 | } |
517 | 0 | } else { |
518 | 0 | while (--num_rows >= 0) { |
519 | | /* It seems fastest to make a separate pass for each component. */ |
520 | 0 | for (ci = 0; ci < nc; ci++) { |
521 | 0 | inptr = *input_buf; |
522 | 0 | outptr = output_buf[ci][output_row]; |
523 | 0 | for (col = 0; col < num_cols; col++) { |
524 | 0 | outptr[col] = inptr[ci]; |
525 | 0 | inptr += nc; |
526 | 0 | } |
527 | 0 | } |
528 | 0 | input_buf++; |
529 | 0 | output_row++; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | | |
535 | | /* |
536 | | * Empty method for start_pass. |
537 | | */ |
538 | | |
539 | | METHODDEF(void) |
540 | | null_method(j_compress_ptr cinfo) |
541 | 0 | { |
542 | | /* no work needed */ |
543 | 0 | } |
544 | | |
545 | | |
546 | | /* |
547 | | * Module initialization routine for input colorspace conversion. |
548 | | */ |
549 | | |
550 | | GLOBAL(void) |
551 | | _jinit_color_converter(j_compress_ptr cinfo) |
552 | 0 | { |
553 | 0 | my_cconvert_ptr cconvert; |
554 | |
|
555 | 0 | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
556 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
557 | |
|
558 | 0 | cconvert = (my_cconvert_ptr) |
559 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
560 | 0 | sizeof(my_color_converter)); |
561 | 0 | cinfo->cconvert = (struct jpeg_color_converter *)cconvert; |
562 | | /* set start_pass to null method until we find out differently */ |
563 | 0 | cconvert->pub.start_pass = null_method; |
564 | | |
565 | | /* Make sure input_components agrees with in_color_space */ |
566 | 0 | switch (cinfo->in_color_space) { |
567 | 0 | case JCS_GRAYSCALE: |
568 | 0 | if (cinfo->input_components != 1) |
569 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
570 | 0 | break; |
571 | | |
572 | 0 | case JCS_RGB: |
573 | 0 | case JCS_EXT_RGB: |
574 | 0 | case JCS_EXT_RGBX: |
575 | 0 | case JCS_EXT_BGR: |
576 | 0 | case JCS_EXT_BGRX: |
577 | 0 | case JCS_EXT_XBGR: |
578 | 0 | case JCS_EXT_XRGB: |
579 | 0 | case JCS_EXT_RGBA: |
580 | 0 | case JCS_EXT_BGRA: |
581 | 0 | case JCS_EXT_ABGR: |
582 | 0 | case JCS_EXT_ARGB: |
583 | 0 | if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) |
584 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
585 | 0 | break; |
586 | | |
587 | 0 | case JCS_YCbCr: |
588 | 0 | if (cinfo->input_components != 3) |
589 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
590 | 0 | break; |
591 | | |
592 | 0 | case JCS_CMYK: |
593 | 0 | case JCS_YCCK: |
594 | 0 | if (cinfo->input_components != 4) |
595 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
596 | 0 | break; |
597 | | |
598 | 0 | default: /* JCS_UNKNOWN can be anything */ |
599 | 0 | if (cinfo->input_components < 1) |
600 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
601 | 0 | break; |
602 | 0 | } |
603 | | |
604 | | /* Check num_components, set conversion method based on requested space. |
605 | | * NOTE: We do not allow any lossy color conversion algorithms in lossless |
606 | | * mode. |
607 | | */ |
608 | 0 | switch (cinfo->jpeg_color_space) { |
609 | 0 | case JCS_GRAYSCALE: |
610 | 0 | if (cinfo->master->lossless && |
611 | 0 | cinfo->in_color_space != cinfo->jpeg_color_space) |
612 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
613 | 0 | if (cinfo->num_components != 1) |
614 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
615 | 0 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
616 | 0 | cconvert->pub._color_convert = grayscale_convert; |
617 | 0 | else if (IsExtRGB(cinfo->in_color_space)) { |
618 | | #ifdef WITH_SIMD |
619 | 0 | if (jsimd_can_rgb_gray()) |
620 | 0 | cconvert->pub._color_convert = jsimd_rgb_gray_convert; |
621 | 0 | else |
622 | 0 | #endif |
623 | 0 | { |
624 | 0 | cconvert->pub.start_pass = rgb_ycc_start; |
625 | 0 | cconvert->pub._color_convert = rgb_gray_convert; |
626 | 0 | } |
627 | 0 | } else if (cinfo->in_color_space == JCS_YCbCr) |
628 | 0 | cconvert->pub._color_convert = grayscale_convert; |
629 | 0 | else |
630 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
631 | 0 | break; |
632 | | |
633 | 0 | case JCS_RGB: |
634 | 0 | if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space)) |
635 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
636 | 0 | if (cinfo->num_components != 3) |
637 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
638 | 0 | if (rgb_red[cinfo->in_color_space] == 0 && |
639 | 0 | rgb_green[cinfo->in_color_space] == 1 && |
640 | 0 | rgb_blue[cinfo->in_color_space] == 2 && |
641 | 0 | rgb_pixelsize[cinfo->in_color_space] == 3) { |
642 | | #if defined(WITH_SIMD) && defined(__mips__) |
643 | | if (jsimd_c_can_null_convert()) |
644 | | cconvert->pub._color_convert = jsimd_c_null_convert; |
645 | | else |
646 | | #endif |
647 | 0 | cconvert->pub._color_convert = null_convert; |
648 | 0 | } else if (IsExtRGB(cinfo->in_color_space)) |
649 | 0 | cconvert->pub._color_convert = rgb_rgb_convert; |
650 | 0 | else |
651 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
652 | 0 | break; |
653 | | |
654 | 0 | case JCS_YCbCr: |
655 | 0 | if (cinfo->master->lossless && |
656 | 0 | cinfo->in_color_space != cinfo->jpeg_color_space) |
657 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
658 | 0 | if (cinfo->num_components != 3) |
659 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
660 | 0 | if (IsExtRGB(cinfo->in_color_space)) { |
661 | | #ifdef WITH_SIMD |
662 | 0 | if (jsimd_can_rgb_ycc()) |
663 | 0 | cconvert->pub._color_convert = jsimd_rgb_ycc_convert; |
664 | 0 | else |
665 | 0 | #endif |
666 | 0 | { |
667 | 0 | cconvert->pub.start_pass = rgb_ycc_start; |
668 | 0 | cconvert->pub._color_convert = rgb_ycc_convert; |
669 | 0 | } |
670 | 0 | } else if (cinfo->in_color_space == JCS_YCbCr) { |
671 | | #if defined(WITH_SIMD) && defined(__mips__) |
672 | | if (jsimd_c_can_null_convert()) |
673 | | cconvert->pub._color_convert = jsimd_c_null_convert; |
674 | | else |
675 | | #endif |
676 | 0 | cconvert->pub._color_convert = null_convert; |
677 | 0 | } else |
678 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
679 | 0 | break; |
680 | | |
681 | 0 | case JCS_CMYK: |
682 | 0 | if (cinfo->master->lossless && |
683 | 0 | cinfo->in_color_space != cinfo->jpeg_color_space) |
684 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
685 | 0 | if (cinfo->num_components != 4) |
686 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
687 | 0 | if (cinfo->in_color_space == JCS_CMYK) { |
688 | | #if defined(WITH_SIMD) && defined(__mips__) |
689 | | if (jsimd_c_can_null_convert()) |
690 | | cconvert->pub._color_convert = jsimd_c_null_convert; |
691 | | else |
692 | | #endif |
693 | 0 | cconvert->pub._color_convert = null_convert; |
694 | 0 | } else |
695 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
696 | 0 | break; |
697 | | |
698 | 0 | case JCS_YCCK: |
699 | 0 | if (cinfo->master->lossless && |
700 | 0 | cinfo->in_color_space != cinfo->jpeg_color_space) |
701 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
702 | 0 | if (cinfo->num_components != 4) |
703 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
704 | 0 | if (cinfo->in_color_space == JCS_CMYK) { |
705 | 0 | cconvert->pub.start_pass = rgb_ycc_start; |
706 | 0 | cconvert->pub._color_convert = cmyk_ycck_convert; |
707 | 0 | } else if (cinfo->in_color_space == JCS_YCCK) { |
708 | | #if defined(WITH_SIMD) && defined(__mips__) |
709 | | if (jsimd_c_can_null_convert()) |
710 | | cconvert->pub._color_convert = jsimd_c_null_convert; |
711 | | else |
712 | | #endif |
713 | 0 | cconvert->pub._color_convert = null_convert; |
714 | 0 | } else |
715 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
716 | 0 | break; |
717 | | |
718 | 0 | default: /* allow null conversion of JCS_UNKNOWN */ |
719 | 0 | if (cinfo->jpeg_color_space != cinfo->in_color_space || |
720 | 0 | cinfo->num_components != cinfo->input_components) |
721 | 0 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
722 | | #if defined(WITH_SIMD) && defined(__mips__) |
723 | | if (jsimd_c_can_null_convert()) |
724 | | cconvert->pub._color_convert = jsimd_c_null_convert; |
725 | | else |
726 | | #endif |
727 | 0 | cconvert->pub._color_convert = null_convert; |
728 | 0 | break; |
729 | 0 | } |
730 | 0 | } Unexecuted instantiation: j12init_color_converter Unexecuted instantiation: j16init_color_converter Unexecuted instantiation: jinit_color_converter |
731 | | |
732 | | #endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |