Yalantis/uCrop · error · CImgArgumentException

save_webp(): Specified filename is (null).

Error message

save_webp(): Specified filename is (null).

What it means

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.

Source

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

        }
      }
      }
      if (!file) cimg::fclose(nfile);
      return *this;
    }

    //! Save image as a WebP file.
    /**
      \param filename Filename, as a C-string.
      \param quality Image quality (in %)
    **/
    const CImg<T>& save_webp(const char *const filename, const int quality=100) const {
      return _save_webp(filename,quality);
    }

    const CImg<T>& _save_webp(const char *const filename, const int quality) const {
      if (!filename)
        throw CImgArgumentException(_cimg_instance
                                    "save_webp(): Specified filename is (null).",
                                    cimg_instance);
      if (_spectrum!=3 && _spectrum!=4)
        throw CImgArgumentException(_cimg_instance
                                    "save_webp(): WebP only supports (A)RGB colorspace.",
                                    cimg_instance);
#ifndef cimg_use_webp
      cimg::unused(quality);
      return save_other(filename);
#else
      std::FILE *file = cimg::fopen(filename, "wb");
      CImg<uint8_t> rgbaBuffer(size());
      T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height,
        *ptr_b = _data + 2UL*_width*_height, *ptr_a = _spectrum==3?NULL:_data + 3UL*_width*_height;
      uint8_t *ptr = rgbaBuffer._data;
      cimg_forY(*this,y) {
        cimg_forX(*this,x) {
          *(ptr++) = (T)*(ptr_r++);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Ensure a non-null filename is passed.
  2. 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.
  3. Use PNG/JPEG for grayscale or 2-channel data instead.
  4. Null-check the path variable before the call.

Example fix

// before
CImg<float> gray(64,64,1,1,0); gray.save_webp(path); // spectrum=1
// after
CImg<float> rgb = gray.get_channels(0,0,0,2); // expand to 3 channels
if (path) rgb.save_webp(path);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!filename || !*filename) { /* abort or default path */ }
if (img.spectrum()!=3 && img.spectrum()!=4) { /* convert channels first */ }

Type guard

bool webpReady(const CImg<T>& img, const char* p){ return p && *p && (img.spectrum()==3 || img.spectrum()==4); }

Try / catch

try { img.save_webp(path); } catch (CImgArgumentException& e) { fprintf(stderr,"WebP save failed: %s\n", e.what()); }

Prevention

When it happens

Trigger: 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.

Common situations: Saving single-channel masks/scans to .webp, optional-path logic passing NULL, filename from a failed config lookup.

Related errors


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