{"record":{"id":"717ac90555d627dc","repo":"JuliusBrussee/caveman","slug":"encodergbpng-pixels-length-d-dx-dx3","errorCode":null,"errorMessage":"encodeRGBPNG: pixels length %d != %dx%dx3","messagePattern":"encodeRGBPNG: pixels length (.+?) != (.+?)x(.+?)x3","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/pixel/png.go","lineNumber":40,"sourceCode":"\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],\n\t\t\tG: pixels[i*3+1],\n\t\t\tB: pixels[i*3+2],\n\t\t\tA: 255,\n\t\t})\n\t}\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","sourceCodeStart":22,"sourceCodeEnd":57,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/pixel/png.go#L22-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the buffer is allocated as make([]uint8, width*height*3) with the exact width/height passed to the encoder.","For two-layer rendering, verify both layers are packed into the same RGB buffer before the call.","Confirm you are not reusing a grayscale buffer for the RGB renderer; pack 3 bytes (R,G,B) per pixel."],"exampleFix":"// before\npixels := make([]uint8, width*height)\nencodeRGBPNG(pixels, width, height)\n// after\npixels := make([]uint8, width*height*3)\nencodeRGBPNG(pixels, width, height)","handlingStrategy":"validation","validationCode":"if len(pixels) != width*height*3 {\n    return fmt.Errorf(\"rgb buffer %d bytes, need %d (wxhx3=%dx%dx3)\", len(pixels), width*height*3, 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        // check for grayscale buffer reuse or missing layer packing\n    }\n    return err\n}","preventionTips":["Always allocate RGB buffers as make([]uint8, width*height*3) from the same w/h used in the render call.","Pack every composited layer into the buffer before rendering; never pass a 1-byte-per-pixel buffer to the RGB path.","Centralize chunk slicing in one helper so buffer size and declared geometry can't drift."],"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"}