Debian Codegraph @

extern/agg24-svn/include/agg_rasterizer_compound_aa.h

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
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_COMPOUND_AA_INCLUDED
30 #define AGG_RASTERIZER_COMPOUND_AA_INCLUDED
31
32 #include "agg_rasterizer_cells_aa.h"
33 #include "agg_rasterizer_sl_clip.h"
34
35 namespace agg
36 {
37
38 //-----------------------------------------------------------cell_style_aa
39 // A pixel cell. There're no constructors defined and it was done
40 // intentionally in order to avoid extra overhead when allocating an
41 // array of cells.
42 struct cell_style_aa
43 {
44 int x;
45 int y;
46 int cover;
47 int area;
48 int16 left, right;
49
50 void initial()
51 {
52 x = 0x7FFFFFFF;
53 y = 0x7FFFFFFF;
54 cover = 0;
55 area = 0;
56 left = -1;
57 right = -1;
58 }
59
60 void style(const cell_style_aa& c)
61 {
62 left = c.left;
63 right = c.right;
64 }
65
66 int not_equal(int ex, int ey, const cell_style_aa& c) const
67 {
68 return (ex - x) | (ey - y) | (left - c.left) | (right - c.right);
69 }
70 };
71
72
73 //===========================================================layer_order_e
74 enum layer_order_e
75 {
76 layer_unsorted, //------layer_unsorted
77 layer_direct, //------layer_direct
78 layer_inverse //------layer_inverse
79 };
80
81
82 //==================================================rasterizer_compound_aa
83 template<class Clip=rasterizer_sl_clip_int> class rasterizer_compound_aa
84 {
85 struct style_info
86 {
87 unsigned start_cell;
88 unsigned num_cells;
89 int last_x;
90 };
91
92 struct cell_info
93 {
94 int x, area, cover;
95 };
96
97 public:
98 typedef Clip clip_type;
99 typedef typename Clip::conv_type conv_type;
100 typedef typename Clip::coord_type coord_type;
101
102 enum aa_scale_e
103 {
104 aa_shift = 8,
105 aa_scale = 1 << aa_shift,
106 aa_mask = aa_scale - 1,
107 aa_scale2 = aa_scale * 2,
108 aa_mask2 = aa_scale2 - 1
109 };
110
111 //--------------------------------------------------------------------
112 rasterizer_compound_aa(unsigned cell_block_limit=1024) :
113 m_outline(cell_block_limit),
114 m_clipper(),
115 m_filling_rule(fill_non_zero),
116 m_layer_order(layer_direct),
117 m_styles(), // Active Styles
118 m_ast(), // Active Style Table (unique values)
119 m_asm(), // Active Style Mask
120 m_cells(),
121 m_cover_buf(),
122 m_min_style(0x7FFFFFFF),
123 m_max_style(-0x7FFFFFFF),
124 m_start_x(0),
125 m_start_y(0),
126 m_scan_y(0x7FFFFFFF),
127 m_sl_start(0),
128 m_sl_len(0)
129 {}
130
131 //--------------------------------------------------------------------
132 void reset();
133 void reset_clipping();
134 void clip_box(double x1, double y1, double x2, double y2);
135 void filling_rule(filling_rule_e filling_rule);
136 void layer_order(layer_order_e order);
137
138 //--------------------------------------------------------------------
139 void styles(int left, int right);
140 void move_to(int x, int y);
141 void line_to(int x, int y);
142 void move_to_d(double x, double y);
143 void line_to_d(double x, double y);
144 void add_vertex(double x, double y, unsigned cmd);
145
146 void edge(int x1, int y1, int x2, int y2);
147 void edge_d(double x1, double y1, double x2, double y2);
148
149 //-------------------------------------------------------------------
150 template<class VertexSource>
151 void add_path(VertexSource& vs, unsigned path_id=0)
152 {
153 double x;
154 double y;
155
156 unsigned cmd;
157 vs.rewind(path_id);
158 if(m_outline.sorted()) reset();
159 while(!is_stop(cmd = vs.vertex(&x, &y)))
160 {
161 add_vertex(x, y, cmd);
162 }
163 }
164
165
166 //--------------------------------------------------------------------
167 int min_x() const { return m_outline.min_x(); }
168 int min_y() const { return m_outline.min_y(); }
169 int max_x() const { return m_outline.max_x(); }
170 int max_y() const { return m_outline.max_y(); }
171 int min_style() const { return m_min_style; }
172 int max_style() const { return m_max_style; }
173
174 //--------------------------------------------------------------------
175 void sort();
176 bool rewind_scanlines();
177 unsigned sweep_styles();
178 int scanline_start() const { return m_sl_start; }
179 unsigned scanline_length() const { return m_sl_len; }
180 unsigned style(unsigned style_idx) const;
181
182 cover_type* allocate_cover_buffer(unsigned len);
183
184 //--------------------------------------------------------------------
185 bool navigate_scanline(int y);
186 bool hit_test(int tx, int ty);
187
188 //--------------------------------------------------------------------
189 AGG_INLINE unsigned calculate_alpha(int area) const
190 {
191 int cover = area >> (poly_subpixel_shift*2 + 1 - aa_shift);
192 if(cover < 0) cover = -cover;
193 if(m_filling_rule == fill_even_odd)
194 {
195 cover &= aa_mask2;
196 if(cover > aa_scale)
197 {
198 cover = aa_scale2 - cover;
199 }
200 }
201 if(cover > aa_mask) cover = aa_mask;
202 return cover;
203 }
204
205 //--------------------------------------------------------------------
206 // Sweeps one scanline with one style index. The style ID can be
207 // determined by calling style().
208 template<class Scanline> bool sweep_scanline(Scanline& sl, int style_idx)
209 {
210 int scan_y = m_scan_y - 1;
211 if(scan_y > m_outline.max_y()) return false;
212
213 sl.reset_spans();
214
215 if(style_idx < 0)
216 {
217 style_idx = 0;
218 }
219 else
220 {
221 style_idx++;
222 }
223
224 const style_info& st = m_styles[m_ast[style_idx]];
225
226 unsigned num_cells = st.num_cells;
227 cell_info* cell = &m_cells[st.start_cell];
228
229 int cover = 0;
230 while(num_cells--)
231 {
232 unsigned alpha;
233 int x = cell->x;
234 int area = cell->area;
235
236 cover += cell->cover;
237
238 ++cell;
239
240 if(area)
241 {
242 alpha = calculate_alpha((cover << (poly_subpixel_shift + 1)) - area);
243 sl.add_cell(x, alpha);
244 x++;
245 }
246
247 if(num_cells && cell->x > x)
248 {
249 alpha = calculate_alpha(cover << (poly_subpixel_shift + 1));
250 if(alpha)
251 {
252 sl.add_span(x, cell->x - x, alpha);
253 }
254 }
255 }
256
257 if(sl.num_spans() == 0) return false;
258 sl.finalize(scan_y);
259 return true;
260 }
261
262 private:
263 void add_style(int style_id);
264
265 //--------------------------------------------------------------------
266 // Disable copying
267 rasterizer_compound_aa(const rasterizer_compound_aa<Clip>&);
268 const rasterizer_compound_aa<Clip>&
269 operator = (const rasterizer_compound_aa<Clip>&);
270
271 private:
272 rasterizer_cells_aa<cell_style_aa> m_outline;
273 clip_type m_clipper;
274 filling_rule_e m_filling_rule;
275 layer_order_e m_layer_order;
276 pod_vector<style_info> m_styles; // Active Styles
277 pod_vector<unsigned> m_ast; // Active Style Table (unique values)
278 pod_vector<int8u> m_asm; // Active Style Mask
279 pod_vector<cell_info> m_cells;
280 pod_vector<cover_type> m_cover_buf;
281
282 int m_min_style;
283 int m_max_style;
284 coord_type m_start_x;
285 coord_type m_start_y;
286 int m_scan_y;
287 int m_sl_start;
288 unsigned m_sl_len;
289 };
290
291
292
293
294
295
296
297
298
299
300 //------------------------------------------------------------------------
301 template<class Clip>
302 void rasterizer_compound_aa<Clip>::reset()
303 {
304 m_outline.reset();
305 m_min_style = 0x7FFFFFFF;
306 m_max_style = -0x7FFFFFFF;
307 m_scan_y = 0x7FFFFFFF;
308 m_sl_start = 0;
309 m_sl_len = 0;
310 }
311
312 //------------------------------------------------------------------------
313 template<class Clip>
314 void rasterizer_compound_aa<Clip>::filling_rule(filling_rule_e filling_rule)
315 {
316 m_filling_rule = filling_rule;
317 }
318
319 //------------------------------------------------------------------------
320 template<class Clip>
321 void rasterizer_compound_aa<Clip>::layer_order(layer_order_e order)
322 {
323 m_layer_order = order;
324 }
325
326 //------------------------------------------------------------------------
327 template<class Clip>
328 void rasterizer_compound_aa<Clip>::clip_box(double x1, double y1,
329 double x2, double y2)
330 {
331 reset();
332 m_clipper.clip_box(conv_type::upscale(x1), conv_type::upscale(y1),
333 conv_type::upscale(x2), conv_type::upscale(y2));
334 }
335
336 //------------------------------------------------------------------------
337 template<class Clip>
338 void rasterizer_compound_aa<Clip>::reset_clipping()
339 {
340 reset();
341 m_clipper.reset_clipping();
342 }
343
344 //------------------------------------------------------------------------
345 template<class Clip>
346 void rasterizer_compound_aa<Clip>::styles(int left, int right)
347 {
348 cell_style_aa cell;
349 cell.initial();
350 cell.left = (int16)left;
351 cell.right = (int16)right;
352 m_outline.style(cell);
353 if(left >= 0 && left < m_min_style) m_min_style = left;
354 if(left >= 0 && left > m_max_style) m_max_style = left;
355 if(right >= 0 && right < m_min_style) m_min_style = right;
356 if(right >= 0 && right > m_max_style) m_max_style = right;
357 }
358
359 //------------------------------------------------------------------------
360 template<class Clip>
361 void rasterizer_compound_aa<Clip>::move_to(int x, int y)
362 {
363 if(m_outline.sorted()) reset();
364 m_clipper.move_to(m_start_x = conv_type::downscale(x),
365 m_start_y = conv_type::downscale(y));
366 }
367
368 //------------------------------------------------------------------------
369 template<class Clip>
370 void rasterizer_compound_aa<Clip>::line_to(int x, int y)
371 {
372 m_clipper.line_to(m_outline,
373 conv_type::downscale(x),
374 conv_type::downscale(y));
375 }
376
377 //------------------------------------------------------------------------
378 template<class Clip>
379 void rasterizer_compound_aa<Clip>::move_to_d(double x, double y)
380 {
381 if(m_outline.sorted()) reset();
382 m_clipper.move_to(m_start_x = conv_type::upscale(x),
383 m_start_y = conv_type::upscale(y));
384 }
385
386 //------------------------------------------------------------------------
387 template<class Clip>
388 void rasterizer_compound_aa<Clip>::line_to_d(double x, double y)
389 {
390 m_clipper.line_to(m_outline,
391 conv_type::upscale(x),
392 conv_type::upscale(y));
393 }
394
395 //------------------------------------------------------------------------
396 template<class Clip>
397 void rasterizer_compound_aa<Clip>::add_vertex(double x, double y, unsigned cmd)
398 {
399 if(is_move_to(cmd))
400 {
401 move_to_d(x, y);
402 }
403 else
404 if(is_vertex(cmd))
405 {
406 line_to_d(x, y);
407 }
408 else
409 if(is_close(cmd))
410 {
411 m_clipper.line_to(m_outline, m_start_x, m_start_y);
412 }
413 }
414
415 //------------------------------------------------------------------------
416 template<class Clip>
417 void rasterizer_compound_aa<Clip>::edge(int x1, int y1, int x2, int y2)
418 {
419 if(m_outline.sorted()) reset();
420 m_clipper.move_to(conv_type::downscale(x1), conv_type::downscale(y1));
421 m_clipper.line_to(m_outline,
422 conv_type::downscale(x2),
423 conv_type::downscale(y2));
424 }
425
426 //------------------------------------------------------------------------
427 template<class Clip>
428 void rasterizer_compound_aa<Clip>::edge_d(double x1, double y1,
429 double x2, double y2)
430 {
431 if(m_outline.sorted()) reset();
432 m_clipper.move_to(conv_type::upscale(x1), conv_type::upscale(y1));
433 m_clipper.line_to(m_outline,
434 conv_type::upscale(x2),
435 conv_type::upscale(y2));
436 }
437
438 //------------------------------------------------------------------------
439 template<class Clip>
440 AGG_INLINE void rasterizer_compound_aa<Clip>::sort()
441 {
442 m_outline.sort_cells();
443 }
444
445 //------------------------------------------------------------------------
446 template<class Clip>
447 AGG_INLINE bool rasterizer_compound_aa<Clip>::rewind_scanlines()
448 {
449 m_outline.sort_cells();
450 if(m_outline.total_cells() == 0)
451 {
452 return false;
453 }
454 if(m_max_style < m_min_style)
455 {
456 return false;
457 }
458 m_scan_y = m_outline.min_y();
459 m_styles.allocate(m_max_style - m_min_style + 2, 128);
460 return true;
461 }
462
463 //------------------------------------------------------------------------
464 template<class Clip>
465 AGG_INLINE void rasterizer_compound_aa<Clip>::add_style(int style_id)
466 {
467 if(style_id < 0) style_id = 0;
468 else style_id -= m_min_style - 1;
469
470 unsigned nbyte = style_id >> 3;
471 unsigned mask = 1 << (style_id & 7);
472
473 style_info* style = &m_styles[style_id];
474 if((m_asm[nbyte] & mask) == 0)
475 {
476 m_ast.add(style_id);
477 m_asm[nbyte] |= mask;
478 style->start_cell = 0;
479 style->num_cells = 0;
480 style->last_x = -0x7FFFFFFF;
481 }
482 ++style->start_cell;
483 }
484
485 //------------------------------------------------------------------------
486 // Returns the number of styles
487 template<class Clip>
488 unsigned rasterizer_compound_aa<Clip>::sweep_styles()
489 {
490 for(;;)
491 {
492 if(m_scan_y > m_outline.max_y()) return 0;
493 unsigned num_cells = m_outline.scanline_num_cells(m_scan_y);
494 const cell_style_aa* const* cells = m_outline.scanline_cells(m_scan_y);
495 unsigned num_styles = m_max_style - m_min_style + 2;
496 const cell_style_aa* curr_cell;
497 unsigned style_id;
498 style_info* style;
499 cell_info* cell;
500
501 m_cells.allocate(num_cells * 2, 256); // Each cell can have two styles
502 m_ast.capacity(num_styles, 64);
503 m_asm.allocate((num_styles + 7) >> 3, 8);
504 m_asm.zero();
505
506 if(num_cells)
507 {
508 // Pre-add zero (for no-fill style, that is, -1).
509 // We need that to ensure that the "-1 style" would go first.
510 m_asm[0] |= 1;
511 m_ast.add(0);
512 style = &m_styles[0];
513 style->start_cell = 0;
514 style->num_cells = 0;
515 style->last_x = -0x7FFFFFFF;
516
517 m_sl_start = cells[0]->x;
518 m_sl_len = cells[num_cells-1]->x - m_sl_start + 1;
519 while(num_cells--)
520 {
521 curr_cell = *cells++;
522 add_style(curr_cell->left);
523 add_style(curr_cell->right);
524 }
525
526 // Convert the Y-histogram into the array of starting indexes
527 unsigned i;
528 unsigned start_cell = 0;
529 for(i = 0; i < m_ast.size(); i++)
530 {
531 style_info& st = m_styles[m_ast[i]];
532 unsigned v = st.start_cell;
533 st.start_cell = start_cell;
534 start_cell += v;
535 }
536
537 cells = m_outline.scanline_cells(m_scan_y);
538 num_cells = m_outline.scanline_num_cells(m_scan_y);
539
540 while(num_cells--)
541 {
542 curr_cell = *cells++;
543 style_id = (curr_cell->left < 0) ? 0 :
544 curr_cell->left - m_min_style + 1;
545
546 style = &m_styles[style_id];
547 if(curr_cell->x == style->last_x)
548 {
549 cell = &m_cells[style->start_cell + style->num_cells - 1];
550 cell->area += curr_cell->area;
551 cell->cover += curr_cell->cover;
552 }
553 else
554 {
555 cell = &m_cells[style->start_cell + style->num_cells];
556 cell->x = curr_cell->x;
557 cell->area = curr_cell->area;
558 cell->cover = curr_cell->cover;
559 style->last_x = curr_cell->x;
560 style->num_cells++;
561 }
562
563 style_id = (curr_cell->right < 0) ? 0 :
564 curr_cell->right - m_min_style + 1;
565
566 style = &m_styles[style_id];
567 if(curr_cell->x == style->last_x)
568 {
569 cell = &m_cells[style->start_cell + style->num_cells - 1];
570 cell->area -= curr_cell->area;
571 cell->cover -= curr_cell->cover;
572 }
573 else
574 {
575 cell = &m_cells[style->start_cell + style->num_cells];
576 cell->x = curr_cell->x;
577 cell->area = -curr_cell->area;
578 cell->cover = -curr_cell->cover;
579 style->last_x = curr_cell->x;
580 style->num_cells++;
581 }
582 }
583 }
584 if(m_ast.size() > 1) break;
585 ++m_scan_y;
586 }
587 ++m_scan_y;
588
589 if(m_layer_order != layer_unsorted)
590 {
591 range_adaptor<pod_vector<unsigned> > ra(m_ast, 1, m_ast.size() - 1);
592 if(m_layer_order == layer_direct) quick_sort(ra, unsigned_greater);
593 else quick_sort(ra, unsigned_less);
594 }
595
596 return m_ast.size() - 1;
597 }
598
599 //------------------------------------------------------------------------
600 // Returns style ID depending of the existing style index
601 template<class Clip>
602 AGG_INLINE
603 unsigned rasterizer_compound_aa<Clip>::style(unsigned style_idx) const
604 {
605 return m_ast[style_idx + 1] + m_min_style - 1;
606 }
607
608 //------------------------------------------------------------------------
609 template<class Clip>
610 AGG_INLINE bool rasterizer_compound_aa<Clip>::navigate_scanline(int y)
611 {
612 m_outline.sort_cells();
613 if(m_outline.total_cells() == 0)
614 {
615 return false;
616 }
617 if(m_max_style < m_min_style)
618 {
619 return false;
620 }
621 if(y < m_outline.min_y() || y > m_outline.max_y())
622 {
623 return false;
624 }
625 m_scan_y = y;
626 m_styles.allocate(m_max_style - m_min_style + 2, 128);
627 return true;
628 }
629
630 //------------------------------------------------------------------------
631 template<class Clip>
632 bool rasterizer_compound_aa<Clip>::hit_test(int tx, int ty)
633 {
634 if(!navigate_scanline(ty))
635 {
636 return false;
637 }
638
639 unsigned num_styles = sweep_styles();
640 if(num_styles <= 0)
641 {
642 return false;
643 }
644
645 scanline_hit_test sl(tx);
646 sweep_scanline(sl, -1);
647 return sl.hit();
648 }
649
650 //------------------------------------------------------------------------
651 template<class Clip>
652 cover_type* rasterizer_compound_aa<Clip>::allocate_cover_buffer(unsigned len)
653 {
654 m_cover_buf.allocate(len, 256);
655 return &m_cover_buf[0];
656 }
657
658 }
659
660
661
662 #endif
663