Skip to content

Commit

Permalink
Merge pull request unoplatform#16816 from ramezgerges/imagebrush_down…
Browse files Browse the repository at this point in the history
…scaling_blur

fix(imagebrush): downscaling images was very blurry
  • Loading branch information
ramezgerges authored Jun 1, 2024
2 parents 18022fd + a124067 commit 81e55a4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9602,6 +9602,7 @@
<Content Include="$(MSBuildThisFileDirectory)Assets\RemoteFonts\antikytheraoutline.woff" />
<Content Include="$(MSBuildThisFileDirectory)Assets\RemoteFonts\antikytheraoutlineital.woff" />
<Content Include="$(MSBuildThisFileDirectory)Assets\RemoteFonts\GALACTIC VANGUARDIAN NCV.woff" />
<Content Include="$(MSBuildThisFileDirectory)Assets\ResizedLargeWisteria.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\square100.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\testimage_exif_rotated.jpg" />
<Content Include="$(MSBuildThisFileDirectory)Assets\test_image_200_200.png" />
Expand Down
19 changes: 13 additions & 6 deletions src/Uno.UI.Composition/Composition/CompositionSurfaceBrush.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,20 @@ internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds)
{
if (TryGetSkiaCompositionSurface(Surface, out var scs))
{
var sourceImageSize = new Size(scs.Image!.Width, scs.Image.Height);
var backgroundArea = GetArrangedImageRect(sourceImageSize, bounds);
var matrix = Matrix3x2.CreateScale((float)(backgroundArea.Width / sourceImageSize.Width), (float)(backgroundArea.Height / sourceImageSize.Height));
matrix *= Matrix3x2.CreateTranslation((float)backgroundArea.Left, (float)backgroundArea.Top);
var backgroundArea = GetArrangedImageRect(new Size(scs.Image!.Width, scs.Image.Height), bounds);

// Adding image downscaling in the shader matrix directly is very blurry
// since the default downsampler in Skia is really low quality (but really fast).
// We force Lanczos instead.
// https://github.com/mono/SkiaSharp/issues/520#issuecomment-444973518
#pragma warning disable CS0612 // Type or member is obsolete
var resizedBitmap = scs.Image.ToSKBitmap().Resize(scs.Image.Info.WithSize((int)backgroundArea.Size.Width, (int)backgroundArea.Size.Height), SKBitmapResizeMethod.Lanczos3.ToFilterQuality());
#pragma warning restore CS0612 // Type or member is obsolete
var resizedImage = SKImage.FromBitmap(resizedBitmap);

var matrix = Matrix3x2.CreateTranslation((float)backgroundArea.Left, (float)backgroundArea.Top);
matrix *= TransformMatrix;

var imageShader = SKShader.CreateImage(scs.Image, SKShaderTileMode.Decal, SKShaderTileMode.Decal, matrix.ToSKMatrix());
var imageShader = SKShader.CreateImage(resizedImage, SKShaderTileMode.Decal, SKShaderTileMode.Decal, matrix.ToSKMatrix());

if (UsePaintColorToColorSurface)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media
{
[TestClass]
[RunsOnUIThread]
public class Given_ImageBrushStretch
public class Given_ImageBrush
{
[DataRow(Stretch.Fill, false)]
[DataRow(Stretch.Fill, true)]
Expand Down Expand Up @@ -100,10 +100,47 @@ public async Task When_Stretch(Stretch stretch, bool useRectangle)

var bitmap = await UITestHelper.ScreenShot(SUT);

ImageAssert.HasColorAt(bitmap, centerX, BorderOffset, expectations.Top, tolerance: 21);
ImageAssert.HasColorAt(bitmap, centerX, height - BorderOffset, expectations.Bottom, tolerance: 21);
ImageAssert.HasColorAt(bitmap, BorderOffset, centerY, expectations.Left, tolerance: 21);
ImageAssert.HasColorAt(bitmap, width - BorderOffset, centerY, expectations.Right, tolerance: 21);
ImageAssert.HasColorAt(bitmap, centerX, BorderOffset, expectations.Top, tolerance: 28);
ImageAssert.HasColorAt(bitmap, centerX, height - BorderOffset, expectations.Bottom, tolerance: 28);
ImageAssert.HasColorAt(bitmap, BorderOffset, centerY, expectations.Left, tolerance: 28);
ImageAssert.HasColorAt(bitmap, width - BorderOffset, centerY, expectations.Right, tolerance: 28);
}

#if __SKIA__
[TestMethod]
public async Task When_DownSampling()
{
var border = new Border
{
Width = 100,
Height = 100,
Background = new ImageBrush
{
ImageSource = new Uri("ms-appx:/Assets/LargeWisteria.jpg")
}
};

var image = new Image
{
Width = 100,
Height = 100,
Source = new Uri("ms-appx:/Assets/ResizedLargeWisteria.png")
};

await UITestHelper.Load(new StackPanel
{
Children =
{
border,
image
}
});
await Task.Delay(1000); // wait for idle might not be enough here

var bitmap = await UITestHelper.ScreenShot(border);
var bitmap2 = await UITestHelper.ScreenShot(image);
await ImageAssert.AreEqualAsync(bitmap, bitmap2);
}
#endif
}
}

0 comments on commit 81e55a4

Please sign in to comment.