update dev300-m58
[ooovba.git] / agg / inc / agg_color_gray.h
blob6196177458e3094314d6be8ed2ead1436d7eeba8
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Adaptation for high precision colors has been sponsored by
17 // Liberty Technology Systems, Inc., visit http://lib-sys.com
19 // Liberty Technology Systems, Inc. is the provider of
20 // PostScript and PDF technology for software developers.
21 //
22 //----------------------------------------------------------------------------
24 // color types gray8, gray16
26 //----------------------------------------------------------------------------
28 #ifndef AGG_COLOR_GRAY_INCLUDED
29 #define AGG_COLOR_GRAY_INCLUDED
31 #include "agg_basics.h"
32 #include "agg_color_rgba.h"
34 namespace agg
37 //===================================================================gray8
38 struct gray8
40 typedef int8u value_type;
41 typedef int32u calc_type;
42 typedef int32 long_type;
43 enum
45 base_shift = 8,
46 base_size = 1 << base_shift,
47 base_mask = base_size - 1
49 typedef gray8 self_type;
51 value_type v;
52 value_type a;
54 //--------------------------------------------------------------------
55 gray8() {}
57 //--------------------------------------------------------------------
58 gray8(unsigned v_, unsigned a_=base_mask) :
59 v(int8u(v_)), a(int8u(a_)) {}
61 //--------------------------------------------------------------------
62 gray8(const self_type& c, unsigned a_) :
63 v(c.v), a(value_type(a_)) {}
65 //--------------------------------------------------------------------
66 gray8(const rgba& c) :
67 v(value_type((0.299*c.r + 0.587*c.g + 0.114*c.b) * double(base_mask) + 0.5)),
68 a(value_type(c.a * double(base_mask))) {}
70 //--------------------------------------------------------------------
71 gray8(const rgba& c, double a_) :
72 v(value_type((0.299*c.r + 0.587*c.g + 0.114*c.b) * double(base_mask) + 0.5)),
73 a(value_type(a_ * double(base_mask))) {}
75 //--------------------------------------------------------------------
76 gray8(const rgba8& c) :
77 v((c.r*77 + c.g*150 + c.b*29) >> 8),
78 a(c.a) {}
80 //--------------------------------------------------------------------
81 gray8(const rgba8& c, unsigned a_) :
82 v((c.r*77 + c.g*150 + c.b*29) >> 8),
83 a(a_) {}
85 //--------------------------------------------------------------------
86 void clear()
88 v = a = 0;
91 //--------------------------------------------------------------------
92 const self_type& transparent()
94 a = 0;
95 return *this;
98 //--------------------------------------------------------------------
99 void opacity(double a_)
101 if(a_ < 0.0) a_ = 0.0;
102 if(a_ > 1.0) a_ = 1.0;
103 a = value_type(a_ * double(base_mask));
106 //--------------------------------------------------------------------
107 double opacity() const
109 return double(a) / double(base_mask);
113 //--------------------------------------------------------------------
114 const self_type& premultiply()
116 if(a == base_mask) return *this;
117 if(a == 0)
119 v = 0;
120 return *this;
122 v = value_type((calc_type(v) * a) >> base_shift);
123 return *this;
126 //--------------------------------------------------------------------
127 const self_type& premultiply(unsigned a_)
129 if(a == base_mask && a_ >= base_mask) return *this;
130 if(a == 0 || a_ == 0)
132 v = a = 0;
133 return *this;
135 calc_type v_ = (calc_type(v) * a_) / a;
136 v = value_type((v_ > a_) ? a_ : v_);
137 a = value_type(a_);
138 return *this;
141 //--------------------------------------------------------------------
142 const self_type& demultiply()
144 if(a == base_mask) return *this;
145 if(a == 0)
147 v = 0;
148 return *this;
150 calc_type v_ = (calc_type(v) * base_mask) / a;
151 v = value_type((v_ > base_mask) ? base_mask : v_);
152 return *this;
155 //--------------------------------------------------------------------
156 self_type gradient(self_type c, double k) const
158 self_type ret;
159 calc_type ik = calc_type(k * base_size);
160 ret.v = value_type(calc_type(v) + (((calc_type(c.v) - v) * ik) >> base_shift));
161 ret.a = value_type(calc_type(a) + (((calc_type(c.a) - a) * ik) >> base_shift));
162 return ret;
165 //--------------------------------------------------------------------
166 static self_type no_color() { return self_type(0,0); }
170 //-------------------------------------------------------------gray8_pre
171 inline gray8 gray8_pre(unsigned v, unsigned a = gray8::base_mask)
173 return gray8(v,a).premultiply();
175 inline gray8 gray8_pre(const gray8& c, unsigned a)
177 return gray8(c,a).premultiply();
179 inline gray8 gray8_pre(const rgba& c)
181 return gray8(c).premultiply();
183 inline gray8 gray8_pre(const rgba& c, double a)
185 return gray8(c,a).premultiply();
187 inline gray8 gray8_pre(const rgba8& c)
189 return gray8(c).premultiply();
191 inline gray8 gray8_pre(const rgba8& c, unsigned a)
193 return gray8(c,a).premultiply();
199 //==================================================================gray16
200 struct gray16
202 typedef int16u value_type;
203 typedef int32u calc_type;
204 typedef int64 long_type;
205 enum
207 base_shift = 16,
208 base_size = 1 << base_shift,
209 base_mask = base_size - 1
211 typedef gray16 self_type;
213 value_type v;
214 value_type a;
216 //--------------------------------------------------------------------
217 gray16() {}
219 //--------------------------------------------------------------------
220 gray16(unsigned v_, unsigned a_=base_mask) :
221 v(int16u(v_)), a(int16u(a_)) {}
223 //--------------------------------------------------------------------
224 gray16(const self_type& c, unsigned a_) :
225 v(c.v), a(value_type(a_)) {}
227 //--------------------------------------------------------------------
228 gray16(const rgba& c) :
229 v(value_type((0.299*c.r + 0.587*c.g + 0.114*c.b) * double(base_mask) + 0.5)),
230 a(value_type(c.a * double(base_mask))) {}
232 //--------------------------------------------------------------------
233 gray16(const rgba& c, double a_) :
234 v(value_type((0.299*c.r + 0.587*c.g + 0.114*c.b) * double(base_mask) + 0.5)),
235 a(value_type(a_ * double(base_mask))) {}
237 //--------------------------------------------------------------------
238 gray16(const rgba8& c) :
239 v(c.r*77 + c.g*150 + c.b*29),
240 a((value_type(c.a) << 8) | c.a) {}
242 //--------------------------------------------------------------------
243 gray16(const rgba8& c, unsigned a_) :
244 v(c.r*77 + c.g*150 + c.b*29),
245 a((value_type(a_) << 8) | c.a) {}
247 //--------------------------------------------------------------------
248 void clear()
250 v = a = 0;
253 //--------------------------------------------------------------------
254 const self_type& transparent()
256 a = 0;
257 return *this;
260 //--------------------------------------------------------------------
261 void opacity(double a_)
263 if(a_ < 0.0) a_ = 0.0;
264 if(a_ > 1.0) a_ = 1.0;
265 a = value_type(a_ * double(base_mask));
268 //--------------------------------------------------------------------
269 double opacity() const
271 return double(a) / double(base_mask);
275 //--------------------------------------------------------------------
276 const self_type& premultiply()
278 if(a == base_mask) return *this;
279 if(a == 0)
281 v = 0;
282 return *this;
284 v = value_type((calc_type(v) * a) >> base_shift);
285 return *this;
288 //--------------------------------------------------------------------
289 const self_type& premultiply(unsigned a_)
291 if(a == base_mask && a_ >= base_mask) return *this;
292 if(a == 0 || a_ == 0)
294 v = a = 0;
295 return *this;
297 calc_type v_ = (calc_type(v) * a_) / a;
298 v = value_type((v_ > a_) ? a_ : v_);
299 a = value_type(a_);
300 return *this;
303 //--------------------------------------------------------------------
304 const self_type& demultiply()
306 if(a == base_mask) return *this;
307 if(a == 0)
309 v = 0;
310 return *this;
312 calc_type v_ = (calc_type(v) * base_mask) / a;
313 v = value_type((v_ > base_mask) ? base_mask : v_);
314 return *this;
317 //--------------------------------------------------------------------
318 self_type gradient(self_type c, double k) const
320 self_type ret;
321 calc_type ik = calc_type(k * base_size);
322 ret.v = value_type(calc_type(v) + (((calc_type(c.v) - v) * ik) >> base_shift));
323 ret.a = value_type(calc_type(a) + (((calc_type(c.a) - a) * ik) >> base_shift));
324 return ret;
327 //--------------------------------------------------------------------
328 static self_type no_color() { return self_type(0,0); }
332 //------------------------------------------------------------gray16_pre
333 inline gray16 gray16_pre(unsigned v, unsigned a = gray16::base_mask)
335 return gray16(v,a).premultiply();
337 inline gray16 gray16_pre(const gray16& c, unsigned a)
339 return gray16(c,a).premultiply();
341 inline gray16 gray16_pre(const rgba& c)
343 return gray16(c).premultiply();
345 inline gray16 gray16_pre(const rgba& c, double a)
347 return gray16(c,a).premultiply();
349 inline gray16 gray16_pre(const rgba8& c)
351 return gray16(c).premultiply();
353 inline gray16 gray16_pre(const rgba8& c, unsigned a)
355 return gray16(c,a).premultiply();
364 #endif