{"record":{"id":"cb5421987d02e5c8","repo":"Yalantis/uCrop","slug":"xyztorgb-instance-is-not-a-xyz-image","errorCode":null,"errorMessage":"XYZtoRGB(): Instance is not a XYZ image.","messagePattern":"XYZtoRGB\\(\\): Instance is not a XYZ image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37568,"sourceCode":"          p2[N] = (T)(0.22248840*R + 0.71690369*G + 0.06060791*B);\n          p3[N] = (T)(0.01391602*R + 0.09706116*G + 0.71392822*B);\n        }\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to XYZ color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtoXYZ(const bool use_D65=true) const {\n      return CImg<Tfloat>(*this,false).RGBtoXYZ(use_D65);\n    }\n\n    //! Convert pixel values from XYZ to RGB color spaces.\n    /**\n       \\param use_D65 Tell to use the D65 illuminant (D50 otherwise).\n    **/\n    CImg<T>& XYZtoRGB(const bool use_D65=true) {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"XYZtoRGB(): Instance is not a XYZ 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          X = (Tfloat)p1[N]*255,\n          Y = (Tfloat)p2[N]*255,\n          Z = (Tfloat)p3[N]*255;\n        if (use_D65) {\n          p1[N] = (T)cimg::cut(3.2404542*X - 1.5371385*Y - 0.4985314*Z,0,255);\n          p2[N] = (T)cimg::cut(-0.9692660*X + 1.8760108*Y + 0.0415560*Z,0,255);\n          p3[N] = (T)cimg::cut(0.0556434*X - 0.2040259*Y + 1.0572252*Z,0,255);\n        } else {\n          p1[N] = (T)cimg::cut(3.134274799724*X  - 1.617275708956*Y - 0.490724283042*Z,0,255);\n          p2[N] = (T)cimg::cut(-0.978795575994*X + 1.916161689117*Y + 0.033453331711*Z,0,255);","sourceCodeStart":37550,"sourceCodeEnd":37586,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37550-L37586","documentation":"XYZtoRGB() converts CIE XYZ values back to RGB in place and requires exactly 3 channels (_spectrum==3); the same spectrum check as the other conversions triggers this exception when it fails. It reads and writes the three channel planes directly.","triggerScenarios":"Calling img.XYZtoRGB() on grayscale, RGBA, or empty images, or on XYZ buffers produced with a different channel layout.","commonSituations":"Color pipelines where XYZ intermediates were stored with extra channels (e.g.XYZA); grayscale XYZ luminance-only buffers; round-trip code skipping the spectrum check.","solutions":["Guard with if (img.spectrum()==3) before the call.","Reduce 4-channel XYZ buffers with channels(0,2).","Expand 1-channel luminance buffers with get_append before conversion.","Catch CImgInstanceException and log img.spectrum() for diagnosis."],"exampleFix":"// before\nCImg<float> xyz = loadXYZ(); // may include alpha\nxyz.XYZtoRGB();\n// after\nCImg<float> xyz = loadXYZ();\nif (xyz.spectrum()==4) xyz.channels(0,2);\nassert(xyz.spectrum()==3);\nxyz.XYZtoRGB();","handlingStrategy":"validation","validationCode":"if (img.spectrum()==4) img.channels(0,2);\nif (img.spectrum()!=3) throw std::runtime_error(\"XYZ image must have 3 channels\");","typeGuard":"bool isXYZ(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.XYZtoRGB();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"XYZtoRGB needs 3 channels; got \" << img.spectrum() << std::endl;\n}","preventionTips":["Keep XYZ intermediates at exactly 3 channels (no alpha)","Check spectrum() before the reverse conversion in round-trip code","Validate channel count after any intermediate serialization/deserialization"],"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"}