2017-09-05 18:29:58 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2017-12-14 02:51:18 +03:00
|
|
|
|
using Xunit;
|
2017-07-17 02:46:12 +03:00
|
|
|
|
|
|
|
|
|
namespace SkiaSharp.Tests
|
|
|
|
|
{
|
|
|
|
|
public class SKPixmapTest : SKTest
|
|
|
|
|
{
|
2018-11-29 20:26:57 +03:00
|
|
|
|
[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));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 04:19:59 +03:00
|
|
|
|
[SkippableFact]
|
2017-07-17 02:46:12 +03:00
|
|
|
|
public void ReadPixelSucceeds()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(10, 10);
|
|
|
|
|
|
|
|
|
|
var ptr1 = Marshal.AllocCoTaskMem(info.BytesSize);
|
|
|
|
|
var pix1 = new SKPixmap(info, ptr1);
|
|
|
|
|
|
|
|
|
|
var ptr2 = Marshal.AllocCoTaskMem(info.BytesSize);
|
|
|
|
|
|
|
|
|
|
var result = pix1.ReadPixels(info, ptr2, info.RowBytes);
|
|
|
|
|
|
|
|
|
|
Assert.True(result);
|
|
|
|
|
}
|
2017-09-05 18:29:58 +03:00
|
|
|
|
|
2017-12-14 04:19:59 +03:00
|
|
|
|
[SkippableFact]
|
2017-09-05 18:29:58 +03:00
|
|
|
|
public void WithMethodsDoNotModifySource()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(100, 30, SKColorType.Rgb565, SKAlphaType.Unpremul);
|
|
|
|
|
var pixmap = new SKPixmap(info, (IntPtr)123);
|
|
|
|
|
|
2017-12-14 02:51:18 +03:00
|
|
|
|
Assert.Equal(SKColorType.Rgb565, pixmap.ColorType);
|
|
|
|
|
Assert.Equal((IntPtr)123, pixmap.GetPixels());
|
2017-09-05 18:29:58 +03:00
|
|
|
|
|
2018-07-12 14:32:02 +03:00
|
|
|
|
var copy = pixmap.WithColorType(SKColorType.Gray8);
|
2017-09-05 18:29:58 +03:00
|
|
|
|
|
2017-12-14 02:51:18 +03:00
|
|
|
|
Assert.Equal(SKColorType.Rgb565, pixmap.ColorType);
|
|
|
|
|
Assert.Equal((IntPtr)123, pixmap.GetPixels());
|
2018-07-12 14:32:02 +03:00
|
|
|
|
Assert.Equal(SKColorType.Gray8, copy.ColorType);
|
2017-12-14 02:51:18 +03:00
|
|
|
|
Assert.Equal((IntPtr)123, copy.GetPixels());
|
2017-09-05 18:29:58 +03:00
|
|
|
|
}
|
2017-07-17 02:46:12 +03:00
|
|
|
|
|
2017-12-14 04:19:59 +03:00
|
|
|
|
[SkippableFact]
|
2017-07-17 02:46:12 +03:00
|
|
|
|
public void ReadPixelCopiesData()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(10, 10);
|
|
|
|
|
|
|
|
|
|
using (var bmp1 = new SKBitmap(info))
|
|
|
|
|
using (var pix1 = bmp1.PeekPixels())
|
|
|
|
|
using (var bmp2 = new SKBitmap(info))
|
|
|
|
|
using (var pix2 = bmp2.PeekPixels())
|
|
|
|
|
{
|
|
|
|
|
bmp1.Erase(SKColors.Blue);
|
|
|
|
|
bmp1.Erase(SKColors.Green);
|
|
|
|
|
|
2017-12-14 02:51:18 +03:00
|
|
|
|
Assert.NotEqual(Marshal.ReadInt64(pix1.GetPixels()), Marshal.ReadInt64(pix2.GetPixels()));
|
2017-07-17 02:46:12 +03:00
|
|
|
|
|
|
|
|
|
var result = pix1.ReadPixels(pix2);
|
|
|
|
|
|
|
|
|
|
Assert.True(result);
|
|
|
|
|
|
2017-12-14 02:51:18 +03:00
|
|
|
|
Assert.Equal(Marshal.ReadInt64(pix1.GetPixels()), Marshal.ReadInt64(pix2.GetPixels()));
|
2017-07-17 02:46:12 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-24 23:42:29 +03:00
|
|
|
|
|
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void SwizzleSwapsRedAndBlue()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(10, 10);
|
|
|
|
|
|
|
|
|
|
using (var bmp = new SKBitmap(info))
|
|
|
|
|
{
|
|
|
|
|
bmp.Erase(SKColors.Red);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKColors.Red, bmp.Pixels[0]);
|
|
|
|
|
|
|
|
|
|
SKSwizzle.SwapRedBlue(bmp.GetPixels(out var length), info.Width * info.Height);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKColors.Blue, bmp.Pixels[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-25 02:41:33 +03:00
|
|
|
|
|
2020-02-01 21:58:44 +03:00
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void EraseWithColor()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(1, 1);
|
|
|
|
|
|
|
|
|
|
using var bmp = new SKBitmap(info);
|
|
|
|
|
using var pixmap = bmp.PeekPixels();
|
|
|
|
|
|
|
|
|
|
pixmap.Erase(SKColors.Red);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKColors.Red, pixmap.GetPixelColor(0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void EraseWithColorF()
|
|
|
|
|
{
|
|
|
|
|
var info = new SKImageInfo(1, 1);
|
|
|
|
|
|
|
|
|
|
using var bmp = new SKBitmap(info);
|
|
|
|
|
using var pixmap = bmp.PeekPixels();
|
|
|
|
|
|
|
|
|
|
pixmap.Erase(new SKColorF(1, 0, 0));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKColors.Red, pixmap.GetPixelColor(0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 02:41:33 +03:00
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void EncodeWithPngEncoder()
|
|
|
|
|
{
|
|
|
|
|
var bitmap = CreateTestBitmap();
|
|
|
|
|
var pixmap = bitmap.PeekPixels();
|
|
|
|
|
|
|
|
|
|
var data = pixmap.Encode(SKPngEncoderOptions.Default);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(data);
|
|
|
|
|
|
|
|
|
|
var codec = SKCodec.Create(data);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKEncodedImageFormat.Png, codec.EncodedFormat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void EncodeWithJpegEncoder()
|
|
|
|
|
{
|
|
|
|
|
var bitmap = CreateTestBitmap();
|
|
|
|
|
var pixmap = bitmap.PeekPixels();
|
|
|
|
|
|
|
|
|
|
var data = pixmap.Encode(SKJpegEncoderOptions.Default);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(data);
|
|
|
|
|
|
|
|
|
|
var codec = SKCodec.Create(data);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKEncodedImageFormat.Jpeg, codec.EncodedFormat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SkippableFact]
|
|
|
|
|
public void EncodeWithWebpEncoder()
|
|
|
|
|
{
|
|
|
|
|
var bitmap = CreateTestBitmap();
|
|
|
|
|
var pixmap = bitmap.PeekPixels();
|
|
|
|
|
|
|
|
|
|
var data = pixmap.Encode(SKWebpEncoderOptions.Default);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(data);
|
|
|
|
|
|
|
|
|
|
var codec = SKCodec.Create(data);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SKEncodedImageFormat.Webp, codec.EncodedFormat);
|
|
|
|
|
}
|
2017-07-17 02:46:12 +03:00
|
|
|
|
}
|
|
|
|
|
}
|