{"record":{"id":"f6936d664807ae37","repo":"JuliusBrussee/caveman","slug":"encodegraypng-pixels-length-d-dx-d","errorCode":null,"errorMessage":"encodeGrayPNG: pixels length %d != %dx%d","messagePattern":"encodeGrayPNG: pixels length (.+?) != (.+?)x(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/pixel/png.go","lineNumber":27,"sourceCode":"\t\"image/color\"\n\t\"image/png\"\n)\n\ntype RenderedImage struct {\n\tPNG               []byte\n\tWidth, Height     int\n\tCharsRendered     int\n\tDroppedChars      int\n\tDroppedCodepoints map[rune]int\n\t// Layers is the number of interleaved text layers baked into this image: 1\n\t// for an ordinary page, 2 for a max-level red/blue overlay page. Zero is\n\t// treated as 1 by callers (existing single-layer renderers leave it unset).\n\tLayers int\n}\n\nfunc encodeGrayPNG(pixels []uint8, width, height int) ([]byte, error) {\n\tif len(pixels) != width*height {\n\t\treturn nil, fmt.Errorf(\"encodeGrayPNG: pixels length %d != %dx%d\", len(pixels), width, height)\n\t}\n\timg := image.NewGray(image.Rect(0, 0, width, height))\n\tcopy(img.Pix, pixels)\n\tvar buf bytes.Buffer\n\tif err := png.Encode(&buf, img); err != nil {\n\t\treturn nil, err\n\t}\n\treturn buf.Bytes(), nil\n}\n\nfunc encodeRGBPNG(pixels []uint8, width, height int) ([]byte, error) {\n\tif len(pixels) != width*height*3 {\n\t\treturn nil, fmt.Errorf(\"encodeRGBPNG: pixels length %d != %dx%dx3\", len(pixels), width, height)\n\t}\n\timg := image.NewNRGBA(image.Rect(0, 0, width, height))\n\tfor i := 0; i < width*height; i++ {\n\t\timg.SetNRGBA(i%width, i/width, color.NRGBA{\n\t\t\tR: pixels[i*3],","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/pixel/png.go#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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)'.","solutions":["Verify the pixel slice is built with exactly the same width and height values passed to the render call: make([]uint8, width*height).","If Layers > 0, ensure the buffer is width*height*Layers or reset Layers to 0/1 for single-layer rendering.","Add a debug log of len(pixels), width, height at the call site and compare against the chunk geometry."],"exampleFix":"// before\npixels := make([]uint8, width*chunkHeight)\nencodeGrayPNG(pixels, width, height)\n// after\npixels := make([]uint8, width*height)\nencodeGrayPNG(pixels, width, height)","handlingStrategy":"validation","validationCode":"if len(pixels) != width*height {\n    return fmt.Errorf(\"gray buffer %d bytes, need %d (wxh=%dx%d)\", len(pixels), width*height, width, height)\n}\nRenderChunkToPNG(pixels, width, height)","typeGuard":null,"tryCatchPattern":"pngBytes, err := RenderChunkToPNG(pixels, w, h)\nif err != nil {\n    if strings.Contains(err.Error(), \"pixels length\") {\n        // re-derive geometry from len(pixels) or re-slice the buffer\n    }\n    return err\n}","preventionTips":["Allocate pixel buffers with the same width/height expressions used at the render call site.","Handle the partial last row/column of chunk grids explicitly before rendering.","If Layers > 0, size the buffer as width*height*Layers; otherwise leave Layers unset.","Add an assertion/helper that slices chunk pixels from a full frame so geometry is derived once."],"tags":["png","rendering","pixel-buffer","size-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}