Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
.
  • Loading branch information
SimonCropp committed Apr 16, 2026
commit 9fb7539a33fbff5f646eca0d75e8a9a237f203d9
46 changes: 46 additions & 0 deletions src/Verify.Tests/Compare/Png/PngDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,52 @@ public void Large_256x256()
Assert.Equal(rgba, image.Rgba);
}

[Fact]
public void Small_GrayAlpha()
{
const int width = 4;
const int height = 4;
var ga = new byte[width * height * 2];
for (var i = 0; i < width * height; i++)
{
ga[i * 2] = (byte)(i * 16);
ga[i * 2 + 1] = (byte)(255 - i * 8);
}

var png = PngTestHelper.EncodeGrayAlpha(width, height, ga);
var image = PngDecoder.Decode(new MemoryStream(png));
Assert.Equal(width, image.Width);
Assert.Equal(height, image.Height);
for (var i = 0; i < width * height; i++)
{
var g = ga[i * 2];
Assert.Equal(g, image.Rgba[i * 4]);
Assert.Equal(g, image.Rgba[i * 4 + 1]);
Assert.Equal(g, image.Rgba[i * 4 + 2]);
Assert.Equal(ga[i * 2 + 1], image.Rgba[i * 4 + 3]);
}
}

[Fact]
public void Multiple_Idat_Chunks()
{
const int width = 32;
const int height = 32;
var rgba = new byte[width * height * 4];
new Random(7).NextBytes(rgba);
var png = PngTestHelper.EncodeRgbaMultipleIdat(width, height, rgba, chunkSize: 64);
var image = PngDecoder.Decode(new MemoryStream(png));
Assert.Equal(rgba, image.Rgba);
}

[Fact]
public void Rejects_Missing_Idat()
{
var png = PngTestHelper.EncodeWithoutIdat(1, 1);
var exception = Assert.Throws<Exception>(() => PngDecoder.Decode(new MemoryStream(png)));
Assert.Contains("IDAT", exception.Message);
}

[Fact]
public void Rejects_Bad_Signature()
{
Expand Down
35 changes: 35 additions & 0 deletions src/Verify.Tests/Compare/Png/PngTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public static byte[] EncodeGray(int width, int height, byte[] gray)
return BuildPng(ihdr, null, null, raw);
}

public static byte[] EncodeGrayAlpha(int width, int height, byte[] grayAlpha)
{
var raw = AddFilterBytes(grayAlpha, width * 2, height);
var ihdr = BuildIhdr(width, height, colorType: 4);
return BuildPng(ihdr, null, null, raw);
}

public static byte[] EncodePaletted(int width, int height, byte[] indices, byte[] palette, byte[]? trns = null)
{
var raw = AddFilterBytes(indices, width, height);
Expand Down Expand Up @@ -61,6 +68,34 @@ static byte[] BuildIhdr(int width, int height, byte colorType)
return data;
}

public static byte[] EncodeWithoutIdat(int width, int height)
{
var ihdr = BuildIhdr(width, height, colorType: 6);
using var stream = new MemoryStream();
stream.Write(signature, 0, signature.Length);
WriteChunk(stream, "IHDR", ihdr);
WriteChunk(stream, "IEND", []);
return stream.ToArray();
}

public static byte[] EncodeRgbaMultipleIdat(int width, int height, byte[] rgba, int chunkSize)
{
var raw = AddFilterBytes(rgba, width * 4, height);
var compressed = ZlibCompress(raw);
var ihdr = BuildIhdr(width, height, colorType: 6);
using var stream = new MemoryStream();
stream.Write(signature, 0, signature.Length);
WriteChunk(stream, "IHDR", ihdr);
for (var offset = 0; offset < compressed.Length; offset += chunkSize)
{
var length = Math.Min(chunkSize, compressed.Length - offset);
WriteChunk(stream, "IDAT", compressed.AsSpan(offset, length).ToArray());
}

WriteChunk(stream, "IEND", []);
return stream.ToArray();
}

static byte[] BuildPng(byte[] ihdr, byte[]? plte, byte[]? trns, byte[] raw)
{
var compressed = ZlibCompress(raw);
Expand Down
10 changes: 10 additions & 0 deletions src/Verify/Compare/Png/PngDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public static PngImage Decode(Stream stream)
throw new("PNG missing IHDR.");
}

if (idat.Length == 0)
{
throw new("PNG missing IDAT.");
}

ReadExact(stream, crc);
idat.Position = 0;
return Reconstruct(idat, width, height, colorType, palette, transparency);
Expand All @@ -128,6 +133,11 @@ public static PngImage Decode(Stream stream)
throw new("PNG missing IHDR.");
}

if (idat.Length == 0)
{
throw new("PNG missing IDAT.");
}

idat.Position = 0;
return Reconstruct(idat, width, height, colorType, palette, transparency);
}
Expand Down
Loading