{"record":{"id":"fe30ff38c2785c29","repo":"Yalantis/uCrop","slug":"save-rgb-image-instance-has-not-exactly-3-chann","errorCode":null,"errorMessage":"save_rgb(): image instance has not exactly 3 channels, for file '%s'.","messagePattern":"save_rgb\\(\\): image instance has not exactly 3 channels, for file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62574,"sourceCode":"      \\param filename Filename, as a C-string.\n    **/\n    const CImg<T>& save_rgb(const char *const filename) const {\n      return _save_rgb(0,filename);\n    }\n\n    //! Save image as a RGB file \\overloading.\n    const CImg<T>& save_rgb(std::FILE *const file) const {\n      return _save_rgb(file,0);\n    }\n\n    const CImg<T>& _save_rgb(std::FILE *const file, const char *const filename) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_rgb(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_spectrum!=3)\n        cimg::warn(_cimg_instance\n                   \"save_rgb(): image instance has not exactly 3 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[3*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      switch (_spectrum) {\n      case 1 : { // Scalar image\n        for (ulongT k = 0; k<wh; ++k) {\n          const unsigned char val = (unsigned char)*(ptr1++);\n          *(nbuffer++) = val;\n          *(nbuffer++) = val;\n          *(nbuffer++) = val;","sourceCodeStart":62556,"sourceCodeEnd":62592,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62556-L62592","documentation":"save_rgb() writes raw 24-bit RGB bytes (no header); CImg warns when the image does not have exactly 3 channels because the raw dump will not match the expected interleaved RGB layout — data will be misinterpreted or channels dropped depending on count. Emitted via cimg::warn(), the save still proceeds.","triggerScenarios":"Calling save_rgb() on a grayscale image (_spectrum==1), an RGBA image (_spectrum==4), or any other channel count != 3.","commonSituations":"Exporting frames for external tools that expect raw RGB; converting from a color format that kept alpha; loading grayscale images and writing them as if they were RGB.","solutions":["Append/drop channels to reach exactly 3: grayscale -> img.get_resize(...,1,1,1,3) or repeat channel; RGBA -> img.get_channels(0,2)","Use save_ppm() or save_png() which handle arbitrary channel counts with proper headers","If writing single-channel data, use save_pgm()/save_pnk() instead"],"exampleFix":"// before\ngrayImage.save_rgb(\"out.rgb\");      // not 3 channels\n// after\nCImg<unsigned char> rgb = grayImage.get_resize(-100,-100,-100,3);\nrgb.save_rgb(\"out.rgb\");","handlingStrategy":"validation","validationCode":"if (img.spectrum() != 3) img = img.get_resize(-100, -100, -100, 3); // replicate or truncate to 3 channels\nimg.save_rgb(\"out.rgb\");","typeGuard":"bool is_rgb(const cimg_library::CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try { img.save_rgb(\"out.rgb\"); } catch (CImgException& e) { /* handle */ }","preventionTips":["Assert spectrum()==3 before raw RGB dumps","Promote grayscale to 3 channels explicitly","Prefer headered formats (PNG/PPM) to avoid layout mismatches"],"tags":["cimg","raw-rgb","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"}