JuliusBrussee/caveman · error
encodeGrayPNG: pixels length %d != %dx%d
Error message
encodeGrayPNG: pixels length %d != %dx%d
What it means
Raised by encodeGrayPNG: the grayscale pixel buffer length does not equal width*height. It is a caller-contract guard in the PNG rasterizer — the pixel slice was built with different dimensions than passed, so encoding would read out of bounds or truncate rows.
Source
Thrown at engine/pixel/png.go:27
"image/color"
"image/png"
)
type RenderedImage struct {
PNG []byte
Width, Height int
CharsRendered int
DroppedChars int
DroppedCodepoints map[rune]int
// Layers is the number of interleaved text layers baked into this image: 1
// for an ordinary page, 2 for a max-level red/blue overlay page. Zero is
// treated as 1 by callers (existing single-layer renderers leave it unset).
Layers int
}
func encodeGrayPNG(pixels []uint8, width, height int) ([]byte, error) {
if len(pixels) != width*height {
return nil, fmt.Errorf("encodeGrayPNG: pixels length %d != %dx%d", len(pixels), width, height)
}
img := image.NewGray(image.Rect(0, 0, width, height))
copy(img.Pix, pixels)
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func encodeRGBPNG(pixels []uint8, width, height int) ([]byte, error) {
if len(pixels) != width*height*3 {
return nil, fmt.Errorf("encodeRGBPNG: pixels length %d != %dx%dx3", len(pixels), width, height)
}
img := image.NewNRGBA(image.Rect(0, 0, width, height))
for i := 0; i < width*height; i++ {
img.SetNRGBA(i%width, i/width, color.NRGBA{
R: pixels[i*3],View on GitHub (pinned to 766dce6b13)
Solutions
- Ensure the pixel slice has exactly width*height entries before calling encodeGrayPNG
- Allocate pixels with make([]uint8, width*height) at the same dimensions passed to the renderer
- Fix the renderer that produced a mismatched buffer (check stride/row padding)
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at engine/pixel/png.go:27 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18).
Data as JSON: /api/errors/f6936d664807ae37.
Report an issue: GitHub.