From 9a92a145d77a89a5c9148dee8b73623bd9757575 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 21 Jan 2023 17:41:51 +1000 Subject: [PATCH 1/7] Add extensions and use overloads --- .../Extensions/Drawing/DrawImageExtensions.cs | 216 +++++++++++++----- .../Processors/Drawing/DrawImageProcessor.cs | 6 +- .../DrawImageProcessor{TPixelBg,TPixelFg}.cs | 4 +- 3 files changed, 168 insertions(+), 58 deletions(-) diff --git a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs index 2cb8dc3211..eba107545d 100644 --- a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs @@ -23,14 +23,26 @@ public static class DrawImageExtensions Image image, float opacity) { - var options = source.GetGraphicsOptions(); - return source.ApplyProcessor( - new DrawImageProcessor( - image, - Point.Empty, - options.ColorBlendingMode, - options.AlphaCompositionMode, - opacity)); + GraphicsOptions options = source.GetGraphicsOptions(); + return DrawImage(source, image, options.ColorBlendingMode, options.AlphaCompositionMode, opacity); + } + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The structure that specifies the portion of the image to draw. + /// The opacity of the image to blend. Must be between 0 and 1. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Rectangle rectangle, + float opacity) + { + GraphicsOptions options = source.GetGraphicsOptions(); + return DrawImage(source, image, rectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity); } /// @@ -45,14 +57,25 @@ public static class DrawImageExtensions this IImageProcessingContext source, Image image, PixelColorBlendingMode colorBlending, - float opacity) => - source.ApplyProcessor( - new DrawImageProcessor( - image, - Point.Empty, - colorBlending, - source.GetGraphicsOptions().AlphaCompositionMode, - opacity)); + float opacity) + => DrawImage(source, image, Point.Empty, colorBlending, opacity); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The structure that specifies the portion of the image to draw. + /// The blending mode. + /// The opacity of the image to blend. Must be between 0 and 1. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Rectangle rectangle, + PixelColorBlendingMode colorBlending, + float opacity) + => DrawImage(source, image, rectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); /// /// Draws the given image together with the current one by blending their pixels. @@ -68,8 +91,27 @@ public static class DrawImageExtensions Image image, PixelColorBlendingMode colorBlending, PixelAlphaCompositionMode alphaComposition, - float opacity) => - source.ApplyProcessor(new DrawImageProcessor(image, Point.Empty, colorBlending, alphaComposition, opacity)); + float opacity) + => DrawImage(source, image, Point.Empty, colorBlending, alphaComposition, opacity); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The structure that specifies the portion of the image to draw. + /// The color blending mode. + /// The alpha composition mode. + /// The opacity of the image to blend. Must be between 0 and 1. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Rectangle rectangle, + PixelColorBlendingMode colorBlending, + PixelAlphaCompositionMode alphaComposition, + float opacity) + => DrawImage(source, image, Point.Empty, rectangle, colorBlending, alphaComposition, opacity); /// /// Draws the given image together with the current one by blending their pixels. @@ -81,14 +123,23 @@ public static class DrawImageExtensions public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, - GraphicsOptions options) => - source.ApplyProcessor( - new DrawImageProcessor( - image, - Point.Empty, - options.ColorBlendingMode, - options.AlphaCompositionMode, - options.BlendPercentage)); + GraphicsOptions options) + => DrawImage(source, image, Point.Empty, options); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The structure that specifies the portion of the image to draw. + /// The options, including the blending type and blending amount. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Rectangle rectangle, + GraphicsOptions options) + => DrawImage(source, image, Point.Empty, rectangle, options); /// /// Draws the given image together with the current one by blending their pixels. @@ -104,14 +155,28 @@ public static class DrawImageExtensions Point location, float opacity) { - var options = source.GetGraphicsOptions(); - return source.ApplyProcessor( - new DrawImageProcessor( - image, - location, - options.ColorBlendingMode, - options.AlphaCompositionMode, - opacity)); + GraphicsOptions options = source.GetGraphicsOptions(); + return DrawImage(source, image, location, options.ColorBlendingMode, options.AlphaCompositionMode, opacity); + } + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The location to draw the blended image. + /// The structure that specifies the portion of the image to draw. + /// The opacity of the image to blend. Must be between 0 and 1. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Point location, + Rectangle rectangle, + float opacity) + { + GraphicsOptions options = source.GetGraphicsOptions(); + return DrawImage(source, image, location, rectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity); } /// @@ -128,14 +193,59 @@ public static class DrawImageExtensions Image image, Point location, PixelColorBlendingMode colorBlending, - float opacity) => - source.ApplyProcessor( - new DrawImageProcessor( - image, - location, - colorBlending, - source.GetGraphicsOptions().AlphaCompositionMode, - opacity)); + float opacity) + => DrawImage(source, image, location, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The location to draw the blended image. + /// The structure that specifies the portion of the image to draw. + /// The color blending to apply. + /// The opacity of the image to blend. Must be between 0 and 1. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Point location, + Rectangle rectangle, + PixelColorBlendingMode colorBlending, + float opacity) + => DrawImage(source, image, location, rectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The location to draw the blended image. + /// The options containing the blend mode and opacity. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Point location, + GraphicsOptions options) + => DrawImage(source, image, location, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage); + + /// + /// Draws the given image together with the current one by blending their pixels. + /// + /// The image this method extends. + /// The image to blend with the currently processing image. + /// The location to draw the blended image. + /// The structure that specifies the portion of the image to draw. + /// The options containing the blend mode and opacity. + /// The . + public static IImageProcessingContext DrawImage( + this IImageProcessingContext source, + Image image, + Point location, + Rectangle rectangle, + GraphicsOptions options) + => DrawImage(source, image, location, rectangle, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage); /// /// Draws the given image together with the current one by blending their pixels. @@ -153,8 +263,8 @@ public static class DrawImageExtensions Point location, PixelColorBlendingMode colorBlending, PixelAlphaCompositionMode alphaComposition, - float opacity) => - source.ApplyProcessor(new DrawImageProcessor(image, location, colorBlending, alphaComposition, opacity)); + float opacity) + => source.ApplyProcessor(new DrawImageProcessor(image, location, colorBlending, alphaComposition, opacity)); /// /// Draws the given image together with the current one by blending their pixels. @@ -162,18 +272,20 @@ public static class DrawImageExtensions /// The image this method extends. /// The image to blend with the currently processing image. /// The location to draw the blended image. - /// The options containing the blend mode and opacity. + /// The structure that specifies the portion of the image to draw. + /// The color blending to apply. + /// The alpha composition mode. + /// The opacity of the image to blend. Must be between 0 and 1. /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, Point location, - GraphicsOptions options) => + Rectangle rectangle, + PixelColorBlendingMode colorBlending, + PixelAlphaCompositionMode alphaComposition, + float opacity) => source.ApplyProcessor( - new DrawImageProcessor( - image, - location, - options.ColorBlendingMode, - options.AlphaCompositionMode, - options.BlendPercentage)); + new DrawImageProcessor(image, location, colorBlending, alphaComposition, opacity), + rectangle); } diff --git a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs index b7d5b42f8a..9de4f862b8 100644 --- a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs @@ -63,7 +63,7 @@ public class DrawImageProcessor : IImageProcessor public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixelBg : unmanaged, IPixel { - var visitor = new ProcessorFactoryVisitor(configuration, this, source, sourceRectangle); + ProcessorFactoryVisitor visitor = new(configuration, this, source, sourceRectangle); this.Image.AcceptVisitor(visitor); return visitor.Result; } @@ -88,8 +88,7 @@ public class DrawImageProcessor : IImageProcessor public void Visit(Image image) where TPixelFg : unmanaged, IPixel - { - this.Result = new DrawImageProcessor( + => this.Result = new DrawImageProcessor( this.configuration, image, this.source, @@ -98,6 +97,5 @@ public class DrawImageProcessor : IImageProcessor this.definition.ColorBlendingMode, this.definition.AlphaCompositionMode, this.definition.Opacity); - } } } diff --git a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs index 82e639f1bd..436a447972 100644 --- a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs +++ b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs @@ -89,7 +89,7 @@ internal class DrawImageProcessor : ImageProcessor int width = maxX - minX; - var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); + Rectangle workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); // Not a valid operation because rectangle does not overlap with this image. if (workingRect.Width <= 0 || workingRect.Height <= 0) @@ -98,7 +98,7 @@ internal class DrawImageProcessor : ImageProcessor "Cannot draw image because the source image does not overlap the target image."); } - var operation = new RowOperation(source.PixelBuffer, targetImage.Frames.RootFrame.PixelBuffer, blender, configuration, minX, width, locationY, targetX, this.Opacity); + DrawImageProcessor.RowOperation operation = new(source.PixelBuffer, targetImage.Frames.RootFrame.PixelBuffer, blender, configuration, minX, width, locationY, targetX, this.Opacity); ParallelRowIterator.IterateRows( configuration, workingRect, From 8a291f61fe4a5fb614fb2071fa20efdaf335ffa6 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 22 Jan 2023 22:09:19 +1000 Subject: [PATCH 2/7] Add test + references --- .../Drawing/DrawImageTests.cs | 38 +++++++++++++++---- .../WorksWithDifferentBounds_10_10.png | 3 ++ .../WorksWithDifferentBounds_25_50.png | 3 ++ .../WorksWithDifferentBounds_50_25.png | 3 ++ .../WorksWithDifferentBounds_50_50.png | 3 ++ 5 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_10_10.png create mode 100644 tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_25_50.png create mode 100644 tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_25.png create mode 100644 tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_50.png diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs index 0dcb961f84..60e8b4ccff 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs @@ -138,6 +138,33 @@ public class DrawImageTests appendSourceFileOrDescription: false); } + [Theory] + [WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 10, 10)] + [WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 50, 25)] + [WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 25, 50)] + [WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 50, 50)] + public void WorksWithDifferentBounds(TestImageProvider provider, int width, int height) + { + using Image background = provider.GetImage(); + using Image overlay = new(50, 50); + Assert.True(overlay.DangerousTryGetSinglePixelMemory(out Memory overlayMem)); + overlayMem.Span.Fill(Color.Black); + + background.Mutate(c => c.DrawImage(overlay, new Rectangle(0, 0, width, height), PixelColorBlendingMode.Normal, 1F)); + + background.DebugSave( + provider, + testOutputDetails: $"{width}_{height}", + appendPixelTypeToFileName: false, + appendSourceFileOrDescription: false); + + background.CompareToReferenceOutput( + provider, + testOutputDetails: $"{width}_{height}", + appendPixelTypeToFileName: false, + appendSourceFileOrDescription: false); + } + [Theory] [WithFile(TestImages.Png.Splash, PixelTypes.Rgba32)] public void DrawTransformed(TestImageProvider provider) @@ -158,12 +185,12 @@ public class DrawImageTests Point position = new((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2); image.Mutate(x => x.DrawImage(blend, position, .75F)); - image.DebugSave(provider, appendSourceFileOrDescription: false, appendPixelTypeToFileName: false); + image.DebugSave(provider, appendPixelTypeToFileName: false, appendSourceFileOrDescription: false); image.CompareToReferenceOutput( ImageComparer.TolerantPercentage(0.002f), provider, - appendSourceFileOrDescription: false, - appendPixelTypeToFileName: false); + appendPixelTypeToFileName: false, + appendSourceFileOrDescription: false); } [Theory] @@ -179,9 +206,6 @@ public class DrawImageTests Assert.Contains("does not overlap", ex.ToString()); - void Test() - { - background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions())); - } + void Test() => background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions())); } } diff --git a/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_10_10.png b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_10_10.png new file mode 100644 index 0000000000..64ce14d509 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_10_10.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f353ee06664a6ff27c533af63cffb9eac4917103ba0b7fff9084fb4d2d42fed7 +size 155 diff --git a/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_25_50.png b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_25_50.png new file mode 100644 index 0000000000..7e0466186a --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_25_50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af198da8f611eb97f3e4e358cb097cd292771d52e991de76495f073c1f1b9338 +size 154 diff --git a/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_25.png b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_25.png new file mode 100644 index 0000000000..6b93e597eb --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_25.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35bda87f28e03c5ed0d45412e367fae9b04b7684f5242dc20e7709e8ed71cd86 +size 156 diff --git a/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_50.png b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_50.png new file mode 100644 index 0000000000..540082dfdf --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Drawing/DrawImageTests/WorksWithDifferentBounds_50_50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8b9ff745592bb0e0a365cb0985a5519bf567fc73a09211c88bcda51356f9202 +size 155 From 6c2d6fa2fc2cab0b5b15f1bad6dbc04dfe60f6d5 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 23 Jan 2023 21:57:14 +1100 Subject: [PATCH 3/7] Update tests/ImageSharp.Tests/Drawing/DrawImageTests.cs Co-authored-by: Scott Williams --- tests/ImageSharp.Tests/Drawing/DrawImageTests.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs index 60e8b4ccff..33068e5442 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs @@ -146,9 +146,7 @@ public class DrawImageTests public void WorksWithDifferentBounds(TestImageProvider provider, int width, int height) { using Image background = provider.GetImage(); - using Image overlay = new(50, 50); - Assert.True(overlay.DangerousTryGetSinglePixelMemory(out Memory overlayMem)); - overlayMem.Span.Fill(Color.Black); + using Image overlay = new(50, 50, Color.Black.ToRgba32()); background.Mutate(c => c.DrawImage(overlay, new Rectangle(0, 0, width, height), PixelColorBlendingMode.Normal, 1F)); From 493ad9f5a47030ee5cd1ccb491f6711a9b4959b0 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 23 Jan 2023 21:11:08 +1000 Subject: [PATCH 4/7] Update intellisense docs. --- .../Extensions/Drawing/DrawImageExtensions.cs | 188 +++++++++--------- .../Drawing/DrawImageTests.cs | 6 +- 2 files changed, 96 insertions(+), 98 deletions(-) diff --git a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs index eba107545d..ad93d6f167 100644 --- a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs @@ -12,12 +12,12 @@ namespace SixLabors.ImageSharp.Processing; public static class DrawImageExtensions { /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -28,13 +28,13 @@ public static class DrawImageExtensions } /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The structure that specifies the portion of the image to draw. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The rectangle structure that specifies the portion of the image to draw. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -46,13 +46,13 @@ public static class DrawImageExtensions } /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The blending mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The color blending mode. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -61,14 +61,14 @@ public static class DrawImageExtensions => DrawImage(source, image, Point.Empty, colorBlending, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The structure that specifies the portion of the image to draw. - /// The blending mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The rectangle structure that specifies the portion of the image to draw. + /// The color blending mode. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -78,14 +78,14 @@ public static class DrawImageExtensions => DrawImage(source, image, rectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. + /// The current image processing context. + /// The image to draw on the currently processing image. /// The color blending mode. /// The alpha composition mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -95,15 +95,15 @@ public static class DrawImageExtensions => DrawImage(source, image, Point.Empty, colorBlending, alphaComposition, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The structure that specifies the portion of the image to draw. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The rectangle structure that specifies the portion of the image to draw. /// The color blending mode. /// The alpha composition mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -114,12 +114,12 @@ public static class DrawImageExtensions => DrawImage(source, image, Point.Empty, rectangle, colorBlending, alphaComposition, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. + /// The current image processing context. + /// The image to draw on the currently processing image. /// The options, including the blending type and blending amount. - /// The . + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -127,13 +127,13 @@ public static class DrawImageExtensions => DrawImage(source, image, Point.Empty, options); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The structure that specifies the portion of the image to draw. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The rectangle structure that specifies the portion of the image to draw. /// The options, including the blending type and blending amount. - /// The . + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -142,13 +142,13 @@ public static class DrawImageExtensions => DrawImage(source, image, Point.Empty, rectangle, options); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -160,14 +160,14 @@ public static class DrawImageExtensions } /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. - /// The structure that specifies the portion of the image to draw. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. + /// The rectangle structure that specifies the portion of the image to draw. + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -180,14 +180,14 @@ public static class DrawImageExtensions } /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. /// The color blending to apply. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -197,15 +197,15 @@ public static class DrawImageExtensions => DrawImage(source, image, location, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. - /// The structure that specifies the portion of the image to draw. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. + /// The rectangle structure that specifies the portion of the image to draw. /// The color blending to apply. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -216,13 +216,13 @@ public static class DrawImageExtensions => DrawImage(source, image, location, rectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. /// The options containing the blend mode and opacity. - /// The . + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -231,14 +231,14 @@ public static class DrawImageExtensions => DrawImage(source, image, location, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. - /// The structure that specifies the portion of the image to draw. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. + /// The rectangle structure that specifies the portion of the image to draw. /// The options containing the blend mode and opacity. - /// The . + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -248,15 +248,15 @@ public static class DrawImageExtensions => DrawImage(source, image, location, rectangle, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. /// The color blending to apply. /// The alpha composition mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, @@ -267,16 +267,16 @@ public static class DrawImageExtensions => source.ApplyProcessor(new DrawImageProcessor(image, location, colorBlending, alphaComposition, opacity)); /// - /// Draws the given image together with the current one by blending their pixels. + /// Draws the given image together with the currently processing image by blending their pixels. /// - /// The image this method extends. - /// The image to blend with the currently processing image. - /// The location to draw the blended image. - /// The structure that specifies the portion of the image to draw. + /// The current image processing context. + /// The image to draw on the currently processing image. + /// The location on the currenty processing image at which to draw. + /// The rectangle structure that specifies the portion of the image to draw. /// The color blending to apply. /// The alpha composition mode. - /// The opacity of the image to blend. Must be between 0 and 1. - /// The . + /// The opacity of the image to draw. Must be between 0 and 1. + /// The . public static IImageProcessingContext DrawImage( this IImageProcessingContext source, Image image, diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs index 33068e5442..d017e5ad42 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTests.cs @@ -119,9 +119,7 @@ public class DrawImageTests public void WorksWithDifferentLocations(TestImageProvider provider, int x, int y) { using Image background = provider.GetImage(); - using Image overlay = new(50, 50); - Assert.True(overlay.DangerousTryGetSinglePixelMemory(out Memory overlayMem)); - overlayMem.Span.Fill(Color.Black); + using Image overlay = new(50, 50, Color.Black.ToRgba32()); background.Mutate(c => c.DrawImage(overlay, new Point(x, y), PixelColorBlendingMode.Normal, 1F)); @@ -146,7 +144,7 @@ public class DrawImageTests public void WorksWithDifferentBounds(TestImageProvider provider, int width, int height) { using Image background = provider.GetImage(); - using Image overlay = new(50, 50, Color.Black.ToRgba32()); + using Image overlay = new(50, 50, Color.Black.ToRgba32()); background.Mutate(c => c.DrawImage(overlay, new Rectangle(0, 0, width, height), PixelColorBlendingMode.Normal, 1F)); From 3c2885e382e9c5057d4e059ca6da4bdadb01f8a8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 23 Jan 2023 23:13:53 +1000 Subject: [PATCH 5/7] Allow zero DPI and only throw at EOF when not enough data --- .../Jpeg/Components/Decoder/JFifMarker.cs | 22 ++++--------------- .../Components/Decoder/SpectralConverter.cs | 6 +++++ .../Decoder/SpectralConverter{TPixel}.cs | 6 +++++ .../Formats/Jpeg/JpegDecoderCore.cs | 12 ++++++++++ .../Formats/Jpg/JpegDecoderTests.cs | 11 ++++++++++ .../Formats/Jpg/SpectralJpegTests.cs | 2 ++ tests/ImageSharp.Tests/TestImages.cs | 1 + tests/Images/Input/Jpg/issues/issue-2315.jpg | 3 +++ 8 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 tests/Images/Input/Jpg/issues/issue-2315.jpg diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs index 42dcf7200a..e13b26f9a9 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs @@ -26,16 +26,6 @@ internal readonly struct JFifMarker : IEquatable /// The vertical pixel density. private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, short xDensity, short yDensity) { - if (xDensity <= 0) - { - JpegThrowHelper.ThrowInvalidImageContentException($"X-Density {xDensity} must be greater than 0."); - } - - if (yDensity <= 0) - { - JpegThrowHelper.ThrowInvalidImageContentException($"Y-Density {yDensity} must be greater than 0."); - } - this.MajorVersion = majorVersion; this.MinorVersion = minorVersion; @@ -64,12 +54,12 @@ internal readonly struct JFifMarker : IEquatable public PixelResolutionUnit DensityUnits { get; } /// - /// Gets the horizontal pixel density. Must not be zero. + /// Gets the horizontal pixel density. /// public short XDensity { get; } /// - /// Gets the vertical pixel density. Must not be zero. + /// Gets the vertical pixel density. /// public short YDensity { get; } @@ -88,12 +78,8 @@ internal readonly struct JFifMarker : IEquatable byte densityUnits = bytes[7]; short xDensity = (short)((bytes[8] << 8) | bytes[9]); short yDensity = (short)((bytes[10] << 8) | bytes[11]); - - if (xDensity > 0 && yDensity > 0) - { - marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity); - return true; - } + marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity); + return true; } marker = default; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs index 0eb484b94a..51d9bfbced 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs @@ -121,4 +121,10 @@ internal abstract class SpectralConverter return size; } + + /// + /// Gets a value indicating whether the converter has a pixel buffer. + /// + /// if the converter has a pixel buffer; otherwise, . + public abstract bool HasPixelBuffer(); } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 4bb58d8a12..d6250127f5 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -85,6 +85,12 @@ internal class SpectralConverter : SpectralConverter, IDisposable /// public Configuration Configuration { get; } + /// + /// Gets a value indicating whether the converter has a pixel buffer. + /// + /// if the converter has a pixel buffer; otherwise, . + public override bool HasPixelBuffer() => this.pixelBuffer is not null; + /// /// Gets converted pixel buffer. /// diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 511ec1ba1d..51de9ffbd1 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -356,6 +356,18 @@ internal sealed class JpegDecoderCore : IRawJpegData, IImageDecoderInternals // to uint to avoid sign extension. if (stream.RemainingBytes < (uint)markerContentByteSize) { + if (metadataOnly && this.Metadata != null && this.Frame != null) + { + // We have enough data to decode the image, so we can stop parsing. + return; + } + + if (this.Metadata != null && this.Frame != null && spectralConverter.HasPixelBuffer()) + { + // We have enough data to decode the image, so we can stop parsing. + return; + } + JpegThrowHelper.ThrowNotEnoughBytesForMarker(fileMarker.Marker); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 1d5a7e0ff8..425d12497c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -300,4 +300,15 @@ public partial class JpegDecoderTests image.DebugSave(provider); image.CompareToOriginal(provider); } + + // https://github.com/SixLabors/ImageSharp/issues/2315 + [Theory] + [WithFile(TestImages.Jpeg.Issues.Issue2315_NotEnoughBytes, PixelTypes.Rgba32)] + public void Issue2315_DecodeWorks(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(JpegDecoder.Instance); + image.DebugSave(provider); + image.CompareToOriginal(provider); + } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 58347a0724..805ee586a8 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -187,6 +187,8 @@ public class SpectralJpegTests } } + public override bool HasPixelBuffer() => throw new NotImplementedException(); + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { this.frame = frame; diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 29223b1fe0..22a48ebee4 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -281,6 +281,7 @@ public static class TestImages public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg"; public const string Issue2133_DeduceColorSpace = "Jpg/issues/Issue2133.jpg"; public const string Issue2136_ScanMarkerExtraneousBytes = "Jpg/issues/Issue2136-scan-segment-extraneous-bytes.jpg"; + public const string Issue2315_NotEnoughBytes = "Jpg/issues/issue-2315.jpg"; public static class Fuzz { diff --git a/tests/Images/Input/Jpg/issues/issue-2315.jpg b/tests/Images/Input/Jpg/issues/issue-2315.jpg new file mode 100644 index 0000000000..fe3001eddc --- /dev/null +++ b/tests/Images/Input/Jpg/issues/issue-2315.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f129b057efb499d492e9afcffdd98de62aac1e04b97a09a75b4799ba498cd3c1 +size 319056 From c44cd6c244f4671fdb8b4c7fd426afc865e55084 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 23 Jan 2023 23:26:15 +1000 Subject: [PATCH 6/7] Update DecodeJpegParseStreamOnly.cs --- .../Codecs/Jpeg/DecodeJpegParseStreamOnly.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs index aa88242ce4..b5a7245292 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -54,6 +54,8 @@ public class DecodeJpegParseStreamOnly { } + public override bool HasPixelBuffer() => throw new NotImplementedException(); + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { } From c224dece1765026ca0da5086af88e71ed1fb30fc Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 24 Jan 2023 08:57:39 +1000 Subject: [PATCH 7/7] Update JFifMarkerTests.cs --- tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs index 3890c20e93..3b7e20eb4e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs @@ -46,15 +46,6 @@ public class JFifMarkerTests Assert.Equal(default, marker); } - [Fact] - public void MarkerIgnoresCorrectHeaderButInvalidDensities() - { - bool isJFif = JFifMarker.TryParse(this.bytes3, out JFifMarker marker); - - Assert.False(isJFif); - Assert.Equal(default, marker); - } - [Fact] public void MarkerEqualityIsCorrect() {