2 Vampyre Imaging Library
4 http://imaginglib.sourceforge.net
6 The contents of this file are used with permission, subject to the Mozilla
7 Public License Version 1.1 (the "License"); you may not use this file except
8 in compliance with the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/MPL-1.1.html
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 the specific language governing rights and limitations under the License.
15 Alternatively, the contents of this file may be used under the terms of the
16 GNU Lesser General Public License (the "LGPL License"), in which case the
17 provisions of the LGPL License are applicable instead of those above.
18 If you wish to allow use of your version of this file only under the terms
19 of the LGPL License and not to allow others to use your version of this file
20 under the MPL, indicate your decision by deleting the provisions above and
21 replace them with the notice and other provisions required by the LGPL
22 License. If you do not delete the provisions above, a recipient may use
23 your version of this file under either the MPL or the LGPL License.
25 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
28 { This unit contains functions for manipulating and converting color values.}
33 {$I ImagingOptions.inc}
36 SysUtils
, ImagingTypes
, ImagingUtility
;
38 { Converts RGB color to YUV.}
39 procedure RGBToYUV(R
, G
, B
: Byte; var Y
, U
, V
: Byte);
40 { Converts YIV to RGB color.}
41 procedure YUVToRGB(Y
, U
, V
: Byte; var R
, G
, B
: Byte);
43 { Converts RGB color to YCbCr as used in JPEG.}
44 procedure RGBToYCbCr(R
, G
, B
: Byte; var Y
, Cb
, Cr
: Byte);
45 { Converts YCbCr as used in JPEG to RGB color.}
46 procedure YCbCrToRGB(Y
, Cb
, Cr
: Byte; var R
, G
, B
: Byte);
47 { Converts RGB color to YCbCr as used in JPEG.}
48 procedure RGBToYCbCr16(R
, G
, B
: Word; var Y
, Cb
, Cr
: Word);
49 { Converts YCbCr as used in JPEG to RGB color.}
50 procedure YCbCrToRGB16(Y
, Cb
, Cr
: Word; var R
, G
, B
: Word);
52 { Converts RGB color to CMY.}
53 procedure RGBToCMY(R
, G
, B
: Byte; var C
, M
, Y
: Byte);
54 { Converts CMY to RGB color.}
55 procedure CMYToRGB(C
, M
, Y
: Byte; var R
, G
, B
: Byte);
56 { Converts RGB color to CMY.}
57 procedure RGBToCMY16(R
, G
, B
: Word; var C
, M
, Y
: Word);
58 { Converts CMY to RGB color.}
59 procedure CMYToRGB16(C
, M
, Y
: Word; var R
, G
, B
: Word);
61 { Converts RGB color to CMYK.}
62 procedure RGBToCMYK(R
, G
, B
: Byte; var C
, M
, Y
, K
: Byte);
63 { Converts CMYK to RGB color.}
64 procedure CMYKToRGB(C
, M
, Y
, K
: Byte; var R
, G
, B
: Byte);
65 { Converts RGB color to CMYK.}
66 procedure RGBToCMYK16(R
, G
, B
: Word; var C
, M
, Y
, K
: Word);
67 { Converts CMYK to RGB color.}
68 procedure CMYKToRGB16(C
, M
, Y
, K
: Word; var R
, G
, B
: Word);
70 { Converts RGB color to YCoCg.}
71 procedure RGBToYCoCg(R
, G
, B
: Byte; var Y
, Co
, Cg
: Byte);
72 { Converts YCoCg to RGB color.}
73 procedure YCoCgToRGB(Y
, Co
, Cg
: Byte; var R
, G
, B
: Byte);
75 //procedure RGBToHSL(R, G, B: Byte; var H, S, L: Byte);
76 //procedure HSLToRGB(H, S, L: Byte; var R, G, B: Byte);
80 procedure RGBToYUV(R
, G
, B
: Byte; var Y
, U
, V
: Byte);
82 Y
:= ClampToByte(Round( 0.257 * R
+ 0.504 * G
+ 0.098 * B
) + 16);
83 V
:= ClampToByte(Round( 0.439 * R
- 0.368 * G
- 0.071 * B
) + 128);
84 U
:= ClampToByte(Round(-0.148 * R
- 0.291 * G
+ 0.439 * B
) + 128);
87 procedure YUVToRGB(Y
, U
, V
: Byte; var R
, G
, B
: Byte);
94 R
:= ClampToByte(Round(1.164 * CY
- 0.002 * CU
+ 1.596 * CV
));
95 G
:= ClampToByte(Round(1.164 * CY
- 0.391 * CU
- 0.813 * CV
));
96 B
:= ClampToByte(Round(1.164 * CY
+ 2.018 * CU
- 0.001 * CV
));
99 procedure RGBToYCbCr(R
, G
, B
: Byte; var Y
, Cb
, Cr
: Byte);
101 Y
:= ClampToByte(Round( 0.29900 * R
+ 0.58700 * G
+ 0.11400 * B
));
102 Cb
:= ClampToByte(Round(-0.16874 * R
- 0.33126 * G
+ 0.50000 * B
+ 128));
103 Cr
:= ClampToByte(Round( 0.50000 * R
- 0.41869 * G
- 0.08131 * B
+ 128));
106 procedure YCbCrToRGB(Y
, Cb
, Cr
: Byte; var R
, G
, B
: Byte);
108 R
:= ClampToByte(Round(Y
+ 1.40200 * (Cr
- 128)));
109 G
:= ClampToByte(Round(Y
- 0.34414 * (Cb
- 128) - 0.71414 * (Cr
- 128)));
110 B
:= ClampToByte(Round(Y
+ 1.77200 * (Cb
- 128)));
113 procedure RGBToYCbCr16(R
, G
, B
: Word; var Y
, Cb
, Cr
: Word);
115 Y
:= ClampToWord(Round( 0.29900 * R
+ 0.58700 * G
+ 0.11400 * B
));
116 Cb
:= ClampToWord(Round(-0.16874 * R
- 0.33126 * G
+ 0.50000 * B
+ 32768));
117 Cr
:= ClampToWord(Round( 0.50000 * R
- 0.41869 * G
- 0.08131 * B
+ 32768));
120 procedure YCbCrToRGB16(Y
, Cb
, Cr
: Word; var R
, G
, B
: Word);
122 R
:= ClampToWord(Round(Y
+ 1.40200 * (Cr
- 32768)));
123 G
:= ClampToWord(Round(Y
- 0.34414 * (Cb
- 32768) - 0.71414 * (Cr
- 32768)));
124 B
:= ClampToWord(Round(Y
+ 1.77200 * (Cb
- 32768)));
127 procedure RGBToCMY(R
, G
, B
: Byte; var C
, M
, Y
: Byte);
134 procedure CMYToRGB(C
, M
, Y
: Byte; var R
, G
, B
: Byte);
141 procedure RGBToCMY16(R
, G
, B
: Word; var C
, M
, Y
: Word);
148 procedure CMYToRGB16(C
, M
, Y
: Word; var R
, G
, B
: Word);
155 procedure RGBToCMYK(R
, G
, B
: Byte; var C
, M
, Y
, K
: Byte);
157 RGBToCMY(R
, G
, B
, C
, M
, Y
);
158 K
:= Min(C
, Min(M
, Y
));
167 C
:= ClampToByte(Round((C
- K
) / (255 - K
) * 255));
168 M
:= ClampToByte(Round((M
- K
) / (255 - K
) * 255));
169 Y
:= ClampToByte(Round((Y
- K
) / (255 - K
) * 255));
173 procedure CMYKToRGB(C
, M
, Y
, K
: Byte; var R
, G
, B
: Byte);
175 R
:= (255 - (C
- MulDiv(C
, K
, 255) + K
));
176 G
:= (255 - (M
- MulDiv(M
, K
, 255) + K
));
177 B
:= (255 - (Y
- MulDiv(Y
, K
, 255) + K
));
180 procedure RGBToCMYK16(R
, G
, B
: Word; var C
, M
, Y
, K
: Word);
182 RGBToCMY16(R
, G
, B
, C
, M
, Y
);
183 K
:= Min(C
, Min(M
, Y
));
192 C
:= ClampToWord(Round((C
- K
) / (65535 - K
) * 65535));
193 M
:= ClampToWord(Round((M
- K
) / (65535 - K
) * 65535));
194 Y
:= ClampToWord(Round((Y
- K
) / (65535 - K
) * 65535));
198 procedure CMYKToRGB16(C
, M
, Y
, K
: Word; var R
, G
, B
: Word);
200 R
:= 65535 - (C
- MulDiv(C
, K
, 65535) + K
);
201 G
:= 65535 - (M
- MulDiv(M
, K
, 65535) + K
);
202 B
:= 65535 - (Y
- MulDiv(Y
, K
, 65535) + K
);
205 procedure RGBToYCoCg(R
, G
, B
: Byte; var Y
, Co
, Cg
: Byte);
207 // C and Delphi's SHR behaviour differs for negative numbers, use div instead.
208 Y
:= ClampToByte(( R
+ G
shl 1 + B
+ 2) div 4);
209 Co
:= ClampToByte(( R
shl 1 - B
shl 1 + 2) div 4 + 128);
210 Cg
:= ClampToByte((-R
+ G
shl 1 - B
+ 2) div 4 + 128);
213 procedure YCoCgToRGB(Y
, Co
, Cg
: Byte; var R
, G
, B
: Byte);
215 CoInt
, CgInt
: Integer;
219 R
:= ClampToByte(Y
+ CoInt
- CgInt
);
220 G
:= ClampToByte(Y
+ CgInt
);
221 B
:= ClampToByte(Y
- CoInt
- CgInt
);
227 -- TODOS ----------------------------------------------------
230 -- 0.26.3 Changes/Bug Fixes ---------------------------------
231 - Added RGB<>YCoCg conversion functions.
232 - Fixed RGB>>CMYK conversions.
234 -- 0.23 Changes/Bug Fixes -----------------------------------
235 - Added RGB<>CMY(K) converion functions for 16 bit channels
236 (needed by PSD loading code).
238 -- 0.21 Changes/Bug Fixes -----------------------------------
239 - Added some color space conversion functions and LUTs
240 (RGB/YUV/YCrCb/CMY/CMYK).
242 -- 0.17 Changes/Bug Fixes -----------------------------------
243 - unit created (empty!)