{"record":{"id":"6ed6eb957850af06","repo":"Yalantis/uCrop","slug":"rgbtohsi-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoHSI(): Instance is not a RGB image.","messagePattern":"RGBtoHSI\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37068,"sourceCode":"      cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))\n      cimg_rofoff(*this,off) {\n        const Tfloat\n          val = (Tfloat)_data[off]/255,\n          sval = (Tfloat)(val<=0.0031308f?val*12.92f:1.055f*std::pow(val,0.416667f) - 0.055f);\n        _data[off] = (T)cimg::cut(sval*255,0,255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to sRGB color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtosRGB() const {\n      return CImg<Tfloat>(*this,false).RGBtosRGB();\n    }\n\n    //! Convert pixel values from RGB to HSI color spaces.\n    CImg<T>& RGBtoHSI() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoHSI(): Instance is not a RGB image.\",\n                                    cimg_instance);\n\n      T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);\n      const longT whd = (longT)width()*height()*depth();\n      cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          R = (Tfloat)p1[N],\n          G = (Tfloat)p2[N],\n          B = (Tfloat)p3[N],\n          m = cimg::min(R,G,B),\n          M = cimg::max(R,G,B),\n          C = M - m,\n          sum = R + G + B,\n          H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),\n          S = sum<=0?0:1 - 3*m/sum,\n          I = sum/(3*255);","sourceCodeStart":37050,"sourceCodeEnd":37086,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37050-L37086","documentation":"CImg<T>::RGBtoHSI() converts pixel values from the RGB color space to HSI and requires exactly 3 channels (_spectrum == 3), one each for R, G and B. If the image has a different channel count the conversion is meaningless, so it throws CImgInstanceException. Note it checks only channel count — it cannot verify the data actually is RGB, so calling it on a 3-channel BGR or Lab image produces wrong colors, not an exception.","triggerScenarios":"Calling img.RGBtoHSI() on a grayscale image (spectrum=1), an RGBA image (spectrum=4), or any image whose spectrum != 3.","commonSituations":"Grayscale-loaded photos or masks that were never converted to RGB; RGBA images loaded from PNG with alpha and not reduced to 3 channels; assuming a loaded image is RGB without checking channels().","solutions":["Check img.spectrum() == 3 before the call; if grayscale, replicate channels (e.g. img.get_resize(..., -100, -100, -100, 3) or construct a 3-channel copy).","If RGBA, drop the alpha channel with img.get_channels(0,2) first.","Then call RGBtoHSI on the 3-channel result.","Audit the load path to force 3-channel loading where possible."],"exampleFix":"// before\nCImg<unsigned char> img(\"photo.png\"); // RGBA, spectrum=4\nimg.RGBtoHSI(); // throws\n// after\nCImg<unsigned char> img(\"photo.png\");\nif (img.spectrum() == 4) img.channels(0,2);\nif (img.spectrum() == 1) { unsigned char g = img(0,0,0,0); img.resize(-100,-100,-100,3); } // replicate as needed\nimg.RGBtoHSI();","handlingStrategy":"type-guard","validationCode":"bool isRgbLike(const CImg<T>& img) { return img.spectrum() == 3; }\ntemplate<typename T> bool isRgbLike(const CImg<T>& img) { return img.spectrum() == 3; }\nif (isRgbLike(img)) img.RGBtoHSI();","typeGuard":"template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.RGBtoHSI();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"RGBtoHSI requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Check spectrum() before every color-space conversion.","Drop alpha (channels(0,2)) immediately after loading RGBA images.","Convert grayscale to 3 channels once at load time.","Remember the check is channel count only — confirm the data really is RGB."],"tags":["cimg","colorspace","image-processing","channel-count"],"backgroundTag":"incompatible-source-type","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}