d2phap/ImageGlass · error · Exception

Crop options exceeded WebP image dimensions

Error message

Crop options exceeded WebP image dimensions

What it means

Thrown in WebPWrapper.Decode when use_cropping==1 and the requested crop rectangle falls outside the image: crop_left+crop_width > input.Width OR crop_top+crop_height > input.Height. The crop options are validated against the real image dimensions reported by WebPGetFeatures, so this is purely a caller-supplied crop-window error, not a decode problem.

Source

Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:157

            if (LibWebp.WebPInitDecoderConfig(ref config) == 0)
            {
                throw new Exception("WebPInitDecoderConfig failed. Wrong version?");
            }
            // Read the .webp input file information
            IntPtr ptrRawWebP = pinnedWebP.AddrOfPinnedObject();
            int height;
            int width;
            if (options.use_scaling == 0)
            {
                result = LibWebp.WebPGetFeatures(ptrRawWebP, rawWebP.Length, ref config.input);
                if (result != VP8StatusCode.VP8_STATUS_OK)
                    throw new Exception("Failed WebPGetFeatures with error " + result);

                //Test cropping values
                if (options.use_cropping == 1)
                {
                    if (options.crop_left + options.crop_width > config.input.Width || options.crop_top + options.crop_height > config.input.Height)
                        throw new Exception("Crop options exceeded WebP image dimensions");
                    width = options.crop_width;
                    height = options.crop_height;
                }
            }
            else
            {
                width = options.scaled_width;
                height = options.scaled_height;
            }

            config.options.bypass_filtering = options.bypass_filtering;
            config.options.no_fancy_upsampling = options.no_fancy_upsampling;
            config.options.use_cropping = options.use_cropping;
            config.options.crop_left = options.crop_left;
            config.options.crop_top = options.crop_top;
            config.options.crop_width = options.crop_width;
            config.options.crop_height = options.crop_height;
            config.options.use_scaling = options.use_scaling;

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Clamp the crop rectangle to the source dimensions: crop_left = clamp(0, W - crop_width); ensure crop_left+crop_width <= W and crop_top+crop_height <= H.
  2. Fetch the image dimensions first (WebPGetFeatures) and compute the crop window from the real W/H rather than an assumption.
  3. If the crop is optional, set use_cropping=0 when it would exceed bounds and decode the full image instead.
  4. Validate crop_left >= 0, crop_top >= 0, crop_width > 0, crop_height > 0 before calling.

Example fix

// before
options.use_cropping = 1;
options.crop_left = x; options.crop_top = y;
options.crop_width = w; options.crop_height = h;
var bmp = WebPWrapper.Decode(raw, options);

// after: clamp to image bounds
// (imgW, imgH come from a prior WebPGetFeatures call)
w = Math.Min(w, imgW - x); h = Math.Min(h, imgH - y);
if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > imgW || y + h > imgH)
    options.use_cropping = 0;
var bmp = WebPWrapper.Decode(raw, options);
Defensive patterns

Strategy: validation

Validate before calling

// Clamp the crop window to image bounds (imgW/imgH from WebPGetFeatures).
options.crop_left = Math.Clamp(options.crop_left, 0, imgW);
options.crop_top = Math.Clamp(options.crop_top, 0, imgH);
options.crop_width = Math.Min(options.crop_width, imgW - options.crop_left);
options.crop_height = Math.Min(options.crop_height, imgH - options.crop_top);
if (options.crop_width <= 0 || options.crop_height <= 0) options.use_cropping = 0;

Try / catch

try { bmp = WebPWrapper.Decode(rawWebP, options); }
catch (Exception ex) when (ex.Message.Contains("Crop options exceeded"))
{ options.use_cropping = 0; bmp = WebPWrapper.Decode(rawWebP, options); }

Prevention

When it happens

Trigger: Passing WebPDecoderOptions with use_cropping=1 and crop coordinates computed without clamping to the source dimensions; assuming a thumbnail size that exceeds the actual image; off-by-one or sign errors in crop math; reusing crop options across images of different sizes.

Common situations: A UI crop tool sends the selection rectangle without intersecting it with the decoded image bounds; generating thumbnails with a fixed crop window on a smaller source image; coordinates swapped (width/height vs right/bottom).

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/07db658847579a42. Report an issue: GitHub.