-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added shared library for test application: Svg.Tests.Common (#1136)
- Refactored the shared test codes - Removed Svg.Benchmark dependency on Svg.UnitTests
- Loading branch information
Showing
20 changed files
with
270 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System; | ||
using System.Drawing.Imaging; | ||
using System.Drawing; | ||
using System.Drawing.Drawing2D; | ||
|
||
|
||
namespace Svg.Tests.Common | ||
{ | ||
/// <summary> | ||
/// Taken from https://web.archive.org/web/20130111215043/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale | ||
/// and slightly modified. | ||
/// Image width and height, default threshold and handling of alpha values have been adapted. | ||
/// </summary> | ||
public static class ExtensionMethods | ||
{ | ||
private static readonly int ImageWidth = 64; | ||
private static readonly int ImageHeight = 64; | ||
|
||
public static float PercentageDifference(this Image img1, Image img2, byte threshold = 10) | ||
{ | ||
byte[,] differences = img1.GetDifferences(img2); | ||
|
||
int diffPixels = 0; | ||
|
||
foreach (byte b in differences) | ||
{ | ||
if (b > threshold) { diffPixels++; } | ||
} | ||
|
||
return diffPixels / (float)(differences.GetLength(0) * differences.GetLength(1)); | ||
} | ||
|
||
public static Bitmap Resize(this Image originalImage, int newWidth, int newHeight) | ||
{ | ||
if (originalImage.Width > originalImage.Height) | ||
newWidth = originalImage.Width * newHeight / originalImage.Height; | ||
else | ||
newHeight = originalImage.Height * newWidth / originalImage.Width; | ||
|
||
var smallVersion = new Bitmap(newWidth, newHeight); | ||
using (var g = Graphics.FromImage(smallVersion)) | ||
{ | ||
g.SmoothingMode = SmoothingMode.HighQuality; | ||
g.InterpolationMode = InterpolationMode.HighQualityBicubic; | ||
g.PixelOffsetMode = PixelOffsetMode.HighQuality; | ||
g.DrawImage(originalImage, 0, 0, smallVersion.Width, smallVersion.Height); | ||
} | ||
|
||
return smallVersion; | ||
} | ||
|
||
public static byte[,] GetGrayScaleValues(this Bitmap img) | ||
{ | ||
byte[,] grayScale = new byte[img.Width, img.Height]; | ||
|
||
for (int y = 0; y < grayScale.GetLength(1); y++) | ||
{ | ||
for (int x = 0; x < grayScale.GetLength(0); x++) | ||
{ | ||
var alpha = img.GetPixel(x, y).A; | ||
var gray = img.GetPixel(x, y).R; | ||
grayScale[x, y] = (byte)Math.Abs(gray * alpha / 255); | ||
} | ||
} | ||
return grayScale; | ||
} | ||
|
||
// the colormatrix needed to grayscale an image | ||
static readonly ColorMatrix ColorMatrix = new ColorMatrix(new float[][] | ||
{ | ||
new float[] {.3f, .3f, .3f, 0, 0}, | ||
new float[] {.59f, .59f, .59f, 0, 0}, | ||
new float[] {.11f, .11f, .11f, 0, 0}, | ||
new float[] {0, 0, 0, 1, 0}, | ||
new float[] {0, 0, 0, 0, 1} | ||
}); | ||
|
||
public static Bitmap GetGrayScaleVersion(this Bitmap original) | ||
{ | ||
// create a blank bitmap the same size as original | ||
// https://web.archive.org/web/20130111215043/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale | ||
var newBitmap = new Bitmap(original.Width, original.Height); | ||
|
||
// get a graphics object from the new image | ||
using (var g = Graphics.FromImage(newBitmap)) | ||
// create some image attributes | ||
using (var attributes = new ImageAttributes()) | ||
{ | ||
// set the color matrix attribute | ||
attributes.SetColorMatrix(ColorMatrix); | ||
|
||
// draw the original image on the new image | ||
// using the grayscale color matrix | ||
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), | ||
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); | ||
} | ||
|
||
return newBitmap; | ||
} | ||
|
||
public static byte[,] GetDifferences(this Image img1, Image img2) | ||
{ | ||
using (var resizedThisOne = img1.Resize(ImageWidth, ImageHeight)) | ||
using (var thisOne = resizedThisOne.GetGrayScaleVersion()) | ||
using (var resizedTheOtherOne = img2.Resize(ImageWidth, ImageHeight)) | ||
using (var theOtherOne = resizedTheOtherOne.GetGrayScaleVersion()) | ||
{ | ||
byte[,] differences = new byte[thisOne.Width, thisOne.Height]; | ||
byte[,] firstGray = thisOne.GetGrayScaleValues(); | ||
byte[,] secondGray = theOtherOne.GetGrayScaleValues(); | ||
|
||
for (int y = 0; y < differences.GetLength(1); y++) | ||
{ | ||
for (int x = 0; x < differences.GetLength(0); x++) | ||
{ | ||
differences[x, y] = (byte)Math.Abs(firstGray[x, y] - secondGray[x, y]); | ||
} | ||
} | ||
return differences; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.CompilerServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("Svg.Tests.Common")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Svg.Tests.Common")] | ||
[assembly: AssemblyCopyright("Copyright © 2024")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("6ab1d266-f201-46c0-9d14-523768eb18db")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] | ||
|
||
[assembly: InternalsVisibleTo("Svg.Benchmark,PublicKey=" + | ||
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" + | ||
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" + | ||
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" + | ||
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" + | ||
"5a7f0cb4")] | ||
|
||
[assembly: InternalsVisibleTo("Svg.UnitTests,PublicKey=" + | ||
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" + | ||
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" + | ||
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" + | ||
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" + | ||
"5a7f0cb4")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;netcoreapp3.1;net462</TargetFrameworks> | ||
<SignAssembly>False</SignAssembly> | ||
<DelaySign>False</DelaySign> | ||
<IsPackable>False</IsPackable> | ||
<SignAssembly>True</SignAssembly> | ||
<AssemblyOriginatorKeyFile>svgkey.snk</AssemblyOriginatorKeyFile> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net462'"> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.IO.Compression" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> | ||
<PackageReference Include="System.Drawing.Common" Version="5.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> | ||
<PackageReference Include="System.Drawing.Common" Version="5.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<PackageReference Include="System.Drawing.Common" Version="5.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Source\Svg.csproj" /> | ||
<PackageReference Include="Fizzler" Version="1.3.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
6 changes: 2 additions & 4 deletions
6
Tests/Svg.UnitTests/TestsUtils.cs → Tests/Svg.Tests.Common/TestsUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.