Skip to content

Commit 49d2142

Browse files
committed
Small refactor and added unit test for the new ToWriteableBitmapWithDensity method.
1 parent a80eb05 commit 49d2142

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/Magick.NET.AvaloniaMediaImaging/IMagickImageExtentions.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,29 @@ namespace ImageMagick;
1515
public static partial class IMagickImageExtentions
1616
{
1717
/// <summary>
18-
/// Converts this instance to a <see cref="WriteableBitmap"/> with a default DPI of 96x96.
18+
/// Converts this instance to a <see cref="WriteableBitmap"/> with a default dpi of 96x96.
1919
/// </summary>
2020
/// <param name="self">The image.</param>
2121
/// <typeparam name="TQuantumType">The quantum type.</typeparam>
2222
/// <returns>A <see cref="WriteableBitmap"/>.</returns>
23-
public static unsafe WriteableBitmap ToWriteableBitmap<TQuantumType>(this IMagickImage<TQuantumType> self)
23+
public static WriteableBitmap ToWriteableBitmap<TQuantumType>(this IMagickImage<TQuantumType> self)
2424
where TQuantumType : struct, IConvertible
25-
{
26-
return self.ToWriteableBitmapInternal(false);
27-
}
28-
25+
=> self.ToWriteableBitmapInternal(new Vector(96, 96));
26+
2927
/// <summary>
3028
/// Converts this instance to a <see cref="WriteableBitmap"/>.
3129
/// </summary>
3230
/// <param name="self">The image.</param>
3331
/// <typeparam name="TQuantumType">The quantum type.</typeparam>
3432
/// <returns>A <see cref="WriteableBitmap"/>.</returns>
35-
public static unsafe WriteableBitmap ToWriteableBitmapWithDensity<TQuantumType>(this IMagickImage<TQuantumType> self)
33+
public static WriteableBitmap ToWriteableBitmapWithDensity<TQuantumType>(this IMagickImage<TQuantumType> self)
3634
where TQuantumType : struct, IConvertible
37-
{
38-
return self.ToWriteableBitmapInternal(true);
39-
}
35+
=> self.ToWriteableBitmapInternal(new Vector(self.Density.X, self.Density.Y));
4036

41-
private static unsafe WriteableBitmap ToWriteableBitmapInternal<TQuantumType>(this IMagickImage<TQuantumType> self, bool withDensity)
37+
private static unsafe WriteableBitmap ToWriteableBitmapInternal<TQuantumType>(this IMagickImage<TQuantumType> self, Vector density)
4238
where TQuantumType : struct, IConvertible
4339
{
4440
var size = new PixelSize((int)self.Width, (int)self.Height);
45-
var density = withDensity ? new Vector(self.Density.X, self.Density.Y) : new Vector(96, 96);
4641
var bitmap = new WriteableBitmap(size, density, PixelFormats.Rgba8888, AlphaFormat.Unpremul);
4742

4843
using var framebuffer = bitmap.Lock();

tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapMethod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void ShouldReturnWriteableBitmap()
3131
var distortion = output.Compare(input, ErrorMetric.RootMeanSquared);
3232

3333
Assert.Equal(0.0, distortion);
34+
Assert.Equal(new Vector(96, 96), bitmap.Dpi);
3435
}
3536
}
3637
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.IO;
5+
using Avalonia;
6+
using ImageMagick;
7+
using Xunit;
8+
9+
namespace Magick.NET.AvaloniaMediaImaging.Tests;
10+
11+
public partial class IMagickImageExtentionsTests
12+
{
13+
public class TheToWriteableBitmapWithDensityMethod
14+
{
15+
[Fact]
16+
public void ShouldReturnWriteableBitmap()
17+
{
18+
AppBuilder
19+
.Configure<Application>()
20+
.UsePlatformDetect()
21+
.SetupWithoutStarting();
22+
23+
using var input = new MagickImage(Files.MagickNETIconPNG);
24+
input.Density = new Density(150, 300);
25+
using var bitmap = input.ToWriteableBitmapWithDensity();
26+
27+
using var outputStream = new MemoryStream();
28+
bitmap.Save(outputStream);
29+
30+
outputStream.Position = 0;
31+
using var output = new MagickImage(outputStream);
32+
var distortion = output.Compare(input, ErrorMetric.RootMeanSquared);
33+
34+
Assert.Equal(0.0, distortion);
35+
Assert.Equal(new Vector(150, 300), bitmap.Dpi);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)