{"record":{"id":"bef6249e087a8ddd","repo":"Yalantis/uCrop","slug":"rgbtoxyz-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoXYZ(): Instance is not a RGB image.","messagePattern":"RGBtoXYZ\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37532,"sourceCode":"          K = (Tfloat)ps4[N],\n          K1 = 1 - K/255,\n          nC = C*K1 + K,\n          nM = M*K1 + K,\n          nY = Y*K1 + K;\n        pd1[N] = (Tfloat)cimg::cut(nC,0,255),\n        pd2[N] = (Tfloat)cimg::cut(nM,0,255),\n        pd3[N] = (Tfloat)cimg::cut(nY,0,255);\n      }\n      return res;\n    }\n\n    //! Convert pixel values from RGB to XYZ color spaces.\n    /**\n       \\param use_D65 Tell to use the D65 illuminant (D50 otherwise).\n    **/\n    CImg<T>& RGBtoXYZ(const bool use_D65=true) {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoXYZ(): 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,2048))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          R = (Tfloat)p1[N]/255,\n          G = (Tfloat)p2[N]/255,\n          B = (Tfloat)p3[N]/255;\n        if (use_D65) { // D65\n          p1[N] = (T)(0.4124564*R + 0.3575761*G + 0.1804375*B);\n          p2[N] = (T)(0.2126729*R + 0.7151522*G + 0.0721750*B);\n          p3[N] = (T)(0.0193339*R + 0.1191920*G + 0.9503041*B);\n        } else { // D50\n          p1[N] = (T)(0.43603516*R + 0.38511658*G + 0.14305115*B);\n          p2[N] = (T)(0.22248840*R + 0.71690369*G + 0.06060791*B);","sourceCodeStart":37514,"sourceCodeEnd":37550,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37514-L37550","documentation":"RGBtoXYZ() converts pixel values from sRGB to the CIE XYZ color space in place (D65 by default) and requires exactly 3 channels. CImg throws CImgInstanceException when _spectrum!=3 because the transform operates directly on the three channel planes.","triggerScenarios":"Calling img.RGBtoXYZ() (or RGBtoLab/RGBtoHSL chains built on it) on grayscale, RGBA, or empty images.","commonSituations":"Color-management code paths fed grayscale or alpha-bearing images; scientific imaging with multi-band (spectrum>3) data; un-initialized CImg buffers.","solutions":["Guard with if (img.spectrum()==3) before the call.","Expand grayscale: img.get_append(img,img,'c').","Reduce 4-channel images with channels(0,2) or select bands explicitly.","Catch CImgInstanceException to handle non-RGB input."],"exampleFix":"// before\nCImg<float> img = loadMultispectral(); // spectrum>3\nimg.RGBtoXYZ();\n// after\nCImg<float> img = loadMultispectral();\nif (img.spectrum()==3) img.RGBtoXYZ();\nelse CImg<float> rgb = img.get_channels(0,2).RGBtoXYZ();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) {\n  if (img.spectrum()==1) img.append(img,img,img,'c');\n  else if (img.spectrum()>3) img.channels(0,2);\n}\nif (img.spectrum()!=3) throw std::runtime_error(\"RGB required for XYZ conversion\");","typeGuard":"bool isRGB(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.RGBtoXYZ();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"RGBtoXYZ needs 3 channels; got \" << img.spectrum() << std::endl;\n}","preventionTips":["For multispectral data select exactly 3 bands before color conversion","Check spectrum() after loading, since file format determines channel count","Normalize to RGB once at pipeline entry, not at each conversion site"],"tags":["cimg","image-processing","color-space","xyz","spectrum"],"backgroundTag":"type-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}