Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable compatibility with old and new image sharp versions. #427

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions PdfSharpCore.Test/CreateSimplePDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ public void CreateTestPdfWithImageViaImageSharp()
var renderer = XGraphics.FromPdfPage(pageNewRenderer);

// Load image for ImageSharp and apply a simple mutation:
#if NET6_0_OR_GREATER
var image = Image.Load<Rgb24>(PathHelper.GetInstance().GetAssetPath("lenna.png"));
var format = image.Metadata.DecodedImageFormat;
#else
var image = Image.Load<Rgb24>(PathHelper.GetInstance().GetAssetPath("lenna.png"), out var format);
#endif
image.Mutate(ctx => ctx.Grayscale());

// create XImage from that same ImageSharp image:
Expand Down
19 changes: 10 additions & 9 deletions PdfSharpCore.Test/PdfSharpCore.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<langversion>latest</langversion>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
28 changes: 22 additions & 6 deletions PdfSharpCore/PdfSharpCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Core Additionally Mi
<summary>PdfSharp for .NET Core</summary>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

<AssemblyVersion>1.6.0</AssemblyVersion>
<PackageVersion>1.6.0</PackageVersion>
<FileVersion>1.6.0</FileVersion>
<ProductVersion>1.6.0</ProductVersion>

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down Expand Up @@ -46,12 +52,22 @@ PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Core Additionally Mi
<ItemGroup>
<Folder Include="Resources\images\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta17" />
</ItemGroup>

<Choose>
<When Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net8.0'">
<ItemGroup >
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="SixLabors.Fonts" Version="2.0.4" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="SixLabors.Fonts" Version="1.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<None Include="..\LICENSE.md" Pack="true" PackagePath="" />
<None Include="..\README.md" Pack="true" PackagePath="" />
Expand Down
37 changes: 3 additions & 34 deletions PdfSharpCore/Utils/ImageSharpImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,14 @@
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Advanced;

namespace PdfSharpCore.Utils
{
public class ImageSharpImageSource<TPixel> : ImageSource where TPixel : unmanaged, IPixel<TPixel>
public partial class ImageSharpImageSource<TPixel> : ImageSource where TPixel : unmanaged, IPixel<TPixel>
{

public static IImageSource FromImageSharpImage(Image<TPixel> image, IImageFormat imgFormat, int? quality = 75)
{
var _path = "*" + Guid.NewGuid().ToString("B");
return new ImageSharpImageSourceImpl<TPixel>(_path, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromBinaryImpl(string name, Func<byte[]> imageSource, int? quality = 75)
{
var image = Image.Load<TPixel>(imageSource.Invoke(), out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromFileImpl(string path, int? quality = 75)
{
var image = Image.Load<TPixel>(path, out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(path, image, (int) quality, imgFormat is PngFormat);
}

protected override IImageSource FromStreamImpl(string name, Func<Stream> imageStream, int? quality = 75)
{
using (var stream = imageStream.Invoke())
{
var image = Image.Load<TPixel>(stream, out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}
}

private class ImageSharpImageSourceImpl<TPixel2> : IImageSource where TPixel2 : unmanaged, IPixel<TPixel2>
private partial class ImageSharpImageSourceImpl<TPixel2> : IImageSource where TPixel2 : unmanaged, IPixel<TPixel2>
{
private Image<TPixel2> Image { get; }
private readonly int _quality;
Expand All @@ -58,11 +32,6 @@ public ImageSharpImageSourceImpl(string name, Image<TPixel2> image, int quality,
Transparent = isTransparent;
}

public void SaveAsJpeg(MemoryStream ms)
{
Image.SaveAsJpeg(ms, new JpegEncoder() { Quality = this._quality });
}

public void Dispose()
{
Image.Dispose();
Expand Down
68 changes: 68 additions & 0 deletions PdfSharpCore/Utils/ImageSharpImageSource_NET6.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if NET6_0_OR_GREATER

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes;
using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Advanced;

namespace PdfSharpCore.Utils
{
public partial class ImageSharpImageSource<TPixel> : ImageSource where TPixel : unmanaged, IPixel<TPixel>
{

public static IImageSource FromImageSharpImage(Image<TPixel> image, IImageFormat imgFormat, int? quality = 75)
{
var _path = "*" + Guid.NewGuid().ToString("B");
return new ImageSharpImageSourceImpl<TPixel>(_path, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromBinaryImpl(string name, Func<byte[]> imageSource, int? quality = 75)
{
var image = Image.Load<TPixel>(imageSource.Invoke());
var imgFormat = image.Metadata.DecodedImageFormat;

return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromFileImpl(string path, int? quality = 75)
{
var image = Image.Load<TPixel>(path);
var imgFormat = image.Metadata.DecodedImageFormat;

return new ImageSharpImageSourceImpl<TPixel>(path, image, (int) quality, imgFormat is PngFormat);
}

protected override IImageSource FromStreamImpl(string name, Func<Stream> imageStream, int? quality = 75)
{
using (var stream = imageStream.Invoke()) {
var image = Image.Load<TPixel>(stream);
var imgFormat = image.Metadata.DecodedImageFormat;

return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}
}

private partial class ImageSharpImageSourceImpl<TPixel2>
{

public void SaveAsJpeg(MemoryStream ms)
{
Image.SaveAsJpeg(ms, new JpegEncoder() {
Quality = this._quality,
ColorType = JpegEncodingColor.YCbCrRatio420,
Interleaved = true,
});
}

}

}
}

#endif
61 changes: 61 additions & 0 deletions PdfSharpCore/Utils/ImageSharpImageSource_NETSTANDARD.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#if !NET6_0_OR_GREATER

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes;
using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Advanced;

namespace PdfSharpCore.Utils
{
public partial class ImageSharpImageSource<TPixel> : ImageSource where TPixel : unmanaged, IPixel<TPixel>
{

public static IImageSource FromImageSharpImage(Image<TPixel> image, IImageFormat imgFormat, int? quality = 75)
{
var _path = "*" + Guid.NewGuid().ToString("B");
return new ImageSharpImageSourceImpl<TPixel>(_path, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromBinaryImpl(string name, Func<byte[]> imageSource, int? quality = 75)
{
var image = Image.Load<TPixel>(imageSource.Invoke(), out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromFileImpl(string path, int? quality = 75)
{
var image = Image.Load<TPixel>(path, out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(path, image, (int) quality, imgFormat is PngFormat);
}

protected override IImageSource FromStreamImpl(string name, Func<Stream> imageStream, int? quality = 75)
{
using (var stream = imageStream.Invoke())
{
var image = Image.Load<TPixel>(stream, out IImageFormat imgFormat);
return new ImageSharpImageSourceImpl<TPixel>(name, image, (int)quality, imgFormat is PngFormat);
}
}

private partial class ImageSharpImageSourceImpl<TPixel2>
{

public void SaveAsJpeg(MemoryStream ms)
{
Image.SaveAsJpeg(ms, new JpegEncoder() {
Quality = this._quality,
});
}

}

}
}

#endif
3 changes: 2 additions & 1 deletion SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<langversion>latest</langversion>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down