Skip to content

Commit 32d919e

Browse files
Fix #466 (#916)
1 parent 10c834b commit 32d919e

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/ImageSharp/Processing/Extensions/PadExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using SixLabors.ImageSharp.PixelFormats;
54
using SixLabors.Primitives;
65

76
namespace SixLabors.ImageSharp.Processing
@@ -20,6 +19,17 @@ public static class PadExtensions
2019
/// <param name="height">The new height.</param>
2120
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2221
public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height)
22+
=> source.Pad(width, height, default);
23+
24+
/// <summary>
25+
/// Evenly pads an image to fit the new dimensions with the given background color.
26+
/// </summary>
27+
/// <param name="source">The source image to pad.</param>
28+
/// <param name="width">The new width.</param>
29+
/// <param name="height">The new height.</param>
30+
/// <param name="color">The background color with which to pad the image.</param>
31+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
32+
public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height, Color color)
2333
{
2434
var options = new ResizeOptions
2535
{
@@ -28,7 +38,7 @@ public static IImageProcessingContext Pad(this IImageProcessingContext source, i
2838
Sampler = KnownResamplers.NearestNeighbor,
2939
};
3040

31-
return source.Resize(options);
41+
return color.Equals(default) ? source.Resize(options) : source.Resize(options).BackgroundColor(color);
3242
}
3343
}
3444
}

tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
1010
public class PadTest
1111
{
1212
public static readonly string[] CommonTestImages =
13-
{
14-
TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
15-
};
16-
13+
{
14+
TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
15+
};
16+
1717
[Theory]
1818
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
1919
public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
@@ -29,7 +29,30 @@ public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
2929
{
3030
for (int x = 0; x < 25; x++)
3131
{
32-
Assert.Equal(default(TPixel), image[x, y]);
32+
Assert.Equal(default, image[x, y]);
33+
}
34+
}
35+
}
36+
}
37+
38+
[Theory]
39+
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
40+
public void ImageShouldPadWithBackgroundColor<TPixel>(TestImageProvider<TPixel> provider)
41+
where TPixel : struct, IPixel<TPixel>
42+
{
43+
Color color = Color.Red;
44+
TPixel expected = color.ToPixel<TPixel>();
45+
using (Image<TPixel> image = provider.GetImage())
46+
{
47+
image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50, color));
48+
image.DebugSave(provider);
49+
50+
// Check pixels are filled
51+
for (int y = 0; y < 25; y++)
52+
{
53+
for (int x = 0; x < 25; x++)
54+
{
55+
Assert.Equal(expected, image[x, y]);
3356
}
3457
}
3558
}

0 commit comments

Comments
 (0)