Yalantis/uCrop · error · CImgInstanceException
RGBtoHSI(): Instance is not a RGB image.
Error message
RGBtoHSI(): Instance is not a RGB image.
What it means
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.
Source
Thrown at ucrop/src/main/jni/CImg.h:37068
cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))
cimg_rofoff(*this,off) {
const Tfloat
val = (Tfloat)_data[off]/255,
sval = (Tfloat)(val<=0.0031308f?val*12.92f:1.055f*std::pow(val,0.416667f) - 0.055f);
_data[off] = (T)cimg::cut(sval*255,0,255);
}
return *this;
}
//! Convert pixel values from RGB to sRGB color spaces \newinstance.
CImg<Tfloat> get_RGBtosRGB() const {
return CImg<Tfloat>(*this,false).RGBtosRGB();
}
//! Convert pixel values from RGB to HSI color spaces.
CImg<T>& RGBtoHSI() {
if (_spectrum!=3)
throw CImgInstanceException(_cimg_instance
"RGBtoHSI(): Instance is not a RGB image.",
cimg_instance);
T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
const longT whd = (longT)width()*height()*depth();
cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
for (longT N = 0; N<whd; ++N) {
const Tfloat
R = (Tfloat)p1[N],
G = (Tfloat)p2[N],
B = (Tfloat)p3[N],
m = cimg::min(R,G,B),
M = cimg::max(R,G,B),
C = M - m,
sum = R + G + B,
H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
S = sum<=0?0:1 - 3*m/sum,
I = sum/(3*255);View on GitHub (pinned to f788b534b4)
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.
Example fix
// before
CImg<unsigned char> img("photo.png"); // RGBA, spectrum=4
img.RGBtoHSI(); // throws
// after
CImg<unsigned char> img("photo.png");
if (img.spectrum() == 4) img.channels(0,2);
if (img.spectrum() == 1) { unsigned char g = img(0,0,0,0); img.resize(-100,-100,-100,3); } // replicate as needed
img.RGBtoHSI(); Defensive patterns
Strategy: type-guard
Validate before calling
bool isRgbLike(const CImg<T>& img) { return img.spectrum() == 3; }
template<typename T> bool isRgbLike(const CImg<T>& img) { return img.spectrum() == 3; }
if (isRgbLike(img)) img.RGBtoHSI(); Type guard
template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; } Try / catch
try {
img.RGBtoHSI();
} catch (const CImgInstanceException& e) {
std::fprintf(stderr, "RGBtoHSI requires 3 channels, image has %d\n", img.spectrum());
} Prevention
- 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.
When it happens
Trigger: Calling img.RGBtoHSI() on a grayscale image (spectrum=1), an RGBA image (spectrum=4), or any image whose spectrum != 3.
Common situations: 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().
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- HSItoRGB(): Instance is not a HSI image.
- RGBtoHSL(): Instance is not a RGB image.
- HSLtoRGB(): Instance is not a HSL image.
- RGBtoHSV(): Instance is not a RGB image.
- HSVtoRGB(): Instance is not a HSV image.
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/6ed6eb957850af06.
Report an issue: GitHub.