using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using ComputeSharp.Resources;
using Microsoft.Toolkit.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace ComputeSharp.Tests.Extensions;
///
/// A helper class for loading images into resources.
///
public static class ImagingExtensions
{
///
/// Creates a new instance with the specified texture data.
///
/// The input pixel format used in the texture.
/// The target pixel format for the returned image.
/// The source instance to read data from.
/// An image with the data from the input texture.
[Pure]
public static unsafe Image ToImage(this Texture2D texture)
where TFrom : unmanaged
where TTo : unmanaged, IPixel
{
Guard.IsEqualTo(sizeof(TTo), sizeof(TFrom), nameof(TTo));
Image image = new(texture.Width, texture.Height);
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span));
Span pixels = MemoryMarshal.Cast(span);
texture.CopyTo(pixels);
return image;
}
}