<commit_before>﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;

namespace SkiaSharp.Tests
{
	public class SKImageTest : SKTest
	{
		[SkippableFact]
		public void TestLazyImage()
		{
			var data = SKData.Create(Path.Combine(PathToImages, "baboon.jpg"));
			Assert.NotNull(data);

			var image = SKImage.FromEncodedData(data);
			Assert.NotNull(image);

			Assert.True(image.IsLazyGenerated);
		}

		[SkippableFact]
		public void TestNotLazyImage()
		{
			var bitmap = CreateTestBitmap();
			Assert.NotNull(bitmap);

			var image = SKImage.FromBitmap(bitmap);
			Assert.NotNull(image);

			Assert.False(image.IsLazyGenerated);
		}

		[SkippableFact]
		public void DecodingJpegImagePreservesColorSpace()
		{
			var path = Path.Combine(PathToImages, "baboon.jpg");

			var image = SKImage.FromEncodedData(path);

			Assert.NotNull(image.ColorSpace);
		}

		[SkippableFact]
		public void DecodingPngImagePreservesColorSpace()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			var image = SKImage.FromEncodedData(path);

			Assert.NotNull(image.ColorSpace);
		}

		[SkippableFact]
		public void TestFromPixelCopyIntPtr()
		{
			using (var bmp = CreateTestBitmap())
			using (var image = SKImage.FromPixelCopy(bmp.Info, bmp.GetPixels(out var length)))
			{
				ValidateTestPixmap(image.PeekPixels());
			}
		}

		[SkippableFact]
		public void TestFromPixelCopyByteArray()
		{
			using (var bmp = CreateTestBitmap())
			{
				var px = bmp.GetPixels(out var length);
				var dst = new byte[(int)length];
				Marshal.Copy(px, dst, 0, (int)length);
				using (var image = SKImage.FromPixelCopy(bmp.Info, dst))
				{
					ValidateTestPixmap(image.PeekPixels());
				}
			}
		}

		[SkippableFact]
		public void TestFromPixelCopyStream()
		{
			using (var bmp = CreateTestBitmap())
			{
				var px = bmp.GetPixels(out var length);
				var dst = new byte[(int)length];
				Marshal.Copy(px, dst, 0, (int)length);
				using (var stream = new MemoryStream(dst))
				using (var image = SKImage.FromPixelCopy(bmp.Info, stream))
				{
					ValidateTestPixmap(image.PeekPixels());
				}
			}
		}

		[SkippableFact]
		public void SupportsNonASCIICharactersInPath()
		{
			var fileName = Path.Combine(PathToImages, "上田雅美.jpg");

			using (var image = SKImage.FromEncodedData(fileName))
			{
				Assert.NotNull(image);
			}
		}

		[SkippableFact]
		public void TestImageManagedBytesDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			var managed = File.ReadAllBytes(path);
			using (var image = SKImage.FromEncodedData(managed))
			using (var surface = SKSurface.Create(new SKImageInfo(200, 200)))
			{
				var canvas = surface.Canvas;
				canvas.Clear(SKColors.White);
				canvas.DrawImage(image, 0, 0);

				using (var snap = surface.Snapshot())
				using (var bmp = SKBitmap.FromImage(snap))
				{
					Assert.Equal(new SKColor(2, 255, 42), bmp.GetPixel(20, 20));
					Assert.Equal(new SKColor(1, 83, 255), bmp.GetPixel(108, 20));
					Assert.Equal(new SKColor(255, 166, 1), bmp.GetPixel(20, 108));
					Assert.Equal(new SKColor(255, 1, 214), bmp.GetPixel(108, 108));
				}
			}
		}

		[SkippableFact]
		public void TestImageManagedStreamDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			using (var managed = new FileStream(path, FileMode.Open))
			using (var image = SKImage.FromEncodedData(managed))
			using (var surface = SKSurface.Create(new SKImageInfo(200, 200)))
			{
				var canvas = surface.Canvas;
				canvas.Clear(SKColors.White);
				canvas.DrawImage(image, 0, 0);

				using (var snap = surface.Snapshot())
				using (var bmp = SKBitmap.FromImage(snap))
				{
					Assert.Equal(new SKColor(2, 255, 42), bmp.GetPixel(20, 20));
					Assert.Equal(new SKColor(1, 83, 255), bmp.GetPixel(108, 20));
					Assert.Equal(new SKColor(255, 166, 1), bmp.GetPixel(20, 108));
					Assert.Equal(new SKColor(255, 1, 214), bmp.GetPixel(108, 108));
				}
			}
		}

		[SkippableFact]
		public void TestImageFileDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			using (var image = SKImage.FromEncodedData(path))<commit_msg>Make sure not to lock the files<commit_after>﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;

namespace SkiaSharp.Tests
{
	public class SKImageTest : SKTest
	{
		[SkippableFact]
		public void TestLazyImage()
		{
			var data = SKData.Create(Path.Combine(PathToImages, "baboon.jpg"));
			Assert.NotNull(data);

			var image = SKImage.FromEncodedData(data);
			Assert.NotNull(image);

			Assert.True(image.IsLazyGenerated);
		}

		[SkippableFact]
		public void TestNotLazyImage()
		{
			var bitmap = CreateTestBitmap();
			Assert.NotNull(bitmap);

			var image = SKImage.FromBitmap(bitmap);
			Assert.NotNull(image);

			Assert.False(image.IsLazyGenerated);
		}

		[SkippableFact]
		public void DecodingJpegImagePreservesColorSpace()
		{
			var path = Path.Combine(PathToImages, "baboon.jpg");

			var image = SKImage.FromEncodedData(path);

			Assert.NotNull(image.ColorSpace);
		}

		[SkippableFact]
		public void DecodingPngImagePreservesColorSpace()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			var image = SKImage.FromEncodedData(path);

			Assert.NotNull(image.ColorSpace);
		}

		[SkippableFact]
		public void TestFromPixelCopyIntPtr()
		{
			using (var bmp = CreateTestBitmap())
			using (var image = SKImage.FromPixelCopy(bmp.Info, bmp.GetPixels(out var length)))
			{
				ValidateTestPixmap(image.PeekPixels());
			}
		}

		[SkippableFact]
		public void TestFromPixelCopyByteArray()
		{
			using (var bmp = CreateTestBitmap())
			{
				var px = bmp.GetPixels(out var length);
				var dst = new byte[(int)length];
				Marshal.Copy(px, dst, 0, (int)length);
				using (var image = SKImage.FromPixelCopy(bmp.Info, dst))
				{
					ValidateTestPixmap(image.PeekPixels());
				}
			}
		}

		[SkippableFact]
		public void TestFromPixelCopyStream()
		{
			using (var bmp = CreateTestBitmap())
			{
				var px = bmp.GetPixels(out var length);
				var dst = new byte[(int)length];
				Marshal.Copy(px, dst, 0, (int)length);
				using (var stream = new MemoryStream(dst))
				using (var image = SKImage.FromPixelCopy(bmp.Info, stream))
				{
					ValidateTestPixmap(image.PeekPixels());
				}
			}
		}

		[SkippableFact]
		public void SupportsNonASCIICharactersInPath()
		{
			var fileName = Path.Combine(PathToImages, "上田雅美.jpg");

			using (var image = SKImage.FromEncodedData(fileName))
			{
				Assert.NotNull(image);
			}
		}

		[SkippableFact]
		public void TestImageManagedBytesDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			var managed = File.ReadAllBytes(path);
			using (var image = SKImage.FromEncodedData(managed))
			using (var surface = SKSurface.Create(new SKImageInfo(200, 200)))
			{
				var canvas = surface.Canvas;
				canvas.Clear(SKColors.White);
				canvas.DrawImage(image, 0, 0);

				using (var snap = surface.Snapshot())
				using (var bmp = SKBitmap.FromImage(snap))
				{
					Assert.Equal(new SKColor(2, 255, 42), bmp.GetPixel(20, 20));
					Assert.Equal(new SKColor(1, 83, 255), bmp.GetPixel(108, 20));
					Assert.Equal(new SKColor(255, 166, 1), bmp.GetPixel(20, 108));
					Assert.Equal(new SKColor(255, 1, 214), bmp.GetPixel(108, 108));
				}
			}
		}

		[SkippableFact]
		public void TestImageManagedStreamDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			using (var managed = new FileStream(path, FileMode.Open, FileAccess.Read))
			using (var image = SKImage.FromEncodedData(managed))
			using (var surface = SKSurface.Create(new SKImageInfo(200, 200)))
			{
				var canvas = surface.Canvas;
				canvas.Clear(SKColors.White);
				canvas.DrawImage(image, 0, 0);

				using (var snap = surface.Snapshot())
				using (var bmp = SKBitmap.FromImage(snap))
				{
					Assert.Equal(new SKColor(2, 255, 42), bmp.GetPixel(20, 20));
					Assert.Equal(new SKColor(1, 83, 255), bmp.GetPixel(108, 20));
					Assert.Equal(new SKColor(255, 166, 1), bmp.GetPixel(20, 108));
					Assert.Equal(new SKColor(255, 1, 214), bmp.GetPixel(108, 108));
				}
			}
		}

		[SkippableFact]
		public void TestImageFileDecodeDrawsCorrectly()
		{
			var path = Path.Combine(PathToImages, "color-wheel.png");

			using (var image = SKImage.FromEncodedData(path))
{'fraction_non_alphanumeric': 0.13128058240840196, 'fraction_numerical': 0.026256116481680393, 'mean_word_length': 3.796794504865484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6481693', 'n_tokens_mistral': 3636, 'n_tokens_neox': 2901, 'n_words': 582}