{"record":{"id":"d9c0d707d80a8e2a","repo":"Yalantis/uCrop","slug":"ycbcrtorgb-instance-is-not-a-ycbcr-image","errorCode":null,"errorMessage":"YCbCrtoRGB(): Instance is not a YCbCr image.","messagePattern":"YCbCrtoRGB\\(\\): Instance is not a YCbCr image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37314,"sourceCode":"          Y = (66*R + 129*G + 25*B + 128)/256 + 16,\n          Cb = (-38*R - 74*G + 112*B + 128)/256 + 128,\n          Cr = (112*R - 94*G - 18*B + 128)/256 + 128;\n        p1[N] = (T)cimg::cut(Y,(Tfloat)0,(Tfloat)255),\n        p2[N] = (T)cimg::cut(Cb,(Tfloat)0,(Tfloat)255),\n        p3[N] = (T)cimg::cut(Cr,(Tfloat)0,(Tfloat)255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to YCbCr color spaces \\newinstance.\n    CImg<Tuchar> get_RGBtoYCbCr() const {\n      return CImg<Tuchar>(*this,false).RGBtoYCbCr();\n    }\n\n    //! Convert pixel values from RGB to YCbCr color spaces.\n    CImg<T>& YCbCrtoRGB() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"YCbCrtoRGB(): Instance is not a YCbCr 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,512))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          Y = (Tfloat)p1[N] - 16,\n          Cb = (Tfloat)p2[N] - 128,\n          Cr = (Tfloat)p3[N] - 128,\n          R = (298*Y + 409*Cr + 128)/256,\n          G = (298*Y - 100*Cb - 208*Cr + 128)/256,\n          B = (298*Y + 516*Cb + 128)/256;\n        p1[N] = (T)cimg::cut(R,(Tfloat)0,(Tfloat)255),\n        p2[N] = (T)cimg::cut(G,(Tfloat)0,(Tfloat)255),\n        p3[N] = (T)cimg::cut(B,(Tfloat)0,(Tfloat)255);\n      }","sourceCodeStart":37296,"sourceCodeEnd":37332,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37296-L37332","documentation":"YCbCrtoRGB() converts channels back from YCbCr to RGB in place and, like all channel-wise color conversions in CImg, requires exactly 3 channels (_spectrum==3). The exception fires when the image is grayscale, RGBA, or empty. It exists because the kernel reads three channel planes directly.","triggerScenarios":"Calling img.YCbCrtoRGB() on any CImg whose spectrum() != 3, e.g. after loading a grayscale or alpha-channel image, or on an unassigned CImg.","commonSituations":"Round-trip pipelines where an intermediate step dropped or added channels; loading user-supplied images of varying channel counts; 4-channel (CMYK/RGBA) buffers mistakenly fed to YCbCr conversion.","solutions":["Check spectrum()==3 first; if 1, replicate the gray channel into 3; if 4, reduce with channels(0,2).","Ensure the image was previously converted to YCbCr on a 3-channel copy.","Construct/assign images with explicit spectrum=3 when color is expected.","Catch CImgInstanceException and report which channel count was found."],"exampleFix":"// before\nCImg<unsigned char> img(\"input.png\");\nimg.YCbCrtoRGB(); // throws if not 3 channels\n// after\nCImg<unsigned char> img(\"input.png\");\nif (img.spectrum()!=3) throw std::runtime_error(\"expected RGB image\");\nimg.YCbCrtoRGB();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) throw std::runtime_error(\"YCbCrtoRGB requires 3-channel image\");","typeGuard":"bool has3Channels(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.YCbCrtoRGB();\n} catch (const cimg_library::CImgInstanceException& e) {\n  // normalize and retry once\n  if (img.spectrum()==1) img.append(img,img,img,'c').YCbCrtoRGB();\n}","preventionTips":["Only call YCbCrtoRGB on images that passed through RGBtoYCbCr on a 3-channel image","Assert spectrum()==3 at pipeline stage boundaries","Avoid channel-count-reducing operations (e.g. mean over channels) mid-pipeline"],"tags":["cimg","image-processing","color-space","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-14T05:17:10.506Z"}