Yalantis/uCrop · error · CImgIOException

load_ascii(): Invalid ascii header in file '%s', image dimen

Error message

load_ascii(): Invalid ascii header in file '%s', image dimensions are set to (%u,%u,%u,%u).

What it means

load_ascii() read the first line expecting four unsigned integers (width,height,depth,spectrum); if any parsed dimension is 0 the header is rejected with CImgIOException. This catches files that are not valid CImg ASCII dumps.

Source

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

    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);
      const ulongT siz = size();
      ulongT off = 0;
      double val;
      T *ptr = _data;
      for (err = 1, off = 0; off<siz && err==1; ++off) {
        err = std::fscanf(nfile,"%lf%*[^0-9.eEinfa+-]",&val);
        *(ptr++) = (T)val;
      }
      if (err!=1)
        cimg::warn(_cimg_instance
                   "load_ascii(): Only %lu/%lu values read from file '%s'.",
                   cimg_instance,

View on GitHub (pinned to f788b534b4)

Solutions

  1. Verify the file starts with a line of four positive integers (e.g. '256 256 1 3').
  2. Use the correct loader for the actual file type (load_png/load_bmp/load_dlm).
  3. Regenerate or repair the ASCII dump ensuring nonzero dx,dy,dz,dc.
  4. Open the file and check it is non-empty before calling load_ascii.

Example fix

// before
img.load_ascii(path); // first line '0 0 0 0'
// after
std::ifstream in(path);
unsigned dx,dy,dz,dc; in>>dx>>dy>>dz>>dc;
if (dx&&dy&&dz&&dc) img.load_ascii(path);
else throw std::runtime_error("bad cimg ascii header");
Defensive patterns

Strategy: validation

Validate before calling

bool validCimgAsciiHeader(const char* p){ std::ifstream in(p); unsigned dx,dy,dz,dc; return static_cast<bool>(in>>dx>>dy>>dz>>dc) && dx&&dy&&dz&&dc; }

Try / catch

try { img.load_ascii(path); } catch (cimg_library::CImgIOException& e) { /* bad header: not a cimg ascii dump */ }

Prevention

When it happens

Trigger: Pointing load_ascii() at a file whose first line is not 'dx dy dz dc' integers — e.g. an empty file, a PNG mislabeled as .cimg, or a manually edited ascii dump where a dimension became 0.

Common situations: Loading wrong file type (binary or foreign format) with the ASCII loader; truncated downloads whose header line is missing; hand-written data files with zero dimensions.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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