{"record":{"id":"e38a8f694e88cce7","repo":"Yalantis/uCrop","slug":"save-webp-webp-only-supports-a-rgb-colorspace","errorCode":null,"errorMessage":"save_webp(): WebP only supports (A)RGB colorspace.","messagePattern":"save_webp\\(\\): WebP only supports \\(A\\)RGB colorspace\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61598,"sourceCode":"      return *this;\n    }\n\n    //! Save image as a WebP file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param quality Image quality (in %)\n    **/\n    const CImg<T>& save_webp(const char *const filename, const int quality=100) const {\n      return _save_webp(filename,quality);\n    }\n\n    const CImg<T>& _save_webp(const char *const filename, const int quality) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_webp(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (_spectrum!=3 && _spectrum!=4)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_webp(): WebP only supports (A)RGB colorspace.\",\n                                    cimg_instance);\n#ifndef cimg_use_webp\n      cimg::unused(quality);\n      return save_other(filename);\n#else\n      std::FILE *file = cimg::fopen(filename, \"wb\");\n      CImg<uint8_t> rgbaBuffer(size());\n      T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height,\n        *ptr_b = _data + 2UL*_width*_height, *ptr_a = _spectrum==3?NULL:_data + 3UL*_width*_height;\n      uint8_t *ptr = rgbaBuffer._data;\n      cimg_forY(*this,y) {\n        cimg_forX(*this,x) {\n          *(ptr++) = (T)*(ptr_r++);\n          *(ptr++) = (T)*(ptr_g++);\n          *(ptr++) = (T)*(ptr_b++);\n          if (ptr_a) *(ptr++) = (T)*(ptr_a++);\n        }","sourceCodeStart":61580,"sourceCodeEnd":61616,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61580-L61616","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Check img.spectrum() == 3 || img.spectrum() == 4 before calling save_webp and convert otherwise.","Save via a different format (e.g. save_png) if channel count cannot be changed."],"exampleFix":"// before\nimg.save_webp(\"out.webp\"); // fails when img.spectrum() != 3 && != 4\n// after\nif (img.spectrum() == 1) img.resize(-100,-100,-100,3); // grayscale -> RGB\nelse if (img.spectrum() > 4) img.channels(0,2);        // keep first 3 channels\nimg.save_webp(\"out.webp\");","handlingStrategy":"validation","validationCode":"if (img.spectrum() != 3 && img.spectrum() != 4) {\n  img.resize(-100, -100, -100, 3); // convert to RGB before saving\n}\nimg.save_webp(\"out.webp\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check img.spectrum() before format-specific saves.","Normalize images to RGB/RGBA at pipeline ingestion.","Centralize image saving in one helper that converts per-format."],"tags":["cimg","image","webp","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}