JuliusBrussee/caveman · error

encodeGrayPNG: pixels length

Error message

encodeGrayPNG: pixels length %d != %dx%d

What it means

encodeGrayPNG builds an 8-bit grayscale image and requires exactly one byte per pixel: len(pixels) must equal width*height. The buffer passed by RenderChunkToPNG or renderMultiColChunkFromLines had a different length, indicating the caller computed geometry and pixel data inconsistently, so encoding is refused rather than producing a corrupt PNG.

Solutions

  1. Verify the pixel slice is built with exactly the same width and height values passed to the render call: make([]uint8, width*height).
  2. If Layers > 0, ensure the buffer is width*height*Layers or reset Layers to 0/1 for single-layer rendering.
  3. Add a debug log of len(pixels), width, height at the call site and compare against the chunk geometry.

Example fix

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

Strategy: validation

Validate before calling

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

Try / catch

pngBytes, err := RenderChunkToPNG(pixels, w, h)
if err != nil {
    if strings.Contains(err.Error(), "pixels length") {
        // re-derive geometry from len(pixels) or re-slice the buffer
    }
    return err
}

Prevention

When it happens

Trigger: Calling encodeGrayPNG (via RenderChunkToPNG / renderMultiColChunkFromLines) with a pixel slice sized from different width/height values than those passed — e.g. chunk height derived from line count while width comes from a different chunk, or forgetting to multiply by the Layers value when it is set (>0) but the pixel buffer wasn't expanded.

Common situations: Off-by-one in chunk slicing (partial last row/column); changing chunk dimensions without re-slicing the pixel buffer; misreading the Layers semantics documented as 'treated as 1 by callers (existing single-layer renderers leave it unset)'.

Related errors


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

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