mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 63386b63ab93e14d3c50383739c042a656efab9f Former-commit-id: 0a1b7a7367d9ea360cc4cf0a821c788bd468b607 Former-commit-id: 8430af726048214b3820969d1dfe31b9df72a695pull/1/head
4 changed files with 174 additions and 52 deletions
@ -0,0 +1,76 @@ |
|||
// <copyright file="Crop.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>-------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessorCore |
|||
{ |
|||
using Processors; |
|||
|
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Crops an image to the given width and height.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The pixel format.</typeparam>
|
|||
/// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
|
|||
/// <param name="source">The image to resize.</param>
|
|||
/// <param name="width">The target image width.</param>
|
|||
/// <param name="height">The target image height.</param>
|
|||
/// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
|
|||
/// <returns>The <see cref="Image{T,TP}"/></returns>
|
|||
public static Image<T, TP> Crop<T, TP>(this Image<T, TP> source, int width, int height, ProgressEventHandler progressHandler = null) |
|||
where T : IPackedVector<TP> |
|||
where TP : struct |
|||
{ |
|||
return Crop(source, width, height, source.Bounds, progressHandler); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Crops an image to the given width and height with the given source rectangle.
|
|||
/// <remarks>
|
|||
/// If the source rectangle is smaller than the target dimensions then the
|
|||
/// area within the source is resized performing a zoomed crop.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The pixel format.</typeparam>
|
|||
/// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
|
|||
/// <param name="source">The image to crop.</param>
|
|||
/// <param name="width">The target image width.</param>
|
|||
/// <param name="height">The target image height.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
/// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
|
|||
/// <returns>The <see cref="Image"/></returns>
|
|||
public static Image<T, TP> Crop<T, TP>(this Image<T, TP> source, int width, int height, Rectangle sourceRectangle, ProgressEventHandler progressHandler = null) |
|||
where T : IPackedVector<TP> |
|||
where TP : struct |
|||
{ |
|||
Guard.MustBeGreaterThan(width, 0, nameof(width)); |
|||
Guard.MustBeGreaterThan(height, 0, nameof(height)); |
|||
|
|||
if (sourceRectangle.Width < width || sourceRectangle.Height < height) |
|||
{ |
|||
// If the source rectangle is smaller than the target perform a
|
|||
// cropped zoom.
|
|||
source = source.Resize(sourceRectangle.Width, sourceRectangle.Height); |
|||
} |
|||
|
|||
CropProcessor processor = new CropProcessor(); |
|||
processor.OnProgress += progressHandler; |
|||
|
|||
try |
|||
{ |
|||
return source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), processor); |
|||
} |
|||
finally |
|||
{ |
|||
processor.OnProgress -= progressHandler; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
// <copyright file="CropProcessor.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessorCore.Processors |
|||
{ |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// Provides methods to allow the cropping of an image.
|
|||
/// </summary>
|
|||
public class CropProcessor : ImageSampler |
|||
{ |
|||
/// <inheritdoc/>
|
|||
protected override void Apply<T, TP>(ImageBase<T, TP> target, ImageBase<T, TP> source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
int startX = targetRectangle.X; |
|||
int endX = targetRectangle.Right; |
|||
int sourceX = sourceRectangle.X; |
|||
int sourceY = sourceRectangle.Y; |
|||
|
|||
using (IPixelAccessor<T, TP> sourcePixels = source.Lock()) |
|||
using (IPixelAccessor<T, TP> targetPixels = target.Lock()) |
|||
{ |
|||
Parallel.For( |
|||
startY, |
|||
endY, |
|||
y => |
|||
{ |
|||
for (int x = startX; x < endX; x++) |
|||
{ |
|||
targetPixels[x, y] = sourcePixels[x + sourceX, y + sourceY]; |
|||
} |
|||
|
|||
this.OnRowProcessed(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,40 +1,40 @@ |
|||
//namespace ImageProcessorCore.Benchmarks
|
|||
//{
|
|||
// using System.Drawing;
|
|||
// using System.Drawing.Drawing2D;
|
|||
namespace ImageProcessorCore.Benchmarks |
|||
{ |
|||
using System.Drawing; |
|||
using System.Drawing.Drawing2D; |
|||
|
|||
// using BenchmarkDotNet.Attributes;
|
|||
// using CoreImage = ImageProcessorCore.Image;
|
|||
// using CoreSize = ImageProcessorCore.Size;
|
|||
using BenchmarkDotNet.Attributes; |
|||
using CoreSize = ImageProcessorCore.Size; |
|||
using CoreImage = ImageProcessorCore.Image; |
|||
|
|||
// public class Crop
|
|||
// {
|
|||
// [Benchmark(Baseline = true, Description = "System.Drawing Crop")]
|
|||
// public Size CropSystemDrawing()
|
|||
// {
|
|||
// using (Bitmap source = new Bitmap(400, 400))
|
|||
// {
|
|||
// using (Bitmap destination = new Bitmap(100, 100))
|
|||
// {
|
|||
// using (Graphics graphics = Graphics.FromImage(destination))
|
|||
// {
|
|||
// graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|||
// graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|||
// graphics.CompositingQuality = CompositingQuality.HighQuality;
|
|||
// graphics.DrawImage(source, new Rectangle(0, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel);
|
|||
// }
|
|||
public class Crop |
|||
{ |
|||
[Benchmark(Baseline = true, Description = "System.Drawing Crop")] |
|||
public Size CropSystemDrawing() |
|||
{ |
|||
using (Bitmap source = new Bitmap(400, 400)) |
|||
{ |
|||
using (Bitmap destination = new Bitmap(100, 100)) |
|||
{ |
|||
using (Graphics graphics = Graphics.FromImage(destination)) |
|||
{ |
|||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; |
|||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; |
|||
graphics.CompositingQuality = CompositingQuality.HighQuality; |
|||
graphics.DrawImage(source, new Rectangle(0, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel); |
|||
} |
|||
|
|||
// return destination.Size;
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
return destination.Size; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// [Benchmark(Description = "ImageProcessorCore Crop")]
|
|||
// public CoreSize CropResizeCore()
|
|||
// {
|
|||
// CoreImage image = new CoreImage(400, 400);
|
|||
// image.Crop(100, 100);
|
|||
// return new CoreSize(image.Width, image.Height);
|
|||
// }
|
|||
// }
|
|||
//}
|
|||
[Benchmark(Description = "ImageProcessorCore Crop")] |
|||
public CoreSize CropResizeCore() |
|||
{ |
|||
CoreImage image = new CoreImage(400, 400); |
|||
image.Crop(100, 100); |
|||
return new CoreSize(image.Width, image.Height); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue