xref: /haiku/headers/libs/agg/agg_scanline_p.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.2
3 // Copyright (C) 2002-2004 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 scanline_p - a general purpose scanline container with packed spans.
17 //
18 //----------------------------------------------------------------------------
19 #ifndef AGG_SCANLINE_P_INCLUDED
20 #define AGG_SCANLINE_P_INCLUDED
21 
22 #include "agg_basics.h"
23 
24 namespace agg
25 {
26 
27     //==============================================================scanline_p
28     //
29     // This is a general purpose scaline container which supports the interface
30     // used in the rasterizer::render(). See description of agg_scanline_u
31     // for details.
32     //
33     //------------------------------------------------------------------------
34     template<class T> class scanline_p
35     {
36     public:
37         typedef T cover_type;
38 
39         struct span
40         {
41             int16    x;
42             int16    len; // If negative, it's a solid span, covers is valid
43             const T* covers;
44         };
45 
46         typedef span* iterator;
47         typedef const span* const_iterator;
48 
49         ~scanline_p()
50         {
51             delete [] m_spans;
52             delete [] m_covers;
53         }
54 
55         scanline_p() :
56             m_max_len(0),
57             m_last_x(0x7FFFFFF0),
58             m_covers(0),
59             m_cover_ptr(0),
60             m_spans(0),
61             m_cur_span(0)
62         {
63         }
64 
65         void reset(int min_x, int max_x);
66         void add_cell(int x, unsigned cover);
67         void add_cells(int x, unsigned len, const T* covers);
68         void add_span(int x, unsigned len, unsigned cover);
69         void finalize(int y) { m_y = y; }
70         void reset_spans();
71 
72         int            y()         const { return m_y; }
73         unsigned       num_spans() const { return unsigned(m_cur_span - m_spans); }
74         const_iterator begin()     const { return m_spans + 1; }
75 
76     private:
77         scanline_p(const scanline_p<T>&);
78         const scanline_p<T>& operator = (const scanline_p<T>&);
79 
80         unsigned m_max_len;
81         int      m_last_x;
82         int      m_y;
83         T*       m_covers;
84         T*       m_cover_ptr;
85         span*    m_spans;
86         span*    m_cur_span;
87     };
88 
89 
90     //------------------------------------------------------------------------
91     template<class T>
92     void scanline_p<T>::reset(int min_x, int max_x)
93     {
94         unsigned max_len = max_x - min_x + 3;
95         if(max_len > m_max_len)
96         {
97             delete [] m_spans;
98             delete [] m_covers;
99             m_covers  = new T [max_len];
100             m_spans   = new span [max_len];
101             m_max_len = max_len;
102         }
103         m_last_x    = 0x7FFFFFF0;
104         m_cover_ptr = m_covers;
105         m_cur_span  = m_spans;
106         m_cur_span->len = 0;
107     }
108 
109 
110     //------------------------------------------------------------------------
111     template<class T>
112     void scanline_p<T>::reset_spans()
113     {
114         m_last_x    = 0x7FFFFFF0;
115         m_cover_ptr = m_covers;
116         m_cur_span  = m_spans;
117         m_cur_span->len = 0;
118     }
119 
120 
121     //------------------------------------------------------------------------
122     template<class T>
123     void scanline_p<T>::add_cell(int x, unsigned cover)
124     {
125         *m_cover_ptr = (T)cover;
126         if(x == m_last_x+1 && m_cur_span->len > 0)
127         {
128             m_cur_span->len++;
129         }
130         else
131         {
132             m_cur_span++;
133             m_cur_span->covers = m_cover_ptr;
134             m_cur_span->x = (int16)x;
135             m_cur_span->len = 1;
136         }
137         m_last_x = x;
138         m_cover_ptr++;
139     }
140 
141 
142     //------------------------------------------------------------------------
143     template<class T>
144     void scanline_p<T>::add_cells(int x, unsigned len, const T* covers)
145     {
146         memcpy(m_cover_ptr, covers, len * sizeof(T));
147         if(x == m_last_x+1 && m_cur_span->len > 0)
148         {
149             m_cur_span->len += (int16)len;
150         }
151         else
152         {
153             m_cur_span++;
154             m_cur_span->covers = m_cover_ptr;
155             m_cur_span->x = (int16)x;
156             m_cur_span->len = (int16)len;
157         }
158         m_cover_ptr += len;
159         m_last_x = x + len - 1;
160     }
161 
162 
163     //------------------------------------------------------------------------
164     template<class T>
165     void scanline_p<T>::add_span(int x, unsigned len, unsigned cover)
166     {
167         if(x == m_last_x+1 &&
168            m_cur_span->len < 0 &&
169            cover == *m_cur_span->covers)
170         {
171             m_cur_span->len -= (int16)len;
172         }
173         else
174         {
175             *m_cover_ptr = (T)cover;
176             m_cur_span++;
177             m_cur_span->covers = m_cover_ptr++;
178             m_cur_span->x      = (int16)x;
179             m_cur_span->len    = -((int16)len);
180         }
181         m_last_x = x + len - 1;
182     }
183 
184 
185     //=============================================================scanline_p8
186     typedef scanline_p<int8u> scanline_p8;
187 
188     //============================================================scanline_p16
189     typedef scanline_p<int16u> scanline_p16;
190 
191     //============================================================scanline_p32
192     typedef scanline_p<int32u> scanline_p32;
193 }
194 
195 
196 #endif
197 
198