{"record":{"id":"574774b4072bb443","repo":"Yalantis/uCrop","slug":"save-pnk-instance-is-multispectral-only-the-fi","errorCode":null,"errorMessage":"save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.","messagePattern":"save_pnk\\(\\): Instance is multispectral, only the first channel will be saved in file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62415,"sourceCode":"      \\param filename Filename, as a C-string.\n    **/\n    const CImg<T>& save_pnk(const char *const filename) const {\n      return _save_pnk(0,filename);\n    }\n\n    //! Save image as a PNK file \\overloading.\n    const CImg<T>& save_pnk(std::FILE *const file) const {\n      return _save_pnk(file,0);\n    }\n\n    const CImg<T>& _save_pnk(std::FILE *const file, const char *const filename) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_pnk(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_spectrum>1)\n        cimg::warn(_cimg_instance\n                   \"save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n\n      const ulongT buf_size = std::min((ulongT)1024*1024,(ulongT)_width*_height*_depth);\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      const T *ptr = data(0,0,0,0);\n\n      if (!cimg::type<T>::is_float() && sizeof(T)==1 && _depth<2) // Can be saved as regular PNM file\n        _save_pnm(file,filename,0);\n      else if (!cimg::type<T>::is_float() && sizeof(T)==1) { // Save as extended P5 file: Binary byte-valued 3D\n        std::fprintf(nfile,\"P5\\n%u %u %u\\n255\\n\",_width,_height,_depth);\n        CImg<ucharT> buf((unsigned int)buf_size);\n        for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {\n          const ulongT N = std::min((ulongT)to_write,buf_size);\n          unsigned char *ptrd = buf._data;\n          for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr++);\n          cimg::fwrite(buf._data,N,nfile);","sourceCodeStart":62397,"sourceCodeEnd":62433,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62397-L62433","documentation":"save_pnk() writes a single-channel PNM (portable any-map) file; CImg warns when the instance has more than one spectral channel because only channel 0 is written and channels 1..n are silently discarded. It is emitted via cimg::warn() and does not stop the save.","triggerScenarios":"Calling save_pnk() (or save_pnm on a pnk-formatted path) on an image with _spectrum>1, e.g. an RGB or RGBA image loaded from PNG/JPEG, or a 3D volumetric image with multiple channels.","commonSituations":"Converting color images to grayscale-map files without converting channels first; pipelines that assume grayscale input but receive RGB frames (webcam or video frames); accidentally calling save_pnk instead of a color-capable saver like save_ppm or save_png.","solutions":["Convert to a single channel first, e.g. img.get_channels(0,0) or RGB->luminance via img.get_norm().normalize(0,255)","Call a multi-channel saver: save_ppm() or save_png() for RGB data","If only channel 0 is wanted explicitly, pass get_channel(0) so intent is clear"],"exampleFix":"// before\nrgbImage.save_pnk(\"out.pgm\");       // only R channel written\n// after\nrgbImage.get_norm().normalize(0,255).save_pnk(\"out.pgm\");","handlingStrategy":"validation","validationCode":"if (img.spectrum() > 1) img = img.get_norm().normalize(0,255); // or img.get_channels(0,0)\nimg.save_pnk(\"out.pgm\");","typeGuard":"bool is_single_channel(const cimg_library::CImg<T>& img) { return img.spectrum() == 1; }","tryCatchPattern":"try { img.save_pnk(\"out.pgm\"); } catch (CImgException& e) { /* handle */ }","preventionTips":["Check spectrum() before single-channel savers","Convert RGB to luminance explicitly when grayscale output is intended","Use save_ppm/save_png for color images"],"tags":["cimg","pnm","multispectral","channel-truncation"],"backgroundTag":"unsupported-operation","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"}