gohugoio/hugo · error

WebPPictureImportRGBA failed: %d (%s)\n

Error message

WebPPictureImportRGBA failed: %d (%s)\n

What it means

Printed by genwebp's encodeNRGBA (webp.c:138) when WebPPictureImportRGBA returns 0. This step copies the caller's RGBA buffer into libwebp's internal WebPPicture and validates that stride >= width*4 and dimensions are within WebP's limits. Failure most commonly means BAD_DIMENSION (width/height over 16383, or non-positive) or a stride smaller than width*4 bytes. The printed error_code and message pinpoint the cause. The function returns NULL.

Source

Thrown at internal/warpc/genwebp/webp.c:138

    pic.use_argb = 1;
    pic.width = width;
    pic.height = height;
    pic.writer = WebPMemoryWrite;
    pic.custom_ptr = &wrt;
    WebPMemoryWriterInit(&wrt);
    ok = WebPPictureImportRGBA(&pic, rgba, stride);
    if (ok)
    {
        ok = WebPEncode(config, &pic);
        if (!ok)
        {
            fprintf(stderr, "WebPEncode failed: %d (%s)\n", pic.error_code, kErrorMessages[pic.error_code]);
        }
    }
    else
    {
        fprintf(stderr, "WebPPictureImportRGBA failed: %d (%s)\n", pic.error_code, kErrorMessages[pic.error_code]);
    }
    WebPPictureFree(&pic);
    if (!ok)
    {
        WebPMemoryWriterClear(&wrt);
        return NULL;
    }
    *output_size = wrt.size;
    return wrt.mem;
}

static uint8_t *encodeGray(WebPConfig *config, uint8_t *y, int width, int height, int stride, size_t *output_size)
{
    WebPPicture pic;
    WebPMemoryWriter wrt;

    int ok;
    if (!WebPPictureInit(&pic))

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Assert on the Go side that stride == width*4 and that the pixel buffer length is at least stride*height before invoking encodeNRGBA.
  2. Clamp width and height to <= 16383 (downscale or reject the WebP output format).
  3. Reject zero/negative width or height before sending the command.
  4. Verify the rgba buffer actually contains stride*height bytes — re-read the upstream decode output if there is any doubt.
  5. Cross-check that the Go image type really is NRGBA/RGBA and not a premultiplied-alpha format with a different stride.

Example fix

// before: stride computed for 3-byte RGB on an RGBA image
stride := img.Bounds().Dx() * 3

// after: stride must be width*4 for RGBA import
stride := img.Bounds().Dx() * 4
pix := make([]byte, stride*img.Bounds().Dy())
// fill pix, then send to genwebp
Defensive patterns

Strategy: validation

Validate before calling

// Assert stride and buffer length match WebP RGBA expectations.
func validateRGBAForWebp(pix []byte, w, h, stride int) error {
    if w <= 0 || h <= 0 { return fmt.Errorf("invalid %dx%d", w, h) }
    if stride < w*4 { return fmt.Errorf("stride %d < width*4 %d", stride, w*4) }
    if len(pix) < stride*h { return fmt.Errorf("buffer %d < stride*height %d", len(pix), stride*h) }
    return nil
}

Prevention

When it happens

Trigger: Calling encodeNRGBA with width or height > 16383, with stride < width*4, with width/height <= 0, or with a NULL/short rgba buffer. Also if the rgba pointer's allocation is smaller than stride*height bytes (buffer underrun detected by libwebp's internal checks).

Common situations: Hugo passes a stride computed for RGB (3 bytes) when the image is RGBA (4 bytes), an image was resized to dimensions above the WebP cap, an 8-bit grayscale path accidentally routed into the RGBA encoder with the wrong stride, or a corrupt/short pixel buffer from a prior decode step.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/8b6e4b6d38e9add0. Report an issue: GitHub.