mirror of https://github.com/SixLabors/ImageSharp
9 changed files with 376 additions and 93 deletions
@ -0,0 +1,37 @@ |
|||
// <copyright file="IImageProcessor.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Processing |
|||
{ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
using ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates methods to alter the pixels of an image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
public interface ICloningImageProcessor<TPixel> : IImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <summary>
|
|||
/// Applies the process to the specified portion of the specified <see cref="ImageBase{TPixel}"/>.
|
|||
/// </summary>
|
|||
/// <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>
|
|||
/// <exception cref="System.ArgumentNullException">
|
|||
/// <paramref name="source"/> is null.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentException">
|
|||
/// <paramref name="sourceRectangle"/> doesnt fit the dimension of the image.
|
|||
/// </exception>
|
|||
/// <returns>Returns the cloned image after thre processor has been applied to it.</returns>
|
|||
Image<TPixel> CloneAndApply(Image<TPixel> source, Rectangle sourceRectangle); |
|||
} |
|||
} |
|||
@ -0,0 +1,166 @@ |
|||
// <copyright file="CloneingImageProcessor.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Processing |
|||
{ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
using ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
/// <summary>
|
|||
/// Allows the application of processors to images.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
internal abstract class CloneingImageProcessor<TPixel> : IImageProcessor<TPixel>, ICloningImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public virtual ParallelOptions ParallelOptions { get; set; } |
|||
|
|||
/// <inheritdoc/>
|
|||
public virtual bool Compand { get; set; } = false; |
|||
|
|||
/// <inheritdoc/>
|
|||
public Image<TPixel> CloneAndApply(Image<TPixel> source, Rectangle sourceRectangle) |
|||
{ |
|||
if (this.ParallelOptions == null) |
|||
{ |
|||
this.ParallelOptions = source.Configuration.ParallelOptions; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
Image<TPixel> clone = this.CreateDestination(source, sourceRectangle); |
|||
|
|||
if (clone.Frames.Count != source.Frames.Count) |
|||
{ |
|||
throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. The processor changed the number of frames."); |
|||
} |
|||
|
|||
this.BeforeImageApply(source, clone, sourceRectangle); |
|||
|
|||
this.BeforeApply(source, clone, sourceRectangle); |
|||
this.OnApply(source, clone, sourceRectangle); |
|||
this.AfterApply(source, clone, sourceRectangle); |
|||
|
|||
for (int i = 0; i < source.Frames.Count; i++) |
|||
{ |
|||
ImageFrame<TPixel> sourceFrame = source.Frames[i]; |
|||
ImageFrame<TPixel> clonedFrame = clone.Frames[i]; |
|||
|
|||
this.BeforeApply(sourceFrame, clonedFrame, sourceRectangle); |
|||
|
|||
this.OnApply(sourceFrame, clonedFrame, sourceRectangle); |
|||
this.AfterApply(sourceFrame, clonedFrame, sourceRectangle); |
|||
} |
|||
|
|||
this.AfterImageApply(source, clone, sourceRectangle); |
|||
|
|||
return clone; |
|||
} |
|||
#if DEBUG
|
|||
catch (Exception) |
|||
{ |
|||
throw; |
|||
#else
|
|||
catch (Exception ex) |
|||
{ |
|||
throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. See the inner exception for more detail.", ex); |
|||
#endif
|
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public void Apply(Image<TPixel> source, Rectangle sourceRectangle) |
|||
{ |
|||
using (Image<TPixel> cloned = this.CloneAndApply(source, sourceRectangle)) |
|||
{ |
|||
// we now need to move the pixel data/size data from one image base to another
|
|||
if (cloned.Frames.Count != source.Frames.Count) |
|||
{ |
|||
throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. The processor changed the number of frames."); |
|||
} |
|||
|
|||
source.SwapPixelsData(cloned); |
|||
for (int i = 0; i < source.Frames.Count; i++) |
|||
{ |
|||
source.Frames[i].SwapPixelsData(cloned.Frames[i]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Generates the clone of the source image that operatinos should be applied to.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">The source rectangle.</param>
|
|||
/// <returns>The cloned image.</returns>
|
|||
protected virtual Image<TPixel> CreateDestination(Image<TPixel> source, Rectangle sourceRectangle) |
|||
{ |
|||
return source.Clone(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method is called before the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="destination">The cloned/destination image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void BeforeImageApply(Image<TPixel> source, Image<TPixel> destination, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method is called before the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="destination">The cloned/destination image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void BeforeApply(ImageBase<TPixel> source, ImageBase<TPixel> destination, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Applies the process to the specified portion of the specified <see cref="ImageBase{TPixel}"/> at the specified location
|
|||
/// and with the specified size.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="destination">The cloned/destination image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected abstract void OnApply(ImageBase<TPixel> source, ImageBase<TPixel> destination, Rectangle sourceRectangle); |
|||
|
|||
/// <summary>
|
|||
/// This method is called after the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="destination">The cloned/destination image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void AfterApply(ImageBase<TPixel> source, ImageBase<TPixel> destination, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method is called after the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="destination">The cloned/destination image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void AfterImageApply(Image<TPixel> source, Image<TPixel> destination, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue