Debian Codegraph @

extern/agg24-svn/include/agg_renderer_outline_image.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 // Contact: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 #ifndef AGG_RENDERER_OUTLINE_IMAGE_INCLUDED
16 #define AGG_RENDERER_OUTLINE_IMAGE_INCLUDED
17
18 #include "agg_array.h"
19 #include "agg_math.h"
20 #include "agg_line_aa_basics.h"
21 #include "agg_dda_line.h"
22 #include "agg_rendering_buffer.h"
23 #include "agg_clip_liang_barsky.h"
24
25
26 namespace agg
27 {
28 //========================================================line_image_scale
29 template<class Source> class line_image_scale
30 {
31 public:
32 typedef typename Source::color_type color_type;
33
34 line_image_scale(const Source& src, double height) :
35 m_source(src),
36 m_height(height),
37 m_scale(src.height() / height),
38 m_scale_inv(height / src.height())
39 {
40 }
41
42 double width() const { return m_source.width(); }
43 double height() const { return m_height; }
44
45 color_type pixel(int x, int y) const
46 {
47 if (m_scale < 1.0)
48 {
49 // Interpolate between nearest source pixels.
50 double src_y = (y + 0.5) * m_scale - 0.5;
51 int h = m_source.height() - 1;
52 int y1 = ifloor(src_y);
53 int y2 = y1 + 1;
54 rgba pix1 = (y1 < 0) ? rgba::no_color() : m_source.pixel(x, y1);
55 rgba pix2 = (y2 > h) ? rgba::no_color() : m_source.pixel(x, y2);
56 return pix1.gradient(pix2, src_y - y1);
57 }
58 else
59 {
60 // Average source pixels between y and y+1.
61 double src_y1 = (y + 0.5) * m_scale - 0.5;
62 double src_y2 = src_y1 + m_scale;
63 int h = m_source.height() - 1;
64 int y1 = ifloor(src_y1);
65 int y2 = ifloor(src_y2);
66 rgba c = rgba::no_color();
67 if (y1 >= 0) c += rgba(m_source.pixel(x, y1)) *= y1 + 1 - src_y1;
68 while (++y1 < y2)
69 {
70 if (y1 <= h) c += m_source.pixel(x, y1);
71 }
72 if (y2 <= h) c += rgba(m_source.pixel(x, y2)) *= src_y2 - y2;
73 return c *= m_scale_inv;
74 }
75 }
76
77 private:
78 line_image_scale(const line_image_scale<Source>&);
79 const line_image_scale<Source>& operator = (const line_image_scale<Source>&);
80
81 const Source& m_source;
82 double m_height;
83 double m_scale;
84 double m_scale_inv;
85 };
86
87
88
89 //======================================================line_image_pattern
90 template<class Filter> class line_image_pattern
91 {
92 public:
93 typedef Filter filter_type;
94 typedef typename filter_type::color_type color_type;
95
96 //--------------------------------------------------------------------
97 line_image_pattern(Filter& filter) :
98 m_filter(&filter),
99 m_dilation(filter.dilation() + 1),
100 m_dilation_hr(m_dilation << line_subpixel_shift),
101 m_data(),
102 m_width(0),
103 m_height(0),
104 m_width_hr(0),
105 m_half_height_hr(0),
106 m_offset_y_hr(0)
107 {
108 }
109
110 // Create
111 //--------------------------------------------------------------------
112 template<class Source>
113 line_image_pattern(Filter& filter, const Source& src) :
114 m_filter(&filter),
115 m_dilation(filter.dilation() + 1),
116 m_dilation_hr(m_dilation << line_subpixel_shift),
117 m_data(),
118 m_width(0),
119 m_height(0),
120 m_width_hr(0),
121 m_half_height_hr(0),
122 m_offset_y_hr(0)
123 {
124 create(src);
125 }
126
127 // Create
128 //--------------------------------------------------------------------
129 template<class Source> void create(const Source& src)
130 {
131 m_height = uceil(src.height());
132 m_width = uceil(src.width());
133 m_width_hr = uround(src.width() * line_subpixel_scale);
134 m_half_height_hr = uround(src.height() * line_subpixel_scale/2);
135 m_offset_y_hr = m_dilation_hr + m_half_height_hr - line_subpixel_scale/2;
136 m_half_height_hr += line_subpixel_scale/2;
137
138 m_data.resize((m_width + m_dilation * 2) * (m_height + m_dilation * 2));
139
140 m_buf.attach(&m_data[0], m_width + m_dilation * 2,
141 m_height + m_dilation * 2,
142 m_width + m_dilation * 2);
143 unsigned x, y;
144 color_type* d1;
145 color_type* d2;
146 for(y = 0; y < m_height; y++)
147 {
148 d1 = m_buf.row_ptr(y + m_dilation) + m_dilation;
149 for(x = 0; x < m_width; x++)
150 {
151 *d1++ = src.pixel(x, y);
152 }
153 }
154
155 const color_type* s1;
156 const color_type* s2;
157 for(y = 0; y < m_dilation; y++)
158 {
159 //s1 = m_buf.row_ptr(m_height + m_dilation - 1) + m_dilation;
160 //s2 = m_buf.row_ptr(m_dilation) + m_dilation;
161 d1 = m_buf.row_ptr(m_dilation + m_height + y) + m_dilation;
162 d2 = m_buf.row_ptr(m_dilation - y - 1) + m_dilation;
163 for(x = 0; x < m_width; x++)
164 {
165 //*d1++ = color_type(*s1++, 0);
166 //*d2++ = color_type(*s2++, 0);
167 *d1++ = color_type::no_color();
168 *d2++ = color_type::no_color();
169 }
170 }
171
172 unsigned h = m_height + m_dilation * 2;
173 for(y = 0; y < h; y++)
174 {
175 s1 = m_buf.row_ptr(y) + m_dilation;
176 s2 = m_buf.row_ptr(y) + m_dilation + m_width;
177 d1 = m_buf.row_ptr(y) + m_dilation + m_width;
178 d2 = m_buf.row_ptr(y) + m_dilation;
179
180 for(x = 0; x < m_dilation; x++)
181 {
182 *d1++ = *s1++;
183 *--d2 = *--s2;
184 }
185 }
186 }
187
188 //--------------------------------------------------------------------
189 int pattern_width() const { return m_width_hr; }
190 int line_width() const { return m_half_height_hr; }
191 double width() const { return m_height; }
192
193 //--------------------------------------------------------------------
194 void pixel(color_type* p, int x, int y) const
195 {
196 m_filter->pixel_high_res(m_buf.rows(),
197 p,
198 x % m_width_hr + m_dilation_hr,
199 y + m_offset_y_hr);
200 }
201
202 //--------------------------------------------------------------------
203 const filter_type& filter() const { return *m_filter; }
204
205 private:
206 line_image_pattern(const line_image_pattern<filter_type>&);
207 const line_image_pattern<filter_type>&
208 operator = (const line_image_pattern<filter_type>&);
209
210 protected:
211 row_ptr_cache<color_type> m_buf;
212 const filter_type* m_filter;
213 unsigned m_dilation;
214 int m_dilation_hr;
215 pod_array<color_type> m_data;
216 unsigned m_width;
217 unsigned m_height;
218 int m_width_hr;
219 int m_half_height_hr;
220 int m_offset_y_hr;
221 };
222
223
224
225
226
227
228 //=================================================line_image_pattern_pow2
229 template<class Filter> class line_image_pattern_pow2 :
230 public line_image_pattern<Filter>
231 {
232 public:
233 typedef Filter filter_type;
234 typedef typename filter_type::color_type color_type;
235 typedef line_image_pattern<Filter> base_type;
236
237 //--------------------------------------------------------------------
238 line_image_pattern_pow2(Filter& filter) :
239 line_image_pattern<Filter>(filter), m_mask(line_subpixel_mask) {}
240
241 //--------------------------------------------------------------------
242 template<class Source>
243 line_image_pattern_pow2(Filter& filter, const Source& src) :
244 line_image_pattern<Filter>(filter), m_mask(line_subpixel_mask)
245 {
246 create(src);
247 }
248
249 //--------------------------------------------------------------------
250 template<class Source> void create(const Source& src)
251 {
252 line_image_pattern<Filter>::create(src);
253 m_mask = 1;
254 while(m_mask < base_type::m_width)
255 {
256 m_mask <<= 1;
257 m_mask |= 1;
258 }
259 m_mask <<= line_subpixel_shift - 1;
260 m_mask |= line_subpixel_mask;
261 base_type::m_width_hr = m_mask + 1;
262 }
263
264 //--------------------------------------------------------------------
265 void pixel(color_type* p, int x, int y) const
266 {
267 base_type::m_filter->pixel_high_res(
268 base_type::m_buf.rows(),
269 p,
270 (x & m_mask) + base_type::m_dilation_hr,
271 y + base_type::m_offset_y_hr);
272 }
273 private:
274 unsigned m_mask;
275 };
276
277
278
279
280
281
282
283 //===================================================distance_interpolator4
284 class distance_interpolator4
285 {
286 public:
287 //---------------------------------------------------------------------
288 distance_interpolator4() {}
289 distance_interpolator4(int x1, int y1, int x2, int y2,
290 int sx, int sy, int ex, int ey,
291 int len, double scale, int x, int y) :
292 m_dx(x2 - x1),
293 m_dy(y2 - y1),
294 m_dx_start(line_mr(sx) - line_mr(x1)),
295 m_dy_start(line_mr(sy) - line_mr(y1)),
296 m_dx_end(line_mr(ex) - line_mr(x2)),
297 m_dy_end(line_mr(ey) - line_mr(y2)),
298
299 m_dist(iround(double(x + line_subpixel_scale/2 - x2) * double(m_dy) -
300 double(y + line_subpixel_scale/2 - y2) * double(m_dx))),
301
302 m_dist_start((line_mr(x + line_subpixel_scale/2) - line_mr(sx)) * m_dy_start -
303 (line_mr(y + line_subpixel_scale/2) - line_mr(sy)) * m_dx_start),
304
305 m_dist_end((line_mr(x + line_subpixel_scale/2) - line_mr(ex)) * m_dy_end -
306 (line_mr(y + line_subpixel_scale/2) - line_mr(ey)) * m_dx_end),
307 m_len(uround(len / scale))
308 {
309 double d = len * scale;
310 int dx = iround(((x2 - x1) << line_subpixel_shift) / d);
311 int dy = iround(((y2 - y1) << line_subpixel_shift) / d);
312 m_dx_pict = -dy;
313 m_dy_pict = dx;
314 m_dist_pict = ((x + line_subpixel_scale/2 - (x1 - dy)) * m_dy_pict -
315 (y + line_subpixel_scale/2 - (y1 + dx)) * m_dx_pict) >>
316 line_subpixel_shift;
317
318 m_dx <<= line_subpixel_shift;
319 m_dy <<= line_subpixel_shift;
320 m_dx_start <<= line_mr_subpixel_shift;
321 m_dy_start <<= line_mr_subpixel_shift;
322 m_dx_end <<= line_mr_subpixel_shift;
323 m_dy_end <<= line_mr_subpixel_shift;
324 }
325
326 //---------------------------------------------------------------------
327 void inc_x()
328 {
329 m_dist += m_dy;
330 m_dist_start += m_dy_start;
331 m_dist_pict += m_dy_pict;
332 m_dist_end += m_dy_end;
333 }
334
335 //---------------------------------------------------------------------
336 void dec_x()
337 {
338 m_dist -= m_dy;
339 m_dist_start -= m_dy_start;
340 m_dist_pict -= m_dy_pict;
341 m_dist_end -= m_dy_end;
342 }
343
344 //---------------------------------------------------------------------
345 void inc_y()
346 {
347 m_dist -= m_dx;
348 m_dist_start -= m_dx_start;
349 m_dist_pict -= m_dx_pict;
350 m_dist_end -= m_dx_end;
351 }
352
353 //---------------------------------------------------------------------
354 void dec_y()
355 {
356 m_dist += m_dx;
357 m_dist_start += m_dx_start;
358 m_dist_pict += m_dx_pict;
359 m_dist_end += m_dx_end;
360 }
361
362 //---------------------------------------------------------------------
363 void inc_x(int dy)
364 {
365 m_dist += m_dy;
366 m_dist_start += m_dy_start;
367 m_dist_pict += m_dy_pict;
368 m_dist_end += m_dy_end;
369 if(dy > 0)
370 {
371 m_dist -= m_dx;
372 m_dist_start -= m_dx_start;
373 m_dist_pict -= m_dx_pict;
374 m_dist_end -= m_dx_end;
375 }
376 if(dy < 0)
377 {
378 m_dist += m_dx;
379 m_dist_start += m_dx_start;
380 m_dist_pict += m_dx_pict;
381 m_dist_end += m_dx_end;
382 }
383 }
384
385 //---------------------------------------------------------------------
386 void dec_x(int dy)
387 {
388 m_dist -= m_dy;
389 m_dist_start -= m_dy_start;
390 m_dist_pict -= m_dy_pict;
391 m_dist_end -= m_dy_end;
392 if(dy > 0)
393 {
394 m_dist -= m_dx;
395 m_dist_start -= m_dx_start;
396 m_dist_pict -= m_dx_pict;
397 m_dist_end -= m_dx_end;
398 }
399 if(dy < 0)
400 {
401 m_dist += m_dx;
402 m_dist_start += m_dx_start;
403 m_dist_pict += m_dx_pict;
404 m_dist_end += m_dx_end;
405 }
406 }
407
408 //---------------------------------------------------------------------
409 void inc_y(int dx)
410 {
411 m_dist -= m_dx;
412 m_dist_start -= m_dx_start;
413 m_dist_pict -= m_dx_pict;
414 m_dist_end -= m_dx_end;
415 if(dx > 0)
416 {
417 m_dist += m_dy;
418 m_dist_start += m_dy_start;
419 m_dist_pict += m_dy_pict;
420 m_dist_end += m_dy_end;
421 }
422 if(dx < 0)
423 {
424 m_dist -= m_dy;
425 m_dist_start -= m_dy_start;
426 m_dist_pict -= m_dy_pict;
427 m_dist_end -= m_dy_end;
428 }
429 }
430
431 //---------------------------------------------------------------------
432 void dec_y(int dx)
433 {
434 m_dist += m_dx;
435 m_dist_start += m_dx_start;
436 m_dist_pict += m_dx_pict;
437 m_dist_end += m_dx_end;
438 if(dx > 0)
439 {
440 m_dist += m_dy;
441 m_dist_start += m_dy_start;
442 m_dist_pict += m_dy_pict;
443 m_dist_end += m_dy_end;
444 }
445 if(dx < 0)
446 {
447 m_dist -= m_dy;
448 m_dist_start -= m_dy_start;
449 m_dist_pict -= m_dy_pict;
450 m_dist_end -= m_dy_end;
451 }
452 }
453
454 //---------------------------------------------------------------------
455 int dist() const { return m_dist; }
456 int dist_start() const { return m_dist_start; }
457 int dist_pict() const { return m_dist_pict; }
458 int dist_end() const { return m_dist_end; }
459
460 //---------------------------------------------------------------------
461 int dx() const { return m_dx; }
462 int dy() const { return m_dy; }
463 int dx_start() const { return m_dx_start; }
464 int dy_start() const { return m_dy_start; }
465 int dx_pict() const { return m_dx_pict; }
466 int dy_pict() const { return m_dy_pict; }
467 int dx_end() const { return m_dx_end; }
468 int dy_end() const { return m_dy_end; }
469 int len() const { return m_len; }
470
471 private:
472 //---------------------------------------------------------------------
473 int m_dx;
474 int m_dy;
475 int m_dx_start;
476 int m_dy_start;
477 int m_dx_pict;
478 int m_dy_pict;
479 int m_dx_end;
480 int m_dy_end;
481
482 int m_dist;
483 int m_dist_start;
484 int m_dist_pict;
485 int m_dist_end;
486 int m_len;
487 };
488
489
490
491
492
493 //==================================================line_interpolator_image
494 template<class Renderer> class line_interpolator_image
495 {
496 public:
497 typedef Renderer renderer_type;
498 typedef typename Renderer::color_type color_type;
499
500 //---------------------------------------------------------------------
501 enum max_half_width_e
502 {
503 max_half_width = 64
504 };
505
506 //---------------------------------------------------------------------
507 line_interpolator_image(renderer_type& ren, const line_parameters& lp,
508 int sx, int sy, int ex, int ey,
509 int pattern_start,
510 double scale_x) :
511 m_lp(lp),
512 m_li(lp.vertical ? line_dbl_hr(lp.x2 - lp.x1) :
513 line_dbl_hr(lp.y2 - lp.y1),
514 lp.vertical ? abs(lp.y2 - lp.y1) :
515 abs(lp.x2 - lp.x1) + 1),
516 m_di(lp.x1, lp.y1, lp.x2, lp.y2, sx, sy, ex, ey, lp.len, scale_x,
517 lp.x1 & ~line_subpixel_mask, lp.y1 & ~line_subpixel_mask),
518 m_ren(ren),
519 m_x(lp.x1 >> line_subpixel_shift),
520 m_y(lp.y1 >> line_subpixel_shift),
521 m_old_x(m_x),
522 m_old_y(m_y),
523 m_count((lp.vertical ? abs((lp.y2 >> line_subpixel_shift) - m_y) :
524 abs((lp.x2 >> line_subpixel_shift) - m_x))),
525 m_width(ren.subpixel_width()),
526 //m_max_extent(m_width >> (line_subpixel_shift - 2)),
527 m_max_extent((m_width + line_subpixel_scale) >> line_subpixel_shift),
528 m_start(pattern_start + (m_max_extent + 2) * ren.pattern_width()),
529 m_step(0)
530 {
531 agg::dda2_line_interpolator li(0, lp.vertical ?
532 (lp.dy << agg::line_subpixel_shift) :
533 (lp.dx << agg::line_subpixel_shift),
534 lp.len);
535
536 unsigned i;
537 int stop = m_width + line_subpixel_scale * 2;
538 for(i = 0; i < max_half_width; ++i)
539 {
540 m_dist_pos[i] = li.y();
541 if(m_dist_pos[i] >= stop) break;
542 ++li;
543 }
544 m_dist_pos[i] = 0x7FFF0000;
545
546 int dist1_start;
547 int dist2_start;
548 int npix = 1;
549
550 if(lp.vertical)
551 {
552 do
553 {
554 --m_li;
555 m_y -= lp.inc;
556 m_x = (m_lp.x1 + m_li.y()) >> line_subpixel_shift;
557
558 if(lp.inc > 0) m_di.dec_y(m_x - m_old_x);
559 else m_di.inc_y(m_x - m_old_x);
560
561 m_old_x = m_x;
562
563 dist1_start = dist2_start = m_di.dist_start();
564
565 int dx = 0;
566 if(dist1_start < 0) ++npix;
567 do
568 {
569 dist1_start += m_di.dy_start();
570 dist2_start -= m_di.dy_start();
571 if(dist1_start < 0) ++npix;
572 if(dist2_start < 0) ++npix;
573 ++dx;
574 }
575 while(m_dist_pos[dx] <= m_width);
576 if(npix == 0) break;
577
578 npix = 0;
579 }
580 while(--m_step >= -m_max_extent);
581 }
582 else
583 {
584 do
585 {
586 --m_li;
587
588 m_x -= lp.inc;
589 m_y = (m_lp.y1 + m_li.y()) >> line_subpixel_shift;
590
591 if(lp.inc > 0) m_di.dec_x(m_y - m_old_y);
592 else m_di.inc_x(m_y - m_old_y);
593
594 m_old_y = m_y;
595
596 dist1_start = dist2_start = m_di.dist_start();
597
598 int dy = 0;
599 if(dist1_start < 0) ++npix;
600 do
601 {
602 dist1_start -= m_di.dx_start();
603 dist2_start += m_di.dx_start();
604 if(dist1_start < 0) ++npix;
605 if(dist2_start < 0) ++npix;
606 ++dy;
607 }
608 while(m_dist_pos[dy] <= m_width);
609 if(npix == 0) break;
610
611 npix = 0;
612 }
613 while(--m_step >= -m_max_extent);
614 }
615 m_li.adjust_forward();
616 m_step -= m_max_extent;
617 }
618
619 //---------------------------------------------------------------------
620 bool step_hor()
621 {
622 ++m_li;
623 m_x += m_lp.inc;
624 m_y = (m_lp.y1 + m_li.y()) >> line_subpixel_shift;
625
626 if(m_lp.inc > 0) m_di.inc_x(m_y - m_old_y);
627 else m_di.dec_x(m_y - m_old_y);
628
629 m_old_y = m_y;
630
631 int s1 = m_di.dist() / m_lp.len;
632 int s2 = -s1;
633
634 if(m_lp.inc < 0) s1 = -s1;
635
636 int dist_start;
637 int dist_pict;
638 int dist_end;
639 int dy;
640 int dist;
641
642 dist_start = m_di.dist_start();
643 dist_pict = m_di.dist_pict() + m_start;
644 dist_end = m_di.dist_end();
645 color_type* p0 = m_colors + max_half_width + 2;
646 color_type* p1 = p0;
647
648 int npix = 0;
649 p1->clear();
650 if(dist_end > 0)
651 {
652 if(dist_start <= 0)
653 {
654 m_ren.pixel(p1, dist_pict, s2);
655 }
656 ++npix;
657 }
658 ++p1;
659
660 dy = 1;
661 while((dist = m_dist_pos[dy]) - s1 <= m_width)
662 {
663 dist_start -= m_di.dx_start();
664 dist_pict -= m_di.dx_pict();
665 dist_end -= m_di.dx_end();
666 p1->clear();
667 if(dist_end > 0 && dist_start <= 0)
668 {
669 if(m_lp.inc > 0) dist = -dist;
670 m_ren.pixel(p1, dist_pict, s2 - dist);
671 ++npix;
672 }
673 ++p1;
674 ++dy;
675 }
676
677 dy = 1;
678 dist_start = m_di.dist_start();
679 dist_pict = m_di.dist_pict() + m_start;
680 dist_end = m_di.dist_end();
681 while((dist = m_dist_pos[dy]) + s1 <= m_width)
682 {
683 dist_start += m_di.dx_start();
684 dist_pict += m_di.dx_pict();
685 dist_end += m_di.dx_end();
686 --p0;
687 p0->clear();
688 if(dist_end > 0 && dist_start <= 0)
689 {
690 if(m_lp.inc > 0) dist = -dist;
691 m_ren.pixel(p0, dist_pict, s2 + dist);
692 ++npix;
693 }
694 ++dy;
695 }
696 m_ren.blend_color_vspan(m_x,
697 m_y - dy + 1,
698 unsigned(p1 - p0),
699 p0);
700 return npix && ++m_step < m_count;
701 }
702
703
704
705 //---------------------------------------------------------------------
706 bool step_ver()
707 {
708 ++m_li;
709 m_y += m_lp.inc;
710 m_x = (m_lp.x1 + m_li.y()) >> line_subpixel_shift;
711
712 if(m_lp.inc > 0) m_di.inc_y(m_x - m_old_x);
713 else m_di.dec_y(m_x - m_old_x);
714
715 m_old_x = m_x;
716
717 int s1 = m_di.dist() / m_lp.len;
718 int s2 = -s1;
719
720 if(m_lp.inc > 0) s1 = -s1;
721
722 int dist_start;
723 int dist_pict;
724 int dist_end;
725 int dist;
726 int dx;
727
728 dist_start = m_di.dist_start();
729 dist_pict = m_di.dist_pict() + m_start;
730 dist_end = m_di.dist_end();
731 color_type* p0 = m_colors + max_half_width + 2;
732 color_type* p1 = p0;
733
734 int npix = 0;
735 p1->clear();
736 if(dist_end > 0)
737 {
738 if(dist_start <= 0)
739 {
740 m_ren.pixel(p1, dist_pict, s2);
741 }
742 ++npix;
743 }
744 ++p1;
745
746 dx = 1;
747 while((dist = m_dist_pos[dx]) - s1 <= m_width)
748 {
749 dist_start += m_di.dy_start();
750 dist_pict += m_di.dy_pict();
751 dist_end += m_di.dy_end();
752 p1->clear();
753 if(dist_end > 0 && dist_start <= 0)
754 {
755 if(m_lp.inc > 0) dist = -dist;
756 m_ren.pixel(p1, dist_pict, s2 + dist);
757 ++npix;
758 }
759 ++p1;
760 ++dx;
761 }
762
763 dx = 1;
764 dist_start = m_di.dist_start();
765 dist_pict = m_di.dist_pict() + m_start;
766 dist_end = m_di.dist_end();
767 while((dist = m_dist_pos[dx]) + s1 <= m_width)
768 {
769 dist_start -= m_di.dy_start();
770 dist_pict -= m_di.dy_pict();
771 dist_end -= m_di.dy_end();
772 --p0;
773 p0->clear();
774 if(dist_end > 0 && dist_start <= 0)
775 {
776 if(m_lp.inc > 0) dist = -dist;
777 m_ren.pixel(p0, dist_pict, s2 - dist);
778 ++npix;
779 }
780 ++dx;
781 }
782 m_ren.blend_color_hspan(m_x - dx + 1,
783 m_y,
784 unsigned(p1 - p0),
785 p0);
786 return npix && ++m_step < m_count;
787 }
788
789
790 //---------------------------------------------------------------------
791 int pattern_end() const { return m_start + m_di.len(); }
792
793 //---------------------------------------------------------------------
794 bool vertical() const { return m_lp.vertical; }
795 int width() const { return m_width; }
796 int count() const { return m_count; }
797
798 private:
799 line_interpolator_image(const line_interpolator_image<Renderer>&);
800 const line_interpolator_image<Renderer>&
801 operator = (const line_interpolator_image<Renderer>&);
802
803 protected:
804 const line_parameters& m_lp;
805 dda2_line_interpolator m_li;
806 distance_interpolator4 m_di;
807 renderer_type& m_ren;
808 int m_plen;
809 int m_x;
810 int m_y;
811 int m_old_x;
812 int m_old_y;
813 int m_count;
814 int m_width;
815 int m_max_extent;
816 int m_start;
817 int m_step;
818 int m_dist_pos[max_half_width + 1];
819 color_type m_colors[max_half_width * 2 + 4];
820 };
821
822
823
824
825
826
827
828
829 //===================================================renderer_outline_image
830 template<class BaseRenderer, class ImagePattern>
831 class renderer_outline_image
832 {
833 public:
834 //---------------------------------------------------------------------
835 typedef BaseRenderer base_ren_type;
836 typedef renderer_outline_image<BaseRenderer, ImagePattern> self_type;
837 typedef typename base_ren_type::color_type color_type;
838 typedef ImagePattern pattern_type;
839
840
841 //---------------------------------------------------------------------
842 renderer_outline_image(base_ren_type& ren, pattern_type& patt) :
843 m_ren(&ren),
844 m_pattern(&patt),
845 m_start(0),
846 m_scale_x(1.0),
847 m_clip_box(0,0,0,0),
848 m_clipping(false)
849 {}
850 void attach(base_ren_type& ren) { m_ren = &ren; }
851
852 //---------------------------------------------------------------------
853 void pattern(pattern_type& p) { m_pattern = &p; }
854 pattern_type& pattern() const { return *m_pattern; }
855
856 //---------------------------------------------------------------------
857 void reset_clipping() { m_clipping = false; }
858 void clip_box(double x1, double y1, double x2, double y2)
859 {
860 m_clip_box.x1 = line_coord_sat::conv(x1);
861 m_clip_box.y1 = line_coord_sat::conv(y1);
862 m_clip_box.x2 = line_coord_sat::conv(x2);
863 m_clip_box.y2 = line_coord_sat::conv(y2);
864 m_clipping = true;
865 }
866
867 //---------------------------------------------------------------------
868 void scale_x(double s) { m_scale_x = s; }
869 double scale_x() const { return m_scale_x; }
870
871 //---------------------------------------------------------------------
872 void start_x(double s) { m_start = iround(s * line_subpixel_scale); }
873 double start_x() const { return double(m_start) / line_subpixel_scale; }
874
875 //---------------------------------------------------------------------
876 int subpixel_width() const { return m_pattern->line_width(); }
877 int pattern_width() const { return m_pattern->pattern_width(); }
878 double width() const { return double(subpixel_width()) / line_subpixel_scale; }
879
880 //-------------------------------------------------------------------------
881 void pixel(color_type* p, int x, int y) const
882 {
883 m_pattern->pixel(p, x, y);
884 }
885
886 //-------------------------------------------------------------------------
887 void blend_color_hspan(int x, int y, unsigned len, const color_type* colors)
888 {
889 m_ren->blend_color_hspan(x, y, len, colors, 0);
890 }
891
892 //-------------------------------------------------------------------------
893 void blend_color_vspan(int x, int y, unsigned len, const color_type* colors)
894 {
895 m_ren->blend_color_vspan(x, y, len, colors, 0);
896 }
897
898 //-------------------------------------------------------------------------
899 static bool accurate_join_only() { return true; }
900
901 //-------------------------------------------------------------------------
902 template<class Cmp>
903 void semidot(Cmp, int, int, int, int)
904 {
905 }
906
907 //-------------------------------------------------------------------------
908 void pie(int, int, int, int, int, int)
909 {
910 }
911
912 //-------------------------------------------------------------------------
913 void line0(const line_parameters&)
914 {
915 }
916
917 //-------------------------------------------------------------------------
918 void line1(const line_parameters&, int, int)
919 {
920 }
921
922 //-------------------------------------------------------------------------
923 void line2(const line_parameters&, int, int)
924 {
925 }
926
927 //-------------------------------------------------------------------------
928 void line3_no_clip(const line_parameters& lp,
929 int sx, int sy, int ex, int ey)
930 {
931 if(lp.len > line_max_length)
932 {
933 line_parameters lp1, lp2;
934 lp.divide(lp1, lp2);
935 int mx = lp1.x2 + (lp1.y2 - lp1.y1);
936 int my = lp1.y2 - (lp1.x2 - lp1.x1);
937 line3_no_clip(lp1, (lp.x1 + sx) >> 1, (lp.y1 + sy) >> 1, mx, my);
938 line3_no_clip(lp2, mx, my, (lp.x2 + ex) >> 1, (lp.y2 + ey) >> 1);
939 return;
940 }
941
942 fix_degenerate_bisectrix_start(lp, &sx, &sy);
943 fix_degenerate_bisectrix_end(lp, &ex, &ey);
944 line_interpolator_image<self_type> li(*this, lp,
945 sx, sy,
946 ex, ey,
947 m_start, m_scale_x);
948 if(li.vertical())
949 {
950 while(li.step_ver());
951 }
952 else
953 {
954 while(li.step_hor());
955 }
956 m_start += uround(lp.len / m_scale_x);
957 }
958
959 //-------------------------------------------------------------------------
960 void line3(const line_parameters& lp,
961 int sx, int sy, int ex, int ey)
962 {
963 if(m_clipping)
964 {
965 int x1 = lp.x1;
966 int y1 = lp.y1;
967 int x2 = lp.x2;
968 int y2 = lp.y2;
969 unsigned flags = clip_line_segment(&x1, &y1, &x2, &y2, m_clip_box);
970 int start = m_start;
971 if((flags & 4) == 0)
972 {
973 if(flags)
974 {
975 line_parameters lp2(x1, y1, x2, y2,
976 uround(calc_distance(x1, y1, x2, y2)));
977 if(flags & 1)
978 {
979 m_start += uround(calc_distance(lp.x1, lp.y1, x1, y1) / m_scale_x);
980 sx = x1 + (y2 - y1);
981 sy = y1 - (x2 - x1);
982 }
983 else
984 {
985 while(abs(sx - lp.x1) + abs(sy - lp.y1) > lp2.len)
986 {
987 sx = (lp.x1 + sx) >> 1;
988 sy = (lp.y1 + sy) >> 1;
989 }
990 }
991 if(flags & 2)
992 {
993 ex = x2 + (y2 - y1);
994 ey = y2 - (x2 - x1);
995 }
996 else
997 {
998 while(abs(ex - lp.x2) + abs(ey - lp.y2) > lp2.len)
999 {
1000 ex = (lp.x2 + ex) >> 1;
1001 ey = (lp.y2 + ey) >> 1;
1002 }
1003 }
1004 line3_no_clip(lp2, sx, sy, ex, ey);
1005 }
1006 else
1007 {
1008 line3_no_clip(lp, sx, sy, ex, ey);
1009 }
1010 }
1011 m_start = start + uround(lp.len / m_scale_x);
1012 }
1013 else
1014 {
1015 line3_no_clip(lp, sx, sy, ex, ey);
1016 }
1017 }
1018
1019 private:
1020 base_ren_type* m_ren;
1021 pattern_type* m_pattern;
1022 int m_start;
1023 double m_scale_x;
1024 rect_i m_clip_box;
1025 bool m_clipping;
1026 };
1027
1028
1029
1030
1031
1032 }
1033
1034
1035
1036 #endif