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//----------------------------------------------------------------------------
15//
16// class rendering_buffer
17//
18//----------------------------------------------------------------------------
19
20#ifndef AGG_RENDERING_BUFFER_INCLUDED
21#define AGG_RENDERING_BUFFER_INCLUDED
22
23#include "agg_array.h"
24
25namespace agg
26{
27
28    //==========================================================row_ptr_cache
29    template<class T> class row_ptr_cache
30    {
31    public:
32        //--------------------------------------------------------------------
33        struct row_data
34        {
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_) {}
40        };
41
42        //-------------------------------------------------------------------
43        row_ptr_cache() :
44            m_buf(0),
45            m_rows(),
46            m_width(0),
47            m_height(0),
48            m_stride(0)
49        {
50        }
51
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)
59        {
60            attach(buf, width, height, stride);
61        }
62
63        //--------------------------------------------------------------------
64        void attach(T* buf, unsigned width, unsigned height, int stride)
65        {
66            m_buf = buf;
67            m_width = width;
68            m_height = height;
69            m_stride = stride;
70            if(height > m_rows.size())
71            {
72                m_rows.resize(height);
73            }
74
75            T* row_ptr = m_buf;
76
77            if(stride < 0)
78            {
79                row_ptr = m_buf - int(height - 1) * stride;
80            }
81
82            T** rows = &m_rows[0];
83
84            while(height--)
85            {
86                *rows++ = row_ptr;
87                row_ptr += stride;
88            }
89        }
90
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
98        {
99            return (m_stride < 0) ? unsigned(-m_stride) : unsigned(m_stride);
100        }
101
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]); }
107
108        //--------------------------------------------------------------------
109        T const* const* rows() const { return &m_rows[0]; }
110
111        //--------------------------------------------------------------------
112        template<class RenBuf>
113        void copy_from(const RenBuf& src)
114        {
115            unsigned h = height();
116            if(src.height() < h) h = src.height();
117
118            unsigned l = stride_abs();
119            if(src.stride_abs() < l) l = src.stride_abs();
120
121            l *= sizeof(T);
122
123            unsigned y;
124            unsigned w = width();
125            for (y = 0; y < h; y++)
126            {
127                memcpy(row_ptr(0, y, w), src.row_ptr(y), l);
128            }
129        }
130
131        //--------------------------------------------------------------------
132        void clear(T value)
133        {
134            unsigned y;
135            unsigned w = width();
136            unsigned stride = stride_abs();
137            for(y = 0; y < height(); y++)
138            {
139                T* p = row_ptr(0, y, w);
140                unsigned x;
141                for(x = 0; x < stride; x++)
142                {
143                    *p++ = value;
144                }
145            }
146        }
147
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
155    };
156
157
158
159    //========================================================rendering_buffer
160    typedef row_ptr_cache<int8u> rendering_buffer;
161
162}
163
164
165#endif
166