-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathImageTester.cs
More file actions
119 lines (106 loc) · 4.67 KB
/
ImageTester.cs
File metadata and controls
119 lines (106 loc) · 4.67 KB
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
84
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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.IO;
using Stride.Core.Mathematics;
namespace Stride.Graphics.Regression
{
public static class ImageTester
{
public enum ComparisonMode
{
/// <summary>
/// Comparison will fails if image doesn't exist or is different.
/// </summary>
CompareOnly,
/// <summary>
/// Comparison will fails if image is different. If no version of it exist yet, it will be created.
/// </summary>
CompareOrCreate,
CompareOrCreateAlternative,
}
public enum ComparisonResult
{
ReferenceCreated,
Success,
Failed,
}
public static void SaveImage(Image image, string testFilename)
{
Directory.CreateDirectory(Path.GetDirectoryName(testFilename));
using (var stream = File.Open(testFilename, FileMode.Create))
{
image.Save(stream, ImageFileType.Png);
}
}
/// <summary>
/// Send the data of the test to the server.
/// </summary>
/// <param name="image">The image to send.</param>
/// <param name="testFilename">The expected filename.</param>
public static bool CompareImage(Image image, string testFilename)
{
// Compare
using (var stream = File.OpenRead(testFilename))
using (var referenceImage = Image.Load(stream))
{
// Start comparison
if (image.PixelBuffer.Count != referenceImage.PixelBuffer.Count)
{
return false;
}
for (int i = 0; i < image.PixelBuffer.Count; ++i)
{
var buffer = image.PixelBuffer[i];
var referenceBuffer = referenceImage.PixelBuffer[i];
if (buffer.Width != referenceBuffer.Width
|| buffer.Height != referenceBuffer.Height
|| buffer.RowStride != referenceBuffer.RowStride)
return false;
var swapBGR = buffer.Format.IsBGRAOrder() != referenceBuffer.Format.IsBGRAOrder();
// For now, we handle only those specific cases
if ((buffer.Format != PixelFormat.R8G8B8A8_UNorm_SRgb && buffer.Format != PixelFormat.B8G8R8A8_UNorm_SRgb)
|| referenceBuffer.Format != PixelFormat.B8G8R8A8_UNorm)
{
// TODO: support more formats
return false;
}
bool checkAlpha = buffer.Format.AlphaSizeInBits() > 0;
// Compare remaining bytes.
int allowedDiff = 2;
int differentPixels = 0;
unsafe
{
for (int y = 0; y < buffer.Height; ++y)
{
var pSrc = (Color*)(buffer.DataPointer + y * buffer.RowStride);
var pDst = (Color*)(referenceBuffer.DataPointer + y * referenceBuffer.RowStride);
for (int x = 0; x < buffer.Width; ++x, ++pSrc, ++pDst)
{
var src = *pSrc;
if (swapBGR)
{
var tmp = src.B;
src.B = src.R;
src.R = tmp;
}
var r = Math.Abs((int)src.R - (int)pDst->R);
var g = Math.Abs((int)src.G - (int)pDst->G);
var b = Math.Abs((int)src.B - (int)pDst->B);
var a = Math.Abs((int)src.A - (int)pDst->A);
if (r > allowedDiff || g > allowedDiff || b > allowedDiff || (a > allowedDiff && checkAlpha))
{
// Too big difference
differentPixels++;
}
}
}
}
if (differentPixels > 0)
return false;
}
return true;
}
}
}
}