DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / ImagingColors.pas
1 {
2 Vampyre Imaging Library
3 by Marek Mauder
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
26 }
28 { This unit contains functions for manipulating and converting color values.}
29 unit ImagingColors;
31 interface
33 {$I ImagingOptions.inc}
35 uses
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);
78 implementation
80 procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
81 begin
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);
85 end;
87 procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
88 var
89 CY, CU, CV: LongInt;
90 begin
91 CY := Y - 16;
92 CU := U - 128;
93 CV := V - 128;
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));
97 end;
99 procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
100 begin
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));
104 end;
106 procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
107 begin
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)));
111 end;
113 procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
114 begin
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));
118 end;
120 procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
121 begin
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)));
125 end;
127 procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
128 begin
129 C := 255 - R;
130 M := 255 - G;
131 Y := 255 - B;
132 end;
134 procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
135 begin
136 R := 255 - C;
137 G := 255 - M;
138 B := 255 - Y;
139 end;
141 procedure RGBToCMY16(R, G, B: Word; var C, M, Y: Word);
142 begin
143 C := 65535 - R;
144 M := 65535 - G;
145 Y := 65535 - B;
146 end;
148 procedure CMYToRGB16(C, M, Y: Word; var R, G, B: Word);
149 begin
150 R := 65535 - C;
151 G := 65535 - M;
152 B := 65535 - Y;
153 end;
155 procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
156 begin
157 RGBToCMY(R, G, B, C, M, Y);
158 K := Min(C, Min(M, Y));
159 if K = 255 then
160 begin
161 C := 0;
162 M := 0;
163 Y := 0;
164 end
165 else
166 begin
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));
170 end;
171 end;
173 procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
174 begin
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));
178 end;
180 procedure RGBToCMYK16(R, G, B: Word; var C, M, Y, K: Word);
181 begin
182 RGBToCMY16(R, G, B, C, M, Y);
183 K := Min(C, Min(M, Y));
184 if K = 65535 then
185 begin
186 C := 0;
187 M := 0;
188 Y := 0;
189 end
190 else
191 begin
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));
195 end;
196 end;
198 procedure CMYKToRGB16(C, M, Y, K: Word; var R, G, B: Word);
199 begin
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);
203 end;
205 procedure RGBToYCoCg(R, G, B: Byte; var Y, Co, Cg: Byte);
206 begin
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);
211 end;
213 procedure YCoCgToRGB(Y, Co, Cg: Byte; var R, G, B: Byte);
214 var
215 CoInt, CgInt: Integer;
216 begin
217 CoInt := Co - 128;
218 CgInt := Cg - 128;
219 R := ClampToByte(Y + CoInt - CgInt);
220 G := ClampToByte(Y + CgInt);
221 B := ClampToByte(Y - CoInt - CgInt);
222 end;
225 File Notes:
227 -- TODOS ----------------------------------------------------
228 - nothing now
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!)
246 end.