Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
222 lines
9.0 KiB
C#
222 lines
9.0 KiB
C#
using System.Buffers.Binary;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using Titano.Core.Codecs.Apng;
|
|
using Titano.Core.Hashing;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Video;
|
|
|
|
public sealed class ApngEncoderTests
|
|
{
|
|
private const int W = 40, H = 30;
|
|
|
|
[Test]
|
|
public void ChunkOrderSequenceNumbersAndCrcsAreValid()
|
|
{
|
|
var frames = Enumerable.Range(0, 5).Select(i => Pattern(i, 3)).ToList();
|
|
var bytes = Encode(frames, new ApngEncoderOptions(), 3000);
|
|
var chunks = Chunks(bytes);
|
|
Assert.SequenceEqual(
|
|
["IHDR", "acTL", "fcTL", "IDAT", "fcTL", "fdAT", "fcTL", "fdAT", "fcTL", "fdAT", "fcTL", "fdAT", "IEND"],
|
|
chunks.Select(c => c.Type));
|
|
var actl = chunks[1].Data;
|
|
Assert.Equal(5, BinaryPrimitives.ReadInt32BigEndian(actl));
|
|
Assert.Equal(0, BinaryPrimitives.ReadInt32BigEndian(actl.AsSpan(4)));
|
|
var sequence = chunks.Where(c => c.Type is "fcTL" or "fdAT").Select(c => BinaryPrimitives.ReadUInt32BigEndian(c.Data)).ToList();
|
|
Assert.SequenceEqual(Enumerable.Range(0, sequence.Count).Select(i => (uint)i), sequence);
|
|
var first = chunks[2].Data;
|
|
Assert.Equal((W, H, 0, 0), (BinaryPrimitives.ReadInt32BigEndian(first.AsSpan(4)), BinaryPrimitives.ReadInt32BigEndian(first.AsSpan(8)),
|
|
BinaryPrimitives.ReadInt32BigEndian(first.AsSpan(12)), BinaryPrimitives.ReadInt32BigEndian(first.AsSpan(16))));
|
|
Assert.Equal((1, 30), Delay(first));
|
|
}
|
|
|
|
[Test]
|
|
public void FirstFrameIsTheDefaultImageAndAllFramesComposeExactly()
|
|
{
|
|
var frames = new List<byte[]> { Pattern(0, 3), Pattern(0, 3), Pattern(1, 3) };
|
|
// Frame 3 changes only a small block of frame 2.
|
|
var last = (byte[])frames[2].Clone();
|
|
for (var y = 5; y < 9; y++)
|
|
{
|
|
for (var x = 20; x < 26; x++) last[(y * W + x) * 3] ^= 0xFF;
|
|
}
|
|
frames.Add(last);
|
|
var bytes = Encode(frames, new ApngEncoderOptions(), 1500);
|
|
var decoded = Decode(bytes, 3);
|
|
Assert.Equal(3, decoded.Count); // the duplicate merged into the first frame
|
|
Assert.BytesEqual(frames[0], decoded[0].Canvas);
|
|
Assert.BytesEqual(frames[2], decoded[1].Canvas);
|
|
Assert.BytesEqual(frames[3], decoded[2].Canvas);
|
|
Assert.Equal((1, 30), decoded[0].Delay);
|
|
Assert.Equal((20, 5, 6, 4), decoded[2].Rect);
|
|
Assert.Equal(0, decoded[2].Blend); // RGB output always uses SOURCE
|
|
}
|
|
|
|
[Test]
|
|
public void RgbaDifferencingUsesOverWithTransparentUnchangedPixels()
|
|
{
|
|
var a = Pattern(2, 4);
|
|
var b = (byte[])a.Clone();
|
|
b[(10 * W + 10) * 4] ^= 0x80;
|
|
b[(12 * W + 14) * 4 + 1] ^= 0x80;
|
|
var bytes = Encode([a, b], new ApngEncoderOptions { Alpha = true }, 3000);
|
|
var decoded = Decode(bytes, 4);
|
|
Assert.Equal(2, decoded.Count);
|
|
Assert.Equal(1, decoded[1].Blend);
|
|
Assert.Equal((10, 10, 5, 3), decoded[1].Rect);
|
|
Assert.BytesEqual(b, decoded[1].Canvas);
|
|
}
|
|
|
|
[Theory]
|
|
[Case(3003L, 1001, 30000)]
|
|
[Case(90000L * 30, 30, 1)]
|
|
[Case(3001L, 33, 1000)]
|
|
public void DelaysAreExactFractionsOrCumulativelyRounded(long duration, int num, int den)
|
|
{
|
|
var bytes = Encode([Pattern(0, 3), Pattern(1, 3)], new ApngEncoderOptions(), duration);
|
|
var fctl = Chunks(bytes).Where(c => c.Type == "fcTL").ToList();
|
|
Assert.Equal((num, den), Delay(fctl[0].Data));
|
|
}
|
|
|
|
[Test]
|
|
public void LinearBuffersAreEncodedAsSrgb()
|
|
{
|
|
using var image = new ImageBuffer(W, H, 4);
|
|
image.Plane(0).Fill(ColorSpace.Linear8(200));
|
|
image.Plane(1).Fill(ColorSpace.Linear8(100));
|
|
image.Plane(3).Fill(0.5f);
|
|
using var ms = new MemoryStream();
|
|
var encoder = new ApngEncoder(ms, W, H, new ApngEncoderOptions { Alpha = true, LoopCount = 2 });
|
|
encoder.AddFrame(image, 9000);
|
|
encoder.Complete();
|
|
var decoded = Decode(ms.ToArray(), 4);
|
|
Assert.SequenceEqual(new byte[] { 200, 100, 0, 128 }, decoded[0].Canvas.Take(4));
|
|
Assert.Equal(2, BinaryPrimitives.ReadInt32BigEndian(Chunks(ms.ToArray())[1].Data.AsSpan(4)));
|
|
}
|
|
|
|
private static byte[] Encode(List<byte[]> frames, ApngEncoderOptions options, long duration)
|
|
{
|
|
using var ms = new MemoryStream();
|
|
var encoder = new ApngEncoder(ms, W, H, options);
|
|
foreach (var f in frames) encoder.AddFrame(f, duration);
|
|
encoder.Complete();
|
|
return ms.ToArray();
|
|
}
|
|
|
|
private static (int, int) Delay(byte[] fctl) =>
|
|
(BinaryPrimitives.ReadUInt16BigEndian(fctl.AsSpan(20)), BinaryPrimitives.ReadUInt16BigEndian(fctl.AsSpan(22)));
|
|
|
|
private sealed record Chunk(string Type, byte[] Data);
|
|
|
|
/// <summary>Splits the PNG into chunks, checking the signature, lengths and every CRC.</summary>
|
|
private static List<Chunk> Chunks(byte[] png)
|
|
{
|
|
Assert.BytesEqual([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], png.AsSpan(0, 8));
|
|
var list = new List<Chunk>();
|
|
var pos = 8;
|
|
while (pos < png.Length)
|
|
{
|
|
var length = BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(pos));
|
|
var type = Encoding.ASCII.GetString(png, pos + 4, 4);
|
|
Assert.Equal(BinaryPrimitives.ReadUInt32BigEndian(png.AsSpan(pos + 8 + length)), Crc32.Compute(png.AsSpan(pos + 4, length + 4)), $"CRC of {type}");
|
|
list.Add(new Chunk(type, png.AsSpan(pos + 8, length).ToArray()));
|
|
pos += 12 + length;
|
|
}
|
|
Assert.Equal(png.Length, pos);
|
|
return list;
|
|
}
|
|
|
|
private sealed record Frame(byte[] Canvas, (int, int) Delay, (int X, int Y, int W, int H) Rect, int Blend);
|
|
|
|
/// <summary>Inflates, unfilters and composites every frame (dispose NONE, blend SOURCE/OVER with binary alpha).</summary>
|
|
private static List<Frame> Decode(byte[] png, int bpp)
|
|
{
|
|
var chunks = Chunks(png);
|
|
var canvas = new byte[W * H * bpp];
|
|
var frames = new List<Frame>();
|
|
for (var i = 0; i < chunks.Count; i++)
|
|
{
|
|
if (chunks[i].Type != "fcTL") continue;
|
|
var f = chunks[i].Data;
|
|
int w = BinaryPrimitives.ReadInt32BigEndian(f.AsSpan(4)), h = BinaryPrimitives.ReadInt32BigEndian(f.AsSpan(8));
|
|
int x = BinaryPrimitives.ReadInt32BigEndian(f.AsSpan(12)), y = BinaryPrimitives.ReadInt32BigEndian(f.AsSpan(16));
|
|
Assert.Equal((byte)0, f[24]);
|
|
var data = chunks[i + 1].Type == "IDAT" ? chunks[i + 1].Data : chunks[i + 1].Data[4..];
|
|
var pixels = Unfilter(Inflate(data), w, h, bpp);
|
|
for (var row = 0; row < h; row++)
|
|
{
|
|
for (var col = 0; col < w; col++)
|
|
{
|
|
var s = (row * w + col) * bpp;
|
|
var d = ((y + row) * W + x + col) * bpp;
|
|
if (f[25] == 1 && bpp == 4 && pixels[s + 3] == 0) continue;
|
|
pixels.AsSpan(s, bpp).CopyTo(canvas.AsSpan(d));
|
|
}
|
|
}
|
|
frames.Add(new Frame((byte[])canvas.Clone(), Delay(f), (x, y, w, h), f[25]));
|
|
}
|
|
return frames;
|
|
}
|
|
|
|
private static byte[] Inflate(byte[] zlib)
|
|
{
|
|
using var z = new ZLibStream(new MemoryStream(zlib), CompressionMode.Decompress);
|
|
using var ms = new MemoryStream();
|
|
z.CopyTo(ms);
|
|
return ms.ToArray();
|
|
}
|
|
|
|
private static byte[] Unfilter(byte[] filtered, int w, int h, int bpp)
|
|
{
|
|
var stride = w * bpp;
|
|
Assert.Equal((stride + 1) * h, filtered.Length);
|
|
var output = new byte[stride * h];
|
|
for (var y = 0; y < h; y++)
|
|
{
|
|
var filter = filtered[y * (stride + 1)];
|
|
for (var i = 0; i < stride; i++)
|
|
{
|
|
int raw = filtered[y * (stride + 1) + 1 + i];
|
|
int a = i >= bpp ? output[y * stride + i - bpp] : 0;
|
|
int b = y > 0 ? output[(y - 1) * stride + i] : 0;
|
|
int c = i >= bpp && y > 0 ? output[(y - 1) * stride + i - bpp] : 0;
|
|
output[y * stride + i] = (byte)(filter switch
|
|
{
|
|
0 => raw,
|
|
1 => raw + a,
|
|
2 => raw + b,
|
|
3 => raw + ((a + b) >> 1),
|
|
_ => raw + Paeth(a, b, c),
|
|
});
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
private static int Paeth(int a, int b, int c)
|
|
{
|
|
var p = a + b - c;
|
|
int pa = Math.Abs(p - a), pb = Math.Abs(p - b), pc = Math.Abs(p - c);
|
|
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
}
|
|
|
|
private static byte[] Pattern(int variant, int bpp)
|
|
{
|
|
var pixels = new byte[W * H * bpp];
|
|
for (var y = 0; y < H; y++)
|
|
{
|
|
for (var x = 0; x < W; x++)
|
|
{
|
|
var o = (y * W + x) * bpp;
|
|
pixels[o] = (byte)(x * 6 + variant * 40);
|
|
pixels[o + 1] = (byte)(y * 8);
|
|
pixels[o + 2] = (byte)((x ^ y) * 4 + variant);
|
|
if (bpp == 4) pixels[o + 3] = 255;
|
|
}
|
|
}
|
|
return pixels;
|
|
}
|
|
}
|