JuliusBrussee/caveman · error

encodeRGBPNG: pixels length

Error message

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

What it means

encodeRGBPNG builds an NRGBA image from packed RGB bytes and requires exactly 3 bytes per pixel: len(pixels) must equal width*height*3. A different length means the caller's pixel buffer does not match the declared geometry, so the encoder fails fast instead of writing a corrupt PNG.

Solutions

  1. Ensure the buffer is allocated as make([]uint8, width*height*3) with the exact width/height passed to the encoder.
  2. For two-layer rendering, verify both layers are packed into the same RGB buffer before the call.
  3. Confirm you are not reusing a grayscale buffer for the RGB renderer; pack 3 bytes (R,G,B) per pixel.

Example fix

// before
pixels := make([]uint8, width*height)
encodeRGBPNG(pixels, width, height)
// after
pixels := make([]uint8, width*height*3)
encodeRGBPNG(pixels, width, height)
Defensive patterns

Strategy: validation

Validate before calling

if len(pixels) != width*height*3 {
    return fmt.Errorf("rgb buffer %d bytes, need %d (wxhx3=%dx%dx3)", len(pixels), width*height*3, width, height)
}
RenderChunkToPNG(pixels, width, height)

Try / catch

pngBytes, err := RenderChunkToPNG(pixels, w, h)
if err != nil {
    if strings.Contains(err.Error(), "pixels length") {
        // check for grayscale buffer reuse or missing layer packing
    }
    return err
}

Prevention

When it happens

Trigger: Calling renderTwoLayerChunk or RenderChunkToPNG with an RGB buffer built from one width/height pair while passing a different pair; passing a grayscale (1 byte/px) buffer to the RGB path; forgetting a layer multiplier when multiple layers are composited.

Common situations: Two-layer rendering where only one layer's pixels were packed (half the expected size); switching a renderer from gray to RGB without resizing the buffer; off-by-one row/column chunk slicing.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/717ac90555d627dc. Report an issue: GitHub.

Appendix: 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 3ee70a1026)