Fixing the bitmap resize/scale methods

- consistency
    - `Resize` is for methods that create new objects
    - `ScalePixels` is for mthods that COPY pixels INTO a provided object
 - uniformity
    - all 3 imageing types have pixel scaling capabilities
 - improved obsolete desriptions
 - added new methods instead of just obsoleting
 - added more tests
This commit is contained in:
Matthew Leibowitz 2018-11-29 19:26:57 +02:00
Родитель e8bda14614
Коммит 0cf1c6d293
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 3650EBE4AA155AF9
4 изменённых файлов: 137 добавлений и 13 удалений

Просмотреть файл

@ -744,12 +744,22 @@ namespace SkiaSharp
return SkiaApi.sk_bitmap_peek_pixels (Handle, pixmap.Handle);
}
[Obsolete]
public SKBitmap Resize (SKImageInfo info, SKBitmapResizeMethod method)
[Obsolete ("Use Resize(SKImageInfo, SKFilterQuality) instead.")]
public SKBitmap Resize (SKImageInfo info, SKBitmapResizeMethod method) =>
Resize (info, method.ToFilterQuality ());
[Obsolete ("Use ScalePixels(SKBitmap, SKFilterQuality) instead.")]
public bool Resize (SKBitmap dst, SKBitmapResizeMethod method) =>
ScalePixels (dst, method.ToFilterQuality ());
[Obsolete ("Use ScalePixels(SKBitmap, SKFilterQuality) instead.")]
public static bool Resize (SKBitmap dst, SKBitmap src, SKBitmapResizeMethod method) =>
src.ScalePixels (dst, method.ToFilterQuality ());
public SKBitmap Resize (SKImageInfo info, SKFilterQuality quality)
{
var dst = new SKBitmap (info);
var result = Resize (dst, this, method);
if (result) {
if (ScalePixels (dst, quality)) {
return dst;
} else {
dst.Dispose ();
@ -757,18 +767,25 @@ namespace SkiaSharp
}
}
[Obsolete]
public bool Resize (SKBitmap dst, SKBitmapResizeMethod method)
public bool ScalePixels (SKBitmap destination, SKFilterQuality quality)
{
return Resize (dst, this, method);
if (destination == null) {
throw new ArgumentNullException (nameof (destination));
}
using (var dstPix = destination.PeekPixels ()) {
return ScalePixels (dstPix, quality);
}
}
[Obsolete]
public static bool Resize (SKBitmap dst, SKBitmap src, SKBitmapResizeMethod method)
public bool ScalePixels (SKPixmap destination, SKFilterQuality quality)
{
using (var srcPix = src.PeekPixels ())
using (var dstPix = dst.PeekPixels ()) {
return SKPixmap.Resize (dstPix, srcPix, method);// && dst.InstallPixels (dstPix);
if (destination == null) {
throw new ArgumentNullException (nameof (destination));
}
using (var srcPix = PeekPixels ()) {
return srcPix.ScalePixels (destination, quality);
}
}

Просмотреть файл

@ -267,7 +267,7 @@ namespace SkiaSharp.Tests
[SkippableFact]
[Obsolete]
public void BitmapResizes()
public void BitmapResizesObsolete()
{
var srcInfo = new SKImageInfo(200, 200);
var dstInfo = new SKImageInfo(100, 100);
@ -290,6 +290,55 @@ namespace SkiaSharp.Tests
Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
}
[SkippableFact]
public void BitmapResizes()
{
var srcInfo = new SKImageInfo(200, 200);
var dstInfo = new SKImageInfo(100, 100);
var srcBmp = new SKBitmap(srcInfo);
using (var canvas = new SKCanvas(srcBmp))
using (var paint = new SKPaint { Color = SKColors.Green }) {
canvas.Clear(SKColors.Blue);
canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
}
Assert.Equal(SKColors.Green, srcBmp.GetPixel(75, 75));
Assert.Equal(SKColors.Blue, srcBmp.GetPixel(175, 175));
var dstBmp = srcBmp.Resize(dstInfo, SKFilterQuality.High);
Assert.NotNull(dstBmp);
Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
}
[SkippableFact]
public void CanScalePixels()
{
var srcInfo = new SKImageInfo(200, 200);
var dstInfo = new SKImageInfo(100, 100);
var srcBmp = new SKBitmap(srcInfo);
var dstBmp = new SKBitmap(dstInfo);
using (var canvas = new SKCanvas(srcBmp))
using (var paint = new SKPaint { Color = SKColors.Green })
{
canvas.Clear(SKColors.Blue);
canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
}
Assert.Equal(SKColors.Green, srcBmp.GetPixel(75, 75));
Assert.Equal(SKColors.Blue, srcBmp.GetPixel(175, 175));
Assert.True(srcBmp.ScalePixels(dstBmp, SKFilterQuality.High));
Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
}
[SkippableFact]
public void AlphaMaskIsApplied()
{
@ -339,6 +388,7 @@ namespace SkiaSharp.Tests
}
[SkippableFact(Skip = "This test takes a long time (~3mins), so ignore this most of the time.")]
[Obsolete]
public static void ImageScalingMultipleThreadsTest()
{
const int numThreads = 100;
@ -365,6 +415,7 @@ namespace SkiaSharp.Tests
Console.WriteLine($"Test completed for {numThreads} tasks, {numIterationsPerThread} each.");
}
[Obsolete]
private static byte[] ComputeThumbnail(string fileName)
{
using (var ms = new MemoryStream())

Просмотреть файл

@ -263,6 +263,34 @@ namespace SkiaSharp.Tests
}
}
[SkippableFact]
public void CanScalePixels()
{
var srcInfo = new SKImageInfo(200, 200);
var dstInfo = new SKImageInfo(100, 100);
var srcSurface = SKSurface.Create(srcInfo);
var dstBmp = new SKBitmap(dstInfo);
using (var paint = new SKPaint { Color = SKColors.Green })
{
srcSurface.Canvas.Clear(SKColors.Blue);
srcSurface.Canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
}
var srcImage = srcSurface.Snapshot();
var srcPix = srcImage.PeekPixels();
var dstPix = dstBmp.PeekPixels();
Assert.Equal(SKColors.Green, srcPix.GetPixelColor(75, 75));
Assert.Equal(SKColors.Blue, srcPix.GetPixelColor(175, 175));
Assert.True(srcImage.ScalePixels(dstPix, SKFilterQuality.High));
Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
}
[Obsolete]
[SkippableFact]
public void EncodeWithSimpleSerializer()

Просмотреть файл

@ -6,6 +6,34 @@ namespace SkiaSharp.Tests
{
public class SKPixmapTest : SKTest
{
[SkippableFact]
public void CanScalePixels()
{
var srcInfo = new SKImageInfo(200, 200);
var dstInfo = new SKImageInfo(100, 100);
var srcBmp = new SKBitmap(srcInfo);
var dstBmp = new SKBitmap(dstInfo);
using (var canvas = new SKCanvas(srcBmp))
using (var paint = new SKPaint { Color = SKColors.Green })
{
canvas.Clear(SKColors.Blue);
canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
}
Assert.Equal(SKColors.Green, srcBmp.GetPixel(75, 75));
Assert.Equal(SKColors.Blue, srcBmp.GetPixel(175, 175));
var srcPix = srcBmp.PeekPixels();
var dstPix = dstBmp.PeekPixels();
Assert.True(srcPix.ScalePixels(dstPix, SKFilterQuality.High));
Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
}
[SkippableFact]
public void ReadPixelSucceeds()
{