2 * "$Id: image-colorspace.c 4767 2005-10-10 19:23:23Z mike $"
4 * Colorspace conversions for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2005 by Easy Software Products.
8 * The color saturation/hue matrix stuff is provided thanks to Mr. Paul
9 * Haeberli at "http://www.sgi.com/grafica/matrix/index.html".
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
27 * This file is subject to the Apple OS-Developed Software exception.
31 * cupsImageCMYKToBlack() - Convert CMYK data to black.
32 * cupsImageCMYKToCMY() - Convert CMYK colors to CMY.
33 * cupsImageCMYKToCMYK() - Convert CMYK colors to CMYK.
34 * cupsImageCMYKToRGB() - Convert CMYK colors to device-dependent
36 * cupsImageCMYKToWhite() - Convert CMYK colors to luminance.
37 * cupsImageLut() - Adjust all pixel values with the given
39 * cupsImageRGBAdjust() - Adjust the hue and saturation of the
41 * cupsImageRGBToBlack() - Convert RGB data to black.
42 * cupsImageRGBToCMY() - Convert RGB colors to CMY.
43 * cupsImageRGBToCMYK() - Convert RGB colors to CMYK.
44 * cupsImageRGBToRGB() - Convert RGB colors to device-dependent
46 * cupsImageRGBToWhite() - Convert RGB colors to luminance.
47 * cupsImageSetProfile() - Set the device color profile.
48 * cupsImageSetRasterColorSpace() - Set the destination colorspace.
49 * cupsImageWhiteToBlack() - Convert luminance colors to black.
50 * cupsImageWhiteToCMY() - Convert luminance colors to CMY.
51 * cupsImageWhiteToCMYK() - Convert luminance colors to CMYK.
52 * cupsImageWhiteToRGB() - Convert luminance data to RGB.
53 * cupsImageWhiteToWhite() - Convert luminance colors to device-
54 * dependent luminance.
55 * cielab() - Map CIE Lab transformation...
56 * huerotate() - Rotate the hue, maintaining luminance.
57 * ident() - Make an identity matrix.
58 * mult() - Multiply two matrices.
59 * rgb_to_lab() - Convert an RGB color to CIE Lab.
60 * rgb_to_xyz() - Convert an RGB color to CIE XYZ.
61 * saturate() - Make a saturation matrix.
62 * xform() - Transform a 3D point using a matrix...
63 * xrotate() - Rotate about the x (red) axis...
64 * yrotate() - Rotate about the y (green) axis...
65 * zrotate() - Rotate about the z (blue) axis...
66 * zshear() - Shear z using x and y...
70 * Include necessary headers...
73 #include "image-private.h"
77 * Define some math constants that are required...
81 # define M_PI 3.14159265358979323846
85 # define M_SQRT2 1.41421356237309504880
89 # define M_SQRT1_2 0.70710678118654752440
90 #endif /* !M_SQRT1_2 */
93 * CIE XYZ whitepoint...
96 #define D65_X (0.412453 + 0.357580 + 0.180423)
97 #define D65_Y (0.212671 + 0.715160 + 0.072169)
98 #define D65_Z (0.019334 + 0.119193 + 0.950227)
102 * Lookup table structure...
105 typedef int cups_clut_t
[3][256];
112 static int cupsImageHaveProfile
= 0;
113 /* Do we have a color profile? */
114 static int *cupsImageDensity
;
115 /* Ink/marker density LUT */
116 static cups_clut_t
*cupsImageMatrix
;
117 /* Color transform matrix LUT */
118 static cups_cspace_t cupsImageColorSpace
= CUPS_CSPACE_RGB
;
119 /* Destination colorspace */
126 static float cielab(float x
, float xn
);
127 static void huerotate(float [3][3], float);
128 static void ident(float [3][3]);
129 static void mult(float [3][3], float [3][3], float [3][3]);
130 static void rgb_to_lab(cups_ib_t
*val
);
131 static void rgb_to_xyz(cups_ib_t
*val
);
132 static void saturate(float [3][3], float);
133 static void xform(float [3][3], float, float, float, float *, float *, float *);
134 static void xrotate(float [3][3], float, float);
135 static void yrotate(float [3][3], float, float);
136 static void zrotate(float [3][3], float, float);
137 static void zshear(float [3][3], float, float);
141 * 'cupsImageCMYKToBlack()' - Convert CMYK data to black.
145 cupsImageCMYKToBlack(
146 const cups_ib_t
*in
, /* I - Input pixels */
147 cups_ib_t
*out
, /* I - Output pixels */
148 int count
) /* I - Number of pixels */
150 int k
; /* Black value */
153 if (cupsImageHaveProfile
)
156 k
= (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 + in
[3];
159 *out
++ = cupsImageDensity
[k
];
161 *out
++ = cupsImageDensity
[255];
169 k
= (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 + in
[3];
183 * 'cupsImageCMYKToCMY()' - Convert CMYK colors to CMY.
188 const cups_ib_t
*in
, /* I - Input pixels */
189 cups_ib_t
*out
, /* I - Output pixels */
190 int count
) /* I - Number of pixels */
192 int c
, m
, y
, k
; /* CMYK values */
193 int cc
, cm
, cy
; /* Calibrated CMY values */
196 if (cupsImageHaveProfile
)
204 cc
= cupsImageMatrix
[0][0][c
] +
205 cupsImageMatrix
[0][1][m
] +
206 cupsImageMatrix
[0][2][y
] + k
;
207 cm
= cupsImageMatrix
[1][0][c
] +
208 cupsImageMatrix
[1][1][m
] +
209 cupsImageMatrix
[1][2][y
] + k
;
210 cy
= cupsImageMatrix
[2][0][c
] +
211 cupsImageMatrix
[2][1][m
] +
212 cupsImageMatrix
[2][2][y
] + k
;
217 *out
++ = cupsImageDensity
[255];
219 *out
++ = cupsImageDensity
[cc
];
224 *out
++ = cupsImageDensity
[255];
226 *out
++ = cupsImageDensity
[cm
];
231 *out
++ = cupsImageDensity
[255];
233 *out
++ = cupsImageDensity
[cy
];
270 * 'cupsImageCMYKToCMYK()' - Convert CMYK colors to CMYK.
275 const cups_ib_t
*in
, /* I - Input pixels */
276 cups_ib_t
*out
, /* I - Output pixels */
277 int count
) /* I - Number of pixels */
279 int c
, m
, y
, k
; /* CMYK values */
280 int cc
, cm
, cy
; /* Calibrated CMY values */
283 if (cupsImageHaveProfile
)
291 cc
= (cupsImageMatrix
[0][0][c
] +
292 cupsImageMatrix
[0][1][m
] +
293 cupsImageMatrix
[0][2][y
]);
294 cm
= (cupsImageMatrix
[1][0][c
] +
295 cupsImageMatrix
[1][1][m
] +
296 cupsImageMatrix
[1][2][y
]);
297 cy
= (cupsImageMatrix
[2][0][c
] +
298 cupsImageMatrix
[2][1][m
] +
299 cupsImageMatrix
[2][2][y
]);
304 *out
++ = cupsImageDensity
[255];
306 *out
++ = cupsImageDensity
[cc
];
311 *out
++ = cupsImageDensity
[255];
313 *out
++ = cupsImageDensity
[cm
];
318 *out
++ = cupsImageDensity
[255];
320 *out
++ = cupsImageDensity
[cy
];
322 *out
++ = cupsImageDensity
[k
];
342 * 'cupsImageCMYKToRGB()' - Convert CMYK colors to device-dependent RGB.
347 const cups_ib_t
*in
, /* I - Input pixels */
348 cups_ib_t
*out
, /* I - Output pixels */
349 int count
) /* I - Number of pixels */
351 int c
, m
, y
, k
; /* CMYK values */
352 int cr
, cg
, cb
; /* Calibrated RGB values */
355 if (cupsImageHaveProfile
)
364 cr
= cupsImageMatrix
[0][0][c
] +
365 cupsImageMatrix
[0][1][m
] +
366 cupsImageMatrix
[0][2][y
] + k
;
367 cg
= cupsImageMatrix
[1][0][c
] +
368 cupsImageMatrix
[1][1][m
] +
369 cupsImageMatrix
[1][2][y
] + k
;
370 cb
= cupsImageMatrix
[2][0][c
] +
371 cupsImageMatrix
[2][1][m
] +
372 cupsImageMatrix
[2][2][y
] + k
;
377 *out
++ = 255 - cupsImageDensity
[255];
379 *out
++ = 255 - cupsImageDensity
[cr
];
384 *out
++ = 255 - cupsImageDensity
[255];
386 *out
++ = 255 - cupsImageDensity
[cg
];
391 *out
++ = 255 - cupsImageDensity
[255];
393 *out
++ = 255 - cupsImageDensity
[cb
];
426 if (cupsImageColorSpace
>= CUPS_CSPACE_CIELab
)
428 else if (cupsImageColorSpace
== CUPS_CSPACE_CIEXYZ
)
438 * 'cupsImageCMYKToWhite()' - Convert CMYK colors to luminance.
442 cupsImageCMYKToWhite(
443 const cups_ib_t
*in
, /* I - Input pixels */
444 cups_ib_t
*out
, /* I - Output pixels */
445 int count
) /* I - Number of pixels */
447 int w
; /* White value */
450 if (cupsImageHaveProfile
)
454 w
= 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 - in
[3];
457 *out
++ = cupsImageDensity
[w
];
459 *out
++ = cupsImageDensity
[0];
469 w
= 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100 - in
[3];
484 * 'cupsImageLut()' - Adjust all pixel values with the given LUT.
488 cupsImageLut(cups_ib_t
*pixels
, /* IO - Input/output pixels */
489 int count
, /* I - Number of pixels/bytes to adjust */
490 const cups_ib_t
*lut
) /* I - Lookup table */
494 *pixels
= lut
[*pixels
];
502 * 'cupsImageRGBAdjust()' - Adjust the hue and saturation of the given RGB colors.
506 cupsImageRGBAdjust(cups_ib_t
*pixels
, /* IO - Input/output pixels */
507 int count
, /* I - Number of pixels to adjust */
508 int saturation
,/* I - Color saturation (%) */
509 int hue
) /* I - Color hue (degrees) */
511 int i
, j
, k
; /* Looping vars */
512 float mat
[3][3]; /* Color adjustment matrix */
513 static int last_sat
= 100, /* Last saturation used */
514 last_hue
= 0; /* Last hue used */
515 static cups_clut_t
*lut
= NULL
; /* Lookup table for matrix */
518 if (saturation
!= last_sat
||
522 * Build the color adjustment matrix...
526 saturate(mat
, saturation
* 0.01);
527 huerotate(mat
, (float)hue
);
530 * Allocate memory for the lookup table...
534 lut
= calloc(3, sizeof(cups_clut_t
));
540 * Convert the matrix into a 3x3 array of lookup tables...
543 for (i
= 0; i
< 3; i
++)
544 for (j
= 0; j
< 3; j
++)
545 for (k
= 0; k
< 256; k
++)
546 lut
[i
][j
][k
] = mat
[i
][j
] * k
+ 0.5;
549 * Save the saturation and hue to compare later...
552 last_sat
= saturation
;
557 * Adjust each pixel in the given buffer.
562 i
= lut
[0][0][pixels
[0]] +
563 lut
[1][0][pixels
[1]] +
564 lut
[2][0][pixels
[2]];
572 i
= lut
[0][1][pixels
[0]] +
573 lut
[1][1][pixels
[1]] +
574 lut
[2][1][pixels
[2]];
582 i
= lut
[0][2][pixels
[0]] +
583 lut
[1][2][pixels
[1]] +
584 lut
[2][2][pixels
[2]];
599 * 'cupsImageRGBToBlack()' - Convert RGB data to black.
604 const cups_ib_t
*in
, /* I - Input pixels */
605 cups_ib_t
*out
, /* I - Output pixels */
606 int count
) /* I - Number of pixels */
608 if (cupsImageHaveProfile
)
611 *out
++ = cupsImageDensity
[255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100];
618 *out
++ = 255 - (31 * in
[0] + 61 * in
[1] + 8 * in
[2]) / 100;
626 * 'cupsImageRGBToCMY()' - Convert RGB colors to CMY.
631 const cups_ib_t
*in
, /* I - Input pixels */
632 cups_ib_t
*out
, /* I - Output pixels */
633 int count
) /* I - Number of pixels */
635 int c
, m
, y
, k
; /* CMYK values */
636 int cc
, cm
, cy
; /* Calibrated CMY values */
639 if (cupsImageHaveProfile
)
645 k
= min(c
, min(m
, y
));
650 cc
= cupsImageMatrix
[0][0][c
] +
651 cupsImageMatrix
[0][1][m
] +
652 cupsImageMatrix
[0][2][y
] + k
;
653 cm
= cupsImageMatrix
[1][0][c
] +
654 cupsImageMatrix
[1][1][m
] +
655 cupsImageMatrix
[1][2][y
] + k
;
656 cy
= cupsImageMatrix
[2][0][c
] +
657 cupsImageMatrix
[2][1][m
] +
658 cupsImageMatrix
[2][2][y
] + k
;
663 *out
++ = cupsImageDensity
[255];
665 *out
++ = cupsImageDensity
[cc
];
670 *out
++ = cupsImageDensity
[255];
672 *out
++ = cupsImageDensity
[cm
];
677 *out
++ = cupsImageDensity
[255];
679 *out
++ = cupsImageDensity
[cy
];
689 k
= min(c
, min(m
, y
));
691 *out
++ = (255 - in
[1] / 4) * (c
- k
) / 255 + k
;
692 *out
++ = (255 - in
[2] / 4) * (m
- k
) / 255 + k
;
693 *out
++ = (255 - in
[0] / 4) * (y
- k
) / 255 + k
;
701 * 'cupsImageRGBToCMYK()' - Convert RGB colors to CMYK.
706 const cups_ib_t
*in
, /* I - Input pixels */
707 cups_ib_t
*out
, /* I - Output pixels */
708 int count
) /* I - Number of pixels */
710 int c
, m
, y
, k
, /* CMYK values */
711 km
; /* Maximum K value */
712 int cc
, cm
, cy
; /* Calibrated CMY values */
715 if (cupsImageHaveProfile
)
721 k
= min(c
, min(m
, y
));
723 if ((km
= max(c
, max(m
, y
))) > k
)
724 k
= k
* k
* k
/ (km
* km
);
730 cc
= (cupsImageMatrix
[0][0][c
] +
731 cupsImageMatrix
[0][1][m
] +
732 cupsImageMatrix
[0][2][y
]);
733 cm
= (cupsImageMatrix
[1][0][c
] +
734 cupsImageMatrix
[1][1][m
] +
735 cupsImageMatrix
[1][2][y
]);
736 cy
= (cupsImageMatrix
[2][0][c
] +
737 cupsImageMatrix
[2][1][m
] +
738 cupsImageMatrix
[2][2][y
]);
743 *out
++ = cupsImageDensity
[255];
745 *out
++ = cupsImageDensity
[cc
];
750 *out
++ = cupsImageDensity
[255];
752 *out
++ = cupsImageDensity
[cm
];
757 *out
++ = cupsImageDensity
[255];
759 *out
++ = cupsImageDensity
[cy
];
761 *out
++ = cupsImageDensity
[k
];
771 k
= min(c
, min(m
, y
));
773 if ((km
= max(c
, max(m
, y
))) > k
)
774 k
= k
* k
* k
/ (km
* km
);
791 * 'cupsImageRGBToRGB()' - Convert RGB colors to device-dependent RGB.
796 const cups_ib_t
*in
, /* I - Input pixels */
797 cups_ib_t
*out
, /* I - Output pixels */
798 int count
) /* I - Number of pixels */
800 int c
, m
, y
, k
; /* CMYK values */
801 int cr
, cg
, cb
; /* Calibrated RGB values */
804 if (cupsImageHaveProfile
)
811 k
= min(c
, min(m
, y
));
816 cr
= cupsImageMatrix
[0][0][c
] +
817 cupsImageMatrix
[0][1][m
] +
818 cupsImageMatrix
[0][2][y
] + k
;
819 cg
= cupsImageMatrix
[1][0][c
] +
820 cupsImageMatrix
[1][1][m
] +
821 cupsImageMatrix
[1][2][y
] + k
;
822 cb
= cupsImageMatrix
[2][0][c
] +
823 cupsImageMatrix
[2][1][m
] +
824 cupsImageMatrix
[2][2][y
] + k
;
829 *out
++ = 255 - cupsImageDensity
[255];
831 *out
++ = 255 - cupsImageDensity
[cr
];
836 *out
++ = 255 - cupsImageDensity
[255];
838 *out
++ = 255 - cupsImageDensity
[cg
];
843 *out
++ = 255 - cupsImageDensity
[255];
845 *out
++ = 255 - cupsImageDensity
[cb
];
853 memcpy(out
, in
, count
* 3);
855 if (cupsImageColorSpace
>= CUPS_CSPACE_CIEXYZ
)
859 if (cupsImageColorSpace
>= CUPS_CSPACE_CIELab
)
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 cupsImageHaveProfile
= 0;
973 * 'cupsImageWhiteToBlack()' - Convert luminance colors to black.
977 cupsImageWhiteToBlack(
978 const cups_ib_t
*in
, /* I - Input pixels */
979 cups_ib_t
*out
, /* I - Output pixels */
980 int count
) /* I - Number of pixels */
982 if (cupsImageHaveProfile
)
985 *out
++ = cupsImageDensity
[255 - *in
++];
991 *out
++ = 255 - *in
++;
998 * 'cupsImageWhiteToCMY()' - Convert luminance colors to CMY.
1002 cupsImageWhiteToCMY(
1003 const cups_ib_t
*in
, /* I - Input pixels */
1004 cups_ib_t
*out
, /* I - Output pixels */
1005 int count
) /* I - Number of pixels */
1007 if (cupsImageHaveProfile
)
1010 out
[0] = cupsImageDensity
[255 - *in
++];
1021 *out
++ = 255 - *in
++;
1028 * 'cupsImageWhiteToCMYK()' - Convert luminance colors to CMYK.
1032 cupsImageWhiteToCMYK(
1033 const cups_ib_t
*in
, /* I - Input pixels */
1034 cups_ib_t
*out
, /* I - Output pixels */
1035 int count
) /* I - Number of pixels */
1037 if (cupsImageHaveProfile
)
1043 *out
++ = cupsImageDensity
[255 - *in
++];
1052 *out
++ = 255 - *in
++;
1059 * 'cupsImageWhiteToRGB()' - Convert luminance data to RGB.
1063 cupsImageWhiteToRGB(
1064 const cups_ib_t
*in
, /* I - Input pixels */
1065 cups_ib_t
*out
, /* I - Output pixels */
1066 int count
) /* I - Number of pixels */
1068 if (cupsImageHaveProfile
)
1072 out
[0] = 255 - cupsImageDensity
[255 - *in
++];
1087 if (cupsImageColorSpace
>= CUPS_CSPACE_CIELab
)
1088 rgb_to_lab(out
- 3);
1089 else if (cupsImageColorSpace
== CUPS_CSPACE_CIEXYZ
)
1090 rgb_to_xyz(out
- 3);
1099 * 'cupsImageWhiteToWhite()' - Convert luminance colors to device-dependent
1104 cupsImageWhiteToWhite(
1105 const cups_ib_t
*in
, /* I - Input pixels */
1106 cups_ib_t
*out
, /* I - Output pixels */
1107 int count
) /* I - Number of pixels */
1109 if (cupsImageHaveProfile
)
1112 *out
++ = 255 - cupsImageDensity
[255 - *in
++];
1116 memcpy(out
, in
, count
);
1121 * 'cielab()' - Map CIE Lab transformation...
1124 static float /* O - Adjusted color value */
1125 cielab(float x
, /* I - Raw color value */
1126 float xn
) /* I - Whitepoint color value */
1128 float x_xn
; /* Fraction of whitepoint */
1133 if (x_xn
> 0.008856)
1134 return (cbrt(x_xn
));
1136 return (7.787 * x_xn
+ 16.0 / 116.0);
1141 * 'huerotate()' - Rotate the hue, maintaining luminance.
1145 huerotate(float mat
[3][3], /* I - Matrix to append to */
1146 float rot
) /* I - Hue rotation in degrees */
1148 float hmat
[3][3]; /* Hue matrix */
1149 float lx
, ly
, lz
; /* Luminance vector */
1150 float xrs
, xrc
; /* X rotation sine/cosine */
1151 float yrs
, yrc
; /* Y rotation sine/cosine */
1152 float zrs
, zrc
; /* Z rotation sine/cosine */
1153 float zsx
, zsy
; /* Z shear x/y */
1157 * Load the identity matrix...
1163 * Rotate the grey vector into positive Z...
1168 xrotate(hmat
,xrs
,xrc
);
1170 yrs
= -1.0 / sqrt(3.0);
1171 yrc
= -M_SQRT2
* yrs
;
1172 yrotate(hmat
,yrs
,yrc
);
1175 * Shear the space to make the luminance plane horizontal...
1178 xform(hmat
, 0.3086, 0.6094, 0.0820, &lx
, &ly
, &lz
);
1181 zshear(hmat
, zsx
, zsy
);
1187 zrs
= sin(rot
* M_PI
/ 180.0);
1188 zrc
= cos(rot
* M_PI
/ 180.0);
1190 zrotate(hmat
, zrs
, zrc
);
1193 * Unshear the space to put the luminance plane back...
1196 zshear(hmat
, -zsx
, -zsy
);
1199 * Rotate the grey vector back into place...
1202 yrotate(hmat
, -yrs
, yrc
);
1203 xrotate(hmat
, -xrs
, xrc
);
1206 * Append it to the current matrix...
1209 mult(hmat
, mat
, mat
);
1214 * 'ident()' - Make an identity matrix.
1218 ident(float mat
[3][3]) /* I - Matrix to identify */
1233 * 'mult()' - Multiply two matrices.
1237 mult(float a
[3][3], /* I - First matrix */
1238 float b
[3][3], /* I - Second matrix */
1239 float c
[3][3]) /* I - Destination matrix */
1241 int x
, y
; /* Looping vars */
1242 float temp
[3][3]; /* Temporary matrix */
1246 * Multiply a and b, putting the result in temp...
1249 for (y
= 0; y
< 3; y
++)
1250 for (x
= 0; x
< 3; x
++)
1251 temp
[y
][x
] = b
[y
][0] * a
[0][x
] +
1256 * Copy temp to c (that way c can be a pointer to a or b).
1259 memcpy(c
, temp
, sizeof(temp
));
1264 * 'rgb_to_lab()' - Convert an RGB color to CIE Lab.
1268 rgb_to_lab(cups_ib_t
*val
) /* IO - Color value */
1270 float r
, /* Red value */
1271 g
, /* Green value */
1273 ciex
, /* CIE X value */
1274 ciey
, /* CIE Y value */
1275 ciez
, /* CIE Z value */
1276 ciey_yn
, /* Normalized luminance */
1277 ciel
, /* CIE L value */
1278 ciea
, /* CIE a value */
1279 cieb
; /* CIE b value */
1283 * Convert sRGB to linear RGB...
1286 r
= pow(val
[0] / 255.0, 0.58823529412);
1287 g
= pow(val
[1] / 255.0, 0.58823529412);
1288 b
= pow(val
[2] / 255.0, 0.58823529412);
1291 * Convert to CIE XYZ...
1294 ciex
= 0.412453 * r
+ 0.357580 * g
+ 0.180423 * b
;
1295 ciey
= 0.212671 * r
+ 0.715160 * g
+ 0.072169 * b
;
1296 ciez
= 0.019334 * r
+ 0.119193 * g
+ 0.950227 * b
;
1299 * Normalize and convert to CIE Lab...
1302 ciey_yn
= ciey
/ D65_Y
;
1304 if (ciey_yn
> 0.008856)
1305 ciel
= 116 * cbrt(ciey_yn
) - 16;
1307 ciel
= 903.3 * ciey_yn
;
1310 ciea
= 500 * (cielab(ciex
, D65_X
) - cielab(ciey
, D65_Y
));
1311 cieb
= 200 * (cielab(ciey
, D65_Y
) - cielab(ciez
, D65_Z
));
1314 * Scale the L value and bias the a and b values by 128 so that all
1315 * numbers are from 0 to 255.
1323 * Output 8-bit values...
1328 else if (ciel
< 255.0)
1335 else if (ciea
< 255.0)
1342 else if (cieb
< 255.0)
1350 * 'rgb_to_xyz()' - Convert an RGB color to CIE XYZ.
1354 rgb_to_xyz(cups_ib_t
*val
) /* IO - Color value */
1356 float r
, /* Red value */
1357 g
, /* Green value */
1359 ciex
, /* CIE X value */
1360 ciey
, /* CIE Y value */
1361 ciez
; /* CIE Z value */
1365 * Convert sRGB to linear RGB...
1368 r
= pow(val
[0] / 255.0, 0.58823529412);
1369 g
= pow(val
[1] / 255.0, 0.58823529412);
1370 b
= pow(val
[2] / 255.0, 0.58823529412);
1373 * Convert to CIE XYZ...
1376 ciex
= 0.412453 * r
+ 0.357580 * g
+ 0.180423 * b
;
1377 ciey
= 0.212671 * r
+ 0.715160 * g
+ 0.072169 * b
;
1378 ciez
= 0.019334 * r
+ 0.119193 * g
+ 0.950227 * b
;
1381 * Output 8-bit values...
1386 else if (ciex
< 255.0)
1393 else if (ciey
< 255.0)
1400 else if (ciez
< 255.0)
1408 * 'saturate()' - Make a saturation matrix.
1412 saturate(float mat
[3][3], /* I - Matrix to append to */
1413 float sat
) /* I - Desired color saturation */
1415 float smat
[3][3]; /* Saturation matrix */
1418 smat
[0][0] = (1.0 - sat
) * 0.3086 + sat
;
1419 smat
[0][1] = (1.0 - sat
) * 0.3086;
1420 smat
[0][2] = (1.0 - sat
) * 0.3086;
1421 smat
[1][0] = (1.0 - sat
) * 0.6094;
1422 smat
[1][1] = (1.0 - sat
) * 0.6094 + sat
;
1423 smat
[1][2] = (1.0 - sat
) * 0.6094;
1424 smat
[2][0] = (1.0 - sat
) * 0.0820;
1425 smat
[2][1] = (1.0 - sat
) * 0.0820;
1426 smat
[2][2] = (1.0 - sat
) * 0.0820 + sat
;
1428 mult(smat
, mat
, mat
);
1433 * 'xform()' - Transform a 3D point using a matrix...
1437 xform(float mat
[3][3], /* I - Matrix */
1438 float x
, /* I - Input X coordinate */
1439 float y
, /* I - Input Y coordinate */
1440 float z
, /* I - Input Z coordinate */
1441 float *tx
, /* O - Output X coordinate */
1442 float *ty
, /* O - Output Y coordinate */
1443 float *tz
) /* O - Output Z coordinate */
1445 *tx
= x
* mat
[0][0] + y
* mat
[1][0] + z
* mat
[2][0];
1446 *ty
= x
* mat
[0][1] + y
* mat
[1][1] + z
* mat
[2][1];
1447 *tz
= x
* mat
[0][2] + y
* mat
[1][2] + z
* mat
[2][2];
1452 * 'xrotate()' - Rotate about the x (red) axis...
1456 xrotate(float mat
[3][3], /* I - Matrix */
1457 float rs
, /* I - Rotation angle sine */
1458 float rc
) /* I - Rotation angle cosine */
1460 float rmat
[3][3]; /* I - Rotation matrix */
1475 mult(rmat
, mat
, mat
);
1480 * 'yrotate()' - Rotate about the y (green) axis...
1484 yrotate(float mat
[3][3], /* I - Matrix */
1485 float rs
, /* I - Rotation angle sine */
1486 float rc
) /* I - Rotation angle cosine */
1488 float rmat
[3][3]; /* I - Rotation matrix */
1508 * 'zrotate()' - Rotate about the z (blue) axis...
1512 zrotate(float mat
[3][3], /* I - Matrix */
1513 float rs
, /* I - Rotation angle sine */
1514 float rc
) /* I - Rotation angle cosine */
1516 float rmat
[3][3]; /* I - Rotation matrix */
1536 * 'zshear()' - Shear z using x and y...
1540 zshear(float mat
[3][3], /* I - Matrix */
1541 float dx
, /* I - X shear */
1542 float dy
) /* I - Y shear */
1544 float smat
[3][3]; /* Shear matrix */
1559 mult(smat
, mat
, mat
);
1564 * End of "$Id: image-colorspace.c 4767 2005-10-10 19:23:23Z mike $".