Yalantis/uCrop · error · CImgIOException

load_webp(): Failed to decode image '%s'.

Error message

load_webp(): Failed to decode image '%s'.

What it means

CImg called WebPDecode() on the file's raw bytes and the returned status was not VP8_STATUS_OK, meaning libwebp could not decode the image data. The header/meta info read succeeded, but the actual compressed bitstream is corrupt, truncated, or of an unsupported variant. The CImg buffer is left in an indeterminate state and a CImgIOException is thrown.

Source

Thrown at ucrop/src/main/jni/CImg.h:59597

                              cimg_instance,
                              filename);

      if (config.input.has_animation)
        throw CImgIOException(_cimg_instance
                              "load_webp(): Does not support animated WebP '%s'.",
                              cimg_instance,
                              filename);

      int width = config.input.width, height = config.input.height;
      if (config.input.has_alpha) {
        config.output.colorspace = MODE_RGBA;
        assign(width,height,1,4);
      } else {
        config.output.colorspace = MODE_RGB;
        assign(width,height,1,3);
      }
      if (WebPDecode(buffer._data, data_size, &config)!=VP8_STATUS_OK)
        throw CImgIOException(_cimg_instance
                              "load_webp(): Failed to decode image '%s'.",
                              cimg_instance,
                              filename);

      uint8_t *imgData = config.output.u.RGBA.rgba;
      T *ptr_r = _data, *ptr_g = _data + 1UL*width*height,
        *ptr_b = _data + 2UL*width*height;
      T *ptr_a = _spectrum==3 ? NULL : _data + 3UL*width*height;
      cimg_forY(*this,y) {
        const unsigned char *ptrs = (unsigned char*)&imgData[y*width*_spectrum];
        cimg_forX(*this,x) {
          *(ptr_r++) = (T)*(ptrs++);
          *(ptr_g++) = (T)*(ptrs++);
          *(ptr_b++) = (T)*(ptrs++);
          if (ptr_a) *(ptr_a++) = (T)*(ptrs++);
        }
      }
      WebPFreeDecBuffer(&config.output);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Re-obtain or re-download the file and verify integrity (checksum/size) before loading
  2. Validate the file decodes outside CImg first (e.g. `webpinfo file.webp`) to confirm it is intact WebP
  3. Rebuild the NDK/JNI with an up-to-date libwebp that supports lossless (VP8L) and extended formats
  4. Ensure load_webp is only fed genuine WebP files; sniff the RIFF....WEBP magic bytes before calling

Example fix

// before
img.load_webp(path); // throws on truncated/corrupt file
// after
std::ifstream f(path, std::ios::binary);
char hdr[12]; f.read(hdr, 12);
if (f.gcount() == 12 && !std::memcmp(hdr, "RIFF", 4) && !std::memcmp(hdr + 8, "WEBP", 4)) {
  img.load_webp(path);
} else {
  throw std::runtime_error("not a valid webp file");
}
Defensive patterns

Strategy: try-catch

Validate before calling

bool valid_webp(const char* f) {
  std::ifstream in(f, std::ios::binary | std::ios::ate);
  if (!in || in.tellg() < 12) return false;
  char hdr[12]; in.seekg(0); in.read(hdr, 12);
  return !std::memcmp(hdr, "RIFF", 4) && !std::memcmp(hdr+8, "WEBP", 4);
}

Type guard

bool looks_like_webp(const std::string& f) { return valid_webp(f.c_str()); }

Try / catch

try {
  img.load_webp(filename);
} catch (CImgIOException& e) {
  std::cerr << "webp decode failed: " << e.what() << "\n";
  // fallback: re-download, or use external magick loader
  img.load_imagemagick_external(filename);
}

Prevention

When it happens

Trigger: Calling load_webp() (or load() on a WebP-detected file) where the file's pixel data fails WebPDecode: truncated download, bit-rot, VP8L/lossless or extended-format data unsupported by the linked libwebp version, or a file only renamed to .webp.

Common situations: Incomplete HTTP downloads; files corrupted in transit or storage; very old bundled libwebp in the NDK build that cannot decode newer WebP features; non-WebP files with a .webp extension.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/662f05d25ec59580. Report an issue: GitHub.