vfs: check userland buffers before reading them.
[haiku.git] / headers / libs / agg / agg_renderer_primitives.h
blobfe4780d5133ff69eb6ce369380a48e61cfe87967
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
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 // class renderer_primitives
18 //----------------------------------------------------------------------------
20 #ifndef AGG_RENDERER_PRIMITIVES_INCLUDED
21 #define AGG_RENDERER_PRIMITIVES_INCLUDED
23 #include "agg_basics.h"
24 #include "agg_renderer_base.h"
25 #include "agg_dda_line.h"
26 #include "agg_ellipse_bresenham.h"
28 namespace agg
30 //-----------------------------------------------------renderer_primitives
31 template<class BaseRenderer> class renderer_primitives
33 public:
34 typedef BaseRenderer base_ren_type;
35 typedef typename base_ren_type::color_type color_type;
37 //--------------------------------------------------------------------
38 renderer_primitives(base_ren_type& ren) :
39 m_ren(&ren),
40 m_fill_color(),
41 m_line_color(),
42 m_curr_x(0),
43 m_curr_y(0)
45 void attach(base_ren_type& ren) { m_ren = &ren; }
47 //--------------------------------------------------------------------
48 static int coord(double c)
50 return iround(c * line_bresenham_interpolator::subpixel_scale);
53 //--------------------------------------------------------------------
54 void fill_color(const color_type& c) { m_fill_color = c; }
55 void line_color(const color_type& c) { m_line_color = c; }
56 const color_type& fill_color() const { return m_fill_color; }
57 const color_type& line_color() const { return m_line_color; }
59 //--------------------------------------------------------------------
60 void rectangle(int x1, int y1, int x2, int y2)
62 m_ren->blend_hline(x1, y1, x2-1, m_line_color, cover_full);
63 m_ren->blend_vline(x2, y1, y2-1, m_line_color, cover_full);
64 m_ren->blend_hline(x1+1, y2, x2, m_line_color, cover_full);
65 m_ren->blend_vline(x1, y1+1, y2, m_line_color, cover_full);
68 //--------------------------------------------------------------------
69 void solid_rectangle(int x1, int y1, int x2, int y2)
71 m_ren->blend_bar(x1, y1, x2, y2, m_fill_color, cover_full);
74 //--------------------------------------------------------------------
75 void outlined_rectangle(int x1, int y1, int x2, int y2)
77 rectangle(x1, y1, x2, y2);
78 m_ren->blend_bar(x1+1, y1+1, x2-1, y2-1, m_fill_color, cover_full);
81 //--------------------------------------------------------------------
82 void ellipse(int x, int y, int rx, int ry)
84 ellipse_bresenham_interpolator ei(rx, ry);
85 int dx = 0;
86 int dy = -ry;
89 dx += ei.dx();
90 dy += ei.dy();
91 m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
92 m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
93 m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
94 m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
95 ++ei;
97 while(dy < 0);
100 //--------------------------------------------------------------------
101 void solid_ellipse(int x, int y, int rx, int ry)
103 ellipse_bresenham_interpolator ei(rx, ry);
104 int dx = 0;
105 int dy = -ry;
106 int dy0 = dy;
107 int dx0 = dx;
111 dx += ei.dx();
112 dy += ei.dy();
114 if(dy != dy0)
116 m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
117 m_ren->blend_hline(x-dx0, y-dy0, x+dx0, m_fill_color, cover_full);
119 dx0 = dx;
120 dy0 = dy;
121 ++ei;
123 while(dy < 0);
124 m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
127 //--------------------------------------------------------------------
128 void outlined_ellipse(int x, int y, int rx, int ry)
130 ellipse_bresenham_interpolator ei(rx, ry);
131 int dx = 0;
132 int dy = -ry;
136 dx += ei.dx();
137 dy += ei.dy();
139 m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
140 m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
141 m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
142 m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
144 if(ei.dy() && dx)
146 m_ren->blend_hline(x-dx+1, y+dy, x+dx-1, m_fill_color, cover_full);
147 m_ren->blend_hline(x-dx+1, y-dy, x+dx-1, m_fill_color, cover_full);
149 ++ei;
151 while(dy < 0);
154 //--------------------------------------------------------------------
155 void line(int x1, int y1, int x2, int y2, bool last=false)
157 line_bresenham_interpolator li(x1, y1, x2, y2);
159 unsigned len = li.len();
160 if(len == 0)
162 if(last)
164 m_ren->blend_pixel(li.line_lr(x1), li.line_lr(y1), m_line_color, cover_full);
166 return;
169 if(last) ++len;
171 if(li.is_ver())
175 m_ren->blend_pixel(li.x2(), li.y1(), m_line_color, cover_full);
176 li.vstep();
178 while(--len);
180 else
184 m_ren->blend_pixel(li.x1(), li.y2(), m_line_color, cover_full);
185 li.hstep();
187 while(--len);
191 //--------------------------------------------------------------------
192 void move_to(int x, int y)
194 m_curr_x = x;
195 m_curr_y = y;
198 //--------------------------------------------------------------------
199 void line_to(int x, int y, bool last=false)
201 line(m_curr_x, m_curr_y, x, y, last);
202 m_curr_x = x;
203 m_curr_y = y;
206 //--------------------------------------------------------------------
207 const base_ren_type& ren() const { return *m_ren; }
208 base_ren_type& ren() { return *m_ren; }
210 //--------------------------------------------------------------------
211 const rendering_buffer& rbuf() const { return m_ren->rbuf(); }
212 rendering_buffer& rbuf() { return m_ren->rbuf(); }
214 private:
215 base_ren_type* m_ren;
216 color_type m_fill_color;
217 color_type m_line_color;
218 int m_curr_x;
219 int m_curr_y;
224 #endif