{"record":{"id":"2a49231a9f89b1b3","repo":"Yalantis/uCrop","slug":"rgbtoycbcr-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoYCbCr(): Instance is not a RGB image.","messagePattern":"RGBtoYCbCr\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37284,"sourceCode":"        case 4 : R = X; G = 0; B = C; break;\n        default : R = C; G = 0; B = X;\n        }\n        p1[N] = (T)((R + m)*255);\n        p2[N] = (T)((G + m)*255);\n        p3[N] = (T)((B + m)*255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from HSV to RGB color spaces \\newinstance.\n    CImg<Tuchar> get_HSVtoRGB() const {\n      return CImg<Tuchar>(*this,false).HSVtoRGB();\n    }\n\n    //! Convert pixel values from RGB to YCbCr color spaces.\n    CImg<T>& RGBtoYCbCr() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoYCbCr(): 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,512))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          R = (Tfloat)p1[N],\n          G = (Tfloat)p2[N],\n          B = (Tfloat)p3[N],\n          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      }","sourceCodeStart":37266,"sourceCodeEnd":37302,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37266-L37302","documentation":"CImg's RGBtoYCbCr() converts pixel channels in place from RGB to YCbCr and only works on 3-channel images. The library throws CImgInstanceException when _spectrum!=3 because the per-channel pointer arithmetic (data(0,0,0,0..2)) assumes exactly R, G, B planes. It is a precondition check guarding in-place channel-wise transforms.","triggerScenarios":"Calling img.RGBtoYCbCr() on an image whose spectrum() is not 3 — e.g. grayscale (1 channel), RGBA (4 channels), or an empty/default-constructed CImg.","commonSituations":"Loading a grayscale PNG/JPEG or a PNG with alpha and passing it straight into a color-conversion pipeline; creating a CImg via constructor without specifying spectrum=3; channel slices or channel appended results with wrong count.","solutions":["Verify img.spectrum()==3 before calling; convert first, e.g. if (img.spectrum()==1) img.append(img,img,img,'c');","For RGBA images drop the alpha channel: img.channels(0,2) then call RGBtoYCbCr().","Ensure the image was actually loaded/assigned (an empty CImg has spectrum 0).","Wrap the call in try/catch for CImgInstanceException to surface a clear message."],"exampleFix":"// before\nCImg<unsigned char> img(\"photo.png\"); // may be grayscale or RGBA\nimg.RGBtoYCbCr(); // throws if spectrum!=3\n// after\nCImg<unsigned char> img(\"photo.png\");\nif (img.spectrum()==1) img.append(img,img,img,'c');\nelse if (img.spectrum()==4) img.channels(0,2);\nimg.RGBtoYCbCr();","handlingStrategy":"validation","validationCode":"if (img.width()==0 || img.spectrum()!=3) {\n  if (img.spectrum()==1) img.append(img,img,img,'c');\n  else if (img.spectrum()==4) img.channels(0,2);\n  else throw std::runtime_error(\"RGB image with 3 channels required\");\n}","typeGuard":"bool isRGB(const cimg_library::CImg<T>& img) { return img.spectrum()==3 && img.size()>0; }","tryCatchPattern":"try {\n  img.RGBtoYCbCr();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"Need 3-channel image, got spectrum=\" << img.spectrum() << std::endl;\n}","preventionTips":["Check spectrum() after every image load — files may be grayscale or RGBA","Use get_append(img,img,'c') to normalize single-channel images to RGB","Strip alpha with channels(0,2) before color conversions","Never call in-place color conversions on default-constructed CImg objects"],"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"}