Skip to content

Commit 5e35221

Browse files
committed
Migration to SkiaSharp
1 parent 133a0d3 commit 5e35221

File tree

12 files changed

+117
-102
lines changed

12 files changed

+117
-102
lines changed

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Aquality.Selenium.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
<PackageReference Include="NLog" Version="5.1.2" />
5454
<PackageReference Include="Selenium.Support" Version="4.8.1" />
5555
<PackageReference Include="Selenium.WebDriver" Version="4.8.1" />
56-
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
56+
<PackageReference Include="SkiaSharp" Version="2.88.3" />
5757
</ItemGroup>
5858
</Project>

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Aquality.Selenium.Core.xml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/DumpManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Aquality.Selenium.Core.Configurations;
22
using Aquality.Selenium.Core.Elements.Interfaces;
33
using Aquality.Selenium.Core.Localization;
4+
using SkiaSharp;
45
using System;
56
using System.Collections.Generic;
6-
using System.Drawing;
77
using System.Globalization;
88
using System.IO;
99
using System.Linq;
@@ -64,7 +64,7 @@ public virtual float Compare(string dumpName = null)
6464
}
6565
else
6666
{
67-
comparisonResult += existingElements[key].Visual.GetDifference(Image.FromFile(imageFile.FullName));
67+
comparisonResult += existingElements[key].Visual.GetDifference(SKImage.FromEncodedData(imageFile.FullName));
6868
countOfUnproceededElements--;
6969
countOfProceededElements++;
7070
existingElements.Remove(key);

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/IImageComparator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Aquality.Selenium.Core.Configurations;
2-
using System.Drawing;
2+
using SkiaSharp;
33

44
namespace Aquality.Selenium.Core.Visualization
55
{
@@ -17,6 +17,6 @@ public interface IImageComparator
1717
/// <param name="theOtherOne">The image to compare with</param>
1818
/// <param name="threshold">How big a difference will be ignored as a percentage - value between 0 and 1. </param>
1919
/// <returns>The difference between the two images as a percentage - value between 0 and 1.</returns>
20-
float PercentageDifference(Image thisOne, Image theOtherOne, float? threshold = null);
20+
float PercentageDifference(SKImage thisOne, SKImage theOtherOne, float? threshold = null);
2121
}
2222
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/IVisualStateProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Aquality.Selenium.Core.Configurations;
2+
using SkiaSharp;
23
using System.Drawing;
34

45
namespace Aquality.Selenium.Core.Visualization
@@ -22,7 +23,7 @@ public interface IVisualStateProvider
2223
/// <summary>
2324
/// Gets an image containing the screenshot of the element.
2425
/// </summary>
25-
Image Image { get; }
26+
SKImage Image { get; }
2627

2728
/// <summary>
2829
/// Gets the difference between the image of the element and the provided image
@@ -32,6 +33,6 @@ public interface IVisualStateProvider
3233
/// <param name="threshold">How big a difference will be ignored as a percentage - value between 0 and 1.
3334
/// If the value is null, the default value is got from <see cref="IVisualizationConfiguration"/>.</param>
3435
/// <returns>The difference between the two images as a percentage - value between 0 and 1.</returns>
35-
float GetDifference(Image theOtherOne, float? threshold = null);
36+
float GetDifference(SKImage theOtherOne, float? threshold = null);
3637
}
3738
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/ImageComparator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Core.Configurations;
2+
using SkiaSharp;
23
using System;
3-
using System.Drawing;
44

55
namespace Aquality.Selenium.Core.Visualization
66
{
@@ -33,7 +33,7 @@ public ImageComparator(IVisualizationConfiguration visualizationConfiguration)
3333
/// If the value is null, the default value is got from <see cref="IVisualizationConfiguration"/>.</param>
3434
/// <returns>The difference between the two images as a percentage - value between 0 and 1.</returns>
3535
/// <remarks>See https://web.archive.org/web/20130208001434/http://tech.pro:80/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale for more details.</remarks>
36-
public virtual float PercentageDifference(Image thisOne, Image theOtherOne, float? threshold = null)
36+
public virtual float PercentageDifference(SKImage thisOne, SKImage theOtherOne, float? threshold = null)
3737
{
3838
var thresholdValue = threshold ?? DefaultThreshold;
3939
if (thresholdValue < 0 || thresholdValue > 1)
@@ -53,7 +53,7 @@ public virtual float PercentageDifference(Image thisOne, Image theOtherOne, floa
5353
/// <param name="threshold">How big a difference (out of 255) will be ignored - the default is 3.</param>
5454
/// <returns>The difference between the two images as a percentage</returns>
5555
/// <remarks>See https://web.archive.org/web/20130208001434/http://tech.pro:80/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale for more details</remarks>
56-
protected virtual float PercentageDifference(Image thisOne, Image theOtherOne, byte threshold = 3)
56+
protected virtual float PercentageDifference(SKImage thisOne, SKImage theOtherOne, byte threshold = 3)
5757
{
5858
var differences = GetDifferences(thisOne, theOtherOne);
5959

@@ -73,7 +73,7 @@ protected virtual float PercentageDifference(Image thisOne, Image theOtherOne, b
7373
/// <param name="thisOne">The first image</param>
7474
/// <param name="theOtherOne">The image to compare with</param>
7575
/// <returns>the differences between the two images as a double-array</returns>
76-
protected virtual byte[,] GetDifferences(Image thisOne, Image theOtherOne)
76+
protected virtual byte[,] GetDifferences(SKImage thisOne, SKImage theOtherOne)
7777
{
7878
var firstGray = GetResizedGrayScaleValues(thisOne);
7979
var secondGray = GetResizedGrayScaleValues(theOtherOne);
@@ -95,17 +95,17 @@ protected virtual float PercentageDifference(Image thisOne, Image theOtherOne, b
9595
/// </summary>
9696
/// <param name="img">The image to get the lightness for</param>
9797
/// <returns>A double-array (16x16 by default) containing the lightness of the sections(256 by default)</returns>
98-
protected virtual byte[,] GetResizedGrayScaleValues(Image img)
98+
protected virtual byte[,] GetResizedGrayScaleValues(SKImage img)
9999
{
100-
using (var thisOne = (Bitmap)img.Resize(ComparisonWidth, ComparisonHeight).GetGrayScaleVersion())
100+
using (var thisOne = SKBitmap.FromImage(img.Resize(ComparisonWidth, ComparisonHeight).GetGrayScaleVersion()))
101101
{
102102
byte[,] grayScale = new byte[thisOne.Width, thisOne.Height];
103103

104104
for (int y = 0; y < thisOne.Height; y++)
105105
{
106106
for (int x = 0; x < thisOne.Width; x++)
107107
{
108-
grayScale[x, y] = (byte)Math.Abs(thisOne.GetPixel(x, y).R);
108+
grayScale[x, y] = (byte)Math.Abs(thisOne.GetPixel(x, y).Red);
109109
}
110110
}
111111

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Drawing;
2-
using System.Drawing.Drawing2D;
3-
using System.Drawing.Imaging;
1+
using SkiaSharp;
2+
using System.Drawing;
3+
using System.IO;
44

55
namespace Aquality.Selenium.Core.Visualization
66
{
@@ -10,13 +10,12 @@ namespace Aquality.Selenium.Core.Visualization
1010
/// </summary>
1111
public static class ImageExtensions
1212
{
13-
private static readonly ColorMatrix ColorMatrix = new ColorMatrix(new float[][]
13+
private static readonly SKColorFilter ColorMatrix = SKColorFilter.CreateColorMatrix(new float[]
1414
{
15-
new float[] {.3f, .3f, .3f, 0, 0},
16-
new float[] {.59f, .59f, .59f, 0, 0},
17-
new float[] {.11f, .11f, .11f, 0, 0},
18-
new float[] {0, 0, 0, 1, 0},
19-
new float[] {0, 0, 0, 0, 1}
15+
.3f, .3f, .3f, 0, 0,
16+
.59f, .59f, .59f, 0, 0,
17+
.11f, .11f, .11f, 0, 0,
18+
0, 0, 0, 1, 0
2019
});
2120

2221
/// <summary>
@@ -26,18 +25,16 @@ public static class ImageExtensions
2625
/// <param name="newWidth">The new width in pixels</param>
2726
/// <param name="newHeight">The new height in pixels</param>
2827
/// <returns>A resized version of the original image</returns>
29-
public static Image Resize(this Image originalImage, int newWidth, int newHeight)
28+
public static SKImage Resize(this SKImage originalImage, int newWidth, int newHeight)
3029
{
31-
var smallVersion = new Bitmap(newWidth, newHeight);
32-
using (var graphics = Graphics.FromImage(smallVersion))
30+
using (var srcBitmap = SKBitmap.FromImage(originalImage))
3331
{
34-
graphics.SmoothingMode = SmoothingMode.HighQuality;
35-
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
36-
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
37-
graphics.DrawImage(originalImage, 0, 0, newWidth, newHeight);
38-
}
39-
40-
return smallVersion;
32+
var resizeInfo = new SKImageInfo(newWidth, newHeight);
33+
using (var smallVersion = srcBitmap.Resize(resizeInfo, SKFilterQuality.High))
34+
{
35+
return SKImage.FromBitmap(smallVersion);
36+
}
37+
}
4138
}
4239

4340
/// <summary>
@@ -46,27 +43,38 @@ public static Image Resize(this Image originalImage, int newWidth, int newHeight
4643
/// <param name="original">The image to gray scale</param>
4744
/// <returns>A gray scale version of the image</returns>
4845
/// <remarks>See http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale for more details</remarks>
49-
public static Image GetGrayScaleVersion(this Image original)
46+
public static SKImage GetGrayScaleVersion(this SKImage original)
5047
{
5148
//create a blank bitmap the same size as original
52-
var newBitmap = new Bitmap(original.Width, original.Height);
49+
var info = new SKImageInfo(original.Width, original.Height);
50+
var newBitmap = new SKBitmap(info);
51+
5352

53+
//create some image attributes
54+
using (var paint = new SKPaint())
5455
//get a graphics object from the new image
55-
using (var graphics = Graphics.FromImage(newBitmap))
56+
using (var graphics = new SKCanvas(newBitmap))
5657
{
57-
//create some image attributes
58-
var attributes = new ImageAttributes();
59-
6058
//set the color matrix attribute
61-
attributes.SetColorMatrix(ColorMatrix);
59+
paint.ColorFilter = ColorMatrix;
6260

6361
//draw the original image on the new image
6462
//using the gray-scale color matrix
65-
graphics.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
66-
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
63+
graphics.DrawImage(original, 0, 0, paint);
6764
}
6865

69-
return newBitmap;
66+
return SKImage.FromBitmap(newBitmap);
67+
}
68+
69+
public static Size Size(this SKImage image) => new Size(image.Width, image.Height);
70+
71+
public static void Save(this SKImage image, string name, SKEncodedImageFormat format = SKEncodedImageFormat.Png)
72+
{
73+
using (Stream stream = new FileStream(name, FileMode.OpenOrCreate))
74+
{
75+
SKData d = image.Encode(format, 100);
76+
d.SaveTo(stream);
77+
}
7078
}
7179
}
7280
}

0 commit comments

Comments
 (0)