using System.Buffers.Binary;
using Febe.Core.Codecs.Images;
namespace Febe.Tests.Imaging;
/// WebP files put together by the tests from RIFF chunks (RFC 9649 §2), to reach what the reference set lacks.
internal static class WebPTestFiles
{
/// The chunks of a WebP file: FourCC and payload, in order.
public static List<(string Type, byte[] Payload)> Chunks(byte[] file)
{
var chunks = new List<(string, byte[])>();
var end = 8 + (int)BinaryPrimitives.ReadUInt32LittleEndian(file.AsSpan(4));
var pos = 12;
while (pos + 8 <= end)
{
var size = (int)BinaryPrimitives.ReadUInt32LittleEndian(file.AsSpan(pos + 4));
chunks.Add((System.Text.Encoding.ASCII.GetString(file, pos, 4), file.AsSpan(pos + 8, size).ToArray()));
pos += 8 + size + (size & 1);
}
return chunks;
}
/// The payload of the first chunk of that type.
public static byte[] Payload(byte[] file, string type) => Chunks(file).First(c => c.Type == type).Payload;
/// A RIFF/WEBP file of the given chunks (padded to even sizes).
public static byte[] Riff(params (string Type, byte[] Payload)[] chunks)
{
using var body = new MemoryStream();
body.Write("WEBP"u8);
foreach (var (type, payload) in chunks)
{
body.Write(Chunk(type, payload));
}
var file = new byte[8 + body.Length];
"RIFF"u8.CopyTo(file);
BinaryPrimitives.WriteUInt32LittleEndian(file.AsSpan(4), (uint)body.Length);
body.ToArray().CopyTo(file, 8);
return file;
}
public static byte[] Chunk(string type, byte[] payload)
{
var chunk = new byte[8 + payload.Length + (payload.Length & 1)];
System.Text.Encoding.ASCII.GetBytes(type).CopyTo(chunk, 0);
BinaryPrimitives.WriteUInt32LittleEndian(chunk.AsSpan(4), (uint)payload.Length);
payload.CopyTo(chunk, 8);
return chunk;
}
/// A VP8X payload: the flags byte and the canvas size.
public static byte[] Vp8X(int flags, int width, int height)
{
var payload = new byte[10];
payload[0] = (byte)flags;
Uint24(payload.AsSpan(4), width - 1);
Uint24(payload.AsSpan(7), height - 1);
return payload;
}
/// An ANIM payload: background colour (B, G, R, A) and loop count.
public static byte[] Anim(uint backgroundBgra = 0, int loops = 0)
{
var payload = new byte[6];
BinaryPrimitives.WriteUInt32LittleEndian(payload, backgroundBgra);
BinaryPrimitives.WriteUInt16LittleEndian(payload.AsSpan(4), (ushort)loops);
return payload;
}
/// An ANMF payload: offset (even), size, duration, flags (2 = do not blend, 1 = dispose) and frame chunks.
public static byte[] Anmf(int x, int y, int width, int height, int flags, params (string Type, byte[] Payload)[] chunks)
{
using var payload = new MemoryStream();
var header = new byte[16];
Uint24(header, x / 2);
Uint24(header.AsSpan(3), y / 2);
Uint24(header.AsSpan(6), width - 1);
Uint24(header.AsSpan(9), height - 1);
Uint24(header.AsSpan(12), 100);
header[15] = (byte)flags;
payload.Write(header);
foreach (var (type, data) in chunks)
{
payload.Write(Chunk(type, data));
}
return payload.ToArray();
}
///
/// The forward alpha filter of RFC 9649 §2.7.1.2 (1 horizontal, 2 vertical, 3 gradient), written from the
/// predictor definitions, independent of the decoder's inverse.
///
public static byte[] FilterAlpha(ReadOnlySpan alpha, int width, int height, int filter)
{
var output = new byte[alpha.Length];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
int predictor;
if (filter == 0 || (x == 0 && y == 0))
{
predictor = 0;
}
else if (y == 0)
{
predictor = alpha[x - 1];
}
else if (x == 0)
{
predictor = alpha[(y - 1) * width];
}
else
{
int left = alpha[(y * width) + x - 1], top = alpha[((y - 1) * width) + x], topLeft = alpha[((y - 1) * width) + x - 1];
predictor = filter switch
{
1 => left,
2 => top,
_ => Math.Clamp(left + top - topLeft, 0, 255),
};
}
output[(y * width) + x] = (byte)(alpha[(y * width) + x] - predictor);
}
}
return output;
}
/// The alpha bytes of an image.
public static byte[] AlphaOf(RgbaImage image)
{
var alpha = new byte[image.Width * image.Height];
for (var i = 0; i < alpha.Length; i++)
{
alpha[i] = image.Pixels[(i * 4) + 3];
}
return alpha;
}
/// A little-endian TIFF with an IFD0 holding only the Orientation tag.
public static byte[] Tiff(int orientation)
{
var tiff = new byte[8 + 2 + 12 + 4];
"II*\0"u8.CopyTo(tiff);
tiff[4] = 8;
tiff[8] = 1;
tiff[10] = 0x12;
tiff[11] = 0x01;
tiff[12] = 3;
tiff[14] = 1;
tiff[18] = (byte)orientation;
return tiff;
}
/// An image with only distinct colours (some transparent), from a fixed seed.
public static RgbaImage FewColors(int width, int height, int colors, int seed)
{
var random = new Random(seed);
var palette = new byte[colors * 4];
random.NextBytes(palette);
var image = new RgbaImage(width, height);
var pixels = image.Pixels;
for (var i = 0; i < width * height; i++)
{
var c = (((i % width) / 3) + ((i / width) / 2) + random.Next(2)) % colors;
palette.AsSpan(c * 4, 4).CopyTo(pixels[(i * 4)..]);
}
return image;
}
private static void Uint24(Span destination, int value)
{
destination[0] = (byte)value;
destination[1] = (byte)(value >> 8);
destination[2] = (byte)(value >> 16);
}
}