Skip to content

Commit

Permalink
Add ResourceTiffTests with Cube.tiff with multiple dpi and scales
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Nov 19, 2024
1 parent 585d09b commit 71ceab2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `GetSystemDpi` to get the system `dpi` on the fly.
- Add `SystemDpi` to store the system `dpi` value.
- Update `GetBitmapFrameByWidthAndDpi` to round `dpi` frame value.
- Update `Width` to `Math.Round` to improve order by `Width`.
### Example
- Add `Cube-Grey-Light.tiff` and `Cube-Grey-Dark.tiff` in `AppTheme`.
### Tests
- Add `ResourceTiffTests` with `Cube.tiff` with multiple dpi and scales.

## [0.7.0] / 2024-07-06 - 2024-07-25
### Features
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.7.1-alpha.2</Version>
<Version>0.7.1-alpha.3</Version>
</PropertyGroup>
</Project>
Binary file added ricaun.Revit.UI.Tests/Resources/Images/Cube.tiff
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ricaun.Revit.UI.Tests.Resources
{
public class ResourcesFramesTests
public class ResourceIcoFramesTests
{
const string ResourceNameIcoFrames = "Resources/Images/Revit21Frames.ico";

Expand Down
85 changes: 85 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using NUnit.Framework;
using System;

namespace ricaun.Revit.UI.Tests.Resources
{
public class ResourceTiffTests
{
/// <summary>
/// Tiff file with multiple scales, 1.0x, 1.5x, 2.0x, 3.0x, 4.0x for 16x16 and 32x32, 10 frames.
/// </summary>
/// <remarks>
/// Tiff file based in the https://github.com/ricaun-io/Autodesk.Icon.Example?tab=readme-ov-file#autodesk-high-resolution-icons
/// </remarks>
const string ResourceNameTiff = "Resources/Images/Cube.tiff";

System.Windows.Media.Imaging.BitmapFrame BitmapFrame;
[OneTimeSetUp]
public void Setup()
{
BitmapFrame = ResourceNameTiff.GetBitmapSource() as System.Windows.Media.Imaging.BitmapFrame;
}

[Test]
public void GetBitmapSource_NotNull()
{
Console.WriteLine(ResourceNameTiff.GetBitmapSource());
Assert.IsNotNull(ResourceNameTiff.GetBitmapSource());
Assert.IsNotNull(("/" + ResourceNameTiff).GetBitmapSource());
}

[TestCase(32, 384)]
public void GetBitmapSource_Default_WidthAndDpi(int width, int dpi)
{
var source = ResourceNameTiff.GetBitmapSource();
Assert.AreEqual(width, Math.Round(source.Width));
Assert.AreEqual(dpi, Math.Round(source.DpiX));
}

[TestCase(10)]
public void BitmapFrame_CountFrames(int count)
{
Assert.IsNotNull(BitmapFrame);
var decoder = BitmapFrame.Decoder;
Assert.AreEqual(count, decoder.Frames.Count);
}

[TestCase(16, 96)] // 1.0
[TestCase(16, 144)] // 1.5
[TestCase(16, 192)] // 2.0
[TestCase(16, 288)] // 3.0
[TestCase(16, 384)] // 4.0
[TestCase(32, 96)] // 1.0
[TestCase(32, 144)] // 1.5
[TestCase(32, 192)] // 2.0
[TestCase(32, 288)] // 3.0
[TestCase(32, 384)] // 4.0
public void BitmapFrame_ByWidthAndDpi(int width, int dpi)
{
Assert.IsNotNull(BitmapFrame);
var decoder = BitmapFrame.Decoder;
var frame = decoder.GetBitmapFrameByWidthAndDpi(width, dpi) as System.Windows.Media.Imaging.BitmapFrame;
Assert.IsNotNull(frame);
Assert.AreEqual(width, Math.Round(frame.Width));
Assert.AreEqual(dpi, Math.Round(frame.DpiX));
}

[TestCase(16, 168, 192)] // 1.75
[TestCase(32, 168, 192)] // 1.75
[TestCase(16, 240, 288)] // 2.5
[TestCase(32, 240, 288)] // 2.5
[TestCase(16, 336, 384)] // 3.5
[TestCase(32, 336, 384)] // 3.5
[TestCase(16, 480, 384)] // 5.0
[TestCase(32, 480, 384)] // 5.0
public void BitmapFrame_ByWidthAndDpi_Expected(int width, int dpi, int dpiExpected)
{
Assert.IsNotNull(BitmapFrame);
var decoder = BitmapFrame.Decoder;
var frame = decoder.GetBitmapFrameByWidthAndDpi(width, dpi) as System.Windows.Media.Imaging.BitmapFrame;
Assert.IsNotNull(frame);
Assert.AreEqual(width, Math.Round(frame.Width));
Assert.AreEqual(dpiExpected, Math.Round(frame.DpiX));
}
}
}
7 changes: 2 additions & 5 deletions ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
</PropertyGroup>
</Otherwise>
</Choose>
<ItemGroup>
<None Remove="Resources\Images\Revit21Frames.ico" />
<None Remove="Resources\Images\Revit32.png" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="ricaun.RevitTest.TestAdapter" Version="*-*" />
Expand Down Expand Up @@ -69,6 +65,7 @@
</ItemGroup>

<ItemGroup>
<Resource Include="Resources\Images\Cube.tiff" />
<Resource Include="Resources\Images\Revit.ico" />
<Resource Include="Resources\Images\Revit21Frames.ico" />
<Resource Include="Resources\Images\Revit32.png" />
Expand Down
10 changes: 8 additions & 2 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static BitmapFrame UriToBitmapFrame(string uriString)
{
var uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames.OrderBy(e => e.Width).LastOrDefault();
return decoder.Frames.OrderBy(e => Math.Round(e.Width)).LastOrDefault();
}

/// <summary>
Expand Down Expand Up @@ -82,9 +82,15 @@ public static ImageSource Scale(this ImageSource imageSource, double scale)
return imageSource;
}

#if NET47_OR_GREATER || NET
/// <summary>
/// Get the system DPI based on the <see cref="System.Windows.Media.VisualTreeHelper.GetDpi"/> using a new <see cref="System.Windows.Controls.Image"/>.
/// </summary>
#else
/// <summary>
/// Get the system DPI based on the <see cref="System.Drawing.Graphics.DpiX"/> using a new <see cref="System.Drawing.Graphics"/> from <see cref="IntPtr.Zero"/>.
/// </summary>
#endif
internal static double GetSystemDpi()
{
double systemDpi = 96;
Expand Down Expand Up @@ -125,7 +131,7 @@ double OrderDpiX(BitmapFrame frame)
var frames = bitmapDecoder.Frames;
var frame = frames
.OrderBy(OrderDpiX)
.ThenBy(e => e.Width)
.ThenBy(e => Math.Round(e.Width))
.FirstOrDefault(e => Math.Round(e.Width) >= width);

return frame;
Expand Down

0 comments on commit 71ceab2

Please sign in to comment.