2 * "$Id: image-colorspace.c 6649 2007-07-11 21:46:42Z mike $"
4 * Colorspace conversions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2006 by Easy Software Products.
9 * The color saturation/hue matrix stuff is provided thanks to Mr. Paul
10 * Haeberli at "http://www.sgi.com/grafica/matrix/index.html".
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * cupsImageCMYKToBlack() - Convert CMYK data to black.
23 * cupsImageCMYKToCMY() - Convert CMYK colors to CMY.
24 * cupsImageCMYKToCMYK() - Convert CMYK colors to CMYK.
25 * cupsImageCMYKToRGB() - Convert CMYK colors to device-dependent
27 * cupsImageCMYKToWhite() - Convert CMYK colors to luminance.
28 * cupsImageLut() - Adjust all pixel values with the given
30 * cupsImageRGBAdjust() - Adjust the hue and saturation of the
32 * cupsImageRGBToBlack() - Convert RGB data to black.
33 * cupsImageRGBToCMY() - Convert RGB colors to CMY.
34 * cupsImageRGBToCMYK() - Convert RGB colors to CMYK.
35 * cupsImageRGBToRGB() - Convert RGB colors to device-dependent
37 * cupsImageRGBToWhite() - Convert RGB colors to luminance.
38 * cupsImageSetProfile() - Set the device color profile.
39 * cupsImageSetRasterColorSpace() - Set the destination colorspace.
40 * cupsImageWhiteToBlack() - Convert luminance colors to black.
41 * cupsImageWhiteToCMY() - Convert luminance colors to CMY.
42 * cupsImageWhiteToCMYK() - Convert luminance colors to CMYK.
43 * cupsImageWhiteToRGB() - Convert luminance data to RGB.
44 * cupsImageWhiteToWhite() - Convert luminance colors to device-
45 * dependent luminance.
46 * cielab() - Map CIE Lab transformation...
47 * huerotate() - Rotate the hue, maintaining luminance.
48 * ident() - Make an identity matrix.
49 * mult() - Multiply two matrices.
50 * rgb_to_lab() - Convert an RGB color to CIE Lab.
51 * rgb_to_xyz() - Convert an RGB color to CIE XYZ.
52 * saturate() - Make a saturation matrix.
53 * xform() - Transform a 3D point using a matrix...
54 * xrotate() - Rotate about the x (red) axis...
55 * yrotate() - Rotate about the y (green) axis...
56 * zrotate() - Rotate about the z (blue) axis...
57 * zshear() - Shear z using x and y...
61 * Include necessary headers...
64 #include "image-private.h"
68 * Define some math constants that are required...
72 # define M_PI 3.14159265358979323846
76 # define M_SQRT2 1.41421356237309504880
80 # define M_SQRT1_2 0.70710678118654752440
81 #endif /* !M_SQRT1_2 */
84 * CIE XYZ whitepoint...
87 #define D65_X (0.412453 + 0.357580 + 0.180423)
88 #define D65_Y (0.212671 + 0.715160 + 0.072169)
89 #define D65_Z (0.019334 + 0.119193 + 0.950227)
93 * Lookup table structure...
96 typedef int cups_clut_t
[3][256];
103 static int cupsImageHaveProfile
= 0;
104 /* Do we have a color profile? */
105 static int *cupsImageDensity
;
106 /* Ink/marker density LUT */
107 static cups_clut_t
*cupsImageMatrix
;
108 /* Color transform matrix LUT */
109 static cups_cspace_t cupsImageColorSpace
= CUPS_CSPACE_RGB
;
110 /* Destination colorspace */
117 static float cielab(float x
, float xn
);
118 static void huerotate(float [3][3], float);
119 static void ident(float [3][3]);
120 static void mult(float [3][3], float [3][3], float [3][3]);
121 static void rgb_to_lab(cups_ib_t
*val
);
122 static void rgb_to_xyz(cups_ib_t
*val
);
123 static void saturate(float [3][3], float);
124 static void xform(float [3][3], float, float, float, float *, float *, float *);
125 static void xrotate(float [3][3], float, float);
126 static void yrotate(float [3][3], float, float);
127 static void zrotate(float [3][3], float, float);
128 static void zshear(float [3][3], float, float);
132 * 'cupsImageCMYKToBlack()' - Convert CMYK data to black.
136 cupsImageCMYKToBlack(
137 const cups_ib_t
*in
, /* I - Input pixels */
138 cups_ib_t
*out
, /* I - Output pixels */
139 int count
) /* I - Number of pixels */
141 int k
; /* Black value */
144 if (cupsImageHaveProfile
)
147 k
= (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 + in
[3];
150 *out
++ = cupsImageDensity
[k
];
152 *out
++ = cupsImageDensity
[255];
160 k
= (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 + in
[3];
174 * 'cupsImageCMYKToCMY()' - Convert CMYK colors to CMY.
179 const cups_ib_t
*in
, /* I - Input pixels */
180 cups_ib_t
*out
, /* I - Output pixels */
181 int count
) /* I - Number of pixels */
183 int c
, m
, y
, k
; /* CMYK values */
184 int cc
, cm
, cy
; /* Calibrated CMY values */
187 if (cupsImageHaveProfile
)
195 cc
= cupsImageMatrix
[0][0][c
] +
196 cupsImageMatrix
[0][1][m
] +
197 cupsImageMatrix
[0][2][y
] + k
;
198 cm
= cupsImageMatrix
[1][0][c
] +
199 cupsImageMatrix
[1][1][m
] +
200 cupsImageMatrix
[1][2][y
] + k
;
201 cy
= cupsImageMatrix
[2][0][c
] +
202 cupsImageMatrix
[2][1][m
] +
203 cupsImageMatrix
[2][2][y
] + k
;
208 *out
++ = cupsImageDensity
[255];
210 *out
++ = cupsImageDensity
[cc
];
215 *out
++ = cupsImageDensity
[255];
217 *out
++ = cupsImageDensity
[cm
];
222 *out
++ = cupsImageDensity
[255];
224 *out
++ = cupsImageDensity
[cy
];
261 * 'cupsImageCMYKToCMYK()' - Convert CMYK colors to CMYK.
266 const cups_ib_t
*in
, /* I - Input pixels */
267 cups_ib_t
*out
, /* I - Output pixels */
268 int count
) /* I - Number of pixels */
270 int c
, m
, y
, k
; /* CMYK values */
271 int cc
, cm
, cy
; /* Calibrated CMY values */
274 if (cupsImageHaveProfile
)
282 cc
= (cupsImageMatrix
[0][0][c
] +
283 cupsImageMatrix
[0][1][m
] +
284 cupsImageMatrix
[0][2][y
]);
285 cm
= (cupsImageMatrix
[1][0][c
] +
286 cupsImageMatrix
[1][1][m
] +
287 cupsImageMatrix
[1][2][y
]);
288 cy
= (cupsImageMatrix
[2][0][c
] +
289 cupsImageMatrix
[2][1][m
] +
290 cupsImageMatrix
[2][2][y
]);
295 *out
++ = cupsImageDensity
[255];
297 *out
++ = cupsImageDensity
[cc
];
302 *out
++ = cupsImageDensity
[255];
304 *out
++ = cupsImageDensity
[cm
];
309 *out
++ = cupsImageDensity
[255];
311 *out
++ = cupsImageDensity
[cy
];
313 *out
++ = cupsImageDensity
[k
];
333 * 'cupsImageCMYKToRGB()' - Convert CMYK colors to device-dependent RGB.
338 const cups_ib_t
*in
, /* I - Input pixels */
339 cups_ib_t
*out
, /* I - Output pixels */
340 int count
) /* I - Number of pixels */
342 int c
, m
, y
, k
; /* CMYK values */
343 int cr
, cg
, cb
; /* Calibrated RGB values */
346 if (cupsImageHaveProfile
)
355 cr
= cupsImageMatrix
[0][0][c
] +
356 cupsImageMatrix
[0][1][m
] +
357 cupsImageMatrix
[0][2][y
] + k
;
358 cg
= cupsImageMatrix
[1][0][c
] +
359 cupsImageMatrix
[1][1][m
] +
360 cupsImageMatrix
[1][2][y
] + k
;
361 cb
= cupsImageMatrix
[2][0][c
] +
362 cupsImageMatrix
[2][1][m
] +
363 cupsImageMatrix
[2][2][y
] + k
;
368 *out
++ = 255 - cupsImageDensity
[255];
370 *out
++ = 255 - cupsImageDensity
[cr
];
375 *out
++ = 255 - cupsImageDensity
[255];
377 *out
++ = 255 - cupsImageDensity
[cg
];
382 *out
++ = 255 - cupsImageDensity
[255];
384 *out
++ = 255 - cupsImageDensity
[cb
];
417 if (cupsImageColorSpace
== CUPS_CSPACE_CIELab
||
418 cupsImageColorSpace
>= CUPS_CSPACE_ICC1
)
420 else if (cupsImageColorSpace
== CUPS_CSPACE_CIEXYZ
)
430 * 'cupsImageCMYKToWhite()' - Convert CMYK colors to luminance.
434 cupsImageCMYKToWhite(
435 const cups_ib_t
*in
, /* I - Input pixels */
436 cups_ib_t
*out
, /* I - Output pixels */
437 int count
) /* I - Number of pixels */
439 int w
; /* White value */
442 if (cupsImageHaveProfile
)
446 w
= 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 - in
[3];
449 *out
++ = cupsImageDensity
[w
];
451 *out
++ = cupsImageDensity
[0];
461 w
= 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 - in
[3];
476 * 'cupsImageLut()' - Adjust all pixel values with the given LUT.
480 cupsImageLut(cups_ib_t
*pixels
, /* IO - Input/output pixels */
481 int count
, /* I - Number of pixels/bytes to adjust */
482 const cups_ib_t
*lut
) /* I - Lookup table */
486 *pixels
= lut
[*pixels
];
494 * 'cupsImageRGBAdjust()' - Adjust the hue and saturation of the given RGB colors.
498 cupsImageRGBAdjust(cups_ib_t
*pixels
, /* IO - Input/output pixels */
499 int count
, /* I - Number of pixels to adjust */
500 int saturation
,/* I - Color saturation (%) */
501 int hue
) /* I - Color hue (degrees) */
503 int i
, j
, k
; /* Looping vars */
504 float mat
[3][3]; /* Color adjustment matrix */
505 static int last_sat
= 100, /* Last saturation used */
506 last_hue
= 0; /* Last hue used */
507 static cups_clut_t
*lut
= NULL
; /* Lookup table for matrix */
510 if (saturation
!= last_sat
||
514 * Build the color adjustment matrix...
518 saturate(mat
, saturation
* 0.01);
519 huerotate(mat
, (float)hue
);
522 * Allocate memory for the lookup table...
526 lut
= calloc(3, sizeof(cups_clut_t
));
532 * Convert the matrix into a 3x3 array of lookup tables...
535 for (i
= 0; i
< 3; i
++)
536 for (j
= 0; j
< 3; j
++)
537 for (k
= 0; k
< 256; k
++)
538 lut
[i
][j
][k
] = mat
[i
][j
] * k
+ 0.5;
541 * Save the saturation and hue to compare later...
544 last_sat
= saturation
;
549 * Adjust each pixel in the given buffer.
554 i
= lut
[0][0][pixels
[0]] +
555 lut
[1][0][pixels
[1]] +
556 lut
[2][0][pixels
[2]];
564 i
= lut
[0][1][pixels
[0]] +
565 lut
[1][1][pixels
[1]] +
566 lut
[2][1][pixels
[2]];
574 i
= lut
[0][2][pixels
[0]] +
575 lut
[1][2][pixels
[1]] +
576 lut
[2][2][pixels
[2]];
591 * 'cupsImageRGBToBlack()' - Convert RGB data to black.
596 const cups_ib_t
*in
, /* I - Input pixels */
597 cups_ib_t
*out
, /* I - Output pixels */
598 int count
) /* I - Number of pixels */
600 if (cupsImageHaveProfile
)
603 *out
++ = cupsImageDensity
[255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100];
610 *out
++ = 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100;
618 * 'cupsImageRGBToCMY()' - Convert RGB colors to CMY.
623 const cups_ib_t
*in
, /* I - Input pixels */
624 cups_ib_t
*out
, /* I - Output pixels */
625 int count
) /* I - Number of pixels */
627 int c
, m
, y
, k
; /* CMYK values */
628 int cc
, cm
, cy
; /* Calibrated CMY values */
631 if (cupsImageHaveProfile
)
637 k
= min(c
, min(m
, y
));
642 cc
= cupsImageMatrix
[0][0][c
] +
643 cupsImageMatrix
[0][1][m
] +
644 cupsImageMatrix
[0][2][y
] + k
;
645 cm
= cupsImageMatrix
[1][0][c
] +
646 cupsImageMatrix
[1][1][m
] +
647 cupsImageMatrix
[1][2][y
] + k
;
648 cy
= cupsImageMatrix
[2][0][c
] +
649 cupsImageMatrix
[2][1][m
] +
650 cupsImageMatrix
[2][2][y
] + k
;
655 *out
++ = cupsImageDensity
[255];
657 *out
++ = cupsImageDensity
[cc
];
662 *out
++ = cupsImageDensity
[255];
664 *out
++ = cupsImageDensity
[cm
];
669 *out
++ = cupsImageDensity
[255];
671 *out
++ = cupsImageDensity
[cy
];
681 k
= min(c
, min(m
, y
));
683 *out
++ = (255 - in
[1] / 4) * (c
- k
) / 255 + k
;
684 *out
++ = (255 - in
[2] / 4) * (m
- k
) / 255 + k
;
685 *out
++ = (255 - in
[0] / 4) * (y
- k
) / 255 + k
;
693 * 'cupsImageRGBToCMYK()' - Convert RGB colors to CMYK.
698 const cups_ib_t
*in
, /* I - Input pixels */
699 cups_ib_t
*out
, /* I - Output pixels */
700 int count
) /* I - Number of pixels */
702 int c
, m
, y
, k
, /* CMYK values */
703 km
; /* Maximum K value */
704 int cc
, cm
, cy
; /* Calibrated CMY values */
707 if (cupsImageHaveProfile
)
713 k
= min(c
, min(m
, y
));
715 if ((km
= max(c
, max(m
, y
))) > k
)
716 k
= k
* k
* k
/ (km
* km
);
722 cc
= (cupsImageMatrix
[0][0][c
] +
723 cupsImageMatrix
[0][1][m
] +
724 cupsImageMatrix
[0][2][y
]);
725 cm
= (cupsImageMatrix
[1][0][c
] +
726 cupsImageMatrix
[1][1][m
] +
727 cupsImageMatrix
[1][2][y
]);
728 cy
= (cupsImageMatrix
[2][0][c
] +
729 cupsImageMatrix
[2][1][m
] +
730 cupsImageMatrix
[2][2][y
]);
735 *out
++ = cupsImageDensity
[255];
737 *out
++ = cupsImageDensity
[cc
];
742 *out
++ = cupsImageDensity
[255];
744 *out
++ = cupsImageDensity
[cm
];
749 *out
++ = cupsImageDensity
[255];
751 *out
++ = cupsImageDensity
[cy
];
753 *out
++ = cupsImageDensity
[k
];
763 k
= min(c
, min(m
, y
));
765 if ((km
= max(c
, max(m
, y
))) > k
)
766 k
= k
* k
* k
/ (km
* km
);
783 * 'cupsImageRGBToRGB()' - Convert RGB colors to device-dependent RGB.
788 const cups_ib_t
*in
, /* I - Input pixels */
789 cups_ib_t
*out
, /* I - Output pixels */
790 int count
) /* I - Number of pixels */
792 int c
, m
, y
, k
; /* CMYK values */
793 int cr
, cg
, cb
; /* Calibrated RGB values */
796 if (cupsImageHaveProfile
)
803 k
= min(c
, min(m
, y
));
808 cr
= cupsImageMatrix
[0][0][c
] +
809 cupsImageMatrix
[0][1][m
] +
810 cupsImageMatrix
[0][2][y
] + k
;
811 cg
= cupsImageMatrix
[1][0][c
] +
812 cupsImageMatrix
[1][1][m
] +
813 cupsImageMatrix
[1][2][y
] + k
;
814 cb
= cupsImageMatrix
[2][0][c
] +
815 cupsImageMatrix
[2][1][m
] +
816 cupsImageMatrix
[2][2][y
] + k
;
821 *out
++ = 255 - cupsImageDensity
[255];
823 *out
++ = 255 - cupsImageDensity
[cr
];
828 *out
++ = 255 - cupsImageDensity
[255];
830 *out
++ = 255 - cupsImageDensity
[cg
];
835 *out
++ = 255 - cupsImageDensity
[255];
837 *out
++ = 255 - cupsImageDensity
[cb
];
845 memcpy(out
, in
, count
* 3);
847 if (cupsImageColorSpace
== CUPS_CSPACE_CIELab
||
848 cupsImageColorSpace
>= CUPS_CSPACE_ICC1
)
858 else if (cupsImageColorSpace
== CUPS_CSPACE_CIEXYZ
)
873 * 'cupsImageRGBToWhite()' - Convert RGB colors to luminance.
878 const cups_ib_t
*in
, /* I - Input pixels */
879 cups_ib_t
*out
, /* I - Output pixels */
880 int count
) /* I - Number of pixels */
882 if (cupsImageHaveProfile
)
886 *out
++ = 255 - cupsImageDensity
[255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100];
895 *out
++ = (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100;
904 * 'cupsImageSetProfile()' - Set the device color profile.
908 cupsImageSetProfile(float d
, /* I - Ink/marker density */
909 float g
, /* I - Ink/marker gamma */
910 float matrix
[3][3]) /* I - Color transform matrix */
912 int i
, j
, k
; /* Looping vars */
913 float m
; /* Current matrix value */
914 int *im
; /* Pointer into cupsImageMatrix */
918 * Allocate memory for the profile data...
921 if (cupsImageMatrix
== NULL
)
922 cupsImageMatrix
= calloc(3, sizeof(cups_clut_t
));
924 if (cupsImageMatrix
== NULL
)
927 if (cupsImageDensity
== NULL
)
928 cupsImageDensity
= calloc(256, sizeof(int));
930 if (cupsImageDensity
== NULL
)
934 * Populate the profile lookup tables...
937 cupsImageHaveProfile
= 1;
939 for (i
= 0, im
= cupsImageMatrix
[0][0]; i
< 3; i
++)
940 for (j
= 0; j
< 3; j
++)
941 for (k
= 0, m
= matrix
[i
][j
]; k
< 256; k
++)
942 *im
++ = (int)(k
* m
+ 0.5);
944 for (k
= 0, im
= cupsImageDensity
; k
< 256; k
++)
945 *im
++ = 255.0 * d
* pow((float)k
/ 255.0, g
) + 0.5;
950 * 'cupsImageSetRasterColorSpace()' - Set the destination colorspace.
954 cupsImageSetRasterColorSpace(
955 cups_cspace_t cs
) /* I - Destination colorspace */
958 * Set the destination colorspace...
961 cupsImageColorSpace
= cs
;
964 * Don't use color profiles in colorimetric colorspaces...
967 if (cs
== CUPS_CSPACE_CIEXYZ
||
968 cs
== CUPS_CSPACE_CIELab
||
969 cs
>= CUPS_CSPACE_ICC1
)
970 cupsImageHaveProfile
= 0;
975 * 'cupsImageWhiteToBlack()' - Convert luminance colors to black.
979 cupsImageWhiteToBlack(
980 const cups_ib_t
*in
, /* I - Input pixels */
981 cups_ib_t
*out
, /* I - Output pixels */
982 int count
) /* I - Number of pixels */
984 if (cupsImageHaveProfile
)
987 *out
++ = cupsImageDensity
[255 - *in
++];
993 *out
++ = 255 - *in
++;
1000 * 'cupsImageWhiteToCMY()' - Convert luminance colors to CMY.
1004 cupsImageWhiteToCMY(
1005 const cups_ib_t
*in
, /* I - Input pixels */
1006 cups_ib_t
*out
, /* I - Output pixels */
1007 int count
) /* I - Number of pixels */
1009 if (cupsImageHaveProfile
)
1012 out
[0] = cupsImageDensity
[255 - *in
++];
1023 *out
++ = 255 - *in
++;
1030 * 'cupsImageWhiteToCMYK()' - Convert luminance colors to CMYK.
1034 cupsImageWhiteToCMYK(
1035 const cups_ib_t
*in
, /* I - Input pixels */
1036 cups_ib_t
*out
, /* I - Output pixels */
1037 int count
) /* I - Number of pixels */
1039 if (cupsImageHaveProfile
)
1045 *out
++ = cupsImageDensity
[255 - *in
++];
1054 *out
++ = 255 - *in
++;
1061 * 'cupsImageWhiteToRGB()' - Convert luminance data to RGB.
1065 cupsImageWhiteToRGB(
1066 const cups_ib_t
*in
, /* I - Input pixels */
1067 cups_ib_t
*out
, /* I - Output pixels */
1068 int count
) /* I - Number of pixels */
1070 if (cupsImageHaveProfile
)
1074 out
[0] = 255 - cupsImageDensity
[255 - *in
++];
1089 if (cupsImageColorSpace
== CUPS_CSPACE_CIELab
||
1090 cupsImageColorSpace
>= CUPS_CSPACE_ICC1
)
1091 rgb_to_lab(out
- 3);
1092 else if (cupsImageColorSpace
== CUPS_CSPACE_CIEXYZ
)
1093 rgb_to_xyz(out
- 3);
1102 * 'cupsImageWhiteToWhite()' - Convert luminance colors to device-dependent
1107 cupsImageWhiteToWhite(
1108 const cups_ib_t
*in
, /* I - Input pixels */
1109 cups_ib_t
*out
, /* I - Output pixels */
1110 int count
) /* I - Number of pixels */
1112 if (cupsImageHaveProfile
)
1115 *out
++ = 255 - cupsImageDensity
[255 - *in
++];
1119 memcpy(out
, in
, count
);
1124 * 'cielab()' - Map CIE Lab transformation...
1127 static float /* O - Adjusted color value */
1128 cielab(float x
, /* I - Raw color value */
1129 float xn
) /* I - Whitepoint color value */
1131 float x_xn
; /* Fraction of whitepoint */
1136 if (x_xn
> 0.008856)
1137 return (cbrt(x_xn
));
1139 return (7.787 * x_xn
+ 16.0 / 116.0);
1144 * 'huerotate()' - Rotate the hue, maintaining luminance.
1148 huerotate(float mat
[3][3], /* I - Matrix to append to */
1149 float rot
) /* I - Hue rotation in degrees */
1151 float hmat
[3][3]; /* Hue matrix */
1152 float lx
, ly
, lz
; /* Luminance vector */
1153 float xrs
, xrc
; /* X rotation sine/cosine */
1154 float yrs
, yrc
; /* Y rotation sine/cosine */
1155 float zrs
, zrc
; /* Z rotation sine/cosine */
1156 float zsx
, zsy
; /* Z shear x/y */
1160 * Load the identity matrix...
1166 * Rotate the grey vector into positive Z...
1171 xrotate(hmat
,xrs
,xrc
);
1173 yrs
= -1.0 / sqrt(3.0);
1174 yrc
= -M_SQRT2
* yrs
;
1175 yrotate(hmat
,yrs
,yrc
);
1178 * Shear the space to make the luminance plane horizontal...
1181 xform(hmat
, 0.3086, 0.6094, 0.0820, &lx
, &ly
, &lz
);
1184 zshear(hmat
, zsx
, zsy
);
1190 zrs
= sin(rot
* M_PI
/ 180.0);
1191 zrc
= cos(rot
* M_PI
/ 180.0);
1193 zrotate(hmat
, zrs
, zrc
);
1196 * Unshear the space to put the luminance plane back...
1199 zshear(hmat
, -zsx
, -zsy
);
1202 * Rotate the grey vector back into place...
1205 yrotate(hmat
, -yrs
, yrc
);
1206 xrotate(hmat
, -xrs
, xrc
);
1209 * Append it to the current matrix...
1212 mult(hmat
, mat
, mat
);
1217 * 'ident()' - Make an identity matrix.
1221 ident(float mat
[3][3]) /* I - Matrix to identify */
1236 * 'mult()' - Multiply two matrices.
1240 mult(float a
[3][3], /* I - First matrix */
1241 float b
[3][3], /* I - Second matrix */
1242 float c
[3][3]) /* I - Destination matrix */
1244 int x
, y
; /* Looping vars */
1245 float temp
[3][3]; /* Temporary matrix */
1249 * Multiply a and b, putting the result in temp...
1252 for (y
= 0; y
< 3; y
++)
1253 for (x
= 0; x
< 3; x
++)
1254 temp
[y
][x
] = b
[y
][0] * a
[0][x
] +
1259 * Copy temp to c (that way c can be a pointer to a or b).
1262 memcpy(c
, temp
, sizeof(temp
));
1267 * 'rgb_to_lab()' - Convert an RGB color to CIE Lab.
1271 rgb_to_lab(cups_ib_t
*val
) /* IO - Color value */
1273 float r
, /* Red value */
1274 g
, /* Green value */
1276 ciex
, /* CIE X value */
1277 ciey
, /* CIE Y value */
1278 ciez
, /* CIE Z value */
1279 ciey_yn
, /* Normalized luminance */
1280 ciel
, /* CIE L value */
1281 ciea
, /* CIE a value */
1282 cieb
; /* CIE b value */
1286 * Convert sRGB to linear RGB...
1289 r
= pow((val
[0] / 255.0 + 0.055) / 1.055, 2.4);
1290 g
= pow((val
[1] / 255.0 + 0.055) / 1.055, 2.4);
1291 b
= pow((val
[2] / 255.0 + 0.055) / 1.055, 2.4);
1294 * Convert to CIE XYZ...
1297 ciex
= 0.412453 * r
+ 0.357580 * g
+ 0.180423 * b
;
1298 ciey
= 0.212671 * r
+ 0.715160 * g
+ 0.072169 * b
;
1299 ciez
= 0.019334 * r
+ 0.119193 * g
+ 0.950227 * b
;
1302 * Normalize and convert to CIE Lab...
1305 ciey_yn
= ciey
/ D65_Y
;
1307 if (ciey_yn
> 0.008856)
1308 ciel
= 116 * cbrt(ciey_yn
) - 16;
1310 ciel
= 903.3 * ciey_yn
;
1313 ciea
= 500 * (cielab(ciex
, D65_X
) - cielab(ciey
, D65_Y
));
1314 cieb
= 200 * (cielab(ciey
, D65_Y
) - cielab(ciez
, D65_Z
));
1317 * Scale the L value and bias the a and b values by 128 so that all
1318 * numbers are from 0 to 255.
1321 ciel
= ciel
* 2.55 + 0.5;
1326 * Output 8-bit values...
1331 else if (ciel
< 255.0)
1338 else if (ciea
< 255.0)
1345 else if (cieb
< 255.0)
1353 * 'rgb_to_xyz()' - Convert an RGB color to CIE XYZ.
1357 rgb_to_xyz(cups_ib_t
*val
) /* IO - Color value */
1359 float r
, /* Red value */
1360 g
, /* Green value */
1362 ciex
, /* CIE X value */
1363 ciey
, /* CIE Y value */
1364 ciez
; /* CIE Z value */
1368 * Convert sRGB to linear RGB...
1371 r
= pow((val
[0] / 255.0 + 0.055) / 1.055, 2.4);
1372 g
= pow((val
[1] / 255.0 + 0.055) / 1.055, 2.4);
1373 b
= pow((val
[2] / 255.0 + 0.055) / 1.055, 2.4);
1376 * Convert to CIE XYZ...
1379 ciex
= 0.412453 * r
+ 0.357580 * g
+ 0.180423 * b
;
1380 ciey
= 0.212671 * r
+ 0.715160 * g
+ 0.072169 * b
;
1381 ciez
= 0.019334 * r
+ 0.119193 * g
+ 0.950227 * b
;
1384 * Encode as 8-bit XYZ...
1389 else if (ciex
< 1.1f
)
1390 val
[0] = (int)(231.8181f
* ciex
+ 0.5);
1396 else if (ciey
< 1.1f
)
1397 val
[1] = (int)(231.8181f
* ciey
+ 0.5);
1403 else if (ciez
< 1.1f
)
1404 val
[2] = (int)(231.8181f
* ciez
+ 0.5);
1411 * 'saturate()' - Make a saturation matrix.
1415 saturate(float mat
[3][3], /* I - Matrix to append to */
1416 float sat
) /* I - Desired color saturation */
1418 float smat
[3][3]; /* Saturation matrix */
1421 smat
[0][0] = (1.0 - sat
) * 0.3086 + sat
;
1422 smat
[0][1] = (1.0 - sat
) * 0.3086;
1423 smat
[0][2] = (1.0 - sat
) * 0.3086;
1424 smat
[1][0] = (1.0 - sat
) * 0.6094;
1425 smat
[1][1] = (1.0 - sat
) * 0.6094 + sat
;
1426 smat
[1][2] = (1.0 - sat
) * 0.6094;
1427 smat
[2][0] = (1.0 - sat
) * 0.0820;
1428 smat
[2][1] = (1.0 - sat
) * 0.0820;
1429 smat
[2][2] = (1.0 - sat
) * 0.0820 + sat
;
1431 mult(smat
, mat
, mat
);
1436 * 'xform()' - Transform a 3D point using a matrix...
1440 xform(float mat
[3][3], /* I - Matrix */
1441 float x
, /* I - Input X coordinate */
1442 float y
, /* I - Input Y coordinate */
1443 float z
, /* I - Input Z coordinate */
1444 float *tx
, /* O - Output X coordinate */
1445 float *ty
, /* O - Output Y coordinate */
1446 float *tz
) /* O - Output Z coordinate */
1448 *tx
= x
* mat
[0][0] + y
* mat
[1][0] + z
* mat
[2][0];
1449 *ty
= x
* mat
[0][1] + y
* mat
[1][1] + z
* mat
[2][1];
1450 *tz
= x
* mat
[0][2] + y
* mat
[1][2] + z
* mat
[2][2];
1455 * 'xrotate()' - Rotate about the x (red) axis...
1459 xrotate(float mat
[3][3], /* I - Matrix */
1460 float rs
, /* I - Rotation angle sine */
1461 float rc
) /* I - Rotation angle cosine */
1463 float rmat
[3][3]; /* I - Rotation matrix */
1478 mult(rmat
, mat
, mat
);
1483 * 'yrotate()' - Rotate about the y (green) axis...
1487 yrotate(float mat
[3][3], /* I - Matrix */
1488 float rs
, /* I - Rotation angle sine */
1489 float rc
) /* I - Rotation angle cosine */
1491 float rmat
[3][3]; /* I - Rotation matrix */
1511 * 'zrotate()' - Rotate about the z (blue) axis...
1515 zrotate(float mat
[3][3], /* I - Matrix */
1516 float rs
, /* I - Rotation angle sine */
1517 float rc
) /* I - Rotation angle cosine */
1519 float rmat
[3][3]; /* I - Rotation matrix */
1539 * 'zshear()' - Shear z using x and y...
1543 zshear(float mat
[3][3], /* I - Matrix */
1544 float dx
, /* I - X shear */
1545 float dy
) /* I - Y shear */
1547 float smat
[3][3]; /* Shear matrix */
1562 mult(smat
, mat
, mat
);
1567 * End of "$Id: image-colorspace.c 6649 2007-07-11 21:46:42Z mike $".