SvgFilter.cs 2.38 KB
Newer Older
davescriven's avatar
davescriven committed
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Svg.FilterEffects
{
    public class SvgFilter : SvgElement, ISvgFilter
    {
        private readonly List<SvgFilterPrimitive> _primitives;
        private readonly Dictionary<string, Bitmap> _results;
        private SvgUnit _width;
        private SvgUnit _height;

        [SvgAttribute("width")]
        public SvgUnit Width
        {
            get { return this._width; }
            set { this._width = value; }
        }

        [SvgAttribute("height")]
        public SvgUnit Height
        {
            get { return this._height; }
            set { this._height = value; }
        }

        public List<SvgFilterPrimitive> Primitives
        {
            get { return this._primitives; }
        }

        public Dictionary<string, Bitmap> Results
        {
            get { return this._results; }
        }

        public SvgFilter()
        {
            this._primitives = new List<SvgFilterPrimitive>();
            this._results = new Dictionary<string, Bitmap>();
        }

        protected override void Render(Graphics graphics)
        {
            // Do nothing
        }

        public override object Clone()
        {
            return (SvgFilter)this.MemberwiseClone();
        }

        public void ApplyFilter(Bitmap sourceGraphic, Graphics renderer)
        {
            // A bit inefficient to create all the defaults when they may not even be used
            this.PopulateDefaults(sourceGraphic);
            Bitmap result = null;

            foreach (SvgFilterPrimitive primitive in this.Primitives)
            {
                result = primitive.Apply();
                // Add the result to the dictionary for use by other primitives
                this._results.Add(primitive.Result, result);
            }

            // Render the final filtered image
            renderer.DrawImageUnscaled(result, new Point(0,0));
        }

        private void PopulateDefaults(Bitmap sourceGraphic)
        {
            // Source graphic
            //this._results.Add("SourceGraphic", sourceGraphic);

            // Source alpha
            //this._results.Add("SourceAlpha", ImageProcessor.GetAlphaChannel(sourceGraphic));
        }
    }
}