Debian Codegraph @

extern/agg24-svn/include/agg_rasterizer_scanline_aa_nogamma.h

raw
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 //
12 // The author gratefully acknowleges the support of David Turner,
13 // Robert Wilhelm, and Werner Lemberg - the authors of the FreeType
14 // libray - in producing this work. See http://www.freetype.org for details.
15 //
16 //----------------------------------------------------------------------------
17 // Contact: [email protected]
18 // [email protected]
19 // http://www.antigrain.com
20 //----------------------------------------------------------------------------
21 //
22 // Adaptation for 32-bit screen coordinates has been sponsored by
23 // Liberty Technology Systems, Inc., visit http://lib-sys.com
24 //
25 // Liberty Technology Systems, Inc. is the provider of
26 // PostScript and PDF technology for software developers.
27 //
28 //----------------------------------------------------------------------------
29 #ifndef AGG_RASTERIZER_SCANLINE_AA_NOGAMMA_INCLUDED
30 #define AGG_RASTERIZER_SCANLINE_AA_NOGAMMA_INCLUDED
31
32 #include "agg_rasterizer_cells_aa.h"
33 #include "agg_rasterizer_sl_clip.h"
34
35
36 namespace agg
37 {
38
39
40 //-----------------------------------------------------------------cell_aa
41 // A pixel cell. There're no constructors defined and it was done
42 // intentionally in order to avoid extra overhead when allocating an
43 // array of cells.
44 struct cell_aa
45 {
46 int x;
47 int y;
48 int cover;
49 int area;
50
51 void initial()
52 {
53 x = 0x7FFFFFFF;
54 y = 0x7FFFFFFF;
55 cover = 0;
56 area = 0;
57 }
58
59 void style(const cell_aa&) {}
60
61 int not_equal(int ex, int ey, const cell_aa&) const
62 {
63 return ex != x || ey != y;
64 }
65 };
66
67
68 //==================================================rasterizer_scanline_aa_nogamma
69 // Polygon rasterizer that is used to render filled polygons with
70 // high-quality Anti-Aliasing. Internally, by default, the class uses
71 // integer coordinates in format 24.8, i.e. 24 bits for integer part
72 // and 8 bits for fractional - see poly_subpixel_shift. This class can be
73 // used in the following way:
74 //
75 // 1. filling_rule(filling_rule_e ft) - optional.
76 //
77 // 2. gamma() - optional.
78 //
79 // 3. reset()
80 //
81 // 4. move_to(x, y) / line_to(x, y) - make the polygon. One can create
82 // more than one contour, but each contour must consist of at least 3
83 // vertices, i.e. move_to(x1, y1); line_to(x2, y2); line_to(x3, y3);
84 // is the absolute minimum of vertices that define a triangle.
85 // The algorithm does not check either the number of vertices nor
86 // coincidence of their coordinates, but in the worst case it just
87 // won't draw anything.
88 // The orger of the vertices (clockwise or counterclockwise)
89 // is important when using the non-zero filling rule (fill_non_zero).
90 // In this case the vertex order of all the contours must be the same
91 // if you want your intersecting polygons to be without "holes".
92 // You actually can use different vertices order. If the contours do not
93 // intersect each other the order is not important anyway. If they do,
94 // contours with the same vertex order will be rendered without "holes"
95 // while the intersecting contours with different orders will have "holes".
96 //
97 // filling_rule() and gamma() can be called anytime before "sweeping".
98 //------------------------------------------------------------------------
99 template<class Clip=rasterizer_sl_clip_int> class rasterizer_scanline_aa_nogamma
100 {
101 enum status
102 {
103 status_initial,
104 status_move_to,
105 status_line_to,
106 status_closed
107 };
108
109 public:
110 typedef Clip clip_type;
111 typedef typename Clip::conv_type conv_type;
112 typedef typename Clip::coord_type coord_type;
113
114 enum aa_scale_e
115 {
116 aa_shift = 8,
117 aa_scale = 1 << aa_shift,
118 aa_mask = aa_scale - 1,
119 aa_scale2 = aa_scale * 2,
120 aa_mask2 = aa_scale2 - 1
121 };
122
123 //--------------------------------------------------------------------
124 rasterizer_scanline_aa_nogamma(unsigned cell_block_limit=1024) :
125 m_outline(cell_block_limit),
126 m_clipper(),
127 m_filling_rule(fill_non_zero),
128 m_auto_close(true),
129 m_start_x(0),
130 m_start_y(0),
131 m_status(status_initial)
132 {
133 }
134
135 //--------------------------------------------------------------------
136 void reset();
137 void reset_clipping();
138 void clip_box(double x1, double y1, double x2, double y2);
139 void filling_rule(filling_rule_e filling_rule);
140 void auto_close(bool flag) { m_auto_close = flag; }
141
142 //--------------------------------------------------------------------
143 unsigned apply_gamma(unsigned cover) const
144 {
145 return cover;
146 }
147
148 //--------------------------------------------------------------------
149 void move_to(int x, int y);
150 void line_to(int x, int y);
151 void move_to_d(double x, double y);
152 void line_to_d(double x, double y);
153 void close_polygon();
154 void add_vertex(double x, double y, unsigned cmd);
155
156 void edge(int x1, int y1, int x2, int y2);
157 void edge_d(double x1, double y1, double x2, double y2);
158
159 //-------------------------------------------------------------------
160 template<class VertexSource>
161 void add_path(VertexSource& vs, unsigned path_id=0)
162 {
163 double x;
164 double y;
165
166 unsigned cmd;
167 vs.rewind(path_id);
168 if(m_outline.sorted()) reset();
169 while(!is_stop(cmd = vs.vertex(&x, &y)))
170 {
171 add_vertex(x, y, cmd);
172 }
173 }
174
175 //--------------------------------------------------------------------
176 int min_x() const { return m_outline.min_x(); }
177 int min_y() const { return m_outline.min_y(); }
178 int max_x() const { return m_outline.max_x(); }
179 int max_y() const { return m_outline.max_y(); }
180
181 //--------------------------------------------------------------------
182 void sort();
183 bool rewind_scanlines();
184 bool navigate_scanline(int y);
185
186 //--------------------------------------------------------------------
187 AGG_INLINE unsigned calculate_alpha(int area) const
188 {
189 int cover = area >> (poly_subpixel_shift*2 + 1 - aa_shift);
190
191 if(cover < 0) cover = -cover;
192 if(m_filling_rule == fill_even_odd)
193 {
194 cover &= aa_mask2;
195 if(cover > aa_scale)
196 {
197 cover = aa_scale2 - cover;
198 }
199 }
200 if(cover > aa_mask) cover = aa_mask;
201 return cover;
202 }
203
204 //--------------------------------------------------------------------
205 template<class Scanline> bool sweep_scanline(Scanline& sl)
206 {
207 for(;;)
208 {
209 if(m_scan_y > m_outline.max_y()) return false;
210 sl.reset_spans();
211 unsigned num_cells = m_outline.scanline_num_cells(m_scan_y);
212 const cell_aa* const* cells = m_outline.scanline_cells(m_scan_y);
213 int cover = 0;
214
215 while(num_cells)
216 {
217 const cell_aa* cur_cell = *cells;
218 int x = cur_cell->x;
219 int area = cur_cell->area;
220 unsigned alpha;
221
222 cover += cur_cell->cover;
223
224 //accumulate all cells with the same X
225 while(--num_cells)
226 {
227 cur_cell = *++cells;
228 if(cur_cell->x != x) break;
229 area += cur_cell->area;
230 cover += cur_cell->cover;
231 }
232
233 if(area)
234 {
235 alpha = calculate_alpha((cover << (poly_subpixel_shift + 1)) - area);
236 if(alpha)
237 {
238 sl.add_cell(x, alpha);
239 }
240 x++;
241 }
242
243 if(num_cells && cur_cell->x > x)
244 {
245 alpha = calculate_alpha(cover << (poly_subpixel_shift + 1));
246 if(alpha)
247 {
248 sl.add_span(x, cur_cell->x - x, alpha);
249 }
250 }
251 }
252
253 if(sl.num_spans()) break;
254 ++m_scan_y;
255 }
256
257 sl.finalize(m_scan_y);
258 ++m_scan_y;
259 return true;
260 }
261
262 //--------------------------------------------------------------------
263 bool hit_test(int tx, int ty);
264
265
266 private:
267 //--------------------------------------------------------------------
268 // Disable copying
269 rasterizer_scanline_aa_nogamma(const rasterizer_scanline_aa_nogamma<Clip>&);
270 const rasterizer_scanline_aa_nogamma<Clip>&
271 operator = (const rasterizer_scanline_aa_nogamma<Clip>&);
272
273 private:
274 rasterizer_cells_aa<cell_aa> m_outline;
275 clip_type m_clipper;
276 filling_rule_e m_filling_rule;
277 bool m_auto_close;
278 coord_type m_start_x;
279 coord_type m_start_y;
280 unsigned m_status;
281 int m_scan_y;
282 };
283
284
285
286
287
288
289
290
291
292
293
294
295 //------------------------------------------------------------------------
296 template<class Clip>
297 void rasterizer_scanline_aa_nogamma<Clip>::reset()
298 {
299 m_outline.reset();
300 m_status = status_initial;
301 }
302
303 //------------------------------------------------------------------------
304 template<class Clip>
305 void rasterizer_scanline_aa_nogamma<Clip>::filling_rule(filling_rule_e filling_rule)
306 {
307 m_filling_rule = filling_rule;
308 }
309
310 //------------------------------------------------------------------------
311 template<class Clip>
312 void rasterizer_scanline_aa_nogamma<Clip>::clip_box(double x1, double y1,
313 double x2, double y2)
314 {
315 reset();
316 m_clipper.clip_box(conv_type::upscale(x1), conv_type::upscale(y1),
317 conv_type::upscale(x2), conv_type::upscale(y2));
318 }
319
320 //------------------------------------------------------------------------
321 template<class Clip>
322 void rasterizer_scanline_aa_nogamma<Clip>::reset_clipping()
323 {
324 reset();
325 m_clipper.reset_clipping();
326 }
327
328 //------------------------------------------------------------------------
329 template<class Clip>
330 void rasterizer_scanline_aa_nogamma<Clip>::close_polygon()
331 {
332 if(m_status == status_line_to)
333 {
334 m_clipper.line_to(m_outline, m_start_x, m_start_y);
335 m_status = status_closed;
336 }
337 }
338
339 //------------------------------------------------------------------------
340 template<class Clip>
341 void rasterizer_scanline_aa_nogamma<Clip>::move_to(int x, int y)
342 {
343 if(m_outline.sorted()) reset();
344 if(m_auto_close) close_polygon();
345 m_clipper.move_to(m_start_x = conv_type::downscale(x),
346 m_start_y = conv_type::downscale(y));
347 m_status = status_move_to;
348 }
349
350 //------------------------------------------------------------------------
351 template<class Clip>
352 void rasterizer_scanline_aa_nogamma<Clip>::line_to(int x, int y)
353 {
354 m_clipper.line_to(m_outline,
355 conv_type::downscale(x),
356 conv_type::downscale(y));
357 m_status = status_line_to;
358 }
359
360 //------------------------------------------------------------------------
361 template<class Clip>
362 void rasterizer_scanline_aa_nogamma<Clip>::move_to_d(double x, double y)
363 {
364 if(m_outline.sorted()) reset();
365 if(m_auto_close) close_polygon();
366 m_clipper.move_to(m_start_x = conv_type::upscale(x),
367 m_start_y = conv_type::upscale(y));
368 m_status = status_move_to;
369 }
370
371 //------------------------------------------------------------------------
372 template<class Clip>
373 void rasterizer_scanline_aa_nogamma<Clip>::line_to_d(double x, double y)
374 {
375 m_clipper.line_to(m_outline,
376 conv_type::upscale(x),
377 conv_type::upscale(y));
378 m_status = status_line_to;
379 }
380
381 //------------------------------------------------------------------------
382 template<class Clip>
383 void rasterizer_scanline_aa_nogamma<Clip>::add_vertex(double x, double y, unsigned cmd)
384 {
385 if(is_move_to(cmd))
386 {
387 move_to_d(x, y);
388 }
389 else
390 if(is_vertex(cmd))
391 {
392 line_to_d(x, y);
393 }
394 else
395 if(is_close(cmd))
396 {
397 close_polygon();
398 }
399 }
400
401 //------------------------------------------------------------------------
402 template<class Clip>
403 void rasterizer_scanline_aa_nogamma<Clip>::edge(int x1, int y1, int x2, int y2)
404 {
405 if(m_outline.sorted()) reset();
406 m_clipper.move_to(conv_type::downscale(x1), conv_type::downscale(y1));
407 m_clipper.line_to(m_outline,
408 conv_type::downscale(x2),
409 conv_type::downscale(y2));
410 m_status = status_move_to;
411 }
412
413 //------------------------------------------------------------------------
414 template<class Clip>
415 void rasterizer_scanline_aa_nogamma<Clip>::edge_d(double x1, double y1,
416 double x2, double y2)
417 {
418 if(m_outline.sorted()) reset();
419 m_clipper.move_to(conv_type::upscale(x1), conv_type::upscale(y1));
420 m_clipper.line_to(m_outline,
421 conv_type::upscale(x2),
422 conv_type::upscale(y2));
423 m_status = status_move_to;
424 }
425
426 //------------------------------------------------------------------------
427 template<class Clip>
428 void rasterizer_scanline_aa_nogamma<Clip>::sort()
429 {
430 if(m_auto_close) close_polygon();
431 m_outline.sort_cells();
432 }
433
434 //------------------------------------------------------------------------
435 template<class Clip>
436 AGG_INLINE bool rasterizer_scanline_aa_nogamma<Clip>::rewind_scanlines()
437 {
438 if(m_auto_close) close_polygon();
439 m_outline.sort_cells();
440 if(m_outline.total_cells() == 0)
441 {
442 return false;
443 }
444 m_scan_y = m_outline.min_y();
445 return true;
446 }
447
448
449 //------------------------------------------------------------------------
450 template<class Clip>
451 AGG_INLINE bool rasterizer_scanline_aa_nogamma<Clip>::navigate_scanline(int y)
452 {
453 if(m_auto_close) close_polygon();
454 m_outline.sort_cells();
455 if(m_outline.total_cells() == 0 ||
456 y < m_outline.min_y() ||
457 y > m_outline.max_y())
458 {
459 return false;
460 }
461 m_scan_y = y;
462 return true;
463 }
464
465 //------------------------------------------------------------------------
466 template<class Clip>
467 bool rasterizer_scanline_aa_nogamma<Clip>::hit_test(int tx, int ty)
468 {
469 if(!navigate_scanline(ty)) return false;
470 scanline_hit_test sl(tx);
471 sweep_scanline(sl);
472 return sl.hit();
473 }
474
475
476
477 }
478
479
480
481 #endif
482