{"record":{"id":"b6e7117642cfe279","repo":"Yalantis/uCrop","slug":"rgbtohsv-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoHSV(): Instance is not a RGB image.","messagePattern":"RGBtoHSV\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37214,"sourceCode":"        case 4 : R = X; G = 0; B = C; break;\n        default : R = C; G = 0; B = X;\n        }\n        p1[N] = (T)((R + m)*255);\n        p2[N] = (T)((G + m)*255);\n        p3[N] = (T)((B + m)*255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from HSL to RGB color spaces \\newinstance.\n    CImg<Tuchar> get_HSLtoRGB() const {\n      return CImg<Tuchar>(*this,false).HSLtoRGB();\n    }\n\n    //! Convert pixel values from RGB to HSV color spaces.\n    CImg<T>& RGBtoHSV() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoHSV(): 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::max(R,G,B),\n          C = M - cimg::min(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 = M<=0?0:C/M;\n        p1[N] = (T)H;\n        p2[N] = (T)S;\n        p3[N] = (T)(M/255);","sourceCodeStart":37196,"sourceCodeEnd":37232,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37196-L37232","documentation":"CImg<T>::RGBtoHSV() converts pixel values from RGB to the HSV color space and requires exactly 3 channels (R, G, B planes). Any other spectrum throws CImgInstanceException. Like the other color conversions it checks channel count only, not that the data truly encodes RGB.","triggerScenarios":"Calling img.RGBtoHSV() on grayscale (spectrum=1), RGBA (spectrum=4), or other non-3-channel images.","commonSituations":"Grayscale thumbnails or masks; PNGs with alpha channel; images from formats that load as indexed or single-channel; pipelines where a prior op changed the spectrum.","solutions":["Verify img.spectrum()==3; convert grayscale to 3 channels by replication.","Drop alpha with channels(0,2) for RGBA input.","Prefer get_RGBtoHSV() on a validated copy to keep the original intact.","Standardize inputs to RGB at load time so later conversions can assume 3 channels."],"exampleFix":"// before\nCImg<unsigned char> img(\"logo.png\"); // RGBA\nimg.RGBtoHSV(); // throws\n// after\nCImg<unsigned char> img(\"logo.png\");\nif (img.spectrum() == 4) img.channels(0,2);\nif (img.spectrum() == 1) img.resize(-100,-100,-100,3);\nimg.RGBtoHSV();","handlingStrategy":"type-guard","validationCode":"template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; }\nif (isRgb(img)) img.RGBtoHSV();","typeGuard":"template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.RGBtoHSV();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"RGBtoHSV requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Guard conversions with spectrum()==3 checks.","Convert grayscale inputs to RGB once at load time.","Drop alpha channels right after loading.","Prefer get_* variants on validated copies when the original must be preserved."],"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"}