mirror of https://github.com/SixLabors/ImageSharp
Browse Source
ImageFilter = NoCopy ImageSampler = Copy Former-commit-id: 530cc95d3b655175999dc78d95fea988f25416e6 Former-commit-id: 63650e982a0a2bb116a473816692187b186a4d0f Former-commit-id: fe8e7020aa116899a2ae1620b4c78b446a3510e7pull/1/head
51 changed files with 488 additions and 336 deletions
@ -0,0 +1,31 @@ |
|||||
|
namespace ImageProcessorCore.Processors |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Encapsulates methods to alter the pixels of an image. The processor operates on the original source pixels.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
public interface IImageFilter<TColor, TPacked> : IImageProcessor |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Applies the process to the specified portion of the specified <see cref="ImageBase{T, TP}"/>.
|
||||
|
/// </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>
|
||||
|
/// <remarks>
|
||||
|
/// The method keeps the source image unchanged and returns the
|
||||
|
/// the result of image processing filter as new image.
|
||||
|
/// </remarks>
|
||||
|
/// <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>
|
||||
|
void Apply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
namespace ImageProcessorCore.Processors |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates methods to alter the pixels of an image. The processor operates on the original source pixels.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
public abstract class ImageFilter<TColor, TPacked> : ImageProcessor<TColor, TPacked>, IImageFilter<TColor, TPacked> |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public void Apply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
this.OnApply(source, sourceRectangle); |
||||
|
|
||||
|
this.NumRowsProcessed = 0; |
||||
|
this.TotalRows = sourceRectangle.Height; |
||||
|
|
||||
|
this.Apply(source, sourceRectangle, sourceRectangle.Y, sourceRectangle.Bottom); |
||||
|
|
||||
|
this.AfterApply(source, sourceRectangle); |
||||
|
} |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Applies the process to the specified portion of the specified <see cref="ImageBase{TColor, TPacked}"/> at the specified location
|
||||
|
/// and with the specified size.
|
||||
|
/// </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>
|
||||
|
/// <param name="startY">The index of the row within the source image to start processing.</param>
|
||||
|
/// <param name="endY">The index of the row within the source image to end processing.</param>
|
||||
|
protected abstract void Apply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle, int startY, int endY); |
||||
|
|
||||
|
/// <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="sourceRectangle">
|
||||
|
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
||||
|
/// </param>
|
||||
|
protected virtual void OnApply(ImageBase<TColor, TPacked> source, 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="sourceRectangle">
|
||||
|
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
||||
|
/// </param>
|
||||
|
protected virtual void AfterApply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,74 @@ |
|||||
|
// <copyright file="ImageExtensions.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageProcessorCore |
||||
|
{ |
||||
|
using System.IO; |
||||
|
|
||||
|
using Formats; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Extension methods for the <see cref="Image{TColor, TPacked}"/> type.
|
||||
|
/// </summary>
|
||||
|
public static partial class ImageExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Saves the image to the given stream with the bmp format.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
/// <param name="source">The image this method extends.</param>
|
||||
|
/// <param name="stream">The stream to save the image to.</param>
|
||||
|
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
||||
|
public static void SaveAsBmp<TColor, TPacked>(this Image<TColor, TPacked> source, Stream stream) |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
=> new BmpEncoder().Encode(source, stream); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Saves the image to the given stream with the png format.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
/// <param name="source">The image this method extends.</param>
|
||||
|
/// <param name="stream">The stream to save the image to.</param>
|
||||
|
/// <param name="quality">The quality to save the image to representing the number of colors.
|
||||
|
/// Anything equal to 256 and below will cause the encoder to save the image in an indexed format.
|
||||
|
/// </param>
|
||||
|
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
||||
|
public static void SaveAsPng<TColor, TPacked>(this Image<TColor, TPacked> source, Stream stream, int quality = int.MaxValue) |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
=> new PngEncoder { Quality = quality }.Encode(source, stream); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Saves the image to the given stream with the jpeg format.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
/// <param name="source">The image this method extends.</param>
|
||||
|
/// <param name="stream">The stream to save the image to.</param>
|
||||
|
/// <param name="quality">The quality to save the image to. Between 1 and 100.</param>
|
||||
|
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
||||
|
public static void SaveAsJpeg<TColor, TPacked>(this Image<TColor, TPacked> source, Stream stream, int quality = 75) |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
=> new JpegEncoder { Quality = quality }.Encode(source, stream); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Saves the image to the given stream with the gif format.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
/// <param name="source">The image this method extends.</param>
|
||||
|
/// <param name="stream">The stream to save the image to.</param>
|
||||
|
/// <param name="quality">The quality to save the image to representing the number of colors. Between 1 and 256.</param>
|
||||
|
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
||||
|
internal static void SaveAsGif<TColor, TPacked>(this Image<TColor, TPacked> source, Stream stream, int quality = 256) |
||||
|
where TColor : IPackedVector<TPacked> |
||||
|
where TPacked : struct |
||||
|
=> new GifEncoder { Quality = quality }.Encode(source, stream); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue