DEADSOFTWARE

added Vampyre Imaging Library; now textures can be in various formats, including...
[d2df-sdl.git] / src / lib / vampimg / ImagingColors.pas
1 {
2 $Id: ImagingColors.pas 173 2009-09-04 17:05:52Z galfar $
3 Vampyre Imaging Library
4 by Marek Mauder
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
27 }
29 { This unit contains functions for manipulating and converting color values.}
30 unit ImagingColors;
32 interface
34 {$I ImagingOptions.inc}
36 uses
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);
77 implementation
79 procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
80 begin
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);
84 end;
86 procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
87 var
88 CY, CU, CV: LongInt;
89 begin
90 CY := Y - 16;
91 CU := U - 128;
92 CV := V - 128;
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));
96 end;
98 procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
99 begin
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));
103 end;
105 procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
106 begin
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)));
110 end;
112 procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
113 begin
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));
117 end;
119 procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
120 begin
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)));
124 end;
126 procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
127 begin
128 C := 255 - R;
129 M := 255 - G;
130 Y := 255 - B;
131 end;
133 procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
134 begin
135 R := 255 - C;
136 G := 255 - M;
137 B := 255 - Y;
138 end;
140 procedure RGBToCMY16(R, G, B: Word; var C, M, Y: Word);
141 begin
142 C := 65535 - R;
143 M := 65535 - G;
144 Y := 65535 - B;
145 end;
147 procedure CMYToRGB16(C, M, Y: Word; var R, G, B: Word);
148 begin
149 R := 65535 - C;
150 G := 65535 - M;
151 B := 65535 - Y;
152 end;
154 procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
155 begin
156 RGBToCMY(R, G, B, C, M, Y);
157 K := Min(C, Min(M, Y));
158 if K = 255 then
159 begin
160 C := 0;
161 M := 0;
162 Y := 0;
163 end
164 else
165 begin
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));
169 end;
170 end;
172 procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
173 begin
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));
177 end;
179 procedure RGBToCMYK16(R, G, B: Word; var C, M, Y, K: Word);
180 begin
181 RGBToCMY16(R, G, B, C, M, Y);
182 K := Min(C, Min(M, Y));
183 if K = 65535 then
184 begin
185 C := 0;
186 M := 0;
187 Y := 0;
188 end
189 else
190 begin
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));
194 end;
195 end;
197 procedure CMYKToRGB16(C, M, Y, K: Word; var R, G, B: Word);
198 begin
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);
202 end;
204 procedure RGBToYCoCg(R, G, B: Byte; var Y, Co, Cg: Byte);
205 begin
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);
210 end;
212 procedure YCoCgToRGB(Y, Co, Cg: Byte; var R, G, B: Byte);
213 var
214 CoInt, CgInt: Integer;
215 begin
216 CoInt := Co - 128;
217 CgInt := Cg - 128;
218 R := ClampToByte(Y + CoInt - CgInt);
219 G := ClampToByte(Y + CgInt);
220 B := ClampToByte(Y - CoInt - CgInt);
221 end;
224 File Notes:
226 -- TODOS ----------------------------------------------------
227 - nothing now
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!)
245 end.