{"record":{"id":"bbedabd4074b0b0e","repo":"Yalantis/uCrop","slug":"cvmat2cimg-pixel-type-s-is-not-supported","errorCode":null,"errorMessage":"_cvmat2cimg() : pixel type '%s' is not supported.","messagePattern":"_cvmat2cimg\\(\\) : pixel type '(.+?)' is not supported\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":60049,"sourceCode":"                                    cimg_instance);\n      if (_spectrum==2)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').\",\n                                    cimg_instance);\n      if (_depth!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cimg2cvmat() : Invalid number of slices (should be '1').\",\n                                    cimg_instance);\n      int mat_type = -1;\n      if (pixel_type()==cimg::type<unsigned char>::string()) mat_type = CV_8UC1;\n      if (pixel_type()==cimg::type<char>::string()) mat_type = CV_8SC1;\n      if (pixel_type()==cimg::type<unsigned short>::string()) mat_type = CV_16UC1;\n      if (pixel_type()==cimg::type<short>::string()) mat_type = CV_16SC1;\n      if (pixel_type()==cimg::type<int>::string()) mat_type = CV_32SC1;\n      if (pixel_type()==cimg::type<float>::string()) mat_type = CV_32FC1;\n      if (pixel_type()==cimg::type<double>::string()) mat_type = CV_64FC1;\n      if (mat_type<0)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cvmat2cimg() : pixel type '%s' is not supported.\",\n                                    cimg_instance,pixel_type());\n      cv::Mat res;\n      if (_spectrum>1) {\n        cv::Mat *const channels = new cv::Mat[_spectrum];\n        cimg_forC(*this,c)\n          channels[c] = cv::Mat(_height,_width,mat_type,_data + _width*_height*(_spectrum - 1 - c));\n        cv::merge(channels,_spectrum,res);\n        delete[] channels;\n      } else res = cv::Mat(_height,_width,mat_type,_data).clone();\n\n      return res;\n    }\n\n#endif\n\n    //! Load image from a camera stream, using OpenCV.\n    /**","sourceCodeStart":60031,"sourceCodeEnd":60067,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L60031-L60067","documentation":"During conversion, _cvmat2cimg (via the cv::Mat bridge in _cimg2cvmat path) builds a CV_* mat_type from the CImg pixel type; if pixel_type() matches none of uchar/char/ushort/short/int/float/double, mat_type stays -1 and CImgInstanceException is thrown. The CImg<T> instantiation uses a pixel type OpenCV cannot represent.","triggerScenarios":"Instantiating CImg with an exotic type (e.g. CImg<bool>, CImg<long>, CImg<float complex-like custom>) and calling cvmat conversion; compile-time template instantiated with a non-standard arithmetic type.","commonSituations":"Custom fixed-point or extended-precision image types; template-generic code that accidentally instantiates with an unusual T; bool images from threshold operations kept as CImg<bool>.","solutions":["Convert/cast the image to a supported type first, e.g. img.get_cast<unsigned char>() or float.","Use standard CImg<unsigned char>, <unsigned short>, <int>, <float>, or <double> for OpenCV interop.","Store binary results as uchar instead of bool."],"exampleFix":"// before\nCImg<bool> mask = img.get_threshold(128);\ncv::Mat m = mask.get_cvmat(); // unsupported type\n// after\ncv::Mat m = mask.get_cast<unsigned char>().get_cvmat();","handlingStrategy":"type-guard","validationCode":"// compile-time: only instantiate interop with supported types\nstatic_assert(std::is_same_v<T,unsigned char> || std::is_same_v<T,unsigned short> ||\n              std::is_same_v<T,short> || std::is_same_v<T,int> ||\n              std::is_same_v<T,float> || std::is_same_v<T,double>,\n              \"type not supported by cv::Mat conversion\");","typeGuard":"template<class T> bool is_cvmat_supported() { return std::is_same_v<T,unsigned char> || std::is_same_v<T,char> || std::is_same_v<T,unsigned short> || std::is_same_v<T,short> || std::is_same_v<T,int> || std::is_same_v<T,float> || std::is_same_v<T,double>; }","tryCatchPattern":"try { cv::Mat m = img.get_cvmat(); }\ncatch (CImgInstanceException& e) { img.cast<unsigned char>(); /* retry as uchar */ }","preventionTips":["Use only standard 8/16/32-bit integer and float types for OpenCV interop","Cast threshold masks from bool to uchar","Add static_asserts in templated interop code"],"tags":["cimg","opencv","pixel-type","dtype"],"backgroundTag":"unsupported-dtype","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"}