vfs: check userland buffers before reading them.
[haiku.git] / headers / libs / agg / agg_rendering_buffer.h
blob54268bd788ebc2835412ac92229176cf4916670e
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 rendering_buffer
18 //----------------------------------------------------------------------------
20 #ifndef AGG_RENDERING_BUFFER_INCLUDED
21 #define AGG_RENDERING_BUFFER_INCLUDED
23 #include "agg_array.h"
25 namespace agg
28 //==========================================================row_ptr_cache
29 template<class T> class row_ptr_cache
31 public:
32 //--------------------------------------------------------------------
33 struct row_data
35 int x1, x2;
36 const int8u* ptr;
37 row_data() {}
38 row_data(int x1_, int x2_, const int8u* ptr_) :
39 x1(x1_), x2(x2_), ptr(ptr_) {}
42 //-------------------------------------------------------------------
43 row_ptr_cache() :
44 m_buf(0),
45 m_rows(),
46 m_width(0),
47 m_height(0),
48 m_stride(0)
52 //--------------------------------------------------------------------
53 row_ptr_cache(T* buf, unsigned width, unsigned height, int stride) :
54 m_buf(0),
55 m_rows(),
56 m_width(0),
57 m_height(0),
58 m_stride(0)
60 attach(buf, width, height, stride);
63 //--------------------------------------------------------------------
64 void attach(T* buf, unsigned width, unsigned height, int stride)
66 m_buf = buf;
67 m_width = width;
68 m_height = height;
69 m_stride = stride;
70 if(height > m_rows.size())
72 m_rows.resize(height);
75 T* row_ptr = m_buf;
77 if(stride < 0)
79 row_ptr = m_buf - int(height - 1) * stride;
82 T** rows = &m_rows[0];
84 while(height--)
86 *rows++ = row_ptr;
87 row_ptr += stride;
91 //--------------------------------------------------------------------
92 T* buf() { return m_buf; }
93 const T* buf() const { return m_buf; }
94 unsigned width() const { return m_width; }
95 unsigned height() const { return m_height; }
96 int stride() const { return m_stride; }
97 unsigned stride_abs() const
99 return (m_stride < 0) ? unsigned(-m_stride) : unsigned(m_stride);
102 //--------------------------------------------------------------------
103 T* row_ptr(int, int y, unsigned) { return m_rows[y]; }
104 T* row_ptr(int y) { return m_rows[y]; }
105 const T* row_ptr(int y) const { return m_rows[y]; }
106 row_data row (int y) const { return row_data(0, m_width-1, m_rows[y]); }
108 //--------------------------------------------------------------------
109 T const* const* rows() const { return &m_rows[0]; }
111 //--------------------------------------------------------------------
112 template<class RenBuf>
113 void copy_from(const RenBuf& src)
115 unsigned h = height();
116 if(src.height() < h) h = src.height();
118 unsigned l = stride_abs();
119 if(src.stride_abs() < l) l = src.stride_abs();
121 l *= sizeof(T);
123 unsigned y;
124 unsigned w = width();
125 for (y = 0; y < h; y++)
127 memcpy(row_ptr(0, y, w), src.row_ptr(y), l);
131 //--------------------------------------------------------------------
132 void clear(T value)
134 unsigned y;
135 unsigned w = width();
136 unsigned stride = stride_abs();
137 for(y = 0; y < height(); y++)
139 T* p = row_ptr(0, y, w);
140 unsigned x;
141 for(x = 0; x < stride; x++)
143 *p++ = value;
148 private:
149 //--------------------------------------------------------------------
150 T* m_buf; // Pointer to renrdering buffer
151 pod_array<T*> m_rows; // Pointers to each row of the buffer
152 unsigned m_width; // Width in pixels
153 unsigned m_height; // Height in pixels
154 int m_stride; // Number of bytes per row. Can be < 0
159 //========================================================rendering_buffer
160 typedef row_ptr_cache<int8u> rendering_buffer;
165 #endif