Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Color overload to Pad method. #916

Merged
merged 1 commit into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/ImageSharp/Processing/Extensions/PadExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

namespace SixLabors.ImageSharp.Processing
Expand All @@ -20,6 +19,17 @@ public static class PadExtensions
/// <param name="height">The new height.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height)
=> source.Pad(width, height, default);

/// <summary>
/// Evenly pads an image to fit the new dimensions with the given background color.
/// </summary>
/// <param name="source">The source image to pad.</param>
/// <param name="width">The new width.</param>
/// <param name="height">The new height.</param>
/// <param name="color">The background color with which to pad the image.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height, Color color)
{
var options = new ResizeOptions
{
Expand All @@ -28,7 +38,7 @@ public static IImageProcessingContext Pad(this IImageProcessingContext source, i
Sampler = KnownResamplers.NearestNeighbor,
};

return source.Resize(options);
return color.Equals(default) ? source.Resize(options) : source.Resize(options).BackgroundColor(color);
}
}
}
33 changes: 28 additions & 5 deletions tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public class PadTest
{
public static readonly string[] CommonTestImages =
{
TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
};
{
TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
};

[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
Expand All @@ -29,7 +29,30 @@ public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
{
for (int x = 0; x < 25; x++)
{
Assert.Equal(default(TPixel), image[x, y]);
Assert.Equal(default, image[x, y]);
}
}
}
}

[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
public void ImageShouldPadWithBackgroundColor<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
Color color = Color.Red;
TPixel expected = color.ToPixel<TPixel>();
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50, color));
image.DebugSave(provider);

// Check pixels are filled
for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 25; x++)
{
Assert.Equal(expected, image[x, y]);
}
}
}
Expand Down