diff --git a/CHANGELOG.md b/CHANGELOG.md index b31d3fa..961b5c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Directory.Build.props b/Directory.Build.props index 55a80d0..37ccfc5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 0.7.1-alpha.2 + 0.7.1-alpha.3 \ No newline at end of file diff --git a/ricaun.Revit.UI.Tests/Resources/Images/Cube.tiff b/ricaun.Revit.UI.Tests/Resources/Images/Cube.tiff new file mode 100644 index 0000000..c99ad07 Binary files /dev/null and b/ricaun.Revit.UI.Tests/Resources/Images/Cube.tiff differ diff --git a/ricaun.Revit.UI.Tests/Resources/ResourcesFramesTests.cs b/ricaun.Revit.UI.Tests/Resources/ResourceIcoFramesTests.cs similarity index 96% rename from ricaun.Revit.UI.Tests/Resources/ResourcesFramesTests.cs rename to ricaun.Revit.UI.Tests/Resources/ResourceIcoFramesTests.cs index a6917b5..f933c14 100644 --- a/ricaun.Revit.UI.Tests/Resources/ResourcesFramesTests.cs +++ b/ricaun.Revit.UI.Tests/Resources/ResourceIcoFramesTests.cs @@ -3,7 +3,7 @@ namespace ricaun.Revit.UI.Tests.Resources { - public class ResourcesFramesTests + public class ResourceIcoFramesTests { const string ResourceNameIcoFrames = "Resources/Images/Revit21Frames.ico"; diff --git a/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs b/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs new file mode 100644 index 0000000..b5dcc29 --- /dev/null +++ b/ricaun.Revit.UI.Tests/Resources/ResourceTiffTests.cs @@ -0,0 +1,85 @@ +using NUnit.Framework; +using System; + +namespace ricaun.Revit.UI.Tests.Resources +{ + public class ResourceTiffTests + { + /// + /// Tiff file with multiple scales, 1.0x, 1.5x, 2.0x, 3.0x, 4.0x for 16x16 and 32x32, 10 frames. + /// + /// + /// Tiff file based in the https://github.com/ricaun-io/Autodesk.Icon.Example?tab=readme-ov-file#autodesk-high-resolution-icons + /// + 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)); + } + } +} \ No newline at end of file diff --git a/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj b/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj index d309f72..57ee646 100644 --- a/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj +++ b/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj @@ -34,11 +34,7 @@ - - - - - + @@ -69,6 +65,7 @@ + diff --git a/ricaun.Revit.UI/BitmapExtension.cs b/ricaun.Revit.UI/BitmapExtension.cs index 0cf2976..d5d88cb 100644 --- a/ricaun.Revit.UI/BitmapExtension.cs +++ b/ricaun.Revit.UI/BitmapExtension.cs @@ -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(); } /// @@ -82,9 +82,15 @@ public static ImageSource Scale(this ImageSource imageSource, double scale) return imageSource; } +#if NET47_OR_GREATER || NET /// /// Get the system DPI based on the using a new . /// +#else + /// + /// Get the system DPI based on the using a new from . + /// +#endif internal static double GetSystemDpi() { double systemDpi = 96; @@ -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;