{"record":{"id":"7e43b3ce5caf1bec","repo":"Yalantis/uCrop","slug":"save-webp-specified-filename-is-null","errorCode":null,"errorMessage":"save_webp(): Specified filename is (null).","messagePattern":"save_webp\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61594,"sourceCode":"        }\n      }\n      }\n      if (!file) cimg::fclose(nfile);\n      return *this;\n    }\n\n    //! Save image as a WebP file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param quality Image quality (in %)\n    **/\n    const CImg<T>& save_webp(const char *const filename, const int quality=100) const {\n      return _save_webp(filename,quality);\n    }\n\n    const CImg<T>& _save_webp(const char *const filename, const int quality) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_webp(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (_spectrum!=3 && _spectrum!=4)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_webp(): WebP only supports (A)RGB colorspace.\",\n                                    cimg_instance);\n#ifndef cimg_use_webp\n      cimg::unused(quality);\n      return save_other(filename);\n#else\n      std::FILE *file = cimg::fopen(filename, \"wb\");\n      CImg<uint8_t> rgbaBuffer(size());\n      T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height,\n        *ptr_b = _data + 2UL*_width*_height, *ptr_a = _spectrum==3?NULL:_data + 3UL*_width*_height;\n      uint8_t *ptr = rgbaBuffer._data;\n      cimg_forY(*this,y) {\n        cimg_forX(*this,x) {\n          *(ptr++) = (T)*(ptr_r++);","sourceCodeStart":61576,"sourceCodeEnd":61612,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61576-L61612","documentation":"save_webp() encodes to WebP and requires a filename (no FILE* overload); _save_webp() throws CImgArgumentException when the filename is NULL. A second check enforces that WebP only supports 3-channel (RGB) or 4-channel (RGBA) images.","triggerScenarios":"img.save_webp(NULL) or a null path from an uninitialized variable; also save_webp() on images whose spectrum is 1 (grayscale) or 2 triggers the adjacent colorspace error.","commonSituations":"Saving single-channel masks/scans to .webp, optional-path logic passing NULL, filename from a failed config lookup.","solutions":["Ensure a non-null filename is passed.","Convert the image to 3 or 4 channels first (e.g., img.get_resize(...,1,1,3) or append channels) if spectrum is not 3/4.","Use PNG/JPEG for grayscale or 2-channel data instead.","Null-check the path variable before the call."],"exampleFix":"// before\nCImg<float> gray(64,64,1,1,0); gray.save_webp(path); // spectrum=1\n// after\nCImg<float> rgb = gray.get_channels(0,0,0,2); // expand to 3 channels\nif (path) rgb.save_webp(path);","handlingStrategy":"type-guard","validationCode":"if (!filename || !*filename) { /* abort or default path */ }\nif (img.spectrum()!=3 && img.spectrum()!=4) { /* convert channels first */ }","typeGuard":"bool webpReady(const CImg<T>& img, const char* p){ return p && *p && (img.spectrum()==3 || img.spectrum()==4); }","tryCatchPattern":"try { img.save_webp(path); } catch (CImgArgumentException& e) { fprintf(stderr,\"WebP save failed: %s\\n\", e.what()); }","preventionTips":["Null-check the output path before save_webp","Convert grayscale/2-channel images to RGB/RGBA before WebP export","Use PNG for grayscale or unusual channel counts","Default missing output paths to a concrete filename"],"tags":["cimg","save","null-pointer","webp"],"backgroundTag":"null-argument","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"}