{"record":{"id":"662f05d25ec59580","repo":"Yalantis/uCrop","slug":"load-webp-failed-to-decode-image-s","errorCode":null,"errorMessage":"load_webp(): Failed to decode image '%s'.","messagePattern":"load_webp\\(\\): Failed to decode image '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":59597,"sourceCode":"                              cimg_instance,\n                              filename);\n\n      if (config.input.has_animation)\n        throw CImgIOException(_cimg_instance\n                              \"load_webp(): Does not support animated WebP '%s'.\",\n                              cimg_instance,\n                              filename);\n\n      int width = config.input.width, height = config.input.height;\n      if (config.input.has_alpha) {\n        config.output.colorspace = MODE_RGBA;\n        assign(width,height,1,4);\n      } else {\n        config.output.colorspace = MODE_RGB;\n        assign(width,height,1,3);\n      }\n      if (WebPDecode(buffer._data, data_size, &config)!=VP8_STATUS_OK)\n        throw CImgIOException(_cimg_instance\n                              \"load_webp(): Failed to decode image '%s'.\",\n                              cimg_instance,\n                              filename);\n\n      uint8_t *imgData = config.output.u.RGBA.rgba;\n      T *ptr_r = _data, *ptr_g = _data + 1UL*width*height,\n        *ptr_b = _data + 2UL*width*height;\n      T *ptr_a = _spectrum==3 ? NULL : _data + 3UL*width*height;\n      cimg_forY(*this,y) {\n        const unsigned char *ptrs = (unsigned char*)&imgData[y*width*_spectrum];\n        cimg_forX(*this,x) {\n          *(ptr_r++) = (T)*(ptrs++);\n          *(ptr_g++) = (T)*(ptrs++);\n          *(ptr_b++) = (T)*(ptrs++);\n          if (ptr_a) *(ptr_a++) = (T)*(ptrs++);\n        }\n      }\n      WebPFreeDecBuffer(&config.output);","sourceCodeStart":59579,"sourceCodeEnd":59615,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L59579-L59615","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-obtain or re-download the file and verify integrity (checksum/size) before loading","Validate the file decodes outside CImg first (e.g. `webpinfo file.webp`) to confirm it is intact WebP","Rebuild the NDK/JNI with an up-to-date libwebp that supports lossless (VP8L) and extended formats","Ensure load_webp is only fed genuine WebP files; sniff the RIFF....WEBP magic bytes before calling"],"exampleFix":"// before\nimg.load_webp(path); // throws on truncated/corrupt file\n// after\nstd::ifstream f(path, std::ios::binary);\nchar hdr[12]; f.read(hdr, 12);\nif (f.gcount() == 12 && !std::memcmp(hdr, \"RIFF\", 4) && !std::memcmp(hdr + 8, \"WEBP\", 4)) {\n  img.load_webp(path);\n} else {\n  throw std::runtime_error(\"not a valid webp file\");\n}","handlingStrategy":"try-catch","validationCode":"bool valid_webp(const char* f) {\n  std::ifstream in(f, std::ios::binary | std::ios::ate);\n  if (!in || in.tellg() < 12) return false;\n  char hdr[12]; in.seekg(0); in.read(hdr, 12);\n  return !std::memcmp(hdr, \"RIFF\", 4) && !std::memcmp(hdr+8, \"WEBP\", 4);\n}","typeGuard":"bool looks_like_webp(const std::string& f) { return valid_webp(f.c_str()); }","tryCatchPattern":"try {\n  img.load_webp(filename);\n} catch (CImgIOException& e) {\n  std::cerr << \"webp decode failed: \" << e.what() << \"\\n\";\n  // fallback: re-download, or use external magick loader\n  img.load_imagemagick_external(filename);\n}","preventionTips":["Verify RIFF....WEBP magic bytes before decoding","Check file size/completeness after downloads (checksums)","Rebuild JNI with a current libwebp for lossless/extended-format support","Never trust file extensions; sniff content"],"tags":["cimg","webp","image-decoding","libwebp"],"backgroundTag":"file-read-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}