Skip to content

Commit

Permalink
Add jxl support using ImageMagick
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed Jul 6, 2023
1 parent 2df720f commit 58b2134
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 49 deletions.
5 changes: 1 addition & 4 deletions LRReader.Shared/LRReader.Shared.csproj
Expand Up @@ -9,12 +9,9 @@
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.1.3" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.2" Condition="'$(TargetFramework)' == 'uap10.0.17763'" />
<PackageReference Include="Caching.dll" Version="2.0.0.1" />
<PackageReference Include="KeyedSemaphores" Version="4.1.0" />
Expand Down
19 changes: 17 additions & 2 deletions LRReader.Shared/Services/ImageProcessing.cs
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.IO;
using System.Threading.Tasks;
using ImageMagick;
using SixLabors.ImageSharp;
using Size = System.Drawing.Size;

Expand All @@ -15,15 +17,28 @@ public virtual Task<Size> GetImageSize(byte[]? bytes)
{
if (bytes == null)
return Size.Empty;
using var ms = new MemoryStream(bytes);
try
{
var info = Image.Identify(bytes);
var info = Image.Identify(ms);
if (info != null)
return new Size(info.Width, info.Height);
}
catch
{
}
ms.Seek(0, SeekOrigin.Begin);
try
{
using (var magick = new MagickImage())
{
magick.Ping(ms);
return new Size(magick.Width, magick.Height);
}
}
catch
{
}
return Size.Empty;
});
}
Expand Down
3 changes: 3 additions & 0 deletions LRReader.UWP/LRReader.UWP.csproj
Expand Up @@ -415,6 +415,9 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>8.2.0</Version>
</PackageReference>
<PackageReference Include="Magick.NET-Q16-AnyCPU">
<Version>13.1.3</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Crashes">
<Version>5.0.2</Version>
</PackageReference>
Expand Down
97 changes: 54 additions & 43 deletions LRReader.UWP/Services/ImageProcessing.cs
Expand Up @@ -4,10 +4,12 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ImageMagick;
using LRReader.Shared.Services;
using SixLabors.ImageSharp;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Size = System.Drawing.Size;

namespace LRReader.UWP.Services
{
Expand All @@ -34,50 +36,56 @@ public UWPImageProcessingService()
image.DecodePixelWidth = decodeWidth;
if (decodeHeight > 0)
image.DecodePixelHeight = decodeHeight;
using (var ms = new MemoryStream(bytes))
using (var stream = ms.AsRandomAccessStream())
//stream.Seek(0);
if (transcode)

using var ms = new MemoryStream(bytes);

IImageInfo? info = null;
try
{
info = await Image.IdentifyAsync(ms);
}
catch
{
}
ms.Seek(0, SeekOrigin.Begin);
if (transcode || info == null)
{
await semaphore.WaitAsync();
try
{
await semaphore.WaitAsync();
try
using var converted = await Task.Run(() =>
{
var decoder = await BitmapDecoder.CreateAsync(stream);
using (var softwareBitmap = await decoder.GetSoftwareBitmapAsync())
{
SoftwareBitmap? newSource = null;
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
newSource = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
using (var converted = new InMemoryRandomAccessStream())
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, converted);
encoder.SetSoftwareBitmap(newSource ?? softwareBitmap);
await encoder.FlushAsync();
await image.SetSourceAsync(converted);
}
newSource?.Dispose();
}
}
catch (Exception)
{
return null;
}
finally
{
semaphore.Release();
}
using var magick = new MagickImage();
magick.Read(ms);
magick.Format = MagickFormat.Png00;
var converted = new MemoryStream();
magick.Write(converted);
converted.Seek(0, SeekOrigin.Begin);
return converted;
});
await image.SetSourceAsync(converted.AsRandomAccessStream());
}
else
catch (Exception)
{
try
{
await image.SetSourceAsync(stream);
}
catch (Exception)
{
return null;
}
return null;
}
finally
{
semaphore.Release();
}
}
else
{
try
{
await image.SetSourceAsync(ms.AsRandomAccessStream());
}
catch (Exception)
{
return null;
}
}
return image;
}

Expand All @@ -89,17 +97,20 @@ public override async Task<Size> GetImageSize(byte[]? bytes)
return Size.Empty;
var size = await base.GetImageSize(bytes);
if (size.IsEmpty)
{
using (var ms = new MemoryStream(bytes))
using (var stream = ms.AsRandomAccessStream())
{
try
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var decoder = await BitmapDecoder.CreateAsync(ms.AsRandomAccessStream());
return new Size((int)decoder.PixelWidth, (int)decoder.PixelHeight);
}
catch (Exception)
catch
{
return new Size(0, 0);
}
}
}
return size;
}

Expand Down

0 comments on commit 58b2134

Please sign in to comment.