View.cs 6.42 KB
Newer Older
1
2
using System;
using System.Collections.Generic;
3
using System.Drawing.Imaging;
4
5
6
7
8
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Svg;
Eric Domke's avatar
Eric Domke committed
9
using System.Diagnostics;
10
11
12
13
14
15
16
17
18
19
20
21

namespace SvgW3CTestRunner
{
    public partial class View : Form
    {
        private const string _svgBasePath = @"..\..\..\W3CTestSuite\svg\";
        private const string _pngBasePath = @"..\..\..\W3CTestSuite\png\";

        public View()
        {
            InitializeComponent();
            // ignore tests pertaining to javascript or xml reading
Eric Domke's avatar
Eric Domke committed
22
23
            var passes = File.ReadAllLines(_svgBasePath + @"..\PassingTests.txt").ToDictionary((f) => f, (f) => true);
            var files = (from f in
24
25
                         (from g in Directory.GetFiles(_svgBasePath)
                          select Path.GetFileName(g))
26
                         where !f.StartsWith("animate-") && !f.StartsWith("conform-viewer") &&
27
28
                         !f.Contains("-dom-") && !f.StartsWith("linking-") && !f.StartsWith("interact-") &&
                         !f.StartsWith("script-")
29
30
                         orderby f
                         select (object)f);
Eric Domke's avatar
Eric Domke committed
31
32
            files = files.Where((f) => !passes.ContainsKey((string)f)).Union(Enumerable.Repeat((object)"## PASSING ##", 1)).Union(files.Where((f) => passes.ContainsKey((string)f)));

33
34
35
36
37
            lstFiles.Items.AddRange(files.ToArray());
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
38
            //render svg
39
            var fileName = lstFiles.SelectedItem.ToString();
Eric Domke's avatar
Eric Domke committed
40
            if (fileName.StartsWith("#")) return;
41
            
42
43
44
45
46
            //display png
            var png = Image.FromFile(_pngBasePath + Path.GetFileNameWithoutExtension(fileName) + ".png");
            picPng.Image = png;
            
            var doc = new SvgDocument();
Eric Domke's avatar
Eric Domke committed
47
48
            try
            {
Eric Domke's avatar
Eric Domke committed
49
                Debug.Print(fileName);
50
                doc = SvgDocument.Open(_svgBasePath + fileName);
Eric Domke's avatar
Eric Domke committed
51
52
                if (fileName.StartsWith("__"))
                {
53
                    picSvg.Image = doc.Draw();
Eric Domke's avatar
Eric Domke committed
54
55
56
57
58
                }
                else
                {
                    var img = new Bitmap(480, 360);
                    doc.Draw(img);
59
60
61
                    picSvg.Image = img;
                }
                
62
63
64
65
66
67
68
69
70
71
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "SVG Rendering");
                picSvg.Image = null;
            }
            
            //save load
            try 
            {
72
73
74
75
76
77
78
79
                using(var memStream = new MemoryStream())
                {
                    doc.Write(memStream);
                    memStream.Position = 0;
                    doc = SvgDocument.Open<SvgDocument>(memStream);
                    
                    if (fileName.StartsWith("__"))
                    {
80
                        picSaveLoad.Image = doc.Draw();
81
82
83
84
85
                    }
                    else
                    {
                        var img = new Bitmap(480, 360);
                        doc.Draw(img);
86
                        picSaveLoad.Image = img;
87
                    }
Eric Domke's avatar
Eric Domke committed
88
                }
89
90
91
92
93
94
95
96
97
98
99
            } 
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "SVG Serialization");
                picSaveLoad.Image = null;
            }
            
            //compare svg to png
            try
            {
                picSVGPNG.Image = PixelDiff((Bitmap)picSvg.Image, (Bitmap)picPng.Image);
Eric Domke's avatar
Eric Domke committed
100
101
102
            }
            catch (Exception ex)
            {
103
104
                MessageBox.Show(ex.ToString(), "SVG Comparison");
                picSVGPNG.Image = null;
Eric Domke's avatar
Eric Domke committed
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
        
        unsafe Bitmap PixelDiff(Bitmap a, Bitmap b)
        {
            Bitmap output = new Bitmap(a.Width, a.Height, PixelFormat.Format32bppArgb);
            Rectangle rect = new Rectangle(Point.Empty, a.Size);
            using (var aData = a.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
                using (var bData = b.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
                    using (var outputData = output.LockBitsDisposable(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb))
            {
                byte* aPtr = (byte*)aData.Scan0;
                byte* bPtr = (byte*)bData.Scan0;
                byte* outputPtr = (byte*)outputData.Scan0;
                int len = aData.Stride * aData.Height;
                for (int i = 0; i < len; i++)
                {
                    // For alpha use the average of both images (otherwise pixels with the same alpha won't be visible)
                    if ((i + 1) % 4 == 0)
                        *outputPtr = (byte)((*aPtr  + *bPtr) / 2);
                    else
                        *outputPtr = (byte)~(*aPtr ^ *bPtr);

                    outputPtr++;
                    aPtr++;
                    bPtr++;
                }
            }
            return output;
        }


    }
    
    static class BitmapExtensions
    {
        public static DisposableImageData LockBitsDisposable(this Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
        {
            return new DisposableImageData(bitmap, rect, flags, format);
        }

        public class DisposableImageData : IDisposable
        {
            private readonly Bitmap _bitmap;
            private readonly BitmapData _data;

            internal DisposableImageData(Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
            {
                _bitmap = bitmap;
                _data = bitmap.LockBits(rect, flags, format);
            }

            public void Dispose()
            {
                _bitmap.UnlockBits(_data);
            }

            public IntPtr Scan0
            {
                get { return _data.Scan0; }
            }

            public int Stride
            {
                get { return _data.Stride;}
            }

            public int Width
            {
                get { return _data.Width;}
            }

            public int Height
            {
                get { return _data.Height;}
            }

            public PixelFormat PixelFormat
            {
                get { return _data.PixelFormat;}
            }

            public int Reserved
            {
                get { return _data.Reserved;}
            }
        }
194
195
    }
}