{"record":{"id":"2e63be88ffd30d61","repo":"Yalantis/uCrop","slug":"hsvtorgb-instance-is-not-a-hsv-image","errorCode":null,"errorMessage":"HSVtoRGB(): Instance is not a HSV image.","messagePattern":"HSVtoRGB\\(\\): Instance is not a HSV image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37245,"sourceCode":"          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);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to HSV color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtoHSV() const {\n      return CImg<Tfloat>(*this,false).RGBtoHSV();\n    }\n\n    //! Convert pixel values from HSV to RGB color spaces.\n    CImg<T>& HSVtoRGB() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"HSVtoRGB(): Instance is not a HSV 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        Tfloat\n          H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),\n          S = (Tfloat)p2[N],\n          V = (Tfloat)p3[N],\n          C = V*S,\n          X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),\n          m = V - C;\n        Tfloat R, G, B;\n        switch ((int)H) {\n        case 0 : R = C; G = X; B = 0; break;\n        case 1 : R = X; G = C; B = 0; break;","sourceCodeStart":37227,"sourceCodeEnd":37263,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37227-L37263","documentation":"CImg<T>::HSVtoRGB() converts HSV values back to RGB and requires exactly 3 channels (H, S, V planes). Any other spectrum throws CImgInstanceException. It validates only the channel count, so 3-channel data that isn't HSV will convert silently but incorrectly.","triggerScenarios":"Calling img.HSVtoRGB() on grayscale (spectrum=1) or RGBA (spectrum=4) images, or after channel-count changed between RGBtoHSV() and HSVtoRGB().","commonSituations":"Round-trip broken by grayscale save/load or alpha-channel operations; applying HSVtoRGB to raw single-channel hue or brightness buffers.","solutions":["Ensure spectrum()==3 before calling (replicate grayscale channels; strip alpha).","Assert img.spectrum()==3 between the forward and inverse conversions.","Audit intermediate steps (save/load, resize with spectrum change) that could alter the channel count.","If the image is grayscale it needs no HSV round trip — skip the conversion instead of forcing channels."],"exampleFix":"// before\nCImg<float> hsv = rgb.get_RGBtoHSV();\nhsv = hsv.get_channel(2); // kept only V\nhsv.HSVtoRGB(); // throws\n// after\nCImg<float> hsv = rgb.get_RGBtoHSV(); // keep all 3 channels\nhsv.HSVtoRGB(); // ok","handlingStrategy":"type-guard","validationCode":"template<typename T> bool is3Channel(const CImg<T>& img) { return img.spectrum() == 3; }\nif (is3Channel(img)) img.HSVtoRGB();","typeGuard":"template<typename T> bool isHsvShape(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.HSVtoRGB();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"HSVtoRGB requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Keep HSV round trips in one function with no intervening channel ops.","Assert spectrum()==3 between RGBtoHSV and HSVtoRGB.","Skip conversion for grayscale data rather than replicating channels.","Check spectrum after any load, save, or resize."],"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-14T05:17:10.506Z"}