OpenRA/OpenRA · error · InvalidDataException
Invalid PLTE chunk length.
Error message
Invalid PLTE chunk length.
What it means
Thrown by Png(Stream) when a PLTE (palette) chunk's declared length is not evenly divisible by 3. Each palette entry is an RGB triplet (3 bytes), so the total length must be length % 3 == 0. A non-multiple-of-3 length means the palette data is malformed.
Source
Thrown at OpenRA.Game/FileFormats/Png.cs:184
if (compression != 0)
throw new InvalidDataException("Compression method not supported");
if (interlace != 0)
throw new InvalidDataException("Interlacing not supported");
Data = new byte[Width * Height * PixelStride];
// Skip CRC.
s.Seek(4, SeekOrigin.Current);
headerParsed = true;
break;
}
case ChunkPLTE:
{
if (length % 3 != 0)
throw new InvalidDataException("Invalid PLTE chunk length.");
var count = length / 3;
Palette = new Color[count];
var buffer = ArrayPool<byte>.Shared.Rent(length);
try
{
s.ReadBytes(buffer.AsSpan(0, length));
for (var i = 0; i < count; i++)
{
var offset = i * 3;
Palette[i] = Color.FromArgb(buffer[offset], buffer[offset + 1], buffer[offset + 2]);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}View on GitHub (pinned to a520984d91)
Solutions
- Re-export the PNG from the original indexed-color source image.
- Run pngcheck to validate palette chunk integrity.
- Replace the corrupt file with a clean copy from version control or backup.
Defensive patterns
Strategy: try-catch
Try / catch
try
{
var png = new Png(stream);
}
catch (InvalidDataException ex) when (ex.Message.Contains("PLTE chunk length"))
{
logger.LogError($"Corrupt PNG palette: {ex.Message}");
} Prevention
- Re-export indexed PNGs from source images rather than manually editing palette data.
- Run pngcheck on all indexed PNG assets during CI.
- Store original source images (e.g., PSD, XCF) to regenerate assets if corruption occurs.
When it happens
Trigger: Constructing `new Png(stream)` where an indexed-color PNG has a PLTE chunk whose 4-byte big-endian length field is not divisible by 3. The code checks `length % 3 != 0` immediately upon encountering ChunkPLTE.
Common situations: Byte-level corruption in the PLTE chunk's length field. A broken PNG encoder that wrote an incorrect length. File truncation or modification that shifted bytes within the PLTE chunk.
Related errors
- Invalid PNG file - no end chunk found.
- Unexpected end of stream in IDAT chunk.
- Invalid PNG file - header does not appear first.
- Invalid PNG file - duplicate header.
- Compression method not supported
AI-assisted analysis of OpenRA/OpenRA@a520984d91 (2026-08-13).
Data as JSON: /api/errors/0b57b571c67bee48.
Report an issue: GitHub.