Debian Codegraph @

src/main/color-conversions.c

raw symbols SCIP
1 /*
2 *
3 * Gimp-Print color management module - traditional Gimp-Print algorithm.
4 *
5 * Copyright 1997-2000 Michael Sweet ([email protected]) and
6 * Robert Krawitz ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 /*
23 * This file must include only standard C header files. The core code must
24 * compile on generic platforms that don't support glib, gimp, gtk, etc.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <gutenprint/gutenprint.h>
31 #include "gutenprint-internal.h"
32 #include <gutenprint/gutenprint-intl-internal.h>
33 #include <gutenprint/curve-cache.h>
34 #include <math.h>
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #include <string.h>
39 #include "color-conversion.h"
40
41 #ifdef __GNUC__
42 #define inline __inline__
43 // There's no reason to inline the main loop with the dispatch
44 // functions only to fail inlining of calls from the inner loop.
45 // Also, inlining the main loop makes it hard to debug because we lose
46 // context of which print function is called.
47 #define NOINLINE __attribute__ ((noinline))
48 #else
49 $define NOINLINE
50 #endif
51
52 #define CFUNC static unsigned NOINLINE
53
54 /*
55 * RGB to grayscale luminance constants...
56 */
57
58 #define LUM_RED 31
59 #define LUM_GREEN 61
60 #define LUM_BLUE 8
61
62 /* rgb/hsl conversions taken from Gimp common/autostretch_hsv.c */
63
64 #define FMAX(a, b) ((a) > (b) ? (a) : (b))
65 #define FMIN(a, b) ((a) < (b) ? (a) : (b))
66
67 #define MAXB(bits) ((1 << (bits)) - 1)
68
69 static inline void
70 calc_rgb_to_hsl(unsigned short *rgb, double *hue, double *sat,
71 double *lightness)
72 {
73 double red, green, blue;
74 double h, s, l;
75 double min, max;
76 double delta;
77 int maxval;
78
79 red = rgb[0] / 65535.0;
80 green = rgb[1] / 65535.0;
81 blue = rgb[2] / 65535.0;
82
83 if (red > green)
84 {
85 if (red > blue)
86 {
87 max = red;
88 maxval = 0;
89 }
90 else
91 {
92 max = blue;
93 maxval = 2;
94 }
95 min = FMIN(green, blue);
96 }
97 else
98 {
99 if (green > blue)
100 {
101 max = green;
102 maxval = 1;
103 }
104 else
105 {
106 max = blue;
107 maxval = 2;
108 }
109 min = FMIN(red, blue);
110 }
111
112 l = (max + min) / 2.0;
113 delta = max - min;
114
115 if (delta < .000001) /* Suggested by Eugene Anikin <[email protected]> */
116 {
117 s = 0.0;
118 h = 0.0;
119 }
120 else
121 {
122 if (l <= .5)
123 s = delta / (max + min);
124 else
125 s = delta / (2 - max - min);
126
127 if (maxval == 0)
128 h = (green - blue) / delta;
129 else if (maxval == 1)
130 h = 2 + (blue - red) / delta;
131 else
132 h = 4 + (red - green) / delta;
133
134 if (h < 0.0)
135 h += 6.0;
136 else if (h > 6.0)
137 h -= 6.0;
138 }
139
140 *hue = h;
141 *sat = s;
142 *lightness = l;
143 }
144
145 static inline double
146 hsl_value(double n1, double n2, double hue)
147 {
148 if (hue < 0)
149 hue += 6.0;
150 else if (hue > 6)
151 hue -= 6.0;
152 if (hue < 1.0)
153 return (n1 + (n2 - n1) * hue);
154 else if (hue < 3.0)
155 return (n2);
156 else if (hue < 4.0)
157 return (n1 + (n2 - n1) * (4.0 - hue));
158 else
159 return (n1);
160 }
161
162 static inline void
163 calc_hsl_to_rgb(unsigned short *rgb, double h, double s, double l)
164 {
165 if (s < .0000001)
166 {
167 if (l > 1)
168 l = 1;
169 else if (l < 0)
170 l = 0;
171 rgb[0] = l * 65535;
172 rgb[1] = l * 65535;
173 rgb[2] = l * 65535;
174 }
175 else
176 {
177 double m1, m2;
178 double h1, h2;
179 h1 = h + 2;
180 h2 = h - 2;
181
182 if (l < .5)
183 m2 = l * (1 + s);
184 else
185 m2 = l + s - (l * s);
186 m1 = (l * 2) - m2;
187 rgb[0] = 65535 * hsl_value(m1, m2, h1);
188 rgb[1] = 65535 * hsl_value(m1, m2, h);
189 rgb[2] = 65535 * hsl_value(m1, m2, h2);
190 }
191 }
192
193 static inline double
194 update_saturation(double sat, double adjust, double isat, int bright_colors)
195 {
196 if (bright_colors || adjust < 1)
197 sat *= adjust;
198 else if (adjust > 1)
199 {
200 double s1 = sat * adjust;
201 double s2 = 1.0 - ((1.0 - sat) * isat);
202 sat = FMIN(s1, s2);
203 }
204 if (sat > 1)
205 sat = 1.0;
206 return sat;
207 }
208
209 static inline double
210 interpolate_value(const double *vec, double val)
211 {
212 double base = (double)((int)(val));
213 double frac = val - base;
214 int ibase = (int) base;
215 double lval = vec[ibase];
216 if (frac > 0)
217 lval += (vec[ibase + 1] - lval) * frac;
218 return lval;
219 }
220
221 static inline void
222 update_saturation_from_rgb(unsigned short *rgb,
223 const unsigned short *brightness_lookup,
224 double adjust, double isat, int do_usermap)
225 {
226 double h, s, l;
227 calc_rgb_to_hsl(rgb, &h, &s, &l);
228 if (do_usermap)
229 {
230 unsigned short ub = (unsigned short) (l * 65535);
231 unsigned short val = brightness_lookup[ub];
232 l = ((double) val) / 65535;
233 if (val < ub)
234 s = s * (65535 - ub) / (65535 - val);
235 }
236 s = update_saturation(s, adjust, isat, 0);
237 calc_hsl_to_rgb(rgb, h, s, l);
238 }
239
240 static inline double
241 adjust_hue(const double *hue_map, double hue, size_t points)
242 {
243 if (hue_map)
244 {
245 hue += interpolate_value(hue_map, hue * points / 6.0);
246 if (hue < 0.0)
247 hue += 6.0;
248 else if (hue >= 6.0)
249 hue -= 6.0;
250 }
251 return hue;
252 }
253
254 static inline void
255 adjust_hsl(unsigned short *rgbout, lut_t *lut, double ssat, double isat,
256 int split_saturation, int adjust_hue_only, int bright_colors)
257 {
258 const double *hue_map = CURVE_CACHE_FAST_DOUBLE(&(lut->hue_map));
259 const double *lum_map = CURVE_CACHE_FAST_DOUBLE(&(lut->lum_map));
260 const double *sat_map = CURVE_CACHE_FAST_DOUBLE(&(lut->sat_map));
261 size_t hue_count = CURVE_CACHE_FAST_COUNT(&(lut->hue_map));
262 size_t lum_count = CURVE_CACHE_FAST_COUNT(&(lut->lum_map));
263 size_t sat_count = CURVE_CACHE_FAST_COUNT(&(lut->sat_map));
264 double h, s, l;
265 double oh;
266 (void)split_saturation;
267
268 rgbout[0] ^= 65535;
269 rgbout[1] ^= 65535;
270 rgbout[2] ^= 65535;
271 calc_rgb_to_hsl(rgbout, &h, &s, &l);
272 s = update_saturation(s, ssat, isat, 0);
273 if (!adjust_hue_only && lut->sat_map.d_cache)
274 {
275 double nh = h * sat_count / 6.0;
276 double tmp = interpolate_value(sat_map, nh);
277 if (tmp < .9999 || tmp > 1.0001)
278 s = update_saturation(s, tmp, tmp > 1.0 ? 1.0 / tmp : 1.0,
279 bright_colors);
280 }
281 oh = h;
282 h = adjust_hue(hue_map, h, hue_count);
283 calc_hsl_to_rgb(rgbout, h, s, l);
284
285 if (!adjust_hue_only && s > 0.00001)
286 {
287 /*
288 * Perform luminosity adjustment only on color component.
289 * This way the luminosity of the gray component won't be affected.
290 * We'll add the gray back at the end.
291 */
292
293 unsigned gray = FMIN(rgbout[0], FMIN(rgbout[1], rgbout[2]));
294 int i;
295 /*
296 * Scale the components by the amount of color left.
297 * This way the luminosity calculations will come out right.
298 */
299 if (gray > 0)
300 for (i = 0; i < 3; i++)
301 rgbout[i] = (rgbout[i] - gray) * 65535.0 / (65535 - gray);
302
303 if (lut->lum_map.d_cache)
304 {
305 calc_rgb_to_hsl(rgbout, &h, &s, &l);
306 if (lut->lum_map.d_cache && l > 0.00001 && l < .99999)
307 {
308 double nh = oh * lum_count / 6.0;
309 double oel = interpolate_value(lum_map,nh);
310 if (oel <= 1)
311 l *= oel;
312 else
313 {
314 double g1 = pow(l, 1.0 / oel);
315 double g2 = 1.0 - pow(1.0 - l, oel);
316 l = FMIN(g1, g2);
317 }
318 calc_hsl_to_rgb(rgbout, h, s, l);
319 }
320 }
321 if (gray > 0)
322 for (i = 0; i < 3; i++)
323 rgbout[i] = gray + (rgbout[i] * (65535 - gray) / 65535.0);
324 }
325
326 rgbout[0] ^= 65535;
327 rgbout[1] ^= 65535;
328 rgbout[2] ^= 65535;
329 }
330
331 #define GENERIC_COLOR_FUNC(fromname, toname) \
332 CFUNC \
333 fromname##_to_##toname(const stp_vars_t *vars, const unsigned char *in, \
334 unsigned short *out) \
335 { \
336 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
337 if (!lut->printed_colorfunc) \
338 { \
339 lut->printed_colorfunc = 1; \
340 stp_dprintf(STP_DBG_COLORFUNC, vars, \
341 "Colorfunc is %s_%d_to_%s, %s, %s, %d, %d\n", \
342 #fromname, lut->channel_depth, #toname, \
343 lut->input_color_description->name, \
344 lut->output_color_description->name, \
345 lut->steps, lut->invert_output); \
346 } \
347 if (lut->channel_depth == 8) \
348 return fromname##_8_to_##toname(vars, in, out); \
349 else \
350 return fromname##_16_to_##toname(vars, in, out); \
351 }
352
353 #define BD(bits) (65535u / (unsigned) MAXB(bits))
354
355 #define COLOR_TO_COLOR_FUNC(T, bits) \
356 CFUNC \
357 color_##bits##_to_color(const stp_vars_t *vars, const unsigned char *in, \
358 unsigned short *out) \
359 { \
360 int i; \
361 double isat = 1.0; \
362 double ssat = stp_get_float_parameter(vars, "Saturation"); \
363 double sbright = stp_get_float_parameter(vars, "Brightness"); \
364 int i0 = -1; \
365 int i1 = -1; \
366 int i2 = -1; \
367 unsigned short o0 = 0; \
368 unsigned short o1 = 0; \
369 unsigned short o2 = 0; \
370 unsigned short nz0 = 0; \
371 unsigned short nz1 = 0; \
372 unsigned short nz2 = 0; \
373 const unsigned short *red; \
374 const unsigned short *green; \
375 const unsigned short *blue; \
376 const unsigned short *brightness; \
377 const unsigned short *contrast; \
378 const T *s_in = (const T *) in; \
379 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
380 int compute_saturation = ssat <= .99999 || ssat >= 1.00001; \
381 int split_saturation = ssat > 1.4; \
382 int bright_color_adjustment = 0; \
383 int hue_only_color_adjustment = 0; \
384 int do_user_adjustment = 0; \
385 if (lut->color_correction->correction == COLOR_CORRECTION_BRIGHT) \
386 bright_color_adjustment = 1; \
387 if (lut->color_correction->correction == COLOR_CORRECTION_HUE) \
388 hue_only_color_adjustment = 1; \
389 if (sbright != 1) \
390 do_user_adjustment = 1; \
391 compute_saturation |= do_user_adjustment; \
392 \
393 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
394 stp_curve_resample(stp_curve_cache_get_curve(&(lut->channel_curves[i])), \
395 1 << bits); \
396 stp_curve_resample \
397 (stp_curve_cache_get_curve(&(lut->brightness_correction)), 65536); \
398 stp_curve_resample \
399 (stp_curve_cache_get_curve(&(lut->contrast_correction)), 1 << bits); \
400 red = \
401 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
402 green = \
403 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
404 blue = \
405 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
406 brightness= \
407 stp_curve_cache_get_ushort_data(&(lut->brightness_correction)); \
408 contrast = \
409 stp_curve_cache_get_ushort_data(&(lut->contrast_correction)); \
410 (void) stp_curve_cache_get_double_data(&(lut->hue_map)); \
411 (void) stp_curve_cache_get_double_data(&(lut->lum_map)); \
412 (void) stp_curve_cache_get_double_data(&(lut->sat_map)); \
413 const double *hue_map = CURVE_CACHE_FAST_DOUBLE(&(lut->hue_map)); \
414 const double *lum_map = CURVE_CACHE_FAST_DOUBLE(&(lut->lum_map)); \
415 const double *sat_map = CURVE_CACHE_FAST_DOUBLE(&(lut->sat_map)); \
416 \
417 if (split_saturation) \
418 ssat = sqrt(ssat); \
419 if (ssat > 1) \
420 isat = 1.0 / ssat; \
421 for (i = 0; i < lut->image_width; i++) \
422 { \
423 if (i0 == s_in[0] && i1 == s_in[1] && i2 == s_in[2]) \
424 { \
425 out[0] = o0; \
426 out[1] = o1; \
427 out[2] = o2; \
428 } \
429 else \
430 { \
431 i0 = s_in[0]; \
432 i1 = s_in[1]; \
433 i2 = s_in[2]; \
434 out[0] = contrast[i0]; \
435 out[1] = contrast[i1]; \
436 out[2] = contrast[i2]; \
437 if ((compute_saturation)) \
438 update_saturation_from_rgb(out, brightness, ssat, isat, \
439 do_user_adjustment); \
440 if ((split_saturation || lum_map || hue_map || sat_map) && \
441 (out[0] != out[1] || out[0] != out[2])) \
442 adjust_hsl(out, lut, ssat, isat, split_saturation, \
443 hue_only_color_adjustment, bright_color_adjustment); \
444 out[0] = red[out[0] / BD(bits)]; \
445 out[1] = green[out[1] / BD(bits)]; \
446 out[2] = blue[out[2] / BD(bits)]; \
447 o0 = out[0]; \
448 o1 = out[1]; \
449 o2 = out[2]; \
450 nz0 |= o0; \
451 nz1 |= o1; \
452 nz2 |= o2; \
453 } \
454 s_in += 3; \
455 out += 3; \
456 } \
457 return (nz0 ? 0 : 1) + (nz1 ? 0 : 2) + (nz2 ? 0 : 4); \
458 }
459
460 COLOR_TO_COLOR_FUNC(unsigned char, 8) // color_8_to_color
461 COLOR_TO_COLOR_FUNC(unsigned short, 16) // color_16_to_color
462 GENERIC_COLOR_FUNC(color, color)
463
464 #define COLOR_TO_KCMY_FUNC(T, bits) \
465 CFUNC \
466 color_##bits##_to_kcmy(const stp_vars_t *vars, const unsigned char *in, \
467 unsigned short *out) \
468 { \
469 int i; \
470 double isat = 1.0; \
471 double ssat = stp_get_float_parameter(vars, "Saturation"); \
472 double sbright = stp_get_float_parameter(vars, "Brightness"); \
473 union { \
474 unsigned short nz[4]; \
475 unsigned long long nzl; \
476 } nzx; \
477 unsigned retval = 0; \
478 const unsigned short *red; \
479 const unsigned short *green; \
480 const unsigned short *blue; \
481 const unsigned short *brightness; \
482 const unsigned short *contrast; \
483 const T *s_in = (const T *) in; \
484 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
485 int compute_saturation = ssat <= .99999 || ssat >= 1.00001; \
486 int split_saturation = ssat > 1.4; \
487 int bright_color_adjustment = 0; \
488 int hue_only_color_adjustment = 0; \
489 int do_user_adjustment = 0; \
490 if (lut->color_correction->correction == COLOR_CORRECTION_BRIGHT) \
491 bright_color_adjustment = 1; \
492 if (lut->color_correction->correction == COLOR_CORRECTION_HUE) \
493 hue_only_color_adjustment = 1; \
494 if (sbright != 1) \
495 do_user_adjustment = 1; \
496 compute_saturation |= do_user_adjustment; \
497 nzx.nzl = 0ull; \
498 \
499 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
500 stp_curve_resample(stp_curve_cache_get_curve(&(lut->channel_curves[i])), \
501 1 << bits); \
502 stp_curve_resample \
503 (stp_curve_cache_get_curve(&(lut->brightness_correction)), 65536); \
504 stp_curve_resample \
505 (stp_curve_cache_get_curve(&(lut->contrast_correction)), 1 << bits); \
506 red = \
507 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
508 green = \
509 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
510 blue = \
511 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
512 brightness= \
513 stp_curve_cache_get_ushort_data(&(lut->brightness_correction)); \
514 contrast = \
515 stp_curve_cache_get_ushort_data(&(lut->contrast_correction)); \
516 (void) stp_curve_cache_get_double_data(&(lut->hue_map)); \
517 (void) stp_curve_cache_get_double_data(&(lut->lum_map)); \
518 (void) stp_curve_cache_get_double_data(&(lut->sat_map)); \
519 const double *hue_map = CURVE_CACHE_FAST_DOUBLE(&(lut->hue_map)); \
520 const double *lum_map = CURVE_CACHE_FAST_DOUBLE(&(lut->lum_map)); \
521 const double *sat_map = CURVE_CACHE_FAST_DOUBLE(&(lut->sat_map)); \
522 \
523 if (split_saturation) \
524 ssat = sqrt(ssat); \
525 if (ssat > 1) \
526 isat = 1.0 / ssat; \
527 for (i = 0; i < lut->image_width; i++, out += 4, s_in += 3) \
528 { \
529 out[1] = contrast[s_in[0]]; \
530 out[2] = contrast[s_in[1]]; \
531 out[3] = contrast[s_in[2]]; \
532 if ((compute_saturation)) \
533 update_saturation_from_rgb(out + 1, brightness, ssat, isat, \
534 do_user_adjustment); \
535 if ((split_saturation || lum_map || hue_map || sat_map) && \
536 (out[1] != out[2] || out[1] != out[3])) \
537 adjust_hsl(out + 1, lut, ssat, isat, split_saturation, \
538 hue_only_color_adjustment, bright_color_adjustment); \
539 out[1] = red[out[1] / BD(bits)]; \
540 out[2] = green[out[2] / BD(bits)]; \
541 out[3] = blue[out[3] / BD(bits)]; \
542 out[0] = FMIN(out[1], FMIN(out[2], out[3])); \
543 out[1] -= out[0]; \
544 out[2] -= out[0]; \
545 out[3] -= out[0]; \
546 nzx.nzl |= *(unsigned long long *) out; \
547 } \
548 for (i = 0; i < 4; i++) \
549 if (nzx.nz[i] == 0) \
550 retval |= (1 << i); \
551 return retval; \
552 }
553
554 COLOR_TO_KCMY_FUNC(unsigned char, 8) // color_8_to_kcmy
555 COLOR_TO_KCMY_FUNC(unsigned short, 16) // color_16_to_kcmy
556 GENERIC_COLOR_FUNC(color, kcmy)
557
558 /*
559 * 'rgb_to_rgb()' - Convert rgb image data to RGB.
560 */
561
562 #define FAST_COLOR_TO_COLOR_FUNC(T, bits) \
563 CFUNC \
564 color_##bits##_to_color_fast(const stp_vars_t *vars, const unsigned char *in, \
565 unsigned short *out) \
566 { \
567 int i; \
568 int i0 = -1; \
569 int i1 = -1; \
570 int i2 = -1; \
571 int o0 = 0; \
572 int o1 = 0; \
573 int o2 = 0; \
574 int nz0 = 0; \
575 int nz1 = 0; \
576 int nz2 = 0; \
577 const T *s_in = (const T *) in; \
578 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
579 const unsigned short *red; \
580 const unsigned short *green; \
581 const unsigned short *blue; \
582 const unsigned short *brightness; \
583 const unsigned short *contrast; \
584 double isat = 1.0; \
585 double saturation = stp_get_float_parameter(vars, "Saturation"); \
586 double sbright = stp_get_float_parameter(vars, "Brightness"); \
587 int compute_saturation = saturation <= .99999 || saturation >= 1.00001; \
588 int do_user_adjustment = 0; \
589 if (sbright != 1) \
590 do_user_adjustment = 1; \
591 compute_saturation |= do_user_adjustment; \
592 \
593 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
594 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
595 stp_curve_resample \
596 (stp_curve_cache_get_curve(&(lut->brightness_correction)), 65536); \
597 stp_curve_resample \
598 (stp_curve_cache_get_curve(&(lut->contrast_correction)), 1 << bits); \
599 red = \
600 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
601 green = \
602 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
603 blue = \
604 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
605 brightness= \
606 stp_curve_cache_get_ushort_data(&(lut->brightness_correction)); \
607 contrast = \
608 stp_curve_cache_get_ushort_data(&(lut->contrast_correction)); \
609 \
610 if (saturation > 1) \
611 isat = 1.0 / saturation; \
612 for (i = 0; i < lut->image_width; i++) \
613 { \
614 if (i0 == s_in[0] && i1 == s_in[1] && i2 == s_in[2]) \
615 { \
616 out[0] = o0; \
617 out[1] = o1; \
618 out[2] = o2; \
619 } \
620 else \
621 { \
622 i0 = s_in[0]; \
623 i1 = s_in[1]; \
624 i2 = s_in[2]; \
625 out[0] = contrast[s_in[0]]; \
626 out[1] = contrast[s_in[1]]; \
627 out[2] = contrast[s_in[2]]; \
628 if ((compute_saturation)) \
629 update_saturation_from_rgb(out, brightness, saturation, isat, 1); \
630 out[0] = red[out[0]]; \
631 out[1] = green[out[1]]; \
632 out[2] = blue[out[2]]; \
633 o0 = out[0]; \
634 o1 = out[1]; \
635 o2 = out[2]; \
636 nz0 |= o0; \
637 nz1 |= o1; \
638 nz2 |= o2; \
639 } \
640 s_in += 3; \
641 out += 3; \
642 } \
643 return (nz0 ? 0 : 1) + (nz1 ? 0 : 2) + (nz2 ? 0 : 4); \
644 }
645
646 FAST_COLOR_TO_COLOR_FUNC(unsigned char, 8) // color_8_to_color_fast
647 FAST_COLOR_TO_COLOR_FUNC(unsigned short, 16) // color_16_to_color_fast
648 GENERIC_COLOR_FUNC(color, color_fast)
649
650 #define FAST_COLOR_TO_KCMY_FUNC(T, bits) \
651 CFUNC \
652 color_##bits##_to_kcmy_fast(const stp_vars_t *vars, const unsigned char *in, \
653 unsigned short *out) \
654 { \
655 int i; \
656 union { \
657 unsigned short nz[4]; \
658 unsigned long long nzl; \
659 } nzx; \
660 unsigned retval = 0; \
661 unsigned short c, m, y, k; \
662 const T *s_in = (const T *) in; \
663 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
664 const unsigned short *red; \
665 const unsigned short *green; \
666 const unsigned short *blue; \
667 const unsigned short *brightness; \
668 const unsigned short *contrast; \
669 double isat = 1.0; \
670 double saturation = stp_get_float_parameter(vars, "Saturation"); \
671 double sbright = stp_get_float_parameter(vars, "Brightness"); \
672 int compute_saturation = saturation <= .99999 || saturation >= 1.00001; \
673 int do_user_adjustment = 0; \
674 if (sbright != 1) \
675 do_user_adjustment = 1; \
676 compute_saturation |= do_user_adjustment; \
677 nzx.nzl = 0ull; \
678 \
679 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
680 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
681 stp_curve_resample \
682 (stp_curve_cache_get_curve(&(lut->brightness_correction)), 65536); \
683 stp_curve_resample \
684 (stp_curve_cache_get_curve(&(lut->contrast_correction)), 1 << bits); \
685 red = \
686 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
687 green = \
688 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
689 blue = \
690 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
691 brightness= \
692 stp_curve_cache_get_ushort_data(&(lut->brightness_correction)); \
693 contrast = \
694 stp_curve_cache_get_ushort_data(&(lut->contrast_correction)); \
695 \
696 if (saturation > 1) \
697 isat = 1.0 / saturation; \
698 for (i = 0; i < lut->image_width; i++, out += 4, s_in += 3) \
699 { \
700 c = contrast[s_in[0]]; \
701 m = contrast[s_in[1]]; \
702 y = contrast[s_in[2]]; \
703 if (compute_saturation) \
704 { \
705 unsigned short tmp[3]; \
706 tmp[0] = c; \
707 tmp[1] = m; \
708 tmp[2] = y; \
709 update_saturation_from_rgb(tmp, brightness, saturation, \
710 isat, 1); \
711 c = tmp[0]; \
712 m = tmp[1]; \
713 y = tmp[2]; \
714 } \
715 c = red[c]; \
716 m = green[m]; \
717 y = blue[y]; \
718 k = FMIN(c, FMIN(m, y)); \
719 out[0] = k; \
720 out[1] = c - k; \
721 out[2] = m - k; \
722 out[3] = y - k; \
723 nzx.nzl |= *(unsigned long long *) out; \
724 } \
725 for (i = 0; i < 4; i++) \
726 if (nzx.nz[i] == 0) \
727 retval |= (1 << i); \
728 return retval; \
729 }
730
731 FAST_COLOR_TO_KCMY_FUNC(unsigned char, 8) // color_8_to_kcmy_fast
732 FAST_COLOR_TO_KCMY_FUNC(unsigned short, 16) // color_16_to_color_fast
733 GENERIC_COLOR_FUNC(color, kcmy_fast)
734
735 #define RAW_COLOR_TO_COLOR_FUNC(T, bits) \
736 CFUNC \
737 color_##bits##_to_color_raw(const stp_vars_t *vars, const unsigned char *in,\
738 unsigned short *out) \
739 { \
740 int i; \
741 int j; \
742 int nz = 0; \
743 const T *s_in = (const T *) in; \
744 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
745 unsigned mask = 0; \
746 if (lut->invert_output) \
747 mask = 0xffff; \
748 \
749 for (i = 0; i < lut->image_width; i++) \
750 { \
751 unsigned bit = 1; \
752 for (j = 0; j < 3; j++, bit += bit) \
753 { \
754 out[j] = (s_in[j] * (65535 / ((1 << bits) - 1))) ^ mask; \
755 if (out[j]) \
756 nz |= bit; \
757 } \
758 s_in += 3; \
759 out += 3; \
760 } \
761 return nz; \
762 }
763
764 RAW_COLOR_TO_COLOR_FUNC(unsigned char, 8) // color_8_to_color_raw
765 RAW_COLOR_TO_COLOR_FUNC(unsigned short, 16) // color_16_to_color_raw
766 GENERIC_COLOR_FUNC(color, color_raw)
767
768 #define RAW_COLOR_TO_KCMY_FUNC(T, bits) \
769 CFUNC \
770 color_##bits##_to_kcmy_raw(const stp_vars_t *vars, const unsigned char *in, \
771 unsigned short *out) \
772 { \
773 int i; \
774 union { \
775 unsigned short nz[4]; \
776 unsigned long long nzl; \
777 } nzx; \
778 unsigned retval = 0; \
779 const T *s_in = (const T *) in; \
780 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
781 unsigned mask = 0; \
782 if (lut->invert_output) \
783 mask = 0xffff; \
784 nzx.nzl = 0ull; \
785 \
786 for (i = 0; i < lut->image_width; i++, out += 4, s_in += 3) \
787 { \
788 unsigned c = (s_in[0] * BD(bits)) ^ mask; \
789 unsigned m = (s_in[1] * BD(bits)) ^ mask; \
790 unsigned y = (s_in[2] * BD(bits)) ^ mask; \
791 unsigned k = FMIN(c, FMIN(m, y)); \
792 out[0] = k; \
793 out[1] = c - k; \
794 out[2] = m - k; \
795 out[3] = y - k; \
796 nzx.nzl |= *(unsigned long long *) out; \
797 } \
798 for (i = 0; i < 4; i++) \
799 if (nzx.nz[i] == 0) \
800 retval |= (1 << i); \
801 return retval; \
802 }
803
804 RAW_COLOR_TO_KCMY_FUNC(unsigned char, 8) // color_8_to_kcmy_raw
805 RAW_COLOR_TO_KCMY_FUNC(unsigned short, 16) // color_16_to_kcmy_raw
806 GENERIC_COLOR_FUNC(color, kcmy_raw)
807
808 /*
809 * 'gray_to_rgb()' - Convert gray image data to RGB.
810 */
811
812 #define GRAY_TO_COLOR_FUNC(T, bits) \
813 CFUNC \
814 gray_##bits##_to_color(const stp_vars_t *vars, const unsigned char *in, \
815 unsigned short *out) \
816 { \
817 int i; \
818 int i0 = -1; \
819 int o0 = 0; \
820 int o1 = 0; \
821 int o2 = 0; \
822 int nz0 = 0; \
823 int nz1 = 0; \
824 int nz2 = 0; \
825 const T *s_in = (const T *) in; \
826 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
827 const unsigned short *red; \
828 const unsigned short *green; \
829 const unsigned short *blue; \
830 const unsigned short *user; \
831 \
832 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
833 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
834 stp_curve_resample \
835 (stp_curve_cache_get_curve(&(lut->user_color_correction)), 1 << bits); \
836 red = \
837 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
838 green = \
839 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
840 blue = \
841 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
842 user = \
843 stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
844 \
845 for (i = 0; i < lut->image_width; i++) \
846 { \
847 if (i0 == s_in[0]) \
848 { \
849 out[0] = o0; \
850 out[1] = o1; \
851 out[2] = o2; \
852 } \
853 else \
854 { \
855 i0 = s_in[0]; \
856 out[0] = red[user[s_in[0]]]; \
857 out[1] = green[user[s_in[0]]]; \
858 out[2] = blue[user[s_in[0]]]; \
859 o0 = out[0]; \
860 o1 = out[1]; \
861 o2 = out[2]; \
862 nz0 |= o0; \
863 nz1 |= o1; \
864 nz2 |= o2; \
865 } \
866 s_in += 1; \
867 out += 3; \
868 } \
869 return (nz0 ? 0 : 1) + (nz1 ? 0 : 2) + (nz2 ? 0 : 4); \
870 }
871
872 GRAY_TO_COLOR_FUNC(unsigned char, 8) // gray_8_to_color
873 GRAY_TO_COLOR_FUNC(unsigned short, 16) // gray_16_to_color
874 GENERIC_COLOR_FUNC(gray, color)
875
876 #define GRAY_TO_KCMY_FUNC(T, bits) \
877 CFUNC \
878 gray_##bits##_to_kcmy(const stp_vars_t *vars, const unsigned char *in, \
879 unsigned short *out) \
880 { \
881 int i; \
882 union { \
883 unsigned short nz[4]; \
884 unsigned long long nzl; \
885 } nzx; \
886 unsigned retval = 0; \
887 const T *s_in = (const T *) in; \
888 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
889 const unsigned short *red; \
890 const unsigned short *green; \
891 const unsigned short *blue; \
892 const unsigned short *user; \
893 \
894 for (i = CHANNEL_C; i <= CHANNEL_Y; i++) \
895 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
896 stp_curve_resample \
897 (stp_curve_cache_get_curve(&(lut->user_color_correction)), 1 << bits); \
898 red = \
899 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_C])); \
900 green = \
901 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_M])); \
902 blue = \
903 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_Y])); \
904 user = \
905 stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
906 \
907 for (i = 0; i < lut->image_width; i++, out += 4, s_in++) \
908 { \
909 out[1] = red[user[s_in[0]]]; \
910 out[2] = green[user[s_in[0]]]; \
911 out[3] = blue[user[s_in[0]]]; \
912 out[0] = FMIN(out[1], FMIN(out[2], out[3])); \
913 out[1] -= out[0]; \
914 out[2] -= out[0]; \
915 out[3] -= out[0]; \
916 nzx.nzl |= *(unsigned long long *) out; \
917 } \
918 for (i = 0; i < 4; i++) \
919 if (nzx.nz[i] == 0) \
920 retval |= (1 << i); \
921 return retval; \
922 }
923
924 GRAY_TO_KCMY_FUNC(unsigned char, 8) // gray_8_to_kcmy
925 GRAY_TO_KCMY_FUNC(unsigned short, 16) // gray_16_to_kcmy
926 GENERIC_COLOR_FUNC(gray, kcmy)
927
928 #define GRAY_TO_COLOR_RAW_FUNC(T, bits) \
929 CFUNC \
930 gray_##bits##_to_color_raw(const stp_vars_t *vars, const unsigned char *in,\
931 unsigned short *out) \
932 { \
933 int i; \
934 int nz = 7; \
935 const T *s_in = (const T *) in; \
936 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
937 unsigned mask = 0; \
938 if (lut->invert_output) \
939 mask = 0xffff; \
940 \
941 for (i = 0; i < lut->image_width; i++) \
942 { \
943 unsigned outval = (s_in[0] * (65535 / (1 << bits))) ^ mask; \
944 out[0] = outval; \
945 out[1] = outval; \
946 out[2] = outval; \
947 if (outval) \
948 nz = 0; \
949 s_in++; \
950 out += 3; \
951 } \
952 return nz; \
953 }
954
955 GRAY_TO_COLOR_RAW_FUNC(unsigned char, 8) // gray_8_to_color_raw
956 GRAY_TO_COLOR_RAW_FUNC(unsigned short, 16) // gray_16_to_color_raw
957 GENERIC_COLOR_FUNC(gray, color_raw)
958
959 #define GRAY_TO_KCMY_RAW_FUNC(T, bits) \
960 CFUNC \
961 gray_##bits##_to_kcmy_raw(const stp_vars_t *vars, const unsigned char *in, \
962 unsigned short *out) \
963 { \
964 int i; \
965 int nz = 7; \
966 const T *s_in = (const T *) in; \
967 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
968 unsigned mask = 0; \
969 if (lut->invert_output) \
970 mask = 0xffff; \
971 \
972 for (i = 0; i < lut->image_width; i++, out += 4, s_in++) \
973 { \
974 unsigned outval = (s_in[0] * (65535 / (1 << bits))) ^ mask; \
975 out[0] = outval; \
976 out[1] = 0; \
977 out[2] = 0; \
978 out[3] = 0; \
979 if (outval) \
980 nz = 0; \
981 } \
982 return nz; \
983 }
984
985 GRAY_TO_KCMY_RAW_FUNC(unsigned char, 8) // gray_8_to_kcmy_raw
986 GRAY_TO_KCMY_RAW_FUNC(unsigned short, 16) // gray_16_to_kcmy_raw
987 GENERIC_COLOR_FUNC(gray, kcmy_raw)
988
989 #define COLOR_TO_KCMY_THRESHOLD_FUNC(T, name) \
990 CFUNC \
991 name##_to_kcmy_threshold(const stp_vars_t *vars, \
992 const unsigned char *in, \
993 unsigned short *out) \
994 { \
995 int i; \
996 int z = 15; \
997 const T *s_in = (const T *) in; \
998 unsigned high_bit = ((1 << ((sizeof(T) * 8) - 1))); \
999 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1000 int width = lut->image_width; \
1001 unsigned mask = 0; \
1002 memset(out, 0, width * 4 * sizeof(unsigned short)); \
1003 if (lut->invert_output) \
1004 mask = (1 << (sizeof(T) * 8)) - 1; \
1005 \
1006 for (i = 0; i < width; i++, out += 4, s_in += 3) \
1007 { \
1008 unsigned c = s_in[0] ^ mask; \
1009 unsigned m = s_in[1] ^ mask; \
1010 unsigned y = s_in[2] ^ mask; \
1011 unsigned k = (c < m ? (c < y ? c : y) : (m < y ? m : y)); \
1012 if (k >= high_bit) \
1013 { \
1014 c -= k; \
1015 m -= k; \
1016 y -= k; \
1017 } \
1018 if (k >= high_bit) \
1019 { \
1020 z &= 0xe; \
1021 out[0] = 65535; \
1022 } \
1023 if (c >= high_bit) \
1024 { \
1025 z &= 0xd; \
1026 out[1] = 65535; \
1027 } \
1028 if (m >= high_bit) \
1029 { \
1030 z &= 0xb; \
1031 out[2] = 65535; \
1032 } \
1033 if (y >= high_bit) \
1034 { \
1035 z &= 0x7; \
1036 out[3] = 65535; \
1037 } \
1038 } \
1039 return z; \
1040 }
1041
1042 COLOR_TO_KCMY_THRESHOLD_FUNC(unsigned char, color_8) // color_8_to_kcmy_threshold
1043 COLOR_TO_KCMY_THRESHOLD_FUNC(unsigned short, color_16) // color_16_to_kcmy_threshold
1044 GENERIC_COLOR_FUNC(color, kcmy_threshold)
1045
1046 #define CMYK_TO_KCMY_THRESHOLD_FUNC(T, name) \
1047 CFUNC \
1048 name##_to_kcmy_threshold(const stp_vars_t *vars, \
1049 const unsigned char *in, \
1050 unsigned short *out) \
1051 { \
1052 int i; \
1053 int z = 15; \
1054 const T *s_in = (const T *) in; \
1055 unsigned desired_high_bit = 0; \
1056 unsigned high_bit = 1 << ((sizeof(T) * 8) - 1); \
1057 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1058 int width = lut->image_width; \
1059 memset(out, 0, width * 4 * sizeof(unsigned short)); \
1060 if (!lut->invert_output) \
1061 desired_high_bit = high_bit; \
1062 \
1063 for (i = 0; i < width; i++, out += 4, s_in += 4) \
1064 { \
1065 if ((s_in[3] & high_bit) == desired_high_bit) \
1066 { \
1067 z &= 0xe; \
1068 out[0] = 65535; \
1069 } \
1070 if ((s_in[0] & high_bit) == desired_high_bit) \
1071 { \
1072 z &= 0xd; \
1073 out[1] = 65535; \
1074 } \
1075 if ((s_in[1] & high_bit) == desired_high_bit) \
1076 { \
1077 z &= 0xb; \
1078 out[2] = 65535; \
1079 } \
1080 if ((s_in[2] & high_bit) == desired_high_bit) \
1081 { \
1082 z &= 0x7; \
1083 out[3] = 65535; \
1084 } \
1085 } \
1086 return z; \
1087 }
1088
1089 CMYK_TO_KCMY_THRESHOLD_FUNC(unsigned char, cmyk_8) // cmyk_8_to_kcmy_threshold
1090 CMYK_TO_KCMY_THRESHOLD_FUNC(unsigned short, cmyk_16) // cmyk_16_to_kcmy_threshodl
1091 GENERIC_COLOR_FUNC(cmyk, kcmy_threshold)
1092
1093 #define KCMY_TO_KCMY_THRESHOLD_FUNC(T, name) \
1094 CFUNC \
1095 name##_to_kcmy_threshold(const stp_vars_t *vars, \
1096 const unsigned char *in, \
1097 unsigned short *out) \
1098 { \
1099 int i; \
1100 int j; \
1101 unsigned nz[4]; \
1102 unsigned z = 0xf; \
1103 const T *s_in = (const T *) in; \
1104 unsigned desired_high_bit = 0; \
1105 unsigned high_bit = 1 << ((sizeof(T) * 8) - 1); \
1106 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1107 int width = lut->image_width; \
1108 memset(out, 0, width * 4 * sizeof(unsigned short)); \
1109 if (!lut->invert_output) \
1110 desired_high_bit = high_bit; \
1111 for (i = 0; i < 4; i++) \
1112 nz[i] = z & ~(1 << i); \
1113 \
1114 for (i = 0; i < width; i++) \
1115 { \
1116 for (j = 0; j < 4; j++) \
1117 { \
1118 if ((*s_in++ & high_bit) == desired_high_bit) \
1119 { \
1120 z &= nz[j]; \
1121 *out = 65535; \
1122 } \
1123 out++; \
1124 } \
1125 } \
1126 return z; \
1127 }
1128
1129 KCMY_TO_KCMY_THRESHOLD_FUNC(unsigned char, kcmy_8) // kcmy_8_to_kcmy_threshold
1130 KCMY_TO_KCMY_THRESHOLD_FUNC(unsigned short, kcmy_16) // kcmy_8_to_kcmy_threshold
1131 GENERIC_COLOR_FUNC(kcmy, kcmy_threshold)
1132
1133 #define GRAY_TO_COLOR_THRESHOLD_FUNC(T, name, bits, channels) \
1134 CFUNC \
1135 gray_##bits##_to_##name##_threshold(const stp_vars_t *vars, \
1136 const unsigned char *in, \
1137 unsigned short *out) \
1138 { \
1139 int i; \
1140 int z = (1 << channels) - 1; \
1141 int desired_high_bit = 0; \
1142 unsigned high_bit = 1 << ((sizeof(T) * 8) - 1); \
1143 const T *s_in = (const T *) in; \
1144 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1145 int width = lut->image_width; \
1146 memset(out, 0, width * channels * sizeof(unsigned short)); \
1147 if (!lut->invert_output) \
1148 desired_high_bit = high_bit; \
1149 \
1150 for (i = 0; i < width; i++, out += channels, s_in++) \
1151 { \
1152 if ((s_in[0] & high_bit) == desired_high_bit) \
1153 { \
1154 int j; \
1155 z = 0; \
1156 for (j = 0; j < channels; j++) \
1157 out[j] = 65535; \
1158 } \
1159 } \
1160 return z; \
1161 }
1162
1163
1164 GRAY_TO_COLOR_THRESHOLD_FUNC(unsigned char, color, 8, 3) // gray_8_to_color_threshold
1165 GRAY_TO_COLOR_THRESHOLD_FUNC(unsigned short, color, 16, 3) // gray_16_to_color_threshold
1166 GENERIC_COLOR_FUNC(gray, color_threshold)
1167
1168 GRAY_TO_COLOR_THRESHOLD_FUNC(unsigned char, kcmy, 8, 4) // gray_8_to_kcmy_threshold
1169 GRAY_TO_COLOR_THRESHOLD_FUNC(unsigned short, kcmy, 16, 4) // gray_16_to_kcmy_threshold
1170 GENERIC_COLOR_FUNC(gray, kcmy_threshold)
1171
1172 #define COLOR_TO_COLOR_THRESHOLD_FUNC(T, name) \
1173 CFUNC \
1174 name##_to_color_threshold(const stp_vars_t *vars, \
1175 const unsigned char *in, \
1176 unsigned short *out) \
1177 { \
1178 int i; \
1179 int z = 7; \
1180 int desired_high_bit = 0; \
1181 unsigned high_bit = ((1 << ((sizeof(T) * 8) - 1)) * 4); \
1182 const T *s_in = (const T *) in; \
1183 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1184 int width = lut->image_width; \
1185 memset(out, 0, width * 3 * sizeof(unsigned short)); \
1186 if (!lut->invert_output) \
1187 desired_high_bit = high_bit; \
1188 \
1189 for (i = 0; i < width; i++, out += 3, s_in += 3) \
1190 { \
1191 if ((s_in[0] & high_bit) == desired_high_bit) \
1192 { \
1193 z &= 6; \
1194 out[0] = 65535; \
1195 } \
1196 if ((s_in[1] & high_bit) == desired_high_bit) \
1197 { \
1198 z &= 5; \
1199 out[1] = 65535; \
1200 } \
1201 if ((s_in[1] & high_bit) == desired_high_bit) \
1202 { \
1203 z &= 3; \
1204 out[2] = 65535; \
1205 } \
1206 } \
1207 return z; \
1208 }
1209
1210 COLOR_TO_COLOR_THRESHOLD_FUNC(unsigned char, color_8) // color_8_to_color_threshold
1211 COLOR_TO_COLOR_THRESHOLD_FUNC(unsigned short, color_16) // color_8_to_color_threshold
1212 GENERIC_COLOR_FUNC(color, color_threshold)
1213
1214 #define COLOR_TO_GRAY_THRESHOLD_FUNC(T, name, channels, max_channels) \
1215 CFUNC \
1216 name##_to_gray_threshold(const stp_vars_t *vars, \
1217 const unsigned char *in, \
1218 unsigned short *out) \
1219 { \
1220 int i; \
1221 int z = 1; \
1222 int desired_high_bit = 0; \
1223 unsigned high_bit = ((1 << ((sizeof(T) * 8) - 1))); \
1224 const T *s_in = (const T *) in; \
1225 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1226 int width = lut->image_width; \
1227 memset(out, 0, width * sizeof(unsigned short)); \
1228 if (!lut->invert_output) \
1229 desired_high_bit = high_bit; \
1230 \
1231 for (i = 0; i < width; i++, out++, s_in += channels) \
1232 { \
1233 unsigned gval = \
1234 (max_channels - channels) * (1 << ((sizeof(T) * 8) - 1)); \
1235 int j; \
1236 for (j = 0; j < channels; j++) \
1237 gval += s_in[j]; \
1238 gval /= channels; \
1239 if ((gval & high_bit) == desired_high_bit) \
1240 { \
1241 out[0] = 65535; \
1242 z = 0; \
1243 } \
1244 } \
1245 return z; \
1246 }
1247
1248 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned char, cmyk_8, 4, 4) // cmyk_8_to_gray_threshold
1249 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned short, cmyk_16, 4, 4) // cmyk_16_to_gray_threshold
1250 GENERIC_COLOR_FUNC(cmyk, gray_threshold)
1251
1252 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned char, kcmy_8, 4, 4) // kcmy_8_to_gray_threshold
1253 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned short, kcmy_16, 4, 4) // kcmy_16_to_gray_threshold
1254 GENERIC_COLOR_FUNC(kcmy, gray_threshold)
1255
1256 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned char, color_8, 3, 3) // color_8_to_gray_threshold
1257 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned short, color_16, 3, 3) // color_16_to_gray_threshold
1258 GENERIC_COLOR_FUNC(color, gray_threshold)
1259
1260 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned char, gray_8, 1, 1) // gray_8_to_gray_threshold
1261 COLOR_TO_GRAY_THRESHOLD_FUNC(unsigned short, gray_16, 1, 1) // gray_16_to_gray_threshold
1262 GENERIC_COLOR_FUNC(gray, gray_threshold)
1263
1264 #define CMYK_TO_COLOR_FUNC(namein, name2, T, bits, offset) \
1265 static unsigned \
1266 namein##_##bits##_to_##name2(const stp_vars_t *vars, const unsigned char *in, \
1267 unsigned short *out) \
1268 { \
1269 int i; \
1270 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1271 unsigned status; \
1272 size_t real_steps = lut->steps; \
1273 const T *s_in = (const T *) in; \
1274 unsigned short *tmp; \
1275 int width = lut->image_width; \
1276 \
1277 if (!lut->cmy_tmp) \
1278 lut->cmy_tmp = stp_malloc(3 * 2 * lut->image_width); \
1279 tmp = lut->cmy_tmp; \
1280 memset(lut->cmy_tmp, 0, width * 3 * sizeof(unsigned short)); \
1281 \
1282 for (i = 0; i < width; i++, tmp += 3, s_in += 4) \
1283 { \
1284 unsigned c = (s_in[0 + offset] + s_in[(3 + offset) % 4]) * \
1285 (65535 / MAXB(bits)); \
1286 unsigned m = (s_in[1 + offset] + s_in[(3 + offset) % 4]) * \
1287 (65535 / MAXB(bits)); \
1288 unsigned y = (s_in[2 + offset] + s_in[(3 + offset) % 4]) * \
1289 (65535 / MAXB(bits)); \
1290 if (c > MAXB(16)) \
1291 c = MAXB(16); \
1292 if (m > MAXB(16)) \
1293 m = MAXB(16); \
1294 if (y > MAXB(16)) \
1295 y = MAXB(16); \
1296 tmp[0] = c; \
1297 tmp[1] = m; \
1298 tmp[2] = y; \
1299 } \
1300 lut->steps = 65536; \
1301 status = \
1302 color_16_to_##name2(vars, (const unsigned char *) lut->cmy_tmp, out); \
1303 lut->steps = real_steps; \
1304 return status; \
1305 }
1306
1307 CMYK_TO_COLOR_FUNC(cmyk, color, unsigned char, 8, 0) // cmyk_8_to_color
1308 CMYK_TO_COLOR_FUNC(cmyk, color, unsigned short, 16, 0) // cmyk_16_to_color
1309 GENERIC_COLOR_FUNC(cmyk, color)
1310 CMYK_TO_COLOR_FUNC(kcmy, color, unsigned char, 8, 1) // kcmy_8_to_color
1311 CMYK_TO_COLOR_FUNC(kcmy, color, unsigned short, 16, 1) // kcmy_16_to_color
1312 GENERIC_COLOR_FUNC(kcmy, color)
1313 CMYK_TO_COLOR_FUNC(cmyk, color_threshold, unsigned char, 8, 0) // cmyk_8_to_color_threshold
1314 CMYK_TO_COLOR_FUNC(cmyk, color_threshold, unsigned short, 16, 0) // cmyk_16_to_color_threshold
1315 GENERIC_COLOR_FUNC(cmyk, color_threshold)
1316 CMYK_TO_COLOR_FUNC(kcmy, color_threshold, unsigned char, 8, 1) // kcmy_8_to_color_threshold
1317 CMYK_TO_COLOR_FUNC(kcmy, color_threshold, unsigned short, 16, 1) // kcmy_16_to_color_threshold
1318 GENERIC_COLOR_FUNC(kcmy, color_threshold)
1319 CMYK_TO_COLOR_FUNC(cmyk, color_fast, unsigned char, 8, 0) // cmyk_8_to_color_fast
1320 CMYK_TO_COLOR_FUNC(cmyk, color_fast, unsigned short, 16, 0) // cmyk_16_to_color_fast
1321 GENERIC_COLOR_FUNC(cmyk, color_fast)
1322 CMYK_TO_COLOR_FUNC(kcmy, color_fast, unsigned char, 8, 1) // kcmy_8_to_color_fast
1323 CMYK_TO_COLOR_FUNC(kcmy, color_fast, unsigned short, 16, 1) // kcmy_16_to_color_fast
1324 GENERIC_COLOR_FUNC(kcmy, color_fast)
1325 CMYK_TO_COLOR_FUNC(cmyk, color_raw, unsigned char, 8, 0) // cmyk_8_to_color_raw
1326 CMYK_TO_COLOR_FUNC(cmyk, color_raw, unsigned short, 16, 0) // cmyk_16_to_color_raw
1327 GENERIC_COLOR_FUNC(cmyk, color_raw)
1328 CMYK_TO_COLOR_FUNC(kcmy, color_raw, unsigned char, 8, 1) // kcmy_8_to_color_raw
1329 CMYK_TO_COLOR_FUNC(kcmy, color_raw, unsigned short, 16, 1) // kcmy_16_to_color_raw
1330 GENERIC_COLOR_FUNC(kcmy, color_raw)
1331
1332 #define CMYK_TO_KCMY_FUNC(T, size) \
1333 CFUNC \
1334 cmyk_##size##_to_kcmy(const stp_vars_t *vars, \
1335 const unsigned char *in, \
1336 unsigned short *out) \
1337 { \
1338 int i; \
1339 unsigned retval = 0; \
1340 int j; \
1341 int nz[4]; \
1342 const T *s_in = (const T *) in; \
1343 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1344 const unsigned short *user; \
1345 const unsigned short *maps[4]; \
1346 \
1347 for (i = 0; i < 4; i++) \
1348 { \
1349 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
1350 maps[i] = stp_curve_cache_get_ushort_data(&(lut->channel_curves[i])); \
1351 } \
1352 stp_curve_resample(lut->user_color_correction.curve, 1 << size); \
1353 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1354 \
1355 memset(nz, 0, sizeof(nz)); \
1356 \
1357 for (i = 0; i < lut->image_width; i++, out += 4) \
1358 { \
1359 for (j = 0; j < 4; j++) \
1360 { \
1361 int outpos = (j + 1) & 3; \
1362 int inval = *s_in++; \
1363 nz[outpos] |= inval; \
1364 out[outpos] = maps[outpos][user[inval]]; \
1365 } \
1366 } \
1367 for (j = 0; j < 4; j++) \
1368 if (nz[j] == 0) \
1369 retval |= (1 << j); \
1370 return retval; \
1371 }
1372
1373 CMYK_TO_KCMY_FUNC(unsigned char, 8) // cmyk_8_to_kcmy
1374 CMYK_TO_KCMY_FUNC(unsigned short, 16) // cmyk_16_to_kcmy
1375 GENERIC_COLOR_FUNC(cmyk, kcmy)
1376
1377 #define KCMY_TO_KCMY_FUNC(T, size) \
1378 CFUNC \
1379 kcmy_##size##_to_kcmy(const stp_vars_t *vars, \
1380 const unsigned char *in, \
1381 unsigned short *out) \
1382 { \
1383 int i; \
1384 unsigned retval = 0; \
1385 int j; \
1386 int nz[4]; \
1387 const T *s_in = (const T *) in; \
1388 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1389 const unsigned short *user; \
1390 const unsigned short *maps[4]; \
1391 \
1392 for (i = 0; i < 4; i++) \
1393 { \
1394 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
1395 maps[i] = stp_curve_cache_get_ushort_data(&(lut->channel_curves[i])); \
1396 } \
1397 stp_curve_resample(lut->user_color_correction.curve, 1 << size); \
1398 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1399 \
1400 memset(nz, 0, sizeof(nz)); \
1401 \
1402 for (i = 0; i < lut->image_width; i++, out += 4) \
1403 { \
1404 for (j = 0; j < 4; j++) \
1405 { \
1406 int inval = *s_in++; \
1407 nz[j] |= inval; \
1408 out[j] = maps[j][user[inval]]; \
1409 } \
1410 } \
1411 for (j = 0; j < 4; j++) \
1412 if (nz[j] == 0) \
1413 retval |= (1 << j); \
1414 return retval; \
1415 }
1416
1417 KCMY_TO_KCMY_FUNC(unsigned char, 8) // kcmy_8_to_kcmy
1418 KCMY_TO_KCMY_FUNC(unsigned short, 16) // kcmy_16_to_kcmy
1419 GENERIC_COLOR_FUNC(kcmy, kcmy)
1420
1421
1422 #define GRAY_TO_GRAY_FUNC(T, bits) \
1423 CFUNC \
1424 gray_##bits##_to_gray(const stp_vars_t *vars, \
1425 const unsigned char *in, \
1426 unsigned short *out) \
1427 { \
1428 int i; \
1429 int i0 = -1; \
1430 int o0 = 0; \
1431 int nz = 0; \
1432 const T *s_in = (const T *) in; \
1433 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1434 int width = lut->image_width; \
1435 const unsigned short *composite; \
1436 const unsigned short *user; \
1437 \
1438 stp_curve_resample \
1439 (stp_curve_cache_get_curve(&(lut->channel_curves[CHANNEL_K])), 65536); \
1440 composite = \
1441 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_K])); \
1442 stp_curve_resample(lut->user_color_correction.curve, 1 << bits); \
1443 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1444 \
1445 memset(out, 0, width * sizeof(unsigned short)); \
1446 \
1447 for (i = 0; i < lut->image_width; i++) \
1448 { \
1449 if (i0 != s_in[0]) \
1450 { \
1451 i0 = s_in[0]; \
1452 o0 = composite[user[i0]]; \
1453 nz |= o0; \
1454 } \
1455 out[0] = o0; \
1456 s_in ++; \
1457 out ++; \
1458 } \
1459 return nz == 0; \
1460 }
1461
1462 GRAY_TO_GRAY_FUNC(unsigned char, 8) // gray_8_to_gray
1463 GRAY_TO_GRAY_FUNC(unsigned short, 16) // gray_16_to_gray
1464 GENERIC_COLOR_FUNC(gray, gray)
1465
1466 #define COLOR_TO_GRAY_FUNC(T, bits) \
1467 CFUNC \
1468 color_##bits##_to_gray(const stp_vars_t *vars, \
1469 const unsigned char *in, \
1470 unsigned short *out) \
1471 { \
1472 int i; \
1473 int i0 = -1; \
1474 int i1 = -1; \
1475 int i2 = -1; \
1476 int o0 = 0; \
1477 int nz = 0; \
1478 const T *s_in = (const T *) in; \
1479 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1480 int l_red = LUM_RED; \
1481 int l_green = LUM_GREEN; \
1482 int l_blue = LUM_BLUE; \
1483 const unsigned short *composite; \
1484 const unsigned short *user; \
1485 \
1486 stp_curve_resample \
1487 (stp_curve_cache_get_curve(&(lut->channel_curves[CHANNEL_K])), 65536); \
1488 composite = \
1489 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_K])); \
1490 stp_curve_resample(lut->user_color_correction.curve, 1 << bits); \
1491 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1492 \
1493 if (lut->input_color_description->color_model == COLOR_BLACK) \
1494 { \
1495 l_red = (100 - l_red) / 2; \
1496 l_green = (100 - l_green) / 2; \
1497 l_blue = (100 - l_blue) / 2; \
1498 } \
1499 \
1500 for (i = 0; i < lut->image_width; i++) \
1501 { \
1502 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2]) \
1503 { \
1504 i0 = s_in[0]; \
1505 i1 = s_in[1]; \
1506 i2 = s_in[2]; \
1507 o0 = \
1508 composite[user[(i0 * l_red + i1 * l_green + i2 * l_blue) / 100]]; \
1509 nz |= o0; \
1510 } \
1511 out[0] = o0; \
1512 s_in += 3; \
1513 out ++; \
1514 } \
1515 return nz == 0; \
1516 }
1517
1518 COLOR_TO_GRAY_FUNC(unsigned char, 8) // color_8_to_gray
1519 COLOR_TO_GRAY_FUNC(unsigned short, 16) // color_16_to_gray
1520 GENERIC_COLOR_FUNC(color, gray)
1521
1522
1523 #define CMYK_TO_GRAY_FUNC(T, bits) \
1524 CFUNC \
1525 cmyk_##bits##_to_gray(const stp_vars_t *vars, \
1526 const unsigned char *in, \
1527 unsigned short *out) \
1528 { \
1529 int i; \
1530 int i0 = -1; \
1531 int i1 = -1; \
1532 int i2 = -1; \
1533 int i3 = -4; \
1534 int o0 = 0; \
1535 int nz = 0; \
1536 const T *s_in = (const T *) in; \
1537 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1538 int l_red = LUM_RED; \
1539 int l_green = LUM_GREEN; \
1540 int l_blue = LUM_BLUE; \
1541 int l_white = 0; \
1542 const unsigned short *composite; \
1543 const unsigned short *user; \
1544 \
1545 stp_curve_resample \
1546 (stp_curve_cache_get_curve(&(lut->channel_curves[CHANNEL_K])), 65536); \
1547 composite = \
1548 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_K])); \
1549 stp_curve_resample(lut->user_color_correction.curve, 1 << bits); \
1550 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1551 \
1552 if (lut->input_color_description->color_model == COLOR_BLACK) \
1553 { \
1554 l_red = (100 - l_red) / 3; \
1555 l_green = (100 - l_green) / 3; \
1556 l_blue = (100 - l_blue) / 3; \
1557 l_white = (100 - l_white) / 3; \
1558 } \
1559 \
1560 for (i = 0; i < lut->image_width; i++) \
1561 { \
1562 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2] || i3 != s_in[3]) \
1563 { \
1564 i0 = s_in[0]; \
1565 i1 = s_in[1]; \
1566 i2 = s_in[2]; \
1567 i3 = s_in[3]; \
1568 o0 = composite[user[(i0 * l_red + i1 * l_green + \
1569 i2 * l_blue + i3 * l_white) / 100]]; \
1570 nz |= o0; \
1571 } \
1572 out[0] = o0; \
1573 s_in += 4; \
1574 out ++; \
1575 } \
1576 return nz ? 0 : 1; \
1577 }
1578
1579 CMYK_TO_GRAY_FUNC(unsigned char, 8) // cmyk_8_to_gray
1580 CMYK_TO_GRAY_FUNC(unsigned short, 16) // cmyk_16_to_gray
1581 GENERIC_COLOR_FUNC(cmyk, gray)
1582
1583 #define KCMY_TO_GRAY_FUNC(T, bits) \
1584 CFUNC \
1585 kcmy_##bits##_to_gray(const stp_vars_t *vars, \
1586 const unsigned char *in, \
1587 unsigned short *out) \
1588 { \
1589 int i; \
1590 int i0 = -1; \
1591 int i1 = -1; \
1592 int i2 = -1; \
1593 int i3 = -4; \
1594 int o0 = 0; \
1595 int nz = 0; \
1596 const T *s_in = (const T *) in; \
1597 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1598 int l_red = LUM_RED; \
1599 int l_green = LUM_GREEN; \
1600 int l_blue = LUM_BLUE; \
1601 int l_white = 0; \
1602 const unsigned short *composite; \
1603 const unsigned short *user; \
1604 \
1605 stp_curve_resample \
1606 (stp_curve_cache_get_curve(&(lut->channel_curves[CHANNEL_K])), 65536); \
1607 composite = \
1608 stp_curve_cache_get_ushort_data(&(lut->channel_curves[CHANNEL_K])); \
1609 stp_curve_resample(lut->user_color_correction.curve, 1 << bits); \
1610 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
1611 \
1612 if (lut->input_color_description->color_model == COLOR_BLACK) \
1613 { \
1614 l_red = (100 - l_red) / 3; \
1615 l_green = (100 - l_green) / 3; \
1616 l_blue = (100 - l_blue) / 3; \
1617 l_white = (100 - l_white) / 3; \
1618 } \
1619 \
1620 for (i = 0; i < lut->image_width; i++) \
1621 { \
1622 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2] || i3 != s_in[3]) \
1623 { \
1624 i0 = s_in[0]; \
1625 i1 = s_in[1]; \
1626 i2 = s_in[2]; \
1627 i3 = s_in[3]; \
1628 o0 = composite[user[(i0 * l_red + i1 * l_green + \
1629 i2 * l_blue + i3 * l_white) / 100]]; \
1630 nz |= o0; \
1631 } \
1632 out[0] = o0; \
1633 s_in += 4; \
1634 out ++; \
1635 } \
1636 return nz ? 0 : 1; \
1637 }
1638
1639 KCMY_TO_GRAY_FUNC(unsigned char, 8) // kcmy_8_to_gray
1640 KCMY_TO_GRAY_FUNC(unsigned short, 16) // kcmy_16_to_gray
1641 GENERIC_COLOR_FUNC(kcmy, gray)
1642
1643 #define GRAY_TO_GRAY_RAW_FUNC(T, bits) \
1644 CFUNC \
1645 gray_##bits##_to_gray_raw(const stp_vars_t *vars, \
1646 const unsigned char *in, \
1647 unsigned short *out) \
1648 { \
1649 int i; \
1650 int nz = 0; \
1651 const T *s_in = (const T *) in; \
1652 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1653 int width = lut->image_width; \
1654 unsigned mask = 0; \
1655 if (lut->invert_output) \
1656 mask = 0xffff; \
1657 \
1658 memset(out, 0, width * sizeof(unsigned short)); \
1659 \
1660 for (i = 0; i < lut->image_width; i++) \
1661 { \
1662 out[0] = (s_in[0] * (65535 / ((1 << bits) - 1))) ^ mask; \
1663 nz |= out[0]; \
1664 s_in ++; \
1665 out ++; \
1666 } \
1667 return nz == 0; \
1668 }
1669
1670 GRAY_TO_GRAY_RAW_FUNC(unsigned char, 8) // gray_8_to_gray_raw
1671 GRAY_TO_GRAY_RAW_FUNC(unsigned short, 16) // gray_16_to_gray_raw
1672 GENERIC_COLOR_FUNC(gray, gray_raw)
1673
1674 #define COLOR_TO_GRAY_RAW_FUNC(T, bits, invertable, name2) \
1675 CFUNC \
1676 color_##bits##_to_gray_##name2(const stp_vars_t *vars, \
1677 const unsigned char *in, \
1678 unsigned short *out) \
1679 { \
1680 int i; \
1681 int i0 = -1; \
1682 int i1 = -1; \
1683 int i2 = -1; \
1684 int o0 = 0; \
1685 int nz = 0; \
1686 const T *s_in = (const T *) in; \
1687 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1688 int l_red = LUM_RED; \
1689 int l_green = LUM_GREEN; \
1690 int l_blue = LUM_BLUE; \
1691 unsigned mask = 0; \
1692 if (lut->invert_output && invertable) \
1693 mask = 0xffff; \
1694 \
1695 if (lut->input_color_description->color_model == COLOR_BLACK) \
1696 { \
1697 l_red = (100 - l_red) / 2; \
1698 l_green = (100 - l_green) / 2; \
1699 l_blue = (100 - l_blue) / 2; \
1700 } \
1701 \
1702 for (i = 0; i < lut->image_width; i++) \
1703 { \
1704 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2]) \
1705 { \
1706 i0 = s_in[0]; \
1707 i1 = s_in[1]; \
1708 i2 = s_in[2]; \
1709 o0 = (i0 * (65535 / ((1 << bits) - 1)) * l_red + \
1710 i1 * (65535 / ((1 << bits) - 1)) * l_green + \
1711 i2 * (65535 / ((1 << bits) - 1)) * l_blue) / 100; \
1712 o0 ^= mask; \
1713 nz |= o0; \
1714 } \
1715 out[0] = o0; \
1716 s_in += 3; \
1717 out ++; \
1718 } \
1719 return nz == 0; \
1720 }
1721
1722 COLOR_TO_GRAY_RAW_FUNC(unsigned char, 8, 1, raw) // color_8_to_gray_raw
1723 COLOR_TO_GRAY_RAW_FUNC(unsigned short, 16, 1, raw) // color_16_to_gray_raw
1724 GENERIC_COLOR_FUNC(color, gray_raw)
1725 COLOR_TO_GRAY_RAW_FUNC(unsigned char, 8, 0, noninvert) // color_8_to_gray_noninvert
1726 COLOR_TO_GRAY_RAW_FUNC(unsigned short, 16, 0, noninvert) // color_16_to_gray_noninvert
1727 // GENERIC_COLOR_FUNC(color, gray_noninvert)
1728
1729
1730 #define CMYK_TO_GRAY_RAW_FUNC(T, bits, invertable, name2) \
1731 CFUNC \
1732 cmyk_##bits##_to_gray_##name2(const stp_vars_t *vars, \
1733 const unsigned char *in, \
1734 unsigned short *out) \
1735 { \
1736 int i; \
1737 int i0 = -1; \
1738 int i1 = -1; \
1739 int i2 = -1; \
1740 int i3 = -4; \
1741 int o0 = 0; \
1742 int nz = 0; \
1743 const T *s_in = (const T *) in; \
1744 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1745 int l_red = LUM_RED; \
1746 int l_green = LUM_GREEN; \
1747 int l_blue = LUM_BLUE; \
1748 int l_white = 0; \
1749 unsigned mask = 0; \
1750 if (lut->invert_output && invertable) \
1751 mask = 0xffff; \
1752 \
1753 if (lut->input_color_description->color_model == COLOR_BLACK) \
1754 { \
1755 l_red = (100 - l_red) / 3; \
1756 l_green = (100 - l_green) / 3; \
1757 l_blue = (100 - l_blue) / 3; \
1758 l_white = (100 - l_white) / 3; \
1759 } \
1760 \
1761 for (i = 0; i < lut->image_width; i++) \
1762 { \
1763 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2] || i3 != s_in[3]) \
1764 { \
1765 i0 = s_in[0]; \
1766 i1 = s_in[1]; \
1767 i2 = s_in[2]; \
1768 i3 = s_in[3]; \
1769 o0 = (i0 * (65535 / ((1 << bits) - 1)) * l_red + \
1770 i1 * (65535 / ((1 << bits) - 1)) * l_green + \
1771 i2 * (65535 / ((1 << bits) - 1)) * l_blue + \
1772 i3 * (65535 / ((1 << bits) - 1)) * l_white) / 100; \
1773 o0 ^= mask; \
1774 nz |= o0; \
1775 } \
1776 out[0] = o0; \
1777 s_in += 4; \
1778 out ++; \
1779 } \
1780 return nz ? 0 : 1; \
1781 }
1782
1783 CMYK_TO_GRAY_RAW_FUNC(unsigned char, 8, 1, raw) // cmyk_8_to_gray_raw
1784 CMYK_TO_GRAY_RAW_FUNC(unsigned short, 16, 1, raw) // cmyk_16_to_gray_raw
1785 GENERIC_COLOR_FUNC(cmyk, gray_raw)
1786 CMYK_TO_GRAY_RAW_FUNC(unsigned char, 8, 0, noninvert) // cmyk_8_to_gray_noninvert
1787 CMYK_TO_GRAY_RAW_FUNC(unsigned short, 16, 0, noninvert) // cmyk_16_to_gray_noninvert
1788 // GENERIC_COLOR_FUNC(cmyk, gray_noninvert)
1789
1790 #define KCMY_TO_GRAY_RAW_FUNC(T, bits, invertable, name2) \
1791 CFUNC \
1792 kcmy_##bits##_to_gray_##name2(const stp_vars_t *vars, \
1793 const unsigned char *in, \
1794 unsigned short *out) \
1795 { \
1796 int i; \
1797 int i0 = -1; \
1798 int i1 = -1; \
1799 int i2 = -1; \
1800 int i3 = -4; \
1801 int o0 = 0; \
1802 int nz = 0; \
1803 const T *s_in = (const T *) in; \
1804 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1805 int l_red = LUM_RED; \
1806 int l_green = LUM_GREEN; \
1807 int l_blue = LUM_BLUE; \
1808 int l_white = 0; \
1809 unsigned mask = 0; \
1810 if (lut->invert_output && invertable) \
1811 mask = 0xffff; \
1812 \
1813 if (lut->input_color_description->color_model == COLOR_BLACK) \
1814 { \
1815 l_red = (100 - l_red) / 3; \
1816 l_green = (100 - l_green) / 3; \
1817 l_blue = (100 - l_blue) / 3; \
1818 l_white = (100 - l_white) / 3; \
1819 } \
1820 \
1821 for (i = 0; i < lut->image_width; i++) \
1822 { \
1823 if (i0 != s_in[0] || i1 != s_in[1] || i2 != s_in[2] || i3 != s_in[3]) \
1824 { \
1825 i0 = s_in[0]; \
1826 i1 = s_in[1]; \
1827 i2 = s_in[2]; \
1828 i3 = s_in[3]; \
1829 o0 = (i0 * (65535 / ((1 << bits) - 1)) * l_white + \
1830 i1 * (65535 / ((1 << bits) - 1)) * l_red + \
1831 i2 * (65535 / ((1 << bits) - 1)) * l_green + \
1832 i3 * (65535 / ((1 << bits) - 1)) * l_blue) / 100; \
1833 o0 ^= mask; \
1834 nz |= o0; \
1835 } \
1836 out[0] = o0; \
1837 s_in += 4; \
1838 out ++; \
1839 } \
1840 return nz ? 0 : 1; \
1841 }
1842
1843 KCMY_TO_GRAY_RAW_FUNC(unsigned char, 8, 1, raw) // kcmy_8_to_gray_raw
1844 KCMY_TO_GRAY_RAW_FUNC(unsigned short, 16, 1, raw) // kcmy_16_to_gray_raw
1845 GENERIC_COLOR_FUNC(kcmy, gray_raw)
1846 KCMY_TO_GRAY_RAW_FUNC(unsigned char, 8, 0, noninvert) // kcmy_8_to_gray_noninvert
1847 KCMY_TO_GRAY_RAW_FUNC(unsigned short, 16, 0, noninvert) // kcmy_16_to_gray_noninvert
1848 // GENERIC_COLOR_FUNC(kcmy, gray_noninvert)
1849
1850 #define CMYK_TO_KCMY_RAW_FUNC(T, bits) \
1851 CFUNC \
1852 cmyk_##bits##_to_kcmy_raw(const stp_vars_t *vars, \
1853 const unsigned char *in, \
1854 unsigned short *out) \
1855 { \
1856 int i; \
1857 int j; \
1858 int nz[4]; \
1859 unsigned retval = 0; \
1860 const T *s_in = (const T *) in; \
1861 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1862 \
1863 memset(nz, 0, sizeof(nz)); \
1864 for (i = 0; i < lut->image_width; i++) \
1865 { \
1866 out[0] = s_in[3] * (65535 / ((1 << bits) - 1)); \
1867 out[1] = s_in[0] * (65535 / ((1 << bits) - 1)); \
1868 out[2] = s_in[1] * (65535 / ((1 << bits) - 1)); \
1869 out[3] = s_in[2] * (65535 / ((1 << bits) - 1)); \
1870 for (j = 0; j < 4; j++) \
1871 nz[j] |= out[j]; \
1872 s_in += 4; \
1873 out += 4; \
1874 } \
1875 for (j = 0; j < 4; j++) \
1876 if (nz[j] == 0) \
1877 retval |= (1 << j); \
1878 return retval; \
1879 }
1880
1881 CMYK_TO_KCMY_RAW_FUNC(unsigned char, 8) // cmyk_8_to_kcmy_raw
1882 CMYK_TO_KCMY_RAW_FUNC(unsigned short, 16) // cmyk_16_to_kcmy_raw
1883 GENERIC_COLOR_FUNC(cmyk, kcmy_raw)
1884
1885 #define KCMY_TO_KCMY_RAW_FUNC(T, bits) \
1886 CFUNC \
1887 kcmy_##bits##_to_kcmy_raw(const stp_vars_t *vars, \
1888 const unsigned char *in, \
1889 unsigned short *out) \
1890 { \
1891 int i; \
1892 int j; \
1893 int nz[4]; \
1894 unsigned retval = 0; \
1895 const T *s_in = (const T *) in; \
1896 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1897 \
1898 memset(nz, 0, sizeof(nz)); \
1899 for (i = 0; i < lut->image_width; i++) \
1900 { \
1901 for (j = 0; j < 4; j++) \
1902 { \
1903 out[j] = s_in[j] * (65535 / ((1 << bits) - 1)); \
1904 nz[j] |= out[j]; \
1905 } \
1906 s_in += 4; \
1907 out += 4; \
1908 } \
1909 for (j = 0; j < 4; j++) \
1910 if (nz[j] == 0) \
1911 retval |= (1 << j); \
1912 return retval; \
1913 }
1914
1915 KCMY_TO_KCMY_RAW_FUNC(unsigned char, 8) // kcmy_8_to_kcmy_raw
1916 KCMY_TO_KCMY_RAW_FUNC(unsigned short, 16) // kcmy_16_to_kcmy_raw
1917 GENERIC_COLOR_FUNC(kcmy, kcmy_raw)
1918
1919 #define DESATURATED_FUNC(name, name2, bits) \
1920 CFUNC \
1921 name##_##bits##_to_##name2##_desaturated(const stp_vars_t *vars, \
1922 const unsigned char *in, \
1923 unsigned short *out) \
1924 { \
1925 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1926 size_t real_steps = lut->steps; \
1927 unsigned status; \
1928 if (!lut->gray_tmp) \
1929 lut->gray_tmp = stp_malloc(2 * lut->image_width); \
1930 name##_##bits##_to_gray_noninvert(vars, in, lut->gray_tmp); \
1931 lut->steps = 65536; \
1932 status = gray_16_to_##name2(vars, (unsigned char *) lut->gray_tmp, out); \
1933 lut->steps = real_steps; \
1934 return status; \
1935 }
1936
1937 DESATURATED_FUNC(color, color, 8) // color_8_to_color_desaturated
1938 DESATURATED_FUNC(color, color, 16) // color_16_to_color_desaturated
1939 GENERIC_COLOR_FUNC(color, color_desaturated)
1940 DESATURATED_FUNC(color, kcmy, 8) // color_8_to_kcmy_desaturated
1941 DESATURATED_FUNC(color, kcmy, 16) // color_8_to_kcmy_desaturated
1942 GENERIC_COLOR_FUNC(color, kcmy_desaturated)
1943
1944 DESATURATED_FUNC(cmyk, color, 8) // cmyk_8_to_color_desaturated
1945 DESATURATED_FUNC(cmyk, color, 16) // cmyk_16_to_color_desaturated
1946 GENERIC_COLOR_FUNC(cmyk, color_desaturated)
1947 DESATURATED_FUNC(cmyk, kcmy, 8) // cmyk_8_to_kcmy_desaturated
1948 DESATURATED_FUNC(cmyk, kcmy, 16) // cmyk_16_to_kcmy_desaturated
1949 GENERIC_COLOR_FUNC(cmyk, kcmy_desaturated)
1950
1951 DESATURATED_FUNC(kcmy, color, 8) // kcmy_8_to_color_desaturated
1952 DESATURATED_FUNC(kcmy, color, 16) // kcmy_16_to_kcmy_desaturated
1953 GENERIC_COLOR_FUNC(kcmy, color_desaturated)
1954 DESATURATED_FUNC(kcmy, kcmy, 8) // kcmy_8_to_color_desaturated
1955 DESATURATED_FUNC(kcmy, kcmy, 16) // kcmy_16_to_kcmy_desaturated
1956 GENERIC_COLOR_FUNC(kcmy, kcmy_desaturated)
1957
1958 #define CMYK_DISPATCH(name) \
1959 CFUNC \
1960 CMYK_to_##name(const stp_vars_t *vars, const unsigned char *in, \
1961 unsigned short *out) \
1962 { \
1963 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1964 if (lut->input_color_description->color_id == COLOR_ID_CMYK) \
1965 return cmyk_to_##name(vars, in, out); \
1966 else if (lut->input_color_description->color_id == COLOR_ID_KCMY) \
1967 return kcmy_to_##name(vars, in, out); \
1968 else \
1969 { \
1970 stp_eprintf(vars, "Bad dispatch to CMYK_to_%s: %d\n", #name, \
1971 lut->input_color_description->color_id); \
1972 return 0; \
1973 } \
1974 }
1975
1976 CMYK_DISPATCH(color) /* CMYK_to_color */
1977 CMYK_DISPATCH(color_raw) /* CMYK_to_color_raw */
1978 CMYK_DISPATCH(color_fast) /* CMYK_to_color_fast */
1979 CMYK_DISPATCH(color_threshold) /* CMYK_to_color_threshold */
1980 CMYK_DISPATCH(color_desaturated) /* CMYK_to_color_desaturated */
1981 CMYK_DISPATCH(kcmy) /* CMYK_to_kcmy */
1982 CMYK_DISPATCH(kcmy_raw) /* CMYK_to_kcmy_raw */
1983 CMYK_DISPATCH(kcmy_threshold) /* CMYK_to_kcmy_threshold */
1984 CMYK_DISPATCH(kcmy_desaturated) /* CMYK_to_kcmy_desaturated */
1985 CMYK_DISPATCH(gray) /* CMYK_to_gray */
1986 CMYK_DISPATCH(gray_raw) /* CMYK_to_gray_raw */
1987 CMYK_DISPATCH(gray_threshold) /* CMYK_to_gray_threshold */
1988
1989 #define RAW_TO_RAW_THRESHOLD_FUNC(T, name) \
1990 CFUNC \
1991 name##_to_raw_threshold(const stp_vars_t *vars, \
1992 const unsigned char *in, \
1993 unsigned short *out) \
1994 { \
1995 int i; \
1996 int j; \
1997 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
1998 unsigned nz[STP_CHANNEL_LIMIT]; \
1999 unsigned z = (1 << lut->out_channels) - 1; \
2000 const T *s_in = (const T *) in; \
2001 unsigned desired_high_bit = 0; \
2002 unsigned high_bit = 1 << ((sizeof(T) * 8) - 1); \
2003 int width = lut->image_width; \
2004 memset(out, 0, width * lut->out_channels * sizeof(unsigned short)); \
2005 if (!lut->invert_output) \
2006 desired_high_bit = high_bit; \
2007 for (i = 0; i < lut->out_channels; i++) \
2008 nz[i] = z & ~(1 << i); \
2009 \
2010 for (i = 0; i < width; i++) \
2011 { \
2012 for (j = 0; j < lut->out_channels; j++) \
2013 { \
2014 if ((*s_in++ & high_bit) == desired_high_bit) \
2015 { \
2016 z &= nz[j]; \
2017 *out = 65535; \
2018 } \
2019 out++; \
2020 } \
2021 } \
2022 return z; \
2023 }
2024
2025 RAW_TO_RAW_THRESHOLD_FUNC(unsigned char, raw_8) // raw_8_to_raw_threshold
2026 RAW_TO_RAW_THRESHOLD_FUNC(unsigned short, raw_16) // raw_16_to_raw_threshold
2027 GENERIC_COLOR_FUNC(raw, raw_threshold)
2028
2029 #define RAW_TO_RAW_FUNC(T, size) \
2030 CFUNC \
2031 raw_##size##_to_raw(const stp_vars_t *vars, \
2032 const unsigned char *in, \
2033 unsigned short *out) \
2034 { \
2035 int i; \
2036 unsigned retval = 0; \
2037 int j; \
2038 int nz[STP_CHANNEL_LIMIT]; \
2039 const T *s_in = (const T *) in; \
2040 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
2041 const unsigned short *maps[STP_CHANNEL_LIMIT]; \
2042 const unsigned short *user; \
2043 \
2044 for (i = 0; i < lut->out_channels; i++) \
2045 { \
2046 stp_curve_resample(lut->channel_curves[i].curve, 65536); \
2047 maps[i] = stp_curve_cache_get_ushort_data(&(lut->channel_curves[i])); \
2048 } \
2049 stp_curve_resample(lut->user_color_correction.curve, 1 << size); \
2050 user = stp_curve_cache_get_ushort_data(&(lut->user_color_correction)); \
2051 \
2052 memset(nz, 0, sizeof(nz)); \
2053 \
2054 for (i = 0; i < lut->image_width; i++, out += lut->out_channels) \
2055 { \
2056 for (j = 0; j < lut->out_channels; j++) \
2057 { \
2058 int inval = *s_in++; \
2059 nz[j] |= inval; \
2060 out[j] = maps[j][user[inval]]; \
2061 } \
2062 } \
2063 for (j = 0; j < lut->out_channels; j++) \
2064 if (nz[j] == 0) \
2065 retval |= (1 << j); \
2066 return retval; \
2067 }
2068
2069 RAW_TO_RAW_FUNC(unsigned char, 8) // raw_8_to_raw
2070 RAW_TO_RAW_FUNC(unsigned short, 16) // raw_8_to_raw
2071 GENERIC_COLOR_FUNC(raw, raw)
2072
2073
2074 #define RAW_TO_RAW_RAW_FUNC(T, bits) \
2075 CFUNC \
2076 raw_##bits##_to_raw_raw(const stp_vars_t *vars, \
2077 const unsigned char *in, \
2078 unsigned short *out) \
2079 { \
2080 int i; \
2081 int j; \
2082 int nz[STP_CHANNEL_LIMIT]; \
2083 unsigned retval = 0; \
2084 const T *s_in = (const T *) in; \
2085 lut_t *lut = (lut_t *)(stp_get_component_data(vars, "Color")); \
2086 int colors = lut->in_channels; \
2087 \
2088 memset(nz, 0, sizeof(nz)); \
2089 for (i = 0; i < lut->image_width; i++) \
2090 { \
2091 for (j = 0; j < colors; j++) \
2092 { \
2093 nz[j] |= s_in[j]; \
2094 out[j] = s_in[j] * (65535 / ((1 << bits) - 1)); \
2095 } \
2096 s_in += colors; \
2097 out += colors; \
2098 } \
2099 for (j = 0; j < colors; j++) \
2100 if (nz[j] == 0) \
2101 retval |= (1 << j); \
2102 return retval; \
2103 }
2104
2105 RAW_TO_RAW_RAW_FUNC(unsigned char, 8) // raw_8_to_raw_raw
2106 RAW_TO_RAW_RAW_FUNC(unsigned short, 16) // raw_16_to_raw_raw
2107 GENERIC_COLOR_FUNC(raw, raw_raw)
2108
2109
2110 #define CONVERSION_FUNCTION_WITH_FAST(from, to, from2) \
2111 CFUNC \
2112 generic_##from##_to_##to(const stp_vars_t *v, \
2113 const unsigned char *in, \
2114 unsigned short *out) \
2115 { \
2116 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color")); \
2117 switch (lut->color_correction->correction) \
2118 { \
2119 case COLOR_CORRECTION_UNCORRECTED: \
2120 stp_dprintf(STP_DBG_COLORFUNC, v, \
2121 "Colorfunc: %s_to_%s_fast\n", #from2, #to); \
2122 return from2##_to_##to##_fast(v, in, out); \
2123 case COLOR_CORRECTION_ACCURATE: \
2124 case COLOR_CORRECTION_BRIGHT: \
2125 case COLOR_CORRECTION_HUE: \
2126 stp_dprintf(STP_DBG_COLORFUNC, v, \
2127 "Colorfunc: %s_to_%s\n", #from2, #to); \
2128 return from2##_to_##to(v, in, out); \
2129 case COLOR_CORRECTION_DESATURATED: \
2130 stp_dprintf(STP_DBG_COLORFUNC, v, \
2131 "Colorfunc: %s_to_%s_desaturated\n", #from2, #to); \
2132 return from2##_to_##to##_desaturated(v, in, out); \
2133 case COLOR_CORRECTION_THRESHOLD: \
2134 case COLOR_CORRECTION_PREDITHERED: \
2135 stp_dprintf(STP_DBG_COLORFUNC, v, \
2136 "Colorfunc: %s_to_%s_threshold\n", #from2, #to); \
2137 return from2##_to_##to##_threshold(v, in, out); \
2138 case COLOR_CORRECTION_DENSITY: \
2139 case COLOR_CORRECTION_RAW: \
2140 stp_dprintf(STP_DBG_COLORFUNC, v, \
2141 "Colorfunc: %s_to_%s_raw\n", #from2, #to); \
2142 return from2##_to_##to##_raw(v, in, out); \
2143 default: \
2144 return (unsigned) -1; \
2145 } \
2146 }
2147
2148 #define CONVERSION_FUNCTION_WITHOUT_FAST(from, to, from2) \
2149 CFUNC \
2150 generic_##from##_to_##to(const stp_vars_t *v, \
2151 const unsigned char *in, \
2152 unsigned short *out) \
2153 { \
2154 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color")); \
2155 switch (lut->color_correction->correction) \
2156 { \
2157 case COLOR_CORRECTION_UNCORRECTED: \
2158 case COLOR_CORRECTION_ACCURATE: \
2159 case COLOR_CORRECTION_BRIGHT: \
2160 case COLOR_CORRECTION_HUE: \
2161 stp_dprintf(STP_DBG_COLORFUNC, v, \
2162 "Colorfunc: %s_to_%s\n", #from2, #to); \
2163 return from2##_to_##to(v, in, out); \
2164 case COLOR_CORRECTION_DESATURATED: \
2165 stp_dprintf(STP_DBG_COLORFUNC, v, \
2166 "Colorfunc: %s_to_%s_desaturated\n", #from2, #to); \
2167 return from2##_to_##to##_desaturated(v, in, out); \
2168 case COLOR_CORRECTION_THRESHOLD: \
2169 case COLOR_CORRECTION_PREDITHERED: \
2170 stp_dprintf(STP_DBG_COLORFUNC, v, \
2171 "Colorfunc: %s_to_%s_threshold\n", #from2, #to); \
2172 return from2##_to_##to##_threshold(v, in, out); \
2173 case COLOR_CORRECTION_DENSITY: \
2174 case COLOR_CORRECTION_RAW: \
2175 stp_dprintf(STP_DBG_COLORFUNC, v, \
2176 "Colorfunc: %s_to_%s_raw\n", #from2, #to); \
2177 return from2##_to_##to##_raw(v, in, out); \
2178 default: \
2179 return (unsigned) -1; \
2180 } \
2181 }
2182
2183 #define CONVERSION_FUNCTION_WITHOUT_DESATURATED(from, to, from2) \
2184 CFUNC \
2185 generic_##from##_to_##to(const stp_vars_t *v, \
2186 const unsigned char *in, \
2187 unsigned short *out) \
2188 { \
2189 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color")); \
2190 switch (lut->color_correction->correction) \
2191 { \
2192 case COLOR_CORRECTION_UNCORRECTED: \
2193 case COLOR_CORRECTION_ACCURATE: \
2194 case COLOR_CORRECTION_BRIGHT: \
2195 case COLOR_CORRECTION_HUE: \
2196 case COLOR_CORRECTION_DESATURATED: \
2197 stp_dprintf(STP_DBG_COLORFUNC, v, \
2198 "Colorfunc: %s_to_%s\n", #from2, #to); \
2199 return from2##_to_##to(v, in, out); \
2200 case COLOR_CORRECTION_THRESHOLD: \
2201 case COLOR_CORRECTION_PREDITHERED: \
2202 stp_dprintf(STP_DBG_COLORFUNC, v, \
2203 "Colorfunc: %s_to_%s_threshold\n", #from2, #to); \
2204 return from2##_to_##to##_threshold(v, in, out); \
2205 case COLOR_CORRECTION_DENSITY: \
2206 case COLOR_CORRECTION_RAW: \
2207 stp_dprintf(STP_DBG_COLORFUNC, v, \
2208 "Colorfunc: %s_to_%s_raw\n", #from2, #to); \
2209 return from2##_to_##to##_raw(v, in, out); \
2210 default: \
2211 return (unsigned) -1; \
2212 } \
2213 }
2214
2215 CONVERSION_FUNCTION_WITH_FAST(cmyk, color, CMYK) // generic_cmyk_to_color
2216 CONVERSION_FUNCTION_WITH_FAST(color, color, color) // generic_color_to_color
2217 CONVERSION_FUNCTION_WITH_FAST(color, kcmy, color) // generic_color_to_kcmy
2218 CONVERSION_FUNCTION_WITHOUT_FAST(cmyk, kcmy, CMYK) // generic_cmyk_to_kcmy
2219 CONVERSION_FUNCTION_WITHOUT_DESATURATED(cmyk, gray, CMYK) // generic_cmyk_to_gray
2220 CONVERSION_FUNCTION_WITHOUT_DESATURATED(color, gray, color) // generic_color_to_gray
2221 CONVERSION_FUNCTION_WITHOUT_DESATURATED(gray, gray, gray) // generic_gray_to_gray
2222 CONVERSION_FUNCTION_WITHOUT_DESATURATED(gray, color, gray) // generic_gray_to_color
2223 CONVERSION_FUNCTION_WITHOUT_DESATURATED(gray, kcmy, gray) // generic_gray_to_kcmy
2224
2225 unsigned
2226 stpi_color_convert_to_gray(const stp_vars_t *v,
2227 const unsigned char *in,
2228 unsigned short *out)
2229 {
2230 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color"));
2231 switch (lut->input_color_description->color_id)
2232 {
2233 case COLOR_ID_GRAY:
2234 case COLOR_ID_WHITE:
2235 return generic_gray_to_gray(v, in, out);
2236 case COLOR_ID_RGB:
2237 case COLOR_ID_CMY:
2238 return generic_color_to_gray(v, in, out);
2239 case COLOR_ID_CMYK:
2240 case COLOR_ID_KCMY:
2241 return generic_cmyk_to_gray(v, in, out);
2242 default:
2243 return (unsigned) -1;
2244 }
2245 }
2246
2247 unsigned
2248 stpi_color_convert_to_color(const stp_vars_t *v,
2249 const unsigned char *in,
2250 unsigned short *out)
2251 {
2252 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color"));
2253 switch (lut->input_color_description->color_id)
2254 {
2255 case COLOR_ID_GRAY:
2256 case COLOR_ID_WHITE:
2257 return generic_gray_to_color(v, in, out);
2258 case COLOR_ID_RGB:
2259 case COLOR_ID_CMY:
2260 return generic_color_to_color(v, in, out);
2261 case COLOR_ID_CMYK:
2262 case COLOR_ID_KCMY:
2263 return generic_cmyk_to_color(v, in, out);
2264 default:
2265 return (unsigned) -1;
2266 }
2267 }
2268
2269 unsigned
2270 stpi_color_convert_to_kcmy(const stp_vars_t *v,
2271 const unsigned char *in,
2272 unsigned short *out)
2273 {
2274 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color"));
2275 switch (lut->input_color_description->color_id)
2276 {
2277 case COLOR_ID_GRAY:
2278 case COLOR_ID_WHITE:
2279 return generic_gray_to_kcmy(v, in, out);
2280 case COLOR_ID_RGB:
2281 case COLOR_ID_CMY:
2282 return generic_color_to_kcmy(v, in, out);
2283 case COLOR_ID_CMYK:
2284 case COLOR_ID_KCMY:
2285 return generic_cmyk_to_kcmy(v, in, out);
2286 default:
2287 return (unsigned) -1;
2288 }
2289 }
2290
2291 unsigned
2292 stpi_color_convert_raw(const stp_vars_t *v,
2293 const unsigned char *in,
2294 unsigned short *out)
2295 {
2296 lut_t *lut = (lut_t *)(stp_get_component_data(v, "Color"));
2297 switch (lut->color_correction->correction)
2298 {
2299 case COLOR_CORRECTION_THRESHOLD:
2300 case COLOR_CORRECTION_PREDITHERED:
2301 stp_dprintf(STP_DBG_COLORFUNC, v, "Colorfunc: raw_to_raw_threshold\n");
2302 return raw_to_raw_threshold(v, in, out);
2303 case COLOR_CORRECTION_UNCORRECTED:
2304 case COLOR_CORRECTION_BRIGHT:
2305 case COLOR_CORRECTION_HUE:
2306 case COLOR_CORRECTION_ACCURATE:
2307 case COLOR_CORRECTION_DESATURATED:
2308 stp_dprintf(STP_DBG_COLORFUNC, v, "Colorfunc: raw_to_raw_desaturated\n");
2309 return raw_to_raw(v, in, out);
2310 case COLOR_CORRECTION_RAW:
2311 case COLOR_CORRECTION_DEFAULT:
2312 case COLOR_CORRECTION_DENSITY:
2313 stp_dprintf(STP_DBG_COLORFUNC, v, "Colorfunc: raw_to_raw_raw\n");
2314 return raw_to_raw_raw(v, in, out);
2315 default:
2316 return (unsigned) -1;
2317 }
2318 }