//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageProcessorCore
{
using Processors;
///
/// Extension methods for the type.
///
public static partial class ImageExtensions
{
///
/// Crops an image to the given width and height.
///
/// The pixel format.
/// The packed format. uint, long, float.
/// The image to resize.
/// The target image width.
/// The target image height.
/// The
public static Image Crop(this Image source, int width, int height)
where TColor : IPackedVector
where TPacked : struct
{
return Crop(source, width, height, source.Bounds);
}
///
/// Crops an image to the given width and height with the given source rectangle.
///
/// If the source rectangle is smaller than the target dimensions then the
/// area within the source is resized performing a zoomed crop.
///
///
/// The pixel format.
/// The packed format. uint, long, float.
/// The image to crop.
/// The target image width.
/// The target image height.
///
/// The structure that specifies the portion of the image object to draw.
///
/// The
public static Image Crop(this Image source, int width, int height, Rectangle sourceRectangle)
where TColor : IPackedVector
where TPacked : 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();
return source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), processor);
}
}
}