{"record":{"id":"0a5d0ce775060bd6","repo":"Yalantis/uCrop","slug":"save-yuv-specified-chroma-subsampling-u-is-inv","errorCode":null,"errorMessage":"save_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.","messagePattern":"save_yuv\\(\\): Specified chroma subsampling %u is invalid, for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68099,"sourceCode":"      \\param file File to write data to.\n      \\param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.\n      \\param is_rgb Tells if the RGB to YUV conversion must be done for saving.\n    **/\n    const CImgList<T>& save_yuv(std::FILE *const file,\n                                const unsigned int chroma_subsampling=444,\n                                const bool is_rgb=true) const {\n      return _save_yuv(file,0,chroma_subsampling,is_rgb);\n    }\n\n    const CImgList<T>& _save_yuv(std::FILE *const file, const char *const filename,\n                                 const unsigned int chroma_subsampling,\n                                 const bool is_rgb) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"save_yuv(): Specified filename is (null).\",\n                                    cimglist_instance);\n      if (chroma_subsampling!=420 && chroma_subsampling!=422 && chroma_subsampling!=444)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"save_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.\",\n                                    cimglist_instance,\n                                    chroma_subsampling,filename?filename:\"(FILE*)\");\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      const unsigned int\n        cfx = chroma_subsampling==420 || chroma_subsampling==422?2:1,\n        cfy = chroma_subsampling==420?2:1,\n        w0 = (*this)[0]._width, h0 = (*this)[0]._height,\n        width0 = w0 + (w0%cfx), height0 = h0 + (h0%cfy);\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      cimglist_for(*this,l) {\n        const CImg<T> &frame = (*this)[l];\n        cimg_forZ(frame,z) {\n          CImg<ucharT> YUV;\n          if (sizeof(T)==1 && !is_rgb &&\n              frame._width==width0 && frame._height==height0 && frame._depth==1 && frame._spectrum==3)\n            YUV.assign((unsigned char*)frame._data,width0,height0,1,3,true);\n          else {","sourceCodeStart":68081,"sourceCodeEnd":68117,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68081-L68117","documentation":"CImgList::save_yuv validates that the chroma subsampling parameter is one of the YUV formats it can write: 420, 422 or 444. Any other unsigned value is rejected before any I/O happens, thrown as a CImgArgumentException naming both the invalid value and the target file. This is pure argument validation on the caller side.","triggerScenarios":"Calling save_yuv(filename, chroma_subsampling) or the std::ostream variant with chroma_subsampling set to e.g. 0, 411, 410 or any value not in {420,422,444}.","commonSituations":"Passing an uninitialized or default-zero subsampling variable; confusing 4:1:1-style values with the supported set; loading subsampling from config where 4:2:0 is written as '420' string parsed incorrectly or as 4:2:2 variant codes.","solutions":["Set chroma_subsampling to one of 420, 422 or 444 (e.g. imglist.save_yuv(\"out.yuv\", 420)).","Check where the value originates and clamp/default it to 420 before the call.","If you need other subsamplings, convert/upsample the data yourself first, since the writer only supports these three."],"exampleFix":"// before\nlist.save_yuv(\"video.yuv\", subsampling); // subsampling == 0\n// after\nif (subsampling != 420 && subsampling != 422 && subsampling != 444) subsampling = 420;\nlist.save_yuv(\"video.yuv\", subsampling);","handlingStrategy":"validation","validationCode":"bool validChroma(unsigned s){ return s==420 || s==422 || s==444; }\n// call only if validChroma(chroma_subsampling)","typeGuard":"bool isChromaValid(unsigned s) { return s == 420 || s == 422 || s == 444; }","tryCatchPattern":"try { list.save_yuv(file, chroma); } catch (CImgArgumentException &e) { std::fprintf(stderr, \"bad chroma: %s\", e.what()); chroma = 420; }","preventionTips":["Use an enum with only 420/422/444 values instead of raw unsigned","Default uninitialized subsampling variables to 420","Validate config-derived values before passing them to save_yuv"],"tags":["argument-validation","image-io","yuv"],"backgroundTag":"invalid-enum-value","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"}