Debian Codegraph @

extern/agg24-svn/include/agg_span_gradient_image.h

raw
1 //----------------------------------------------------------------------------
2 // AGG Contribution Pack - Gradients 1 (AGG CP - Gradients 1)
3 // http://milan.marusinec.sk/aggcp
4 //
5 // For Anti-Grain Geometry - Version 2.4
6 // http://www.antigrain.org
7 //
8 // Contribution Created By:
9 // Milan Marusinec alias Milano
10 // [email protected]
11 // Copyright (c) 2007-2008
12 //
13 // Permission to copy, use, modify, sell and distribute this software
14 // is granted provided this copyright notice appears in all copies.
15 // This software is provided "as is" without express or implied
16 // warranty, and with no claim as to its suitability for any purpose.
17 //
18 // [History] -----------------------------------------------------------------
19 //
20 // 03.02.2008-Milano: Ported from Object Pascal code of AggPas
21 //
22 #ifndef AGG_SPAN_GRADIENT_IMAGE_INCLUDED
23 #define AGG_SPAN_GRADIENT_IMAGE_INCLUDED
24
25 #include "agg_basics.h"
26 #include "agg_span_gradient.h"
27 #include "agg_color_rgba.h"
28 #include "agg_rendering_buffer.h"
29 #include "agg_pixfmt_rgba.h"
30
31 namespace agg
32 {
33
34 //==========================================================one_color_function
35 template<class ColorT> class one_color_function
36 {
37 public:
38 typedef ColorT color_type;
39
40 color_type m_color;
41
42 one_color_function() :
43 m_color()
44 {
45 }
46
47 static unsigned size() { return 1; }
48
49 const color_type& operator [] (unsigned i) const
50 {
51 return m_color;
52 }
53
54 color_type* operator [] (unsigned i)
55 {
56 return &m_color;
57 }
58 };
59
60 //==========================================================gradient_image
61 template<class ColorT> class gradient_image
62 {
63 private:
64 //------------ fields
65 typedef ColorT color_type;
66 typedef agg::pixfmt_rgba32 pixfmt_type;
67
68 agg::rgba8* m_buffer;
69
70 int m_alocdx;
71 int m_alocdy;
72 int m_width;
73 int m_height;
74
75 color_type* m_color;
76
77 one_color_function<color_type> m_color_function;
78
79 public:
80 gradient_image() :
81 m_color_function(),
82 m_buffer(NULL),
83 m_alocdx(0),
84 m_alocdy(0),
85 m_width(0),
86 m_height(0)
87 {
88 m_color = m_color_function[0 ];
89 }
90
91 ~gradient_image()
92 {
93 if (m_buffer) { delete [] m_buffer; }
94 }
95
96 void* image_create(int width, int height )
97 {
98 void* result = NULL;
99
100 if (width > m_alocdx || height > m_alocdy)
101 {
102 if (m_buffer) { delete [] m_buffer; }
103
104 m_buffer = NULL;
105 m_buffer = new agg::rgba8[width * height];
106
107 if (m_buffer)
108 {
109 m_alocdx = width;
110 m_alocdy = height;
111 }
112 else
113 {
114 m_alocdx = 0;
115 m_alocdy = 0;
116 };
117 };
118
119 if (m_buffer)
120 {
121 m_width = width;
122 m_height = height;
123
124 for (int rows = 0; rows < height; rows++)
125 {
126 agg::rgba8* row = &m_buffer[rows * m_alocdx ];
127 memset(row ,0 ,m_width * 4 );
128 };
129
130 result = m_buffer;
131 };
132 return result;
133 }
134
135 void* image_buffer() { return m_buffer; }
136 int image_width() { return m_width; }
137 int image_height() { return m_height; }
138 int image_stride() { return m_alocdx * 4; }
139
140 int calculate(int x, int y, int d) const
141 {
142 if (m_buffer)
143 {
144 int px = x >> agg::gradient_subpixel_shift;
145 int py = y >> agg::gradient_subpixel_shift;
146
147 px %= m_width;
148
149 if (px < 0)
150 {
151 px += m_width;
152 }
153
154 py %= m_height;
155
156 if (py < 0 )
157 {
158 py += m_height;
159 }
160
161 rgba8* pixel = &m_buffer[py * m_alocdx + px ];
162
163 m_color->r = pixel->r;
164 m_color->g = pixel->g;
165 m_color->b = pixel->b;
166 m_color->a = pixel->a;
167
168 }
169 else
170 {
171 m_color->r = 0;
172 m_color->g = 0;
173 m_color->b = 0;
174 m_color->a = 0;
175 }
176 return 0;
177 }
178
179 const one_color_function<color_type>& color_function() const
180 {
181 return m_color_function;
182 }
183
184 };
185
186 }
187
188 #endif