Skip to content

Commit

Permalink
Image padding corrections:
Browse files Browse the repository at this point in the history
- padding is applied on top of the original image size so that any padding above 1 results in actual padding being applied around the image
  • Loading branch information
bijington committed Jun 27, 2021
1 parent d16e615 commit 031ca2e
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/Mobile.BuildTools/Generators/Images/ImageResizeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,26 @@ private SKBitmap ApplyPadding(OutputImage outputImage, ImageBase image, Context
return outputBitmap;
}

var paddedBitmap = new SKBitmap(context.Size.Width, context.Size.Height);
using var paddedCanvas = new SKCanvas(paddedBitmap);

var paddedContext = CreateContext(
GetBackgroundColor(outputImage.PaddingColor, outputImage.RequiresBackgroundColor && image.HasTransparentBackground),
Log,
1.0,
outputImage.PaddingFactor.Value,
0,
0,
context.Size);
context.Size);

// Build the final canvas including the padding.
var paddedBitmap = new SKBitmap(paddedContext.Size.Width, paddedContext.Size.Height);
using var paddedCanvas = new SKCanvas(paddedBitmap);

paddedCanvas.Clear(paddedContext.BackgroundColor);
paddedCanvas.Save();

using var resizedBitmap = outputBitmap.Resize(new SKImageInfo(paddedContext.Size.Width, paddedContext.Size.Height), SKFilterQuality.High);

paddedCanvas.DrawBitmap(
resizedBitmap,
(context.Size.Width - paddedContext.Size.Width) / 2,
(context.Size.Height - paddedContext.Size.Height) / 2);
outputBitmap,
(paddedContext.Size.Width - context.Size.Width) / 2,
(paddedContext.Size.Height - context.Size.Height) / 2);

return paddedBitmap;
}
Expand Down Expand Up @@ -163,4 +162,4 @@ private static Context CreateContext(SKColor backgroundColor, Logging.ILog log,
(float)height / currentSize.Height);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,29 @@ public void AppliesTextBanner(string text, double scale, WatermarkPosition posit
}

[Theory]
[InlineData("dotnetbot.png", 1.5)]
[InlineData("dotnetbot.png", 1)]
[InlineData("dotnetbot.png", .75)]
[InlineData("dotnetbot.png", .5)]
[InlineData("dotnetbot.svg", 1.5)]
[InlineData("dotnetbot.svg", 1)]
[InlineData("dotnetbot.svg", .75)]
[InlineData("dotnetbot.svg", .5)]
[InlineData("dotnetbot.svg", .5)]
[InlineData("logo.svg", 1.5)]
[InlineData("logo.svg", 1)]
public void AppliesPadding(string inputFile, double paddingFactor)
{
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(paddingFactor), paddingFactor), (nameof(inputFile), inputFile));
var generator = new ImageResizeGenerator(config);
var generator = new ImageResizeGenerator(config);

var outputFilename = $"{Path.GetFileNameWithoutExtension(inputFile)}.png";

var image = new OutputImage
{
Height = 0,
Width = 0,
InputFile = Path.Combine(TestConstants.ImageDirectory, inputFile),
OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
OutputFile = Path.Combine(config.IntermediateOutputPath, outputFilename),
OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", outputFilename),
RequiresBackgroundColor = false,
Scale = 1.0,
ShouldBeVisible = true,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 031ca2e

Please sign in to comment.