Statistics
| Branch: | Tag: | Revision:

root / Imaging / ImagingColors.pas @ 0:95bd93c28625

History | View | Annotate | Download (6.5 kB)

1
{
2
  $Id: ImagingColors.pas 74 2007-03-12 15:04:04Z galfar $
3
  Vampyre Imaging Library
4
  by Marek Mauder 
5
  http://imaginglib.sourceforge.net
6
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
11
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.
15
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.
25
26
  For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
27
}
28
29
{ This unit contains functions for manipulating and converting color values.}
30
unit ImagingColors;
31
32
interface
33
34
{$I ImagingOptions.inc}
35
36
uses
37
  SysUtils, ImagingTypes, ImagingUtility;
38
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);
43
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);
52
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);
61
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);
70
71
implementation
72
73
procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
74
begin
75
  Y := ClampToByte(Round( 0.257 * R + 0.504 * G + 0.098 * B) + 16);
76
  V := ClampToByte(Round( 0.439 * R - 0.368 * G - 0.071 * B) + 128);
77
  U := ClampToByte(Round(-0.148 * R - 0.291 * G + 0.439 * B) + 128);
78
end;
79
80
procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
81
var
82
  CY, CU, CV: LongInt;
83
begin
84
  CY := Y - 16;
85
  CU := U - 128;
86
  CV := V - 128;
87
  R := ClampToByte(Round(1.164 * CY - 0.002 * CU + 1.596 * CV));
88
  G := ClampToByte(Round(1.164 * CY - 0.391 * CU - 0.813 * CV));
89
  B := ClampToByte(Round(1.164 * CY + 2.018 * CU - 0.001 * CV));
90
end;
91
92
procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
93
begin
94
  Y  := ClampToByte(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
95
  Cb := ClampToByte(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B  + 128));
96
  Cr := ClampToByte(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B  + 128));
97
end;
98
99
procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
100
begin
101
  R := ClampToByte(Round(Y                        + 1.40200 * (Cr - 128)));
102
  G := ClampToByte(Round(Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128)));
103
  B := ClampToByte(Round(Y + 1.77200 * (Cb - 128)));
104
end;
105
106
procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
107
begin
108
  Y  := ClampToWord(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
109
  Cb := ClampToWord(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B  + 32768));
110
  Cr := ClampToWord(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B  + 32768));
111
end;
112
113
procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
114
begin
115
  R := ClampToWord(Round(Y                          + 1.40200 * (Cr - 32768)));
116
  G := ClampToWord(Round(Y - 0.34414 * (Cb - 32768) - 0.71414 * (Cr - 32768)));
117
  B := ClampToWord(Round(Y + 1.77200 * (Cb - 32768)));
118
end;
119
120
procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
121
begin
122
  C := 255 - R;
123
  M := 255 - G;
124
  Y := 255 - B;
125
end;
126
127
procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
128
begin
129
  R := 255 - C;
130
  G := 255 - M;
131
  B := 255 - Y;
132
end;
133
134
procedure RGBToCMY16(R, G, B: Word; var C, M, Y: Word);
135
begin
136
  C := 65535 - R;
137
  M := 65535 - G;
138
  Y := 65535 - B;
139
end;
140
141
procedure CMYToRGB16(C, M, Y: Word; var R, G, B: Word);
142
begin
143
  R := 65535 - C;
144
  G := 65535 - M;
145
  B := 65535 - Y;
146
end;
147
148
procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
149
begin
150
  RGBToCMY(R, G, B, C, M, Y);
151
  K := Min(C, Min(M, Y));
152
  if K > 0 then
153
  begin
154
    C := C - K;
155
    M := M - K;
156
    Y := Y - K;
157
  end;
158
end;
159
160
procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
161
begin
162
   R := (255 - (C - MulDiv(C, K, 255) + K));
163
   G := (255 - (M - MulDiv(M, K, 255) + K));
164
   B := (255 - (Y - MulDiv(Y, K, 255) + K));
165
end;
166
167
procedure RGBToCMYK16(R, G, B: Word; var C, M, Y, K: Word);
168
begin
169
  RGBToCMY16(R, G, B, C, M, Y);
170
  K := Min(C, Min(M, Y));
171
  if K > 0 then
172
  begin
173
    C := C - K;
174
    M := M - K;
175
    Y := Y - K;
176
  end;
177
end;
178
179
procedure CMYKToRGB16(C, M, Y, K: Word; var R, G, B: Word);
180
begin
181
  R := 65535 - (C - MulDiv(C, K, 65535) + K);
182
  G := 65535 - (M - MulDiv(M, K, 65535) + K);
183
  B := 65535 - (Y - MulDiv(Y, K, 65535) + K);
184
end;
185
186
{
187
  File Notes:
188
189
  -- TODOS ----------------------------------------------------
190
    - nothing now
191
192
  -- 0.23 Changes/Bug Fixes -----------------------------------
193
    - Added RGB<>CMY(K) converion functions for 16 bit channels
194
      (needed by PSD loading code).
195
196
  -- 0.21 Changes/Bug Fixes -----------------------------------
197
    - Added some color space conversion functions and LUTs
198
      (RGB/YUV/YCrCb/CMY/CMYK).
199
200
  -- 0.17 Changes/Bug Fixes -----------------------------------
201
    - unit created (empty!)
202
}
203
204
end.