From cdb1fff9e2625768c5ec98dc2b636ab55c19531e Mon Sep 17 00:00:00 2001 From: Erik White <26148654+Erik-White@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:15:28 +0200 Subject: [PATCH] Update to .NET8 (#50) * Update to .NET8 Signed-off-by: Erik White <26148654+Erik-White@users.noreply.github.com> * Version bump Signed-off-by: Erik White <26148654+Erik-White@users.noreply.github.com> --------- Signed-off-by: Erik White <26148654+Erik-White@users.noreply.github.com> --- .../Imaging/ImageFileHandlerTests.cs | 21 +++-- .../Mosey.Application.Tests.csproj | 22 +++--- Mosey.Application/Imaging/ImageFileHandler.cs | 5 +- Mosey.Application/Imaging/ImageHandler.cs | 79 +++++++++---------- Mosey.Application/Mosey.Application.csproj | 18 ++--- Mosey.Core/Imaging/IImageHandler.cs | 8 +- Mosey.Core/Mosey.Core.csproj | 10 +-- Mosey.Gui.Tests/Mosey.Gui.Tests.csproj | 22 +++--- Mosey.Gui/App.xaml.cs | 8 +- Mosey.Gui/Mosey.Gui.csproj | 29 +++---- .../Extensions/FileSystemExtensions.cs | 2 +- Mosey.Tests/Mosey.Tests.csproj | 22 +++--- 12 files changed, 120 insertions(+), 126 deletions(-) diff --git a/Mosey.Application.Tests/Imaging/ImageFileHandlerTests.cs b/Mosey.Application.Tests/Imaging/ImageFileHandlerTests.cs index 4f677fb..172746a 100644 --- a/Mosey.Application.Tests/Imaging/ImageFileHandlerTests.cs +++ b/Mosey.Application.Tests/Imaging/ImageFileHandlerTests.cs @@ -34,12 +34,13 @@ public void WriteExpectedData([Frozen] IFileSystem fileSystem, ImageHandler imag try { var pngImage = imageHandler.ConvertToFormat(image, IImagingDevice.ImageFormat.Png); + var mockFileSystem = fileSystem as MockFileSystem ?? throw new InvalidCastException(); var sut = new ImageFileHandler(imageHandler, fileSystem); sut.SaveImage(image, IImagingDevice.ImageFormat.Png, filePath.Path); using (new AssertionScope()) - using (var fileStream = fileSystem.File.OpenRead((fileSystem as MockFileSystem).AllFiles.First())) + using (var fileStream = fileSystem.File.OpenRead(mockFileSystem.AllFiles.First())) using (var savedImage = Image.Load(fileStream)) { savedImage.Should().NotBeSameAs(image); @@ -58,22 +59,24 @@ public void WriteExpectedData([Frozen] IFileSystem fileSystem, ImageHandler imag } [Theory, AutoNSubstituteData] - public void WriteExpectedFormat([Frozen] IFileSystem fileSystem, [Frozen] IImageHandler imageHandler, [Frozen] Image image, ImageFileHandler sut, MockFilePath filePath) + public void WriteExpectedFormat([Frozen] IFileSystem fileSystem, [Frozen] Image image, MockFilePath filePath) { try { - imageHandler.GetImageEncoder(default).ReturnsForAnyArgs(new PngEncoder()); + var mockFileSystem = fileSystem as MockFileSystem ?? throw new InvalidCastException(); + var sut = new ImageFileHandler(new ImageHandler(), fileSystem); - sut.SaveImage(Array.Empty(), default, filePath.Path); + sut.SaveImage(image, IImagingDevice.ImageFormat.Png, filePath.Path); using (new AssertionScope()) - using (var fileStream = fileSystem.File.OpenRead((fileSystem as MockFileSystem).AllFiles.First())) - using (var savedImage = Image.Load(fileStream, out var format)) + using (var fileStream = fileSystem.File.OpenRead(mockFileSystem.AllFiles.First())) + using (var savedImage = Image.Load(fileStream)) { savedImage.Should().NotBeSameAs(image); savedImage.Width.Should().Be(image.Width); savedImage.Height.Should().Be(image.Height); - format.Should().BeOfType(typeof(PngFormat)); + fileStream.Seek(0, System.IO.SeekOrigin.Begin); + Image.DetectFormat(fileStream).Should().BeOfType(typeof(PngFormat)); } } finally @@ -85,9 +88,11 @@ public void WriteExpectedFormat([Frozen] IFileSystem fileSystem, [Frozen] IImage [Theory, AutoNSubstituteData] public void WriteExpectedPath([Frozen] IFileSystem fileSystem, ImageFileHandler sut, MockFilePath filePath) { + var mockFileSystem = fileSystem as MockFileSystem ?? throw new InvalidCastException(); + sut.SaveImage(Array.Empty(), default, filePath.Path); - (fileSystem as MockFileSystem).AllFiles + mockFileSystem.AllFiles .Should().HaveCount(1) .And.ContainSingle(x => x == filePath.Path); } diff --git a/Mosey.Application.Tests/Mosey.Application.Tests.csproj b/Mosey.Application.Tests/Mosey.Application.Tests.csproj index 16543c8..a81424e 100644 --- a/Mosey.Application.Tests/Mosey.Application.Tests.csproj +++ b/Mosey.Application.Tests/Mosey.Application.Tests.csproj @@ -1,22 +1,22 @@ - + - net6.0 + net8.0 enable false - - - - - - - - - + + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Mosey.Application/Imaging/ImageFileHandler.cs b/Mosey.Application/Imaging/ImageFileHandler.cs index 5678b93..0836704 100644 --- a/Mosey.Application/Imaging/ImageFileHandler.cs +++ b/Mosey.Application/Imaging/ImageFileHandler.cs @@ -22,8 +22,7 @@ public void SaveImage(byte[] imageContent, IImagingDevice.ImageFormat imageForma /// public void SaveImage(Image image, IImagingDevice.ImageFormat imageFormat, string filePath) { - var encoder = _imageHandler.GetImageEncoder(imageFormat); - _imageHandler.ApplyEncoderDefaults(encoder); + var encoder = _imageHandler.GetImageEncoderWithDefaults(imageFormat); SaveImage(image, encoder, filePath); } @@ -39,7 +38,7 @@ public void SaveImage(Image image, SixLabors.ImageSharp.Formats.IImageEncoder en throw new ArgumentException("File path must not be empty"); } - using (var fileStream = _fileSystem.FileStream.Create(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Write)) + using (var fileStream = _fileSystem.FileStream.New(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { image.Save(fileStream, encoder); } diff --git a/Mosey.Application/Imaging/ImageHandler.cs b/Mosey.Application/Imaging/ImageHandler.cs index 832c06b..2727df1 100644 --- a/Mosey.Application/Imaging/ImageHandler.cs +++ b/Mosey.Application/Imaging/ImageHandler.cs @@ -1,6 +1,5 @@ using Mosey.Core.Imaging; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; @@ -13,41 +12,6 @@ namespace Mosey.Application.Imaging { public class ImageHandler : IImageHandler { - public void ApplyEncoderDefaults(IImageEncoder encoder) - { - switch (encoder) - { - case BmpEncoder bmpEncoder: - bmpEncoder.BitsPerPixel = BmpBitsPerPixel.Pixel32; - bmpEncoder.SupportTransparency = false; - break; - - case GifEncoder: - // Use the ImageSharp defaults - break; - - case JpegEncoder jpegEncoder: - jpegEncoder.ColorType = JpegColorType.Rgb; - jpegEncoder.Quality = 100; - break; - - case PngEncoder pngEncoder: - pngEncoder.BitDepth = PngBitDepth.Bit16; - pngEncoder.ColorType = PngColorType.Rgb; - pngEncoder.CompressionLevel = PngCompressionLevel.NoCompression; - pngEncoder.TransparentColorMode = PngTransparentColorMode.Clear; - break; - - case TiffEncoder tiffEncoder: - tiffEncoder.CompressionLevel = SixLabors.ImageSharp.Compression.Zlib.DeflateCompressionLevel.NoCompression; - tiffEncoder.BitsPerPixel = TiffBitsPerPixel.Bit24; - break; - - default: - throw new NotSupportedException("Unable to provide any default values for this encoder."); - } - } - public Image ConvertToFormat(byte[] imageContent, IImagingDevice.ImageFormat imageFormat) { using (var image = Image.Load(imageContent)) @@ -60,8 +24,7 @@ public Image ConvertToFormat(Image image, IImagingDevice.ImageFo { using (var stream = new MemoryStream()) { - var encoder = GetImageEncoder(imageFormat); - ApplyEncoderDefaults(encoder); + var encoder = GetImageEncoderWithDefaults(imageFormat); image.Save(stream, encoder); stream.Seek(0,SeekOrigin.Begin); @@ -75,11 +38,8 @@ public Image LoadImage(byte[] imageContent) public byte[] GetImageBytes(Image image) { - var format = image.GetConfiguration().ImageFormats.FirstOrDefault(); - if (format is null) - { - throw new InvalidOperationException("Unable to detect image format."); - } + var format = image.Configuration.ImageFormats.FirstOrDefault() + ?? throw new InvalidOperationException("Unable to detect image format."); using var stream = new MemoryStream(); image.Save(stream, format); @@ -97,5 +57,38 @@ public IImageEncoder GetImageEncoder(IImagingDevice.ImageFormat imageFormat) IImagingDevice.ImageFormat.Tiff => new TiffEncoder(), _ => throw new NotSupportedException() }; + + public IImageEncoder GetImageEncoderWithDefaults(IImagingDevice.ImageFormat imageFormat) + { + var encoder = GetImageEncoder(imageFormat); + + return encoder switch + { + BmpEncoder => new BmpEncoder + { + BitsPerPixel = BmpBitsPerPixel.Pixel32, + SupportTransparency = false + }, + GifEncoder => encoder, // Use the ImageSharp defaults + JpegEncoder => new JpegEncoder + { + ColorType = JpegEncodingColor.Rgb, + Quality = 100 + }, + PngEncoder => new PngEncoder + { + BitDepth = PngBitDepth.Bit16, + ColorType = PngColorType.Rgb, + CompressionLevel = PngCompressionLevel.NoCompression, + TransparentColorMode = PngTransparentColorMode.Clear + }, + TiffEncoder => new TiffEncoder + { + CompressionLevel = SixLabors.ImageSharp.Compression.Zlib.DeflateCompressionLevel.NoCompression, + BitsPerPixel = TiffBitsPerPixel.Bit24 + }, + _ => throw new NotSupportedException("Unable to provide any default values for this encoder.") + }; + } } } diff --git a/Mosey.Application/Mosey.Application.csproj b/Mosey.Application/Mosey.Application.csproj index ae43ab7..9eba621 100644 --- a/Mosey.Application/Mosey.Application.csproj +++ b/Mosey.Application/Mosey.Application.csproj @@ -1,7 +1,7 @@ - + - net6.0 + net8.0 enable enable @@ -16,13 +16,13 @@ - - - - - - - + + + + + + + diff --git a/Mosey.Core/Imaging/IImageHandler.cs b/Mosey.Core/Imaging/IImageHandler.cs index 4a7a944..47b1909 100644 --- a/Mosey.Core/Imaging/IImageHandler.cs +++ b/Mosey.Core/Imaging/IImageHandler.cs @@ -6,12 +6,6 @@ namespace Mosey.Core.Imaging { public interface IImageHandler where TPixel : unmanaged, IPixel { - /// - /// Apply default encoder settings - /// - /// - void ApplyEncoderDefaults(IImageEncoder encoder); - /// /// Convert an image to the specified encoding format, /// using the default encoder properties from . @@ -27,5 +21,7 @@ public interface IImageHandler where TPixel : unmanaged, IPixel byte[] GetImageBytes(Image image); IImageEncoder GetImageEncoder(IImagingDevice.ImageFormat imageFormat); + + IImageEncoder GetImageEncoderWithDefaults(IImagingDevice.ImageFormat imageFormat); } } \ No newline at end of file diff --git a/Mosey.Core/Mosey.Core.csproj b/Mosey.Core/Mosey.Core.csproj index a39948b..f2e8e02 100644 --- a/Mosey.Core/Mosey.Core.csproj +++ b/Mosey.Core/Mosey.Core.csproj @@ -1,14 +1,14 @@ - + - net6.0 + net8.0 true - - - + + + diff --git a/Mosey.Gui.Tests/Mosey.Gui.Tests.csproj b/Mosey.Gui.Tests/Mosey.Gui.Tests.csproj index c790690..b591149 100644 --- a/Mosey.Gui.Tests/Mosey.Gui.Tests.csproj +++ b/Mosey.Gui.Tests/Mosey.Gui.Tests.csproj @@ -1,21 +1,21 @@ - + - net6.0-windows + net8.0-windows false - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + diff --git a/Mosey.Gui/App.xaml.cs b/Mosey.Gui/App.xaml.cs index dc154ea..cee27eb 100644 --- a/Mosey.Gui/App.xaml.cs +++ b/Mosey.Gui/App.xaml.cs @@ -12,9 +12,9 @@ using Mosey.Application; using Mosey.Application.Configuration; using Mosey.Application.Imaging; -using System.IO; using Mosey.Core; using Mosey.Core.Imaging; +using NReco.Logging.File; namespace Mosey.Gui { @@ -90,9 +90,9 @@ private static IServiceCollection SetDefaultImagePath(IServiceCollection service { config.ImageFile = config.ImageFile with { - Directory = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.MyPictures).ToString(), - "Mosey") + Directory = System.IO.Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.MyPictures).ToString(), + "Mosey") }; } }); diff --git a/Mosey.Gui/Mosey.Gui.csproj b/Mosey.Gui/Mosey.Gui.csproj index 3dea54d..b2baef0 100644 --- a/Mosey.Gui/Mosey.Gui.csproj +++ b/Mosey.Gui/Mosey.Gui.csproj @@ -1,20 +1,21 @@ - + WinExe - net6.0-windows + net8.0-windows true + true Mosey.ico Erik White AnyCPU https://github.com/Erik-White/Mosey https://github.com/Erik-White/Mosey.git - 2.0.3 + 2.1.0 GPL-3.0-or-later imaging, scan, scanning - 2.0.3.0 - 2.0.3.0 + 2.1.0.0 + 2.1.0.0 Multiple scanner interval imaging tool git @@ -29,15 +30,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/Mosey.Gui/ViewModels/Extensions/FileSystemExtensions.cs b/Mosey.Gui/ViewModels/Extensions/FileSystemExtensions.cs index d8699ba..e898303 100644 --- a/Mosey.Gui/ViewModels/Extensions/FileSystemExtensions.cs +++ b/Mosey.Gui/ViewModels/Extensions/FileSystemExtensions.cs @@ -37,7 +37,7 @@ public static bool IsNetworkPath(string path, IFileSystem fileSystem) { // Path may not start with a slash, but could be a network drive var rootPath = fileSystem.Path.GetPathRoot(path); - var driveInfo = fileSystem.DriveInfo.FromDriveName(rootPath); + var driveInfo = fileSystem.DriveInfo.New(rootPath); return driveInfo.DriveType == System.IO.DriveType.Network; } diff --git a/Mosey.Tests/Mosey.Tests.csproj b/Mosey.Tests/Mosey.Tests.csproj index 01f1e30..7a0090d 100644 --- a/Mosey.Tests/Mosey.Tests.csproj +++ b/Mosey.Tests/Mosey.Tests.csproj @@ -1,26 +1,26 @@ - net6.0 + net8.0 enable false - - - - - - - - + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + +