2016-06-17 23:54:08 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-01-21 03:38:48 +03:00
|
|
|
|
using Xunit;
|
2016-06-17 23:54:08 +03:00
|
|
|
|
|
|
|
|
|
namespace SkiaSharp.Tests
|
|
|
|
|
{
|
|
|
|
|
public class SKCodecTest : SKTest
|
|
|
|
|
{
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-06-17 23:54:08 +03:00
|
|
|
|
public void MinBufferedBytesNeededHasAValue ()
|
|
|
|
|
{
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.True (SKCodec.MinBufferedBytesNeeded > 0);
|
2016-06-17 23:54:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-06-17 23:54:08 +03:00
|
|
|
|
public void CanCreateStreamCodec ()
|
|
|
|
|
{
|
|
|
|
|
var stream = new SKFileStream (Path.Combine (PathToImages, "color-wheel.png"));
|
|
|
|
|
using (var codec = SKCodec.Create (stream)) {
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (SKEncodedFormat.Png, codec.EncodedFormat);
|
|
|
|
|
Assert.Equal (128, codec.Info.Width);
|
|
|
|
|
Assert.Equal (128, codec.Info.Height);
|
|
|
|
|
Assert.Equal (SKAlphaType.Unpremul, codec.Info.AlphaType);
|
2017-01-25 01:11:41 +03:00
|
|
|
|
Assert.Equal (SKImageInfo.PlatformColorType, codec.Info.ColorType);
|
2016-06-17 23:54:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-26 09:24:25 +03:00
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-11-26 09:24:25 +03:00
|
|
|
|
public void GetGifFrames ()
|
|
|
|
|
{
|
|
|
|
|
const int FrameCount = 16;
|
|
|
|
|
|
|
|
|
|
var stream = new SKFileStream (Path.Combine (PathToImages, "animated-heart.gif"));
|
|
|
|
|
using (var codec = SKCodec.Create (stream)) {
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (-1, codec.RepetitionCount);
|
2016-11-26 09:24:25 +03:00
|
|
|
|
|
|
|
|
|
var frameInfos = codec.FrameInfo;
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (FrameCount, frameInfos.Length);
|
2016-11-26 09:24:25 +03:00
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (-1, frameInfos [0].RequiredFrame);
|
2016-11-26 09:24:25 +03:00
|
|
|
|
|
|
|
|
|
var cachedFrames = new SKBitmap [FrameCount];
|
|
|
|
|
var info = new SKImageInfo (codec.Info.Width, codec.Info.Height);
|
|
|
|
|
|
|
|
|
|
var decode = new Action<SKBitmap, bool, int> ((bm, cached, index) => {
|
|
|
|
|
if (cached) {
|
|
|
|
|
var requiredFrame = frameInfos [index].RequiredFrame;
|
|
|
|
|
if (requiredFrame != -1) {
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.True (cachedFrames [requiredFrame].CopyTo (bm));
|
2016-11-26 09:24:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var opts = new SKCodecOptions (index, cached);
|
|
|
|
|
var result = codec.GetPixels (info, bm.GetPixels (), opts);
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (SKCodecResult.Success, result);
|
2016-11-26 09:24:25 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < FrameCount; i++) {
|
|
|
|
|
var cachedFrame = cachedFrames [i] = new SKBitmap (info);
|
|
|
|
|
decode (cachedFrame, true, i);
|
|
|
|
|
|
|
|
|
|
var uncachedFrame = new SKBitmap (info);
|
|
|
|
|
decode (uncachedFrame, false, i);
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (cachedFrame.Bytes, uncachedFrame.Bytes);
|
2016-11-26 09:24:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-17 23:54:08 +03:00
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-11-25 11:55:59 +03:00
|
|
|
|
public void GetEncodedInfo ()
|
|
|
|
|
{
|
|
|
|
|
var stream = new SKFileStream (Path.Combine (PathToImages, "color-wheel.png"));
|
|
|
|
|
using (var codec = SKCodec.Create (stream)) {
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (SKEncodedInfoColor.Rgba, codec.EncodedInfo.Color);
|
|
|
|
|
Assert.Equal (SKEncodedInfoAlpha.Unpremul, codec.EncodedInfo.Alpha);
|
|
|
|
|
Assert.Equal (8, codec.EncodedInfo.BitsPerComponent);
|
2016-11-25 11:55:59 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-06-17 23:54:08 +03:00
|
|
|
|
public void CanGetPixels ()
|
|
|
|
|
{
|
|
|
|
|
var stream = new SKFileStream (Path.Combine (PathToImages, "baboon.png"));
|
|
|
|
|
using (var codec = SKCodec.Create (stream)) {
|
|
|
|
|
var pixels = codec.Pixels;
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (codec.Info.BytesSize, pixels.Length);
|
2016-06-17 23:54:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-11-17 05:35:51 +03:00
|
|
|
|
public void DecodePartialImage ()
|
|
|
|
|
{
|
|
|
|
|
// read the data here, so we can fake a throttle/download
|
|
|
|
|
var path = Path.Combine (PathToImages, "baboon.png");
|
|
|
|
|
var fileData = File.ReadAllBytes (path);
|
|
|
|
|
SKColor[] correctBytes;
|
|
|
|
|
using (var bitmap = SKBitmap.Decode (path)) {
|
|
|
|
|
correctBytes = bitmap.Pixels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int maxCount = 1024 * 4;
|
|
|
|
|
|
|
|
|
|
// the "download" stream needs some data
|
|
|
|
|
var downloadStream = new MemoryStream ();
|
|
|
|
|
downloadStream.Write (fileData, offset, maxCount);
|
|
|
|
|
downloadStream.Position -= maxCount;
|
|
|
|
|
offset += maxCount;
|
|
|
|
|
|
|
|
|
|
using (var codec = SKCodec.Create (new SKManagedStream (downloadStream))) {
|
|
|
|
|
var info = new SKImageInfo (codec.Info.Width, codec.Info.Height);
|
|
|
|
|
|
|
|
|
|
// the bitmap to be decoded
|
|
|
|
|
using (var incremental = new SKBitmap (info)) {
|
|
|
|
|
|
|
|
|
|
// start decoding
|
|
|
|
|
IntPtr length;
|
|
|
|
|
var pixels = incremental.GetPixels (out length);
|
|
|
|
|
var result = codec.StartIncrementalDecode (info, pixels, info.RowBytes);
|
|
|
|
|
|
|
|
|
|
// make sure the start was successful
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (SKCodecResult.Success, result);
|
2016-11-17 05:35:51 +03:00
|
|
|
|
result = SKCodecResult.IncompleteInput;
|
|
|
|
|
|
|
|
|
|
while (result == SKCodecResult.IncompleteInput) {
|
|
|
|
|
// decode the rest
|
|
|
|
|
int rowsDecoded = 0;
|
|
|
|
|
result = codec.IncrementalDecode (out rowsDecoded);
|
|
|
|
|
|
|
|
|
|
// write some more data to the stream
|
|
|
|
|
maxCount = Math.Min (maxCount, fileData.Length - offset);
|
|
|
|
|
downloadStream.Write (fileData, offset, maxCount);
|
|
|
|
|
downloadStream.Position -= maxCount;
|
|
|
|
|
offset += maxCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compare to original
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (correctBytes, incremental.Pixels);
|
2016-11-17 05:35:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-06-17 23:54:08 +03:00
|
|
|
|
public void BitmapDecodesCorrectly ()
|
|
|
|
|
{
|
|
|
|
|
byte[] codecPixels;
|
|
|
|
|
byte[] bitmapPixels;
|
|
|
|
|
|
|
|
|
|
using (var codec = SKCodec.Create (new SKFileStream (Path.Combine (PathToImages, "baboon.png")))) {
|
|
|
|
|
codecPixels = codec.Pixels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var bitmap = SKBitmap.Decode (Path.Combine (PathToImages, "baboon.png"))) {
|
|
|
|
|
bitmapPixels = bitmap.Bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (codecPixels, bitmapPixels);
|
2016-06-17 23:54:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
[Fact]
|
2016-06-17 23:54:08 +03:00
|
|
|
|
public void BitmapDecodesCorrectlyWithManagedStream ()
|
|
|
|
|
{
|
|
|
|
|
byte[] codecPixels;
|
|
|
|
|
byte[] bitmapPixels;
|
|
|
|
|
|
|
|
|
|
var stream = File.OpenRead (Path.Combine(PathToImages, "baboon.png"));
|
|
|
|
|
using (var codec = SKCodec.Create (new SKManagedStream (stream))) {
|
|
|
|
|
codecPixels = codec.Pixels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var bitmap = SKBitmap.Decode (Path.Combine (PathToImages, "baboon.png"))) {
|
|
|
|
|
bitmapPixels = bitmap.Bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 03:38:48 +03:00
|
|
|
|
Assert.Equal (codecPixels, bitmapPixels);
|
2016-06-17 23:54:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|