Skip to content

Commit

Permalink
Corrected checks for invalid density and added unit tests to make sur…
Browse files Browse the repository at this point in the history
…e this stays fixed (#1419).
  • Loading branch information
dlemstra committed Jul 28, 2023
1 parent e282be6 commit 3687d7d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Magick.NET.SystemDrawing/IMagickImageExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static void SetBitmapDensity<TQuantumType>(IMagickImage<TQuantumType> im

private static Density GetDefaultDensity(IMagickImage image)
{
if (image.Density.Units == DensityUnit.Undefined && image.Density.X == 0 && image.Density.Y == 0)
if (image.Density.X <= 0 || image.Density.Y <= 0)
return new Density(96);

return image.Density.ChangeUnits(DensityUnit.PixelsPerInch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,39 @@ public void ShouldSetTheDensityOfTheBitmapWhenFormatIsUsed()
Assert.Equal(200, (int)bitmap.VerticalResolution);
}

[Fact]
public void ShouldUseTheDensityWhenUnitsAreUndefined()
{
using var image = new MagickImage(MagickColors.Red, 1, 1);
image.Density = new Density(1, 1, DensityUnit.Undefined);

using var bitmap = image.ToBitmapWithDensity(ImageFormat.Jpeg);
Assert.Equal(1, bitmap.HorizontalResolution);
Assert.Equal(1, bitmap.VerticalResolution);
}

[Fact]
public void ShouldUseTheDefaultDensityWhenXIsZero()
{
using var image = new MagickImage(MagickColors.Red, 1, 1);
image.Density = new Density(0, 1, DensityUnit.PixelsPerCentimeter);

using var bitmap = image.ToBitmapWithDensity(ImageFormat.Jpeg);
Assert.Equal(96, bitmap.HorizontalResolution);
Assert.Equal(96, bitmap.VerticalResolution);
}

[Fact]
public void ShouldUseTheDefaultDensityWhenYIsZero()
{
using var image = new MagickImage(MagickColors.Red, 1, 1);
image.Density = new Density(1, 0, DensityUnit.PixelsPerCentimeter);

using var bitmap = image.ToBitmapWithDensity(ImageFormat.Jpeg);
Assert.Equal(96, bitmap.HorizontalResolution);
Assert.Equal(96, bitmap.VerticalResolution);
}

private static void AssertUnsupportedImageFormat(ImageFormat imageFormat)
{
using var image = new MagickImage(MagickColors.Red, 10, 10);
Expand Down

0 comments on commit 3687d7d

Please sign in to comment.