View.cs 7.34 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

namespace SvgW3CTestRunner
{
    public partial class View : Form
    {
15
16
17
18
19
20
		//DIRECTORY SEPARATOR: The value of this field is a slash ("/") on UNIX and on Mac OSX, and a backslash ("\") on the Windows operating systems.
		static private string sprt = Path.DirectorySeparatorChar.ToString (); 

		//Data folders
		private string _svgBasePath = @".."+sprt+".."+sprt+".."+sprt+"W3CTestSuite"+sprt+"svg"+sprt;
		private string _pngBasePath = @".."+sprt+".."+sprt+".."+sprt+"W3CTestSuite"+sprt+"png"+sprt;
21
22
23
24
25

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

37
38
39
40
41
            lstFiles.Items.AddRange(files.ToArray());
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
42
            //render svg
43
            var fileName = lstFiles.SelectedItem.ToString();
Eric Domke's avatar
Eric Domke committed
44
            if (fileName.StartsWith("#")) return;
45
            
46
47
48
49
50
            //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
51
52
            try
            {
Eric Domke's avatar
Eric Domke committed
53
                Debug.Print(fileName);
54
                doc = SvgDocument.Open(_svgBasePath + fileName);
Eric Domke's avatar
Eric Domke committed
55
56
                if (fileName.StartsWith("__"))
                {
57
                    picSvg.Image = doc.Draw();
Eric Domke's avatar
Eric Domke committed
58
59
60
61
62
                }
                else
                {
                    var img = new Bitmap(480, 360);
                    doc.Draw(img);
63
64
65
                    picSvg.Image = img;
                }
                
66
67
68
69
70
71
72
73
74
75
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "SVG Rendering");
                picSvg.Image = null;
            }
            
            //save load
            try 
            {
76
77
                using(var memStream = new MemoryStream())
                {
Eric Domke's avatar
Eric Domke committed
78
79
80
                    doc.Write(memStream);
                    memStream.Position = 0;  
                    var reader = new StreamReader(memStream);
81
82
                    var tempFilePath = Path.Combine(Path.GetTempPath(), "test.svg");
                    System.IO.File.WriteAllText(tempFilePath, reader.ReadToEnd());
83
                    memStream.Position = 0;
Eric Domke's avatar
Eric Domke committed
84
                    var baseUri = doc.BaseUri;
85
                    doc = SvgDocument.Open(tempFilePath);
Eric Domke's avatar
Eric Domke committed
86
                    doc.BaseUri = baseUri;
87
88
89
                    
                    if (fileName.StartsWith("__"))
                    {
90
                        picSaveLoad.Image = doc.Draw();
91
92
93
94
95
                    }
                    else
                    {
                        var img = new Bitmap(480, 360);
                        doc.Draw(img);
96
                        picSaveLoad.Image = img;
97
                    }
Eric Domke's avatar
Eric Domke committed
98
                }
99
100
101
102
103
104
105
106
107
108
            } 
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "SVG Serialization");
                picSaveLoad.Image = null;
            }
            
            //compare svg to png
            try
            {
109
                picSVGPNG.Image = PixelDiff((Bitmap)picPng.Image, (Bitmap)picSvg.Image);
Eric Domke's avatar
Eric Domke committed
110
111
112
            }
            catch (Exception ex)
            {
113
114
                MessageBox.Show(ex.ToString(), "SVG Comparison");
                picSVGPNG.Image = null;
Eric Domke's avatar
Eric Domke committed
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
        
        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;
146
147
148
149
150
151
152
153
154
155
156
        }
        
        
        void RunAllToolStripMenuItemClick(object sender, EventArgs e)
        {
            foreach(string fileName in lstFiles.Items)
            {
                if (fileName.StartsWith("#")) continue;
                
                
            }    
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        }


    }
    
    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;}
            }
        }
215
216
    }
}