{"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":"validation","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/766dce6b1394ebb56a3090748d5a0240a5aefb36/engine/pixel/png.go#L22-L57","documentation":"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.","triggerScenarios":"Thrown at engine/pixel/png.go:40 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Recompute the pixel buffer with len = width*height*3 (one byte per R/G/B channel)","Verify width/height passed to encodeRGBPNG match the layer compositing dimensions","Use encodeGrayPNG instead if the buffer is 1 byte per pixel"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}