Yalantis/uCrop · error · CImgArgumentException

load_ascii(): Specified filename is (null).

Error message

load_ascii(): Specified filename is (null).

What it means

CImg<T>::load_ascii() requires either an open std::FILE* or a non-null filename; when both are null it throws CImgArgumentException before doing any I/O. This guards _load_ascii against dereferencing nothing to read from.

Source

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

    //! Load image from an ascii file \inplace.
    static CImg<T> get_load_ascii(const char *const filename) {
      return CImg<T>().load_ascii(filename);
    }

    //! Load image from an ascii file \overloading.
    CImg<T>& load_ascii(std::FILE *const file) {
      return _load_ascii(file,0);
    }

    //! Loadimage from an ascii file \newinstance.
    static CImg<T> get_load_ascii(std::FILE *const file) {
      return CImg<T>().load_ascii(file);
    }

    CImg<T>& _load_ascii(std::FILE *const file, const char *const filename) {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "load_ascii(): Specified filename is (null).",
                                    cimg_instance);

      std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
      CImg<charT> line(256); *line = 0;
      int err = std::fscanf(nfile,"%255[^\n]",line._data);
      unsigned int dx = 0, dy = 1, dz = 1, dc = 1;
      cimg_sscanf(line,"%u%*c%u%*c%u%*c%u",&dx,&dy,&dz,&dc);
      err = std::fscanf(nfile,"%*[^0-9.eEinfa+-]");
      if (!dx || !dy || !dz || !dc) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                              "load_ascii(): Invalid ascii header in file '%s', image dimensions are set "
                              "to (%u,%u,%u,%u).",
                              cimg_instance,
                              filename?filename:"(FILE*)",dx,dy,dz,dc);
      }
      assign(dx,dy,dz,dc);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Pass a valid filename string or an already-open FILE* to load_ascii.
  2. Null-check the path variable before calling and report a clearer error upstream.
  3. If loading from memory, use load_cimg on a stream/buffer instead of ASCII file loading.

Example fix

// before
img.load_ascii(filename); // filename == nullptr
// after
if (filename) img.load_ascii(filename);
else throw std::runtime_error("ascii filename not set");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!filename) throw std::invalid_argument("load_ascii requires a filename");

Type guard

bool hasAsciiSource(std::FILE* f, const char* name){ return f != nullptr || name != nullptr; }

Try / catch

try { img.load_ascii(filename); } catch (cimg_library::CImgArgumentException& e) { /* null filename */ }

Prevention

When it happens

Trigger: Calling load_ascii(nullptr) or load_ascii((std::FILE*)0, nullptr) — i.e. the argument-dispatch overload resolved to both file and filename being NULL.

Common situations: Passing an uninitialized/NULL CImgFileAssigner or forgetting to supply the path; forwarding a filename variable that was never set.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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