Yalantis/uCrop · warning

save_pnm(): Instance is multispectral, only the three first

Error message

save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.

What it means

Warning from CImg<T>::save_pnm() when the image has more than 3 channels (_spectrum>3). PNM supports at most 3 channels (RGB for PPM), so channels beyond the first three are dropped. The save proceeds normally.

Source

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

      return _save_pnm(file,0,bytes_per_pixel);
    }

    const CImg<T>& _save_pnm(std::FILE *const file, const char *const filename,
                             const unsigned int bytes_per_pixel=0) const {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "save_pnm(): Specified filename is (null).",
                                    cimg_instance);
      if (is_empty()) { cimg::fempty(file,filename); return *this; }

      double stmin, stmax = (double)max_min(stmin);
      if (_depth>1)
        cimg::warn(_cimg_instance
                   "save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.",
                   cimg_instance,
                   filename?filename:"(FILE*)");
      if (_spectrum>3)
        cimg::warn(_cimg_instance
                   "save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
                   cimg_instance,
                   filename?filename:"(FILE*)");
      if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
        cimg::warn(_cimg_instance
                   "save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
                   cimg_instance,
                   stmin,stmax,filename?filename:"(FILE*)");

      std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
      const T
        *ptr_r = data(0,0,0,0),
        *ptr_g = (_spectrum>=2)?data(0,0,0,1):0,
        *ptr_b = (_spectrum>=3)?data(0,0,0,2):0;
      const ulongT buf_size = std::min((ulongT)(1024*1024),(ulongT)(_width*_height*(_spectrum==1?1UL:3UL)));

      std::fprintf(nfile,"P%c\n%u %u\n%u\n",
                   (_spectrum==1?'5':'6'),_width,_height,stmax<256?255:(stmax<4096?4095:65535));

View on GitHub (pinned to f788b534b4)

Solutions

  1. Reduce to RGB before saving: img.get_channels(0,2)
  2. Handle alpha by compositing over a background or saving it as a separate image
  3. Use a format preserving 4+ channels (PNG, TIFF, .cimg)
  4. Ignore if dropping the extra channels is acceptable

Example fix

// before
rgba.save_pnm("out.ppm"); // alpha dropped
// after
rgba.get_channels(0,2).save_pnm("out.ppm");
Defensive patterns

Strategy: validation

Validate before calling

if (img.spectrum() > 3) img = img.get_channels(0,2); // then save_pnm

Try / catch

// warning only; guard by spectrum check before calling save_pnm

Prevention

When it happens

Trigger: Calling save_pnm() on an image with _spectrum>3, e.g. RGBA images (alpha dropped) or multispectral data with 4+ bands.

Common situations: Saving RGBA imagery to PPM/PGM (alpha silently discarded), or scientific multi-band data to PNM-based toolchains.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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