Yalantis/uCrop · error · CImgArgumentException

save_rgba(): Specified filename is (null).

Error message

save_rgba(): Specified filename is (null).

What it means

_save_rgba() throws CImgArgumentException when both the output FILE* and the filename are null, since the raw RGBA writer has nowhere to write. Like the sibling guards, it fails fast before image processing.

Solutions

  1. Pass a valid filename to save_rgba()
  2. Pass an opened, non-null FILE*
  3. Check stream open success before the call

Example fix

// before
std::FILE *f = std::fopen(out, "wb");
img.save_rgba(f);
// after
std::FILE *f = out ? std::fopen(out, "wb") : NULL;
if (!f) throw std::runtime_error("cannot open rgba output");
img.save_rgba(f);
Defensive patterns

Strategy: validation

Validate before calling

if (!file && (!filename || !*filename)) throw std::invalid_argument("save_rgba requires a filename or FILE*");

Type guard

bool hasDestination(std::FILE *f, const char *name) { return f != NULL || (name != NULL && name[0] != '\0'); }

Try / catch

try { img.save_rgba(path.c_str()); } catch (CImgArgumentException &e) { /* handle */ }

Prevention

When it happens

Trigger: Calling save_rgba((std::FILE*)0); a null filename propagated from save()/generic dispatch; forwarding a FILE* that is null because fopen failed.

Common situations: Framebuffer/texture dump code with an unset output file; null path from optional CLI argument; failed stream open not checked.

Related errors


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

Appendix: source

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

      return *this;
    }

    //! Save image as a RGBA file.
    /**
       \param filename Filename, as a C-string.
    **/
    const CImg<T>& save_rgba(const char *const filename) const {
      return _save_rgba(0,filename);
    }

    //! Save image as a RGBA file \overloading.
    const CImg<T>& save_rgba(std::FILE *const file) const {
      return _save_rgba(file,0);
    }

    const CImg<T>& _save_rgba(std::FILE *const file, const char *const filename) const {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "save_rgba(): Specified filename is (null).",
                                    cimg_instance);
      if (is_empty()) { cimg::fempty(file,filename); return *this; }
      if (_spectrum!=4)
        cimg::warn(_cimg_instance
                   "save_rgba(): image instance has not exactly 4 channels, for file '%s'.",
                   cimg_instance,
                   filename?filename:"(FILE*)");

      std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
      const ulongT wh = (ulongT)_width*_height;
      unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer;
      const T
        *ptr1 = data(0,0,0,0),
        *ptr2 = _spectrum>1?data(0,0,0,1):0,
        *ptr3 = _spectrum>2?data(0,0,0,2):0,
        *ptr4 = _spectrum>3?data(0,0,0,3):0;
      switch (_spectrum) {

View on GitHub (pinned to f788b534b4)