{"record":{"id":"b9aa34a3178a5e4c","repo":"Yalantis/uCrop","slug":"quantize-invalid-quantization-request-with-0-va","errorCode":null,"errorMessage":"quantize(): Invalid quantization request with 0 values.","messagePattern":"quantize\\(\\): Invalid quantization request with 0 values\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":35958,"sourceCode":"    //! Cut pixel values in specified range \\newinstance.\n    CImg<T> get_abscut(const T& min_value, const T& max_value, const T& offset) const {\n      return (+*this).abscut(min_value,max_value,offset);\n    }\n\n    //! Uniformly quantize pixel values.\n    /**\n       \\param nb_levels Number of quantization levels.\n       \\param keep_range Tells if resulting values keep the same range as the original ones.\n       \\par Example\n       \\code\n       const CImg<float> img(\"reference.jpg\"), res = img.get_quantize(4);\n       (img,res).display();\n       \\endcode\n       \\image html ref_quantize.jpg\n    **/\n    CImg<T>& quantize(const unsigned int nb_levels, const bool keep_range=true) {\n      if (!nb_levels)\n        throw CImgArgumentException(_cimg_instance\n                                    \"quantize(): Invalid quantization request with 0 values.\",\n                                    cimg_instance);\n\n      if (is_empty()) return *this;\n      Tfloat m, M = (Tfloat)max_min(m), range = M - m;\n      if (range>0) {\n        if (keep_range)\n          cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))\n          cimg_rofoff(*this,off) {\n            const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);\n            _data[off] = (T)(m + std::min(val,nb_levels - 1)*range/nb_levels);\n          } else\n          cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))\n          cimg_rofoff(*this,off) {\n            const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);\n            _data[off] = (T)std::min(val,nb_levels - 1);\n          }\n      }","sourceCodeStart":35940,"sourceCodeEnd":35976,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L35940-L35976","documentation":"CImg<T>::quantize(nb_levels, keep_range) reduces pixel values to nb_levels discrete levels. nb_levels==0 is mathematically meaningless (zero quantization levels), so the method throws this CImgArgumentException immediately. The library throws at the top of the function, before the empty-image early return, so it fires even for empty images.","triggerScenarios":"Calling img.quantize(0) or img.get_quantize(0) directly; computing nb_levels from a formula/user input that evaluates to 0 (e.g. (int)(maxValue/step) with step > maxValue, or an unvalidated config field defaulting to 0).","commonSituations":"Dynamic level count derived from image statistics or a slider that can reach 0; config files where the levels key is missing and parsed as 0; casting a small float level count to unsigned int truncating to 0 (e.g. 0.4 -> 0).","solutions":["Validate nb_levels >= 1 (typically >= 2 for meaningful output) before calling quantize().","Clamp computed level counts: nb_levels = std::max(2u, computed).","Fix the source of the zero: default missing config to a sane level count like 256.","Check float-to-unsigned truncation; round instead of truncating when deriving levels from a float."],"exampleFix":"// before\nunsigned levels = (unsigned)(maxVal / step); // can be 0\nimg.quantize(levels); // throws when 0\n// after\nunsigned levels = std::max(2u, (unsigned)std::round(maxVal / step));\nimg.quantize(levels);","handlingStrategy":"validation","validationCode":"bool canQuantize(unsigned int nbLevels) { return nbLevels >= 2; }\nif (canQuantize(levels)) img.quantize(levels, keepRange);","typeGuard":null,"tryCatchPattern":"try {\n  img.quantize(nbLevels);\n} catch (const CImgArgumentException& e) {\n  std::fprintf(stderr, \"quantize needs >=1 level, got %u; using 256\\n\", nbLevels);\n  img.quantize(256);\n}","preventionTips":["Clamp computed level counts to at least 2 with std::max.","Round rather than truncate when deriving levels from floats.","Default missing config values to a sensible level count (e.g. 256).","Validate user-facing level inputs at the boundary (UI/config)."],"tags":["cimg","argument-validation","image-processing","zero-value"],"backgroundTag":"invalid-argument-value","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"}