mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: fa3e308c24fe7f25fe55573397c16bb8d98bb9d7 Former-commit-id: 31697ba859fab7faa06642f42bf9032ad84cab6f Former-commit-id: 2c3cc280c73dd1c4d7976e57f7c3eca9fc17d194af/merge-core
14 changed files with 192 additions and 182 deletions
@ -1,34 +0,0 @@ |
|||
namespace ImageProcessor.Filters |
|||
{ |
|||
/// <summary>
|
|||
/// Image processing filter interface.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// The interface defines the set of methods, which should be
|
|||
/// provided by all image processing filters. Methods of this interface
|
|||
/// manipulate the original image.
|
|||
/// </remarks>
|
|||
public interface IImageFilter |
|||
{ |
|||
/// <summary>
|
|||
/// Apply filter to an image at the area of the specified rectangle.
|
|||
/// </summary>
|
|||
/// <param name="target">Target image to apply filter to.</param>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="rectangle">The rectangle, which defines the area of the
|
|||
/// image where the filter should be applied to.</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="target"/>
|
|||
/// is null.
|
|||
/// - or -
|
|||
/// <paramref name="source"/>
|
|||
/// is null.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentException">
|
|||
/// <paramref name="rectangle"/> doesnt fit the dimension of the image.
|
|||
/// </exception>
|
|||
void Apply(ImageBase target, ImageBase source, Rectangle rectangle); |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// <copyright file="IImageProcessor.cs" company="James South">
|
|||
// Copyright © James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates methods to alter the pixels of an image.
|
|||
/// </summary>
|
|||
public interface IImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Apply a process to an image to alter the pixels at the area of the specified rectangle.
|
|||
/// </summary>
|
|||
/// <param name="target">Target image to apply the process to.</param>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="rectangle">
|
|||
/// The rectangle, which defines the area of the image where the process should be applied to.
|
|||
/// </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="target"/> is null or <paramref name="source"/> is null.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentException">
|
|||
/// <paramref name="rectangle"/> doesnt fit the dimension of the image.
|
|||
/// </exception>
|
|||
void Apply(ImageBase target, ImageBase source, Rectangle rectangle); |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
// <copyright file="ImageFilterExtensions.cs" company="James South">
|
|||
// Copyright © James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
using Formats; |
|||
|
|||
/// <summary>
|
|||
/// Exstension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the bmp format.
|
|||
/// </summary>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="stream">The stream to save the image to.</param>
|
|||
/// <exception cref="ArgumentNullException">Thrown if the stream is null.</exception>
|
|||
public static void SaveAsBmp(this ImageBase source, Stream stream) => new BmpEncoder().Encode(source, stream); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png format.
|
|||
/// </summary>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="stream">The stream to save the image to.</param>
|
|||
/// <exception cref="ArgumentNullException">Thrown if the stream is null.</exception>
|
|||
public static void SaveAsPng(this ImageBase source, Stream stream) => new PngEncoder().Encode(source, stream); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg format.
|
|||
/// </summary>
|
|||
/// <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="ArgumentNullException">Thrown if the stream is null.</exception>
|
|||
public static void SaveAsJpeg(this ImageBase source, Stream stream, int quality = 80) => new JpegEncoder { Quality = quality }.Encode(source, stream); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif format.
|
|||
/// </summary>
|
|||
/// <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 100.</param>
|
|||
/// <exception cref="ArgumentNullException">Thrown if the stream is null.</exception>
|
|||
public static void SaveAsGif(this ImageBase source, Stream stream, int quality = 256) => new GifEncoder() { Quality = quality }.Encode(source, stream); |
|||
|
|||
/// <summary>
|
|||
/// Applies the collection of processors to the image.
|
|||
/// </summary>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="processors">Any processors to apply to the image.</param>
|
|||
/// <returns>The <see cref="Image"/>.</returns>
|
|||
public static Image Process(this Image source, params IImageProcessor[] processors) => Process(source, source.Bounds, processors); |
|||
|
|||
/// <summary>
|
|||
/// Applies the collection of processors to the image.
|
|||
/// </summary>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="rectangle">
|
|||
/// The rectangle defining the bounds of the pixels the image filter with adjust.</param>
|
|||
/// <param name="processors">Any processors to apply to the image.</param>
|
|||
/// <returns>The <see cref="Image"/>.</returns>
|
|||
public static Image Process(this Image source, Rectangle rectangle, params IImageProcessor[] processors) |
|||
{ |
|||
// ReSharper disable once LoopCanBeConvertedToQuery
|
|||
foreach (IImageProcessor filter in processors) |
|||
{ |
|||
source = PerformAction(source, true, (sourceImage, targetImage) => filter.Apply(targetImage, sourceImage, rectangle)); |
|||
} |
|||
|
|||
return source; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Performs the given action on the source image.
|
|||
/// </summary>
|
|||
/// <param name="source">The image to perform the action against.</param>
|
|||
/// <param name="clone">Whether to clone the image.</param>
|
|||
/// <param name="action">The <see cref="Action"/> to perform against the image.</param>
|
|||
/// <returns>The <see cref="Image"/>.</returns>
|
|||
private static Image PerformAction(Image source, bool clone, Action<ImageBase, ImageBase> action) |
|||
{ |
|||
Image transformedImage = clone ? new Image(source) : new Image(); |
|||
transformedImage.CurrentImageFormat = source.CurrentImageFormat; |
|||
action(source, transformedImage); |
|||
|
|||
for (int i = 0; i < source.Frames.Count; i++) |
|||
{ |
|||
ImageFrame sourceFrame = source.Frames[i]; |
|||
ImageFrame tranformedFrame = clone ? new ImageFrame(sourceFrame) : new ImageFrame(); |
|||
action(sourceFrame, tranformedFrame); |
|||
|
|||
if (!clone) |
|||
{ |
|||
transformedImage.Frames.Add(tranformedFrame); |
|||
} |
|||
else |
|||
{ |
|||
transformedImage.Frames[i] = tranformedFrame; |
|||
} |
|||
} |
|||
|
|||
return transformedImage; |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
// <copyright file="IImageSampler.cs" company="James South">
|
|||
// Copyright © James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Samplers |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the methods required for all image sampling (resizing) algorithms.
|
|||
/// </summary>
|
|||
public interface IImageSampler |
|||
{ |
|||
/// <summary>
|
|||
/// Resizes the specified source image by creating a new image with
|
|||
/// the specified size which is a resized version of the passed image.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image.</param>
|
|||
/// <param name="target">The target image.</param>
|
|||
/// <param name="width">The width.</param>
|
|||
/// <param name="height">The height.</param>
|
|||
void Sample(ImageBase source, ImageBase target, int width, int height); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue