Yalantis/uCrop · error · CImgArgumentException

save_webp(): WebP only supports (A)RGB colorspace.

Error message

save_webp(): WebP only supports (A)RGB colorspace.

What it means

CImg's save_webp() refuses to encode images whose spectrum (number of channels) is neither 3 (RGB) nor 4 (RGBA). WebP encoding in CImg only supports (A)RGB buffers, so any image with 1, 2, or more than 4 channels is rejected with a CImgArgumentException before encoding is attempted.

Source

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

      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++);
          *(ptr++) = (T)*(ptr_g++);
          *(ptr++) = (T)*(ptr_b++);
          if (ptr_a) *(ptr++) = (T)*(ptr_a++);
        }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Convert the image to RGB/RGBA before saving: img.get_channels(0,0,0,0,0,2) or resize the spectrum, e.g. use get_resize(...,-100,-100,-100,3) or fill/append channels.
  2. Check img.spectrum() == 3 || img.spectrum() == 4 before calling save_webp and convert otherwise.
  3. Save via a different format (e.g. save_png) if channel count cannot be changed.

Example fix

// before
img.save_webp("out.webp"); // fails when img.spectrum() != 3 && != 4
// after
if (img.spectrum() == 1) img.resize(-100,-100,-100,3); // grayscale -> RGB
else if (img.spectrum() > 4) img.channels(0,2);        // keep first 3 channels
img.save_webp("out.webp");
Defensive patterns

Strategy: validation

Validate before calling

if (img.spectrum() != 3 && img.spectrum() != 4) {
  img.resize(-100, -100, -100, 3); // convert to RGB before saving
}
img.save_webp("out.webp");

Prevention

When it happens

Trigger: Calling img.save_webp(file) on a CImg whose _spectrum is not 3 or 4 — e.g. a grayscale image (_spectrum==1) loaded from a single-channel source, or a hyperspectral/multi-channel image with more than 4 channels.

Common situations: Developers load grayscale or custom channel-count images (e.g. from raw data, scientific formats, or after channel manipulation) and save them as WebP without first converting to RGB/RGBA. Also common when a pipeline switches output format to webp but the image tensor still has 1 or >4 channels.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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