Files
Febe/tests/Febe.Tests/Imaging/WebPTestFiles.cs
Alby96andClaude Opus 5.5 b4383a6110 Lettore e scrittore WebP di casa (C1, passo 4)
Il lettore WebP, scritto dalla RFC 9649 e dalla RFC 6386, prende il posto di
SkiaSharp per i WebP: VP8 con perdita, VP8L senza perdita, ALPH grezzo e
compresso con ogni filtro, VP8X con ICCP, EXIF e XMP, e di un'animazione il
primo fotogramma fuso sulla tela, con il numero dei fotogrammi. La facciata
ImageDecoder ora legge i WebP.

Contro i riferimenti di SkiaSharp i quattro WebP senza perdita coincidono byte
per byte; i quattro con perdita stanno fra 68,7 e 74,4 dB per canale, scarto
massimo 1, alfa esatta. File danneggiati, tagliati o fuori dai limiti si
rifiutano; i limiti si controllano prima di allocare.

Lo scrittore fa WebP senza perdita (tavolozza, sottrazione del verde,
predittore, trasformata del colore, LZ77, cache dei colori, Huffman) e il
formato esteso solo quando ci sono metadati: i 57 riferimenti tornano esatti e
pesano l'88% del PNG di casa. Niente componenti esterni.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 15:01:47 +01:00

186 lines
6.5 KiB
C#

using System.Buffers.Binary;
using Febe.Core.Codecs.Images;
namespace Febe.Tests.Imaging;
/// <summary>WebP files put together by the tests from RIFF chunks (RFC 9649 §2), to reach what the reference set lacks.</summary>
internal static class WebPTestFiles
{
/// <summary>The chunks of a WebP file: FourCC and payload, in order.</summary>
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;
}
/// <summary>The payload of the first chunk of that type.</summary>
public static byte[] Payload(byte[] file, string type) => Chunks(file).First(c => c.Type == type).Payload;
/// <summary>A RIFF/WEBP file of the given chunks (padded to even sizes).</summary>
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;
}
/// <summary>A VP8X payload: the flags byte and the canvas size.</summary>
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;
}
/// <summary>An ANIM payload: background colour (B, G, R, A) and loop count.</summary>
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;
}
/// <summary>An ANMF payload: offset (even), size, duration, flags (2 = do not blend, 1 = dispose) and frame chunks.</summary>
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();
}
/// <summary>
/// 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.
/// </summary>
public static byte[] FilterAlpha(ReadOnlySpan<byte> 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;
}
/// <summary>The alpha bytes of an image.</summary>
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;
}
/// <summary>A little-endian TIFF with an IFD0 holding only the Orientation tag.</summary>
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;
}
/// <summary>An image with only <paramref name="colors"/> distinct colours (some transparent), from a fixed seed.</summary>
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<byte> destination, int value)
{
destination[0] = (byte)value;
destination[1] = (byte)(value >> 8);
destination[2] = (byte)(value >> 16);
}
}