-
-
Notifications
You must be signed in to change notification settings - Fork 887
Make processors public, refactor cloning. #1011
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
Changes from 1 commit
1796862
75d27b5
ab856a3
464598c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using SixLabors.ImageSharp.PixelFormats; | ||
| using SixLabors.Primitives; | ||
|
|
||
| namespace SixLabors.ImageSharp.Processing.Processors | ||
| { | ||
| /// <summary> | ||
| /// The base class for all cloning image processors. | ||
| /// </summary> | ||
| public abstract class CloningImageProcessor : IImageProcessor | ||
| { | ||
| /// <summary> | ||
| /// Creates a pixel specific <see cref="ICloningImageProcessor{TPixel}"/> that is capable of executing | ||
| /// the processing algorithm on an <see cref="Image{TPixel}"/>. | ||
| /// </summary> | ||
| /// <typeparam name="TPixel">The pixel type.</typeparam> | ||
| /// <param name="source">The source image. Cannot be null.</param> | ||
| /// <param name="sourceRectangle"> | ||
| /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw. | ||
| /// </param> | ||
| /// <returns>The <see cref="ICloningImageProcessor{TPixel}"/></returns> | ||
| public abstract ICloningImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) | ||
| where TPixel : struct, IPixel<TPixel>; | ||
|
|
||
| /// <inheritdoc/> | ||
| IImageProcessor<TPixel> IImageProcessor.CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) | ||
JimBobSquarePants marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| => this.CreatePixelSpecificProcessor(source, sourceRectangle); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,12 @@ | |
| namespace SixLabors.ImageSharp.Processing.Processors | ||
| { | ||
| /// <summary> | ||
| /// Allows the application of processing algorithms to a clone of the original image. | ||
| /// The base class for all pixel specific cloning image processors. | ||
| /// Allows the application of processing algorithms to the image. | ||
| /// The image is cloned before operating upon and the buffers swapped upon completion. | ||
| /// </summary> | ||
| /// <typeparam name="TPixel">The pixel format.</typeparam> | ||
| internal abstract class CloningImageProcessor<TPixel> : ICloningImageProcessor<TPixel> | ||
| public abstract class CloningImageProcessor<TPixel> : ICloningImageProcessor<TPixel> | ||
| where TPixel : struct, IPixel<TPixel> | ||
| { | ||
| private bool isDisposed; | ||
|
|
@@ -45,16 +47,12 @@ protected CloningImageProcessor(Image<TPixel> source, Rectangle sourceRectangle) | |
| protected Configuration Configuration { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Image<TPixel> CloneAndApply() | ||
| public Image<TPixel> CloneAndExecute() | ||
JimBobSquarePants marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| try | ||
| { | ||
| Image<TPixel> clone = this.CreateDestination(); | ||
JimBobSquarePants marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (clone.Frames.Count != this.Source.Frames.Count) | ||
| { | ||
| throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. The processor changed the number of frames."); | ||
| } | ||
| this.CheckFrameCount(this.Source, clone); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if the checks are necessary. The centralized cloning logic should guarantee that the frame counts are matching.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually thought that but was too lazy to check and just merged the code into a single method
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an iterative way to delete bloated code 😉 |
||
|
|
||
| Configuration configuration = this.Source.GetConfiguration(); | ||
| this.BeforeImageApply(clone); | ||
|
|
@@ -86,17 +84,24 @@ public Image<TPixel> CloneAndApply() | |
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Apply() | ||
| public void Execute() | ||
| { | ||
| using (Image<TPixel> cloned = this.CloneAndApply()) | ||
| // Create an interim clone of the source image to operate on. | ||
| // Doing this allows for the application of transforms that will alter | ||
| // the dimensions of the image. | ||
| Image<TPixel> clone = default; | ||
| try | ||
| { | ||
| // we now need to move the pixel data/size data from one image base to another | ||
| if (cloned.Frames.Count != this.Source.Frames.Count) | ||
| { | ||
| throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. The processor changed the number of frames."); | ||
| } | ||
| clone = this.CloneAndExecute(); | ||
|
|
||
| this.Source.SwapOrCopyPixelsBuffersFrom(cloned); | ||
| // We now need to move the pixel data/size data from the clone to the source. | ||
| this.CheckFrameCount(this.Source, clone); | ||
| this.Source.SwapOrCopyPixelsBuffersFrom(clone); | ||
| } | ||
| finally | ||
| { | ||
| // Dispose of the clone now that we have swapped the pixel/size data. | ||
| clone?.Dispose(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -165,5 +170,13 @@ protected virtual void Dispose(bool disposing) | |
| this.isDisposed = true; | ||
| } | ||
| } | ||
|
|
||
| private void CheckFrameCount(Image<TPixel> a, Image<TPixel> b) | ||
| { | ||
| if (a.Frames.Count != b.Frames.Count) | ||
| { | ||
| throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. The processor changed the number of frames."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.