Skip to content

Commit

Permalink
Update to .NET8 (#50)
Browse files Browse the repository at this point in the history
* Update to .NET8

Signed-off-by: Erik White <[email protected]>

* Version bump

Signed-off-by: Erik White <[email protected]>

---------

Signed-off-by: Erik White <[email protected]>
  • Loading branch information
Erik-White authored Aug 11, 2024
1 parent 489516d commit cdb1fff
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 126 deletions.
21 changes: 13 additions & 8 deletions Mosey.Application.Tests/Imaging/ImageFileHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rgba32>(fileStream))
{
savedImage.Should().NotBeSameAs(image);
Expand All @@ -58,22 +59,24 @@ public void WriteExpectedData([Frozen] IFileSystem fileSystem, ImageHandler imag
}

[Theory, AutoNSubstituteData]
public void WriteExpectedFormat([Frozen] IFileSystem fileSystem, [Frozen] IImageHandler<Rgba32> imageHandler, [Frozen] Image<Rgba32> image, ImageFileHandler sut, MockFilePath filePath)
public void WriteExpectedFormat([Frozen] IFileSystem fileSystem, [Frozen] Image<Rgba32> 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<byte>(), 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
Expand All @@ -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<byte>(), default, filePath.Path);

(fileSystem as MockFileSystem).AllFiles
mockFileSystem.AllFiles
.Should().HaveCount(1)
.And.ContainSingle(x => x == filePath.Path);
}
Expand Down
22 changes: 11 additions & 11 deletions Mosey.Application.Tests/Mosey.Application.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.17.0" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.17.0" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 2 additions & 3 deletions Mosey.Application/Imaging/ImageFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public void SaveImage(byte[] imageContent, IImagingDevice.ImageFormat imageForma
/// <inheritdoc cref="SaveImage(byte[], IImagingDevice.ImageFormat, string)"/>
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);
}
Expand All @@ -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);
}
Expand Down
79 changes: 36 additions & 43 deletions Mosey.Application/Imaging/ImageHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,41 +12,6 @@ namespace Mosey.Application.Imaging
{
public class ImageHandler : IImageHandler<Rgba32>
{
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<Rgba32> ConvertToFormat(byte[] imageContent, IImagingDevice.ImageFormat imageFormat)
{
using (var image = Image.Load<Rgba32>(imageContent))
Expand All @@ -60,8 +24,7 @@ public Image<Rgba32> ConvertToFormat(Image<Rgba32> 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);
Expand All @@ -75,11 +38,8 @@ public Image<Rgba32> LoadImage(byte[] imageContent)

public byte[] GetImageBytes(Image<Rgba32> 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);
Expand All @@ -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.")
};
}
}
}
18 changes: 9 additions & 9 deletions Mosey.Application/Mosey.Application.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -16,13 +16,13 @@

<ItemGroup>
<PackageReference Include="DNTScanner.Core" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 2 additions & 6 deletions Mosey.Core/Imaging/IImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ namespace Mosey.Core.Imaging
{
public interface IImageHandler<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{
/// <summary>
/// Apply default encoder settings
/// </summary>
/// <exception cref="NotSupportedException"></exception>
void ApplyEncoderDefaults(IImageEncoder encoder);

/// <summary>
/// Convert an image to the specified encoding format,
/// using the default encoder properties from <see cref="ApplyEncoderDefaults(IImageEncoder)"/>.
Expand All @@ -27,5 +21,7 @@ public interface IImageHandler<TPixel> where TPixel : unmanaged, IPixel<TPixel>
byte[] GetImageBytes(Image<TPixel> image);

IImageEncoder GetImageEncoder(IImagingDevice.ImageFormat imageFormat);

IImageEncoder GetImageEncoderWithDefaults(IImagingDevice.ImageFormat imageFormat);
}
}
10 changes: 5 additions & 5 deletions Mosey.Core/Mosey.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="2.0.0" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.21" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions Mosey.Gui.Tests/Mosey.Gui.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.17.0" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.17.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.18.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Mosey.Tests\Mosey.Tests.csproj" />
Expand Down
8 changes: 4 additions & 4 deletions Mosey.Gui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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")
};
}
});
Expand Down
Loading

0 comments on commit cdb1fff

Please sign in to comment.