2 $Id: ImagingColors.pas 173 2009-09-04 17:05:52Z galfar $
3 Vampyre Imaging Library
5 http://imaginglib.sourceforge.net
7 The contents of this file are used with permission, subject to the Mozilla
8 Public License Version 1.1 (the "License"); you may not use this file except
9 in compliance with the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/MPL-1.1.html
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 the specific language governing rights and limitations under the License.
16 Alternatively, the contents of this file may be used under the terms of the
17 GNU Lesser General Public License (the "LGPL License"), in which case the
18 provisions of the LGPL License are applicable instead of those above.
19 If you wish to allow use of your version of this file only under the terms
20 of the LGPL License and not to allow others to use your version of this file
21 under the MPL, indicate your decision by deleting the provisions above and
22 replace them with the notice and other provisions required by the LGPL
23 License. If you do not delete the provisions above, a recipient may use
24 your version of this file under either the MPL or the LGPL License.
26 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
29 { This unit contains functions for manipulating and converting color values.}
34 {$I ImagingOptions.inc}
37 SysUtils
, ImagingTypes
, ImagingUtility
;
39 { Converts RGB color to YUV.}
40 procedure RGBToYUV(R
, G
, B
: Byte; var Y
, U
, V
: Byte);
41 { Converts YIV to RGB color.}
42 procedure YUVToRGB(Y
, U
, V
: Byte; var R
, G
, B
: Byte);
44 { Converts RGB color to YCbCr as used in JPEG.}
45 procedure RGBToYCbCr(R
, G
, B
: Byte; var Y
, Cb
, Cr
: Byte);
46 { Converts YCbCr as used in JPEG to RGB color.}
47 procedure YCbCrToRGB(Y
, Cb
, Cr
: Byte; var R
, G
, B
: Byte);
48 { Converts RGB color to YCbCr as used in JPEG.}
49 procedure RGBToYCbCr16(R
, G
, B
: Word; var Y
, Cb
, Cr
: Word);
50 { Converts YCbCr as used in JPEG to RGB color.}
51 procedure YCbCrToRGB16(Y
, Cb
, Cr
: Word; var R
, G
, B
: Word);
53 { Converts RGB color to CMY.}
54 procedure RGBToCMY(R
, G
, B
: Byte; var C
, M
, Y
: Byte);
55 { Converts CMY to RGB color.}
56 procedure CMYToRGB(C
, M
, Y
: Byte; var R
, G
, B
: Byte);
57 { Converts RGB color to CMY.}
58 procedure RGBToCMY16(R
, G
, B
: Word; var C
, M
, Y
: Word);
59 { Converts CMY to RGB color.}
60 procedure CMYToRGB16(C
, M
, Y
: Word; var R
, G
, B
: Word);
62 { Converts RGB color to CMYK.}
63 procedure RGBToCMYK(R
, G
, B
: Byte; var C
, M
, Y
, K
: Byte);
64 { Converts CMYK to RGB color.}
65 procedure CMYKToRGB(C
, M
, Y
, K
: Byte; var R
, G
, B
: Byte);
66 { Converts RGB color to CMYK.}
67 procedure RGBToCMYK16(R
, G
, B
: Word; var C
, M
, Y
, K
: Word);
68 { Converts CMYK to RGB color.}
69 procedure CMYKToRGB16(C
, M
, Y
, K
: Word; var R
, G
, B
: Word);
71 { Converts RGB color to YCoCg.}
72 procedure RGBToYCoCg(R
, G
, B
: Byte; var Y
, Co
, Cg
: Byte);
73 { Converts YCoCg to RGB color.}
74 procedure YCoCgToRGB(Y
, Co
, Cg
: Byte; var R
, G
, B
: Byte);
79 procedure RGBToYUV(R
, G
, B
: Byte; var Y
, U
, V
: Byte);
81 Y
:= ClampToByte(Round( 0.257 * R
+ 0.504 * G
+ 0.098 * B
) + 16);
82 V
:= ClampToByte(Round( 0.439 * R
- 0.368 * G
- 0.071 * B
) + 128);
83 U
:= ClampToByte(Round(-0.148 * R
- 0.291 * G
+ 0.439 * B
) + 128);
86 procedure YUVToRGB(Y
, U
, V
: Byte; var R
, G
, B
: Byte);
93 R
:= ClampToByte(Round(1.164 * CY
- 0.002 * CU
+ 1.596 * CV
));
94 G
:= ClampToByte(Round(1.164 * CY
- 0.391 * CU
- 0.813 * CV
));
95 B
:= ClampToByte(Round(1.164 * CY
+ 2.018 * CU
- 0.001 * CV
));
98 procedure RGBToYCbCr(R
, G
, B
: Byte; var Y
, Cb
, Cr
: Byte);
100 Y
:= ClampToByte(Round( 0.29900 * R
+ 0.58700 * G
+ 0.11400 * B
));
101 Cb
:= ClampToByte(Round(-0.16874 * R
- 0.33126 * G
+ 0.50000 * B
+ 128));
102 Cr
:= ClampToByte(Round( 0.50000 * R
- 0.41869 * G
- 0.08131 * B
+ 128));
105 procedure YCbCrToRGB(Y
, Cb
, Cr
: Byte; var R
, G
, B
: Byte);
107 R
:= ClampToByte(Round(Y
+ 1.40200 * (Cr
- 128)));
108 G
:= ClampToByte(Round(Y
- 0.34414 * (Cb
- 128) - 0.71414 * (Cr
- 128)));
109 B
:= ClampToByte(Round(Y
+ 1.77200 * (Cb
- 128)));
112 procedure RGBToYCbCr16(R
, G
, B
: Word; var Y
, Cb
, Cr
: Word);
114 Y
:= ClampToWord(Round( 0.29900 * R
+ 0.58700 * G
+ 0.11400 * B
));
115 Cb
:= ClampToWord(Round(-0.16874 * R
- 0.33126 * G
+ 0.50000 * B
+ 32768));
116 Cr
:= ClampToWord(Round( 0.50000 * R
- 0.41869 * G
- 0.08131 * B
+ 32768));
119 procedure YCbCrToRGB16(Y
, Cb
, Cr
: Word; var R
, G
, B
: Word);
121 R
:= ClampToWord(Round(Y
+ 1.40200 * (Cr
- 32768)));
122 G
:= ClampToWord(Round(Y
- 0.34414 * (Cb
- 32768) - 0.71414 * (Cr
- 32768)));
123 B
:= ClampToWord(Round(Y
+ 1.77200 * (Cb
- 32768)));
126 procedure RGBToCMY(R
, G
, B
: Byte; var C
, M
, Y
: Byte);
133 procedure CMYToRGB(C
, M
, Y
: Byte; var R
, G
, B
: Byte);
140 procedure RGBToCMY16(R
, G
, B
: Word; var C
, M
, Y
: Word);
147 procedure CMYToRGB16(C
, M
, Y
: Word; var R
, G
, B
: Word);
154 procedure RGBToCMYK(R
, G
, B
: Byte; var C
, M
, Y
, K
: Byte);
156 RGBToCMY(R
, G
, B
, C
, M
, Y
);
157 K
:= Min(C
, Min(M
, Y
));
166 C
:= ClampToByte(Round((C
- K
) / (255 - K
) * 255));
167 M
:= ClampToByte(Round((M
- K
) / (255 - K
) * 255));
168 Y
:= ClampToByte(Round((Y
- K
) / (255 - K
) * 255));
172 procedure CMYKToRGB(C
, M
, Y
, K
: Byte; var R
, G
, B
: Byte);
174 R
:= (255 - (C
- MulDiv(C
, K
, 255) + K
));
175 G
:= (255 - (M
- MulDiv(M
, K
, 255) + K
));
176 B
:= (255 - (Y
- MulDiv(Y
, K
, 255) + K
));
179 procedure RGBToCMYK16(R
, G
, B
: Word; var C
, M
, Y
, K
: Word);
181 RGBToCMY16(R
, G
, B
, C
, M
, Y
);
182 K
:= Min(C
, Min(M
, Y
));
191 C
:= ClampToWord(Round((C
- K
) / (65535 - K
) * 65535));
192 M
:= ClampToWord(Round((M
- K
) / (65535 - K
) * 65535));
193 Y
:= ClampToWord(Round((Y
- K
) / (65535 - K
) * 65535));
197 procedure CMYKToRGB16(C
, M
, Y
, K
: Word; var R
, G
, B
: Word);
199 R
:= 65535 - (C
- MulDiv(C
, K
, 65535) + K
);
200 G
:= 65535 - (M
- MulDiv(M
, K
, 65535) + K
);
201 B
:= 65535 - (Y
- MulDiv(Y
, K
, 65535) + K
);
204 procedure RGBToYCoCg(R
, G
, B
: Byte; var Y
, Co
, Cg
: Byte);
206 // C and Delphi's SHR behaviour differs for negative numbers, use div instead.
207 Y
:= ClampToByte(( R
+ G
shl 1 + B
+ 2) div 4);
208 Co
:= ClampToByte(( R
shl 1 - B
shl 1 + 2) div 4 + 128);
209 Cg
:= ClampToByte((-R
+ G
shl 1 - B
+ 2) div 4 + 128);
212 procedure YCoCgToRGB(Y
, Co
, Cg
: Byte; var R
, G
, B
: Byte);
214 CoInt
, CgInt
: Integer;
218 R
:= ClampToByte(Y
+ CoInt
- CgInt
);
219 G
:= ClampToByte(Y
+ CgInt
);
220 B
:= ClampToByte(Y
- CoInt
- CgInt
);
226 -- TODOS ----------------------------------------------------
229 -- 0.26.3 Changes/Bug Fixes ---------------------------------
230 - Added RGB<>YCoCg conversion functions.
231 - Fixed RGB>>CMYK conversions.
233 -- 0.23 Changes/Bug Fixes -----------------------------------
234 - Added RGB<>CMY(K) converion functions for 16 bit channels
235 (needed by PSD loading code).
237 -- 0.21 Changes/Bug Fixes -----------------------------------
238 - Added some color space conversion functions and LUTs
239 (RGB/YUV/YCrCb/CMY/CMYK).
241 -- 0.17 Changes/Bug Fixes -----------------------------------
242 - unit created (empty!)