JuliusBrussee/caveman · error

encodeRGBPNG: pixels length %d != %dx%dx3

Error message

encodeRGBPNG: pixels length %d != %dx%dx3

What it means

encodeRGBPNG rejects a pixel buffer whose length is not exactly width*height*3 before building the image. It fires when callers like renderTwoLayerChunk or RenderChunkToPNG compose layers with mismatched dimensions or pass a grayscale-length buffer to the RGB encoder.

Source

Thrown at engine/pixel/png.go:40

	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],
			G: pixels[i*3+1],
			B: pixels[i*3+2],
			A: 255,
		})
	}
	var buf bytes.Buffer
	if err := png.Encode(&buf, img); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

View on GitHub (pinned to 766dce6b13)

Solutions

  1. Recompute the pixel buffer with len = width*height*3 (one byte per R/G/B channel)
  2. Verify width/height passed to encodeRGBPNG match the layer compositing dimensions
  3. Use encodeGrayPNG instead if the buffer is 1 byte per pixel
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at engine/pixel/png.go:40 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/717ac90555d627dc. Report an issue: GitHub.