From c869a858b03d6dee0e7fb436bd1b3a97ee963937 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 9 Nov 2026 16:38:45 +0800 Subject: [PATCH] Apply EXIF orientation in SkiaSharp resizer and compressor --- .../Volo/Abp/Imaging/SkiaSharpBitmapHelper.cs | 56 +++++++++++++++ .../SkiaSharpImageCompressorContributor.cs | 7 +- .../SkiaSharpImageResizerContributor.cs | 7 +- .../Imaging/SkiaSharpImageCompressorTests.cs | 15 ++++ .../Abp/Imaging/SkiaSharpImageResizerTests.cs | 35 ++++++++++ .../Abp/Imaging/SkiaSharpTestImageHelper.cs | 69 +++++++++++++++++++ 6 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpBitmapHelper.cs create mode 100644 framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpTestImageHelper.cs diff --git a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpBitmapHelper.cs b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpBitmapHelper.cs new file mode 100644 index 0000000000..545d312a5b --- /dev/null +++ b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpBitmapHelper.cs @@ -0,0 +1,56 @@ +using SkiaSharp; + +namespace Volo.Abp.Imaging; + +public static class SkiaSharpBitmapHelper +{ + // The encoders write no EXIF, so the orientation has to be applied to the pixels. + public static SKBitmap? DecodeUpright(SKCodec codec) + { + var bitmap = SKBitmap.Decode(codec); + if (bitmap == null || codec.EncodedOrigin == SKEncodedOrigin.TopLeft) + { + return bitmap; + } + + using (bitmap) + { + return ApplyOrigin(bitmap, codec.EncodedOrigin); + } + } + + private static SKBitmap ApplyOrigin(SKBitmap source, SKEncodedOrigin origin) + { + var matrix = origin switch + { + SKEncodedOrigin.TopRight => new SKMatrix(-1, 0, source.Width, 0, 1, 0, 0, 0, 1), + SKEncodedOrigin.BottomRight => new SKMatrix(-1, 0, source.Width, 0, -1, source.Height, 0, 0, 1), + SKEncodedOrigin.BottomLeft => new SKMatrix(1, 0, 0, 0, -1, source.Height, 0, 0, 1), + SKEncodedOrigin.LeftTop => new SKMatrix(0, 1, 0, 1, 0, 0, 0, 0, 1), + SKEncodedOrigin.RightTop => new SKMatrix(0, -1, source.Height, 1, 0, 0, 0, 0, 1), + SKEncodedOrigin.RightBottom => new SKMatrix(0, -1, source.Height, -1, 0, source.Width, 0, 0, 1), + SKEncodedOrigin.LeftBottom => new SKMatrix(0, 1, 0, -1, 0, source.Width, 0, 0, 1), + _ => SKMatrix.Identity + }; + + var swapSides = origin is SKEncodedOrigin.LeftTop or SKEncodedOrigin.RightTop or SKEncodedOrigin.RightBottom or SKEncodedOrigin.LeftBottom; + var info = swapSides ? source.Info.WithSize(source.Height, source.Width) : source.Info; + + // DrawBitmap copies the pixels of a mutable bitmap, which doubles the memory for a large photo. + source.SetImmutable(); + + var result = new SKBitmap(info); + try + { + using var canvas = new SKCanvas(result); + canvas.SetMatrix(matrix); + canvas.DrawBitmap(source, 0, 0); + return result; + } + catch + { + result.Dispose(); + throw; + } + } +} diff --git a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageCompressorContributor.cs b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageCompressorContributor.cs index c3117d0461..6a24af928c 100644 --- a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageCompressorContributor.cs +++ b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageCompressorContributor.cs @@ -38,7 +38,7 @@ public class SkiaSharpImageCompressorContributor : IImageCompressorContributor, return new ImageCompressResult(stream, ImageProcessState.Unsupported); } - using var bitmap = SKBitmap.Decode(memoryBitmapStream); + using var bitmap = DecodeBitmap(codec); if (bitmap == null) { return new ImageCompressResult(stream, ImageProcessState.Unsupported); @@ -97,6 +97,11 @@ public class SkiaSharpImageCompressorContributor : IImageCompressorContributor, return new ImageCompressResult(newBytes, result.State); } + protected virtual SKBitmap? DecodeBitmap(SKCodec codec) + { + return SkiaSharpBitmapHelper.DecodeUpright(codec); + } + protected virtual bool CanCompress(string? mimeType) { return mimeType switch { diff --git a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs index dd7da3251a..b3d5c751ec 100644 --- a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs @@ -57,7 +57,7 @@ public class SkiaSharpImageResizerContributor : IImageResizerContributor, ITrans return new ImageResizeResult(stream, ImageProcessState.Unsupported); } - using var original = SKBitmap.Decode(memoryBitmapStream); + using var original = DecodeBitmap(codec); if (original == null) { return new ImageResizeResult(stream, ImageProcessState.Unsupported); @@ -119,6 +119,11 @@ public class SkiaSharpImageResizerContributor : IImageResizerContributor, ITrans } } + protected virtual SKBitmap? DecodeBitmap(SKCodec codec) + { + return SkiaSharpBitmapHelper.DecodeUpright(codec); + } + protected virtual bool CanResize(string? mimeType) { return mimeType switch { diff --git a/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageCompressorTests.cs b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageCompressorTests.cs index 4c575d9f04..577eec040a 100644 --- a/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageCompressorTests.cs +++ b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageCompressorTests.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Shouldly; +using SkiaSharp; using Xunit; namespace Volo.Abp.Imaging; @@ -176,4 +177,18 @@ public class SkiaSharpImageCompressorTests : AbpImagingSkiaSharpTestBase base.Dispose(disposing); } } + + [Fact] + public async Task Should_Apply_Exif_Orientation() + { + await using var jpegImage = SkiaSharpTestImageHelper.CreateQuadrantJpegWithExifOrientation(6); + var compressedImage = await ImageCompressor.CompressAsync(jpegImage); + + compressedImage.State.ShouldBe(ImageProcessState.Done); + using var decoded = SKBitmap.Decode(compressedImage.Result); + decoded.Width.ShouldBe(100); + decoded.Height.ShouldBe(200); + SkiaSharpTestImageHelper.GetQuadrantColors(decoded).ShouldBe("BRYG"); + compressedImage.Result.Dispose(); + } } diff --git a/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageResizerTests.cs b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageResizerTests.cs index 593beb506d..001e38c5f4 100644 --- a/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageResizerTests.cs +++ b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpImageResizerTests.cs @@ -189,4 +189,39 @@ public class SkiaSharpImageResizerTests : AbpImagingSkiaSharpTestBase (decoded.Width == 120 || decoded.Height == 80).ShouldBeTrue(); resizedImage.Result.Dispose(); } + + [Theory] + [InlineData(1, 100, 50, "RGBY")] + [InlineData(2, 100, 50, "GRYB")] + [InlineData(3, 100, 50, "YBGR")] + [InlineData(4, 100, 50, "BYRG")] + [InlineData(5, 50, 100, "RBGY")] + [InlineData(6, 50, 100, "BRYG")] + [InlineData(7, 50, 100, "YGBR")] + [InlineData(8, 50, 100, "GYRB")] + public async Task Should_Apply_Exif_Orientation(ushort orientation, int expectedWidth, int expectedHeight, string expectedQuadrants) + { + await using var jpegImage = SkiaSharpTestImageHelper.CreateQuadrantJpegWithExifOrientation(orientation); + var resizedImage = await ImageResizer.ResizeAsync(jpegImage, new ImageResizeArgs(100, 100, ImageResizeMode.Max)); + + resizedImage.State.ShouldBe(ImageProcessState.Done); + using var decoded = SKBitmap.Decode(resizedImage.Result); + decoded.Width.ShouldBe(expectedWidth); + decoded.Height.ShouldBe(expectedHeight); + SkiaSharpTestImageHelper.GetQuadrantColors(decoded).ShouldBe(expectedQuadrants); + resizedImage.Result.Dispose(); + } + + [Fact] + public async Task Should_Apply_Resize_Box_To_Upright_Image() + { + await using var jpegImage = SkiaSharpTestImageHelper.CreateQuadrantJpegWithExifOrientation(6); + var resizedImage = await ImageResizer.ResizeAsync(jpegImage, new ImageResizeArgs(100, 50, ImageResizeMode.Max)); + + resizedImage.State.ShouldBe(ImageProcessState.Done); + using var decoded = SKBitmap.Decode(resizedImage.Result); + decoded.Width.ShouldBe(25); + decoded.Height.ShouldBe(50); + resizedImage.Result.Dispose(); + } } diff --git a/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpTestImageHelper.cs b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpTestImageHelper.cs new file mode 100644 index 0000000000..1333afc9aa --- /dev/null +++ b/framework/test/Volo.Abp.Imaging.SkiaSharp.Tests/Volo/Abp/Imaging/SkiaSharpTestImageHelper.cs @@ -0,0 +1,69 @@ +using System.IO; +using SkiaSharp; + +namespace Volo.Abp.Imaging; + +public static class SkiaSharpTestImageHelper +{ + public static MemoryStream CreateQuadrantJpegWithExifOrientation(ushort orientation) + { + using var bitmap = new SKBitmap(200, 100); + using (var canvas = new SKCanvas(bitmap)) + { + using var paint = new SKPaint(); + paint.Color = SKColors.Red; + canvas.DrawRect(0, 0, 100, 50, paint); + paint.Color = SKColors.Lime; + canvas.DrawRect(100, 0, 100, 50, paint); + paint.Color = SKColors.Blue; + canvas.DrawRect(0, 50, 100, 50, paint); + paint.Color = SKColors.Yellow; + canvas.DrawRect(100, 50, 100, 50, paint); + } + + using var image = SKImage.FromBitmap(bitmap); + using var data = image.Encode(SKEncodedImageFormat.Jpeg, 100); + var jpeg = data.ToArray(); + + var exif = new byte[] + { + 0xFF, 0xE1, 0x00, 0x22, + (byte)'E', (byte)'x', (byte)'i', (byte)'f', 0x00, 0x00, + (byte)'M', (byte)'M', 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08, + 0x00, 0x01, + 0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, (byte)(orientation >> 8), (byte)orientation, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + + var stream = new MemoryStream(); + stream.Write(jpeg, 0, 2); + stream.Write(exif, 0, exif.Length); + stream.Write(jpeg, 2, jpeg.Length - 2); + stream.Position = 0; + return stream; + } + + public static string GetQuadrantColors(SKBitmap bitmap) + { + return GetColorInitial(bitmap.GetPixel(bitmap.Width / 4, bitmap.Height / 4)) + + GetColorInitial(bitmap.GetPixel(bitmap.Width * 3 / 4, bitmap.Height / 4)) + + GetColorInitial(bitmap.GetPixel(bitmap.Width / 4, bitmap.Height * 3 / 4)) + + GetColorInitial(bitmap.GetPixel(bitmap.Width * 3 / 4, bitmap.Height * 3 / 4)); + } + + private static string GetColorInitial(SKColor color) + { + var red = color.Red > 200; + var green = color.Green > 200; + var blue = color.Blue > 200; + + return (red, green, blue) switch + { + (true, true, false) => "Y", + (true, false, false) => "R", + (false, true, false) => "G", + (false, false, true) => "B", + _ => "?" + }; + } +}