//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageProcessor.Samplers
{
///
/// Extensions methods for to apply samplers to the image.
///
public static class ImageSampleExtensions
{
///
/// Crops an image to the given width and height.
///
/// The image to resize.
/// The target image width.
/// The target image height.
/// The
public static Image Crop(this Image source, int width, int height)
{
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 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)
{
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);
}
return source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), new Crop());
}
///
/// Crops an image to the area of greatest entropy.
///
/// The image to crop.
/// The threshold for entropic density.
/// The
public static Image EntropyCrop(this Image source, float threshold = .5f)
{
return source.Process(source.Width, source.Height, source.Bounds, source.Bounds, new EntropyCrop(threshold));
}
///
/// Resizes an image to the given width and height.
///
/// The image to resize.
/// The target image width.
/// The target image height.
/// The
/// Passing zero for one of height or width will automatically preserve the aspect ratio of the original image
public static Image Resize(this Image source, int width, int height)
{
return Resize(source, width, height, new RobidouxResampler());
}
///
/// Resizes an image to the given width and height with the given sampler.
///
/// The image to resize.
/// The target image width.
/// The target image height.
/// The to perform the resampling.
/// The
/// Passing zero for one of height or width will automatically preserve the aspect ratio of the original image
public static Image Resize(this Image source, int width, int height, IResampler sampler)
{
return Resize(source, width, height, sampler, source.Bounds);
}
///
/// Resizes an image to the given width and height with the given sampler and
/// source rectangle.
///
/// The image to resize.
/// The target image width.
/// The target image height.
/// The to perform the resampling.
///
/// The structure that specifies the portion of the image object to draw.
///
/// The
/// Passing zero for one of height or width will automatically preserve the aspect ratio of the original image
public static Image Resize(this Image source, int width, int height, IResampler sampler, Rectangle sourceRectangle)
{
if (width == 0 && height > 0)
{
width = source.Width * height / source.Height;
}
if (height == 0 && width > 0)
{
height = source.Height * width / source.Width;
}
return source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), new Resampler(sampler));
}
///
/// Rotates an image by the given angle in degrees.
///
/// The image to resize.
/// The angle in degrees to perform the rotation.
/// The
public static Image Rotate(this Image source, float degrees)
{
return source.Process(source.Width, source.Height, source.Bounds, source.Bounds, new Resampler(new RobidouxResampler()) { Angle = degrees });
}
///
/// Rotates an image by the given angle in degrees.
///
/// The image to resize.
/// The angle in degrees to perform the rotation.
/// The to perform the resampling.
/// The
public static Image Rotate(this Image source, float degrees, IResampler sampler)
{
return source.Process(source.Width, source.Height, source.Bounds, source.Bounds, new Resampler(sampler) { Angle = degrees });
}
///
/// Rotates and flips an image by the given instructions.
///
/// The image to resize.
/// The to perform the rotation.
/// The to perform the flip.
/// The
public static Image RotateFlip(this Image source, RotateType rotateType, FlipType flipType)
{
return source.Process(source.Width, source.Height, source.Bounds, source.Bounds, new RotateFlip(rotateType, flipType));
}
}
}