//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp
{
using System;
using Processing;
using Processing.Processors;
///
/// Extension methods for the type.
///
public static partial class ImageExtensions
{
///
/// Resizes an image in accordance with the given .
///
/// The pixel format.
/// The image to resize.
/// The resize options.
/// The
/// Passing zero for one of height or width within the resize options will automatically preserve the aspect ratio of the original image
public static Image Resize(this Image source, ResizeOptions options)
where TColor : struct, IPackedPixel, IEquatable
{
// Ensure size is populated across both dimensions.
if (options.Size.Width == 0 && options.Size.Height > 0)
{
options.Size = new Size(source.Width * options.Size.Height / source.Height, options.Size.Height);
}
if (options.Size.Height == 0 && options.Size.Width > 0)
{
options.Size = new Size(options.Size.Width, source.Height * options.Size.Width / source.Width);
}
Rectangle targetRectangle = ResizeHelper.CalculateTargetLocationAndBounds(source, options);
return Resize(source, options.Size.Width, options.Size.Height, options.Sampler, source.Bounds, targetRectangle, options.Compand);
}
///
/// Resizes an image to the given .
///
/// The pixel format.
/// The image to resize.
/// The target image size.
/// 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, Size size)
where TColor : struct, IPackedPixel, IEquatable
{
return Resize(source, size.Width, size.Height, new BicubicResampler(), false);
}
///
/// Resizes an image to the given width and height.
///
/// The pixel format.
/// 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)
where TColor : struct, IPackedPixel, IEquatable
{
return Resize(source, width, height, new BicubicResampler(), false);
}
///
/// Resizes an image to the given width and height.
///
/// The pixel format.
/// The image to resize.
/// The target image width.
/// The target image height.
/// Whether to compress and expand the image color-space to gamma correct the image during processing.
/// 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, bool compand)
where TColor : struct, IPackedPixel, IEquatable
{
return Resize(source, width, height, new BicubicResampler(), compand);
}
///
/// Resizes an image to the given width and height with the given sampler.
///
/// The pixel format.
/// 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)
where TColor : struct, IPackedPixel, IEquatable
{
return Resize(source, width, height, sampler, false);
}
///
/// Resizes an image to the given width and height with the given sampler.
///
/// The pixel format.
/// The image to resize.
/// The target image width.
/// The target image height.
/// The to perform the resampling.
/// Whether to compress and expand the image color-space to gamma correct the image during processing.
/// 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, bool compand)
where TColor : struct, IPackedPixel, IEquatable
{
return Resize(source, width, height, sampler, source.Bounds, new Rectangle(0, 0, width, height), compand);
}
///
/// Resizes an image to the given width and height with the given sampler and
/// source rectangle.
///
/// The pixel format.
/// 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 structure that specifies the portion of the target image object to draw to.
///
/// Whether to compress and expand the image color-space to gamma correct the image during processing.
/// 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, Rectangle targetRectangle, bool compand = false)
where TColor : struct, IPackedPixel, IEquatable
{
if (width == 0 && height > 0)
{
width = source.Width * height / source.Height;
targetRectangle.Width = width;
}
if (height == 0 && width > 0)
{
height = source.Height * width / source.Width;
targetRectangle.Height = height;
}
Guard.MustBeGreaterThan(width, 0, nameof(width));
Guard.MustBeGreaterThan(height, 0, nameof(height));
ResamplingWeightedProcessor processor;
if (compand)
{
processor = new CompandingResizeProcessor(sampler, width, height, targetRectangle);
}
else
{
processor = new ResizeProcessor(sampler, width, height, targetRectangle);
}
source.ApplyProcessor(processor, sourceRectangle);
return source;
}
}
}