{"record":{"id":"e4be9e9bf96ae177","repo":"Yalantis/uCrop","slug":"rgbtocmy-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoCMY(): Instance is not a RGB image.","messagePattern":"RGBtoCMY\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37402,"sourceCode":"          R = (Y + 1.140f*V)*255,\n          G = (Y - 0.395f*U - 0.581f*V)*255,\n          B = (Y + 2.032f*U)*255;\n        p1[N] = (T)cimg::cut(R,0,255),\n        p2[N] = (T)cimg::cut(G,0,255),\n        p3[N] = (T)cimg::cut(B,0,255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from YUV to RGB color spaces \\newinstance.\n    CImg<Tuchar> get_YUVtoRGB() const {\n      return CImg< Tuchar>(*this,false).YUVtoRGB();\n    }\n\n    //! Convert pixel values from RGB to CMY color spaces.\n    CImg<T>& RGBtoCMY() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoCMY(): 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],\n          G = (Tfloat)p2[N],\n          B = (Tfloat)p3[N],\n          C = 255 - R,\n          M = 255 - G,\n          Y = 255 - B;\n        p1[N] = (T)cimg::cut(C,0,255),\n        p2[N] = (T)cimg::cut(M,0,255),\n        p3[N] = (T)cimg::cut(Y,0,255);\n      }","sourceCodeStart":37384,"sourceCodeEnd":37420,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37384-L37420","documentation":"RGBtoCMY() converts RGB channels to CMY in place and requires exactly 3 channels; CImg throws CImgInstanceException when _spectrum!=3. The three channel planes are accessed directly, so any other channel count is invalid input for this operation.","triggerScenarios":"Calling img.RGBtoCMY() on a 1-channel grayscale image, a 4-channel RGBA/CMYK image, or an empty CImg.","commonSituations":"Print pipelines where scanned images may be grayscale or CMYK; loading JPEGs with alpha or PNGs with alpha; forgetting to normalize input to RGB before conversion.","solutions":["Guard with if (img.spectrum()==3).","Convert grayscale to RGB: img.get_append(img,img,'c').","Convert CMYK input via CMYKtoCMY() first, then CMYtoRGB(), before RGBtoCMY() if needed.","Catch CImgInstanceException to handle non-RGB inputs gracefully."],"exampleFix":"// before\nCImg<unsigned char> img(\"scan.png\"); // grayscale\nimg.RGBtoCMY();\n// after\nCImg<unsigned char> img(\"scan.png\");\nif (img.spectrum()==1) img.append(img,img,img,'c');\nimg.RGBtoCMY();","handlingStrategy":"validation","validationCode":"if (img.spectrum()==1) img.append(img,img,img,'c');\nif (img.spectrum()!=3) throw std::runtime_error(\"RGBtoCMY requires 3-channel RGB image\");","typeGuard":"bool isRGB(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.RGBtoCMY();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"Non-RGB input: spectrum=\" << img.spectrum() << std::endl;\n}","preventionTips":["Convert to RGB explicitly (via append/channels) right after loading","Handle CMYK sources with CMYKtoCMY().CMYtoRGB() before re-converting","Check spectrum() on all user-supplied files"],"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"}