{"record":{"id":"1c6419022998775e","repo":"Yalantis/uCrop","slug":"save-rgba-image-instance-has-not-exactly-4-chan","errorCode":null,"errorMessage":"save_rgba(): image instance has not exactly 4 channels, for file '%s'.","messagePattern":"save_rgba\\(\\): image instance has not exactly 4 channels, for file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62636,"sourceCode":"       \\param filename Filename, as a C-string.\n    **/\n    const CImg<T>& save_rgba(const char *const filename) const {\n      return _save_rgba(0,filename);\n    }\n\n    //! Save image as a RGBA file \\overloading.\n    const CImg<T>& save_rgba(std::FILE *const file) const {\n      return _save_rgba(file,0);\n    }\n\n    const CImg<T>& _save_rgba(std::FILE *const file, const char *const filename) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_rgba(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_spectrum!=4)\n        cimg::warn(_cimg_instance\n                   \"save_rgba(): image instance has not exactly 4 channels, for file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      const ulongT wh = (ulongT)_width*_height;\n      unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer;\n      const T\n        *ptr1 = data(0,0,0,0),\n        *ptr2 = _spectrum>1?data(0,0,0,1):0,\n        *ptr3 = _spectrum>2?data(0,0,0,2):0,\n        *ptr4 = _spectrum>3?data(0,0,0,3):0;\n      switch (_spectrum) {\n      case 1 : { // Scalar images\n        for (ulongT k = 0; k<wh; ++k) {\n          const unsigned char val = (unsigned char)*(ptr1++);\n          *(nbuffer++) = val;\n          *(nbuffer++) = val;","sourceCodeStart":62618,"sourceCodeEnd":62654,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62618-L62654","documentation":"save_rgba() writes raw 32-bit RGBA bytes (no header); CImg warns when the image does not have exactly 4 channels because the raw dump layout will not match the expected interleaved RGBA byte stream. Emitted via cimg::warn(), non-fatal; the save proceeds with whatever channels exist.","triggerScenarios":"Calling save_rgba() on an RGB image (_spectrum==3, no alpha), grayscale image (_spectrum==1), or any channel count != 4.","commonSituations":"Exporting images with alpha for compositors/game engines; forgetting that a loaded JPEG has 3 channels (no alpha); loading PNG without alpha and saving as RGBA.","solutions":["Ensure exactly 4 channels before saving: append an opaque alpha channel (e.g. construct CImg with spectrum 4 and fill alpha=255, or use get_channels with appended channel)","Drop to RGB and use save_rgb()/save_ppm() if no alpha is needed","Use save_png() which handles both 3- and 4-channel images with a proper header"],"exampleFix":"// before\nrgbImage.save_rgba(\"out.rgba\");     // only 3 channels\n// after\nCImg<unsigned char> rgba(rgbImage.width(), rgbImage.height(), 1, 4, 0);\ncimg_forXY(rgbImage,x,y) { rgba(x,y,0,0)=rgbImage(x,y,0,0); rgba(x,y,0,1)=rgbImage(x,y,0,1); rgba(x,y,0,2)=rgbImage(x,y,0,2); rgba(x,y,0,3)=255; }\nrgba.save_rgba(\"out.rgba\");","handlingStrategy":"validation","validationCode":"if (img.spectrum() != 4) { /* add opaque alpha or switch format */ }\nimg.save_rgba(\"out.rgba\");","typeGuard":"bool is_rgba(const cimg_library::CImg<T>& img) { return img.spectrum() == 4; }","tryCatchPattern":"try { img.save_rgba(\"out.rgba\"); } catch (CImgException& e) { /* handle */ }","preventionTips":["Check spectrum()==4 before RGBA raw saves","Build an explicit alpha channel when source lacks one","Use save_png for mixed 3/4-channel outputs"],"tags":["cimg","raw-rgba","channel-count","image-save"],"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-17T15:17:12.973Z"}