View.cs 5.75 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
38
            lstFiles.Items.AddRange(files.ToArray());
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            var fileName = lstFiles.SelectedItem.ToString();
Eric Domke's avatar
Eric Domke committed
39
            if (fileName.StartsWith("#")) return;
40
            
Eric Domke's avatar
Eric Domke committed
41
42
            try
            {
Eric Domke's avatar
Eric Domke committed
43
                Debug.Print(fileName);
Eric Domke's avatar
Eric Domke committed
44
                var doc = SvgDocument.Open(_svgBasePath + fileName);
Eric Domke's avatar
Eric Domke committed
45
46
                if (fileName.StartsWith("__"))
                {
47
                    picSvg.Image = doc.Draw();
Eric Domke's avatar
Eric Domke committed
48
49
50
51
52
                }
                else
                {
                    var img = new Bitmap(480, 360);
                    doc.Draw(img);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                    picSvg.Image = img;
                }
                
                //save load
                using(var memStream = new MemoryStream())
                {
                    doc.Write(memStream);
                    memStream.Position = 0;
                    doc = SvgDocument.Open<SvgDocument>(memStream);
                    
                    if (fileName.StartsWith("__"))
                    {
                        picSvg.Image = doc.Draw();
                    }
                    else
                    {
                        var img = new Bitmap(480, 360);
                        doc.Draw(img);
                        picSvg.Image = img;
                    }
Eric Domke's avatar
Eric Domke committed
73
                }
74
                
Eric Domke's avatar
Eric Domke committed
75
76
77
78
79
80
81
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                picSvg.Image = null;
            }
            
82
83
84
            var png = Image.FromFile(_pngBasePath + Path.GetFileNameWithoutExtension(fileName) + ".png");
            picPng.Image = png;
        }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
        
        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;}
            }
        }
170
171
    }
}