1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.2
// Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//----------------------------------------------------------------------------
#ifndef AGG_SCANLINE_U_INCLUDED
#define AGG_SCANLINE_U_INCLUDED
#include <string.h>
#include "agg_basics.h"
namespace agg
{
//==============================================================scanline_u
//
// Unpacked scanline container class
//
// This class is used to transfer data from a scanline rastyerizer
// to the rendering buffer. It's organized very simple. The class stores
// information of horizontal spans to render it into a pixel-map buffer.
// Each span has staring X, length, and an array of bytes that determine the
// cover-values for each pixel.
// Before using this class you should know the minimal and maximal pixel
// coordinates of your scanline. The protocol of using is:
// 1. reset(min_x, max_x)
// 2. add_cell() / add_span() - accumulate scanline.
// When forming one scanline the next X coordinate must be always greater
// than the last stored one, i.e. it works only with ordered coordinates.
// 3. Call finalize(y) and render the scanline.
// 3. Call reset_spans() to prepare for the new scanline.
//
// 4. Rendering:
//
// Scanline provides an iterator class that allows you to extract
// the spans and the cover values for each pixel. Be aware that clipping
// has not been done yet, so you should perform it yourself.
// Use scanline_u8::iterator to render spans:
//-------------------------------------------------------------------------
//
// int y = sl.y(); // Y-coordinate of the scanline
//
// ************************************
// ...Perform vertical clipping here...
// ************************************
//
// scanline_u8::const_iterator span = sl.begin();
//
// unsigned char* row = m_rbuf->row(y); // The the address of the beginning
// // of the current row
//
// unsigned num_spans = sl.num_spans(); // Number of spans. It's guaranteed that
// // num_spans is always greater than 0.
//
// do
// {
// const scanline_u8::cover_type* covers =
// span->covers; // The array of the cover values
//
// int num_pix = span->len; // Number of pixels of the span.
// // Always greater than 0, still it's
// // better to use "int" instead of
// // "unsigned" because it's more
// // convenient for clipping
// int x = span->x;
//
// **************************************
// ...Perform horizontal clipping here...
// ...you have x, covers, and pix_count..
// **************************************
//
// unsigned char* dst = row + x; // Calculate the start address of the row.
// // In this case we assume a simple
// // grayscale image 1-byte per pixel.
// do
// {
// *dst++ = *covers++; // Hypotetical rendering.
// }
// while(--num_pix);
//
// ++span;
// }
// while(--num_spans); // num_spans cannot be 0, so this loop is quite safe
//------------------------------------------------------------------------
//
// The question is: why should we accumulate the whole scanline when we
// could render just separate spans when they're ready?
// That's because using the scaline is generally faster. When is consists
// of more than one span the conditions for the processor cash system
// are better, because switching between two different areas of memory
// (that can be very large) occures less frequently.
//------------------------------------------------------------------------
template<class T> class scanline_u
{
public:
typedef T cover_type;
//--------------------------------------------------------------------
struct span
{
int16 x;
int16 len;
cover_type* covers;
};
typedef span* iterator;
typedef const span* const_iterator;
//--------------------------------------------------------------------
~scanline_u();
scanline_u();
void reset(int min_x, int max_x);
void add_cell(int x, unsigned cover);
void add_cells(int x, unsigned len, const T* covers);
void add_span(int x, unsigned len, unsigned cover);
void finalize(int y) { m_y = y; }
void reset_spans();
int y() const { return m_y; }
unsigned num_spans() const { return unsigned(m_cur_span - m_spans); }
const_iterator begin() const { return m_spans + 1; }
iterator begin() { return m_spans + 1; }
private:
scanline_u<T>(const scanline_u<T>&);
const scanline_u<T>& operator = (const scanline_u<T>&);
private:
int m_min_x;
unsigned m_max_len;
int m_last_x;
int m_y;
cover_type* m_covers;
span* m_spans;
span* m_cur_span;
};
//------------------------------------------------------------------------
template<class T> scanline_u<T>::~scanline_u()
{
delete [] m_spans;
delete [] m_covers;
}
//------------------------------------------------------------------------
template<class T> scanline_u<T>::scanline_u() :
m_min_x(0),
m_max_len(0),
m_last_x(0x7FFFFFF0),
m_covers(0),
m_spans(0),
m_cur_span(0)
{
}
//------------------------------------------------------------------------
template<class T> void scanline_u<T>::reset(int min_x, int max_x)
{
unsigned max_len = max_x - min_x + 2;
if(max_len > m_max_len)
{
delete [] m_spans;
delete [] m_covers;
m_covers = new cover_type [max_len];
m_spans = new span [max_len];
m_max_len = max_len;
}
m_last_x = 0x7FFFFFF0;
m_min_x = min_x;
m_cur_span = m_spans;
}
//------------------------------------------------------------------------
template<class T> inline void scanline_u<T>::reset_spans()
{
m_last_x = 0x7FFFFFF0;
m_cur_span = m_spans;
}
//------------------------------------------------------------------------
template<class T> inline void scanline_u<T>::add_cell(int x, unsigned cover)
{
x -= m_min_x;
m_covers[x] = (unsigned char)cover;
if(x == m_last_x+1)
{
m_cur_span->len++;
}
else
{
m_cur_span++;
m_cur_span->x = (int16)(x + m_min_x);
m_cur_span->len = 1;
m_cur_span->covers = m_covers + x;
}
m_last_x = x;
}
//------------------------------------------------------------------------
template<class T> void scanline_u<T>::add_cells(int x, unsigned len, const T* covers)
{
x -= m_min_x;
memcpy(m_covers + x, covers, len * sizeof(T));
if(x == m_last_x+1)
{
m_cur_span->len += (int16)len;
}
else
{
m_cur_span++;
m_cur_span->x = (int16)(x + m_min_x);
m_cur_span->len = (int16)len;
m_cur_span->covers = m_covers + x;
}
m_last_x = x + len - 1;
}
//------------------------------------------------------------------------
template<class T> void scanline_u<T>::add_span(int x, unsigned len, unsigned cover)
{
x -= m_min_x;
memset(m_covers + x, cover, len);
if(x == m_last_x+1)
{
m_cur_span->len += (int16)len;
}
else
{
m_cur_span++;
m_cur_span->x = (int16)(x + m_min_x);
m_cur_span->len = (int16)len;
m_cur_span->covers = m_covers + x;
}
m_last_x = x + len - 1;
}
//=============================================================scanline_u8
typedef scanline_u<int8u> scanline_u8;
//============================================================scanline_u16
typedef scanline_u<int16u> scanline_u16;
//============================================================scanline_u32
typedef scanline_u<int32u> scanline_u32;
//=============================================================scanline_am
//
// The scanline container with alpha-masking
//
//------------------------------------------------------------------------
template<class AlphaMask, class CoverT>
class scanline_am : public scanline_u<CoverT>
{
public:
typedef AlphaMask alpha_mask_type;
typedef CoverT cover_type;
typedef scanline_u<CoverT> scanline_type;
scanline_am() : scanline_type(), m_alpha_mask(0) {}
scanline_am(const AlphaMask& am) : scanline_type(), m_alpha_mask(&am) {}
//--------------------------------------------------------------------
void finalize(int span_y)
{
scanline_u<CoverT>::finalize(span_y);
if(m_alpha_mask)
{
typename scanline_type::iterator span = scanline_type::begin();
unsigned count = scanline_type::num_spans();
do
{
m_alpha_mask->combine_hspan(span->x,
scanline_type::y(),
span->covers,
span->len);
++span;
}
while(--count);
}
}
private:
const AlphaMask* m_alpha_mask;
};
//==========================================================scanline_u8_am
template<class AlphaMask>
class scanline_u8_am : public scanline_am<AlphaMask, int8u>
{
public:
typedef AlphaMask alpha_mask_type;
typedef int8u cover_type;
typedef scanline_am<alpha_mask_type, cover_type> self_type;
scanline_u8_am() : self_type() {}
scanline_u8_am(const AlphaMask& am) : self_type(am) {}
};
}
#endif
|