{"record":{"id":"c5d35f5166799f5d","repo":"Yalantis/uCrop","slug":"hsitorgb-instance-is-not-a-hsi-image","errorCode":null,"errorMessage":"HSItoRGB(): Instance is not a HSI image.","messagePattern":"HSItoRGB\\(\\): Instance is not a HSI image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37102,"sourceCode":"          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);\n        p1[N] = (T)H;\n        p2[N] = (T)S;\n        p3[N] = (T)I;\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to HSI color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtoHSI() const {\n      return CImg<Tfloat>(*this,false).RGBtoHSI();\n    }\n\n    //! Convert pixel values from HSI to RGB color spaces.\n    CImg<T>& HSItoRGB() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"HSItoRGB(): Instance is not a HSI 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          H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),\n          S = (Tfloat)p2[N],\n          I = (Tfloat)p3[N],\n          Z = 1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1),\n          C = I*S/(1 + Z),\n          X = C*Z,\n          m = I*(1 - S)/3;\n        Tfloat R, G, B;\n        switch ((int)H) {\n        case 0 : R = C; G = X; B = 0; break;","sourceCodeStart":37084,"sourceCodeEnd":37120,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37084-L37120","documentation":"CImg<T>::HSItoRGB() converts HSI pixel values back to RGB and requires the image to have exactly 3 channels (H, S and I planes). With any other spectrum the conversion is undefined, so it throws CImgInstanceException. Like its RGB counterpart, it validates channel count only, not that the values are genuinely in HSI form.","triggerScenarios":"Calling img.HSItoRGB() on a grayscale (spectrum=1) or RGBA (spectrum=4) image; losing channels between RGBtoHSI() and HSItoRGB() (e.g. saving/loading in between, or channels(0,1) extraction).","commonSituations":"Round-trip pipelines where a save to a grayscale format silently drops to 1 channel before the inverse conversion; alpha-channel images; applying HSItoRGB to a raw 1-channel buffer.","solutions":["Ensure spectrum()==3 before calling; for grayscale, replicate the channel to 3 and set S=0, I=value (or just skip conversion since it's already intensity-like).","Strip alpha (channels(0,2)) before the conversion.","Check for intermediate save/load steps that change channel count between RGBtoHSI and HSItoRGB.","Wrap round trips in a sanity assert: assert(hsi.spectrum()==3)."],"exampleFix":"// before\nCImg<float> gray = hsi.get_channel(0); // someone extracted one channel\ngray.HSItoRGB(); // throws\n// after\nCImg<float> rgb = hsi; // keep all 3 channels\nrgb.HSItoRGB();","handlingStrategy":"type-guard","validationCode":"template<typename T> bool is3Channel(const CImg<T>& img) { return img.spectrum() == 3; }\nif (is3Channel(img)) img.HSItoRGB();","typeGuard":"template<typename T> bool isHsiShape(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.HSItoRGB();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"HSItoRGB requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Keep forward/inverse conversions in the same scope with no intermediate save/load.","Assert spectrum()==3 between RGBtoHSI and HSItoRGB.","Strip alpha channels before conversions.","Skip the conversion entirely for grayscale data instead of forcing channels."],"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"}