{"record":{"id":"4e956540813c50c8","repo":"Yalantis/uCrop","slug":"noise-invalid-specified-noise-type-d-should-b","errorCode":null,"errorMessage":"noise(): Invalid specified noise type %d (should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).","messagePattern":"noise\\(\\): Invalid specified noise type (.+?) \\(should be (.+?)\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":35718,"sourceCode":"#if cimg_use_openmp!=0\n          rng+=omp_get_thread_num();\n#endif\n          cimg_pragma_openmp(for)\n          cimg_rofoff(*this,off) {\n            const Tfloat\n              val0 = (Tfloat)_data[off]/sqrt2,\n              re = (Tfloat)(val0 + namplitude*cimg::grand(&rng)),\n              im = (Tfloat)(val0 + namplitude*cimg::grand(&rng));\n            Tfloat val = cimg::hypot(re,im);\n            if (val>vmax) val = vmax;\n            if (val<vmin) val = vmin;\n            _data[off] = (T)val;\n          }\n          cimg::srand(rng);\n        }\n      } break;\n      default :\n        throw CImgArgumentException(_cimg_instance\n                                    \"noise(): Invalid specified noise type %d \"\n                                    \"(should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).\",\n                                    cimg_instance,\n                                    noise_type);\n      }\n      return *this;\n    }\n\n    //! Add random noise to pixel values \\newinstance.\n    CImg<T> get_noise(const double amplitude, const unsigned int noise_type=0) const {\n      return (+*this).noise(amplitude,noise_type);\n    }\n\n    //! Linearly normalize pixel values.\n    /**\n       \\param min_value Minimum desired value of the resulting image.\n       \\param max_value Maximum desired value of the resulting image.\n       \\param constant_case_ratio In case of instance image having a constant value, tell what ratio","sourceCodeStart":35700,"sourceCodeEnd":35736,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L35700-L35736","documentation":"CImg<T>::noise() accepts a noise_type enum restricted to 0=gaussian, 1=uniform, 2=salt&pepper, 3=poisson (plus predefined constants CImg::noise_gaussian etc.). The switch in noise() has no case for any other value, so the default branch throws this CImgArgumentException. The library throws it instead of silently applying a wrong noise model.","triggerScenarios":"Calling img.noise(x, noise_type) with noise_type outside {0,1,2,3} — e.g. passing -1, 4, an uninitialized/reading an enum from config, or passing amplitude where the type was expected (swapped arguments).","commonSituations":"Exposing a 'noise type' option in app config/UI with an unvalidated numeric value; off-by-one custom enum that doesn't match CImg's; argument order mistake noise(type, amplitude) vs noise(amplitude, type).","solutions":["Clamp or validate noise_type against 0..3 (or the CImg::noise_* constants) before calling noise().","Check argument order: the first parameter is sigma/amplitude, the second is noise_type.","Map your application's noise enum to CImg's documented values explicitly instead of passing raw values.","Pass CImg::noise_gaussian / noise_uniform / noise_salt_pepper / noise_poisson constants to avoid magic numbers."],"exampleFix":"// before\nimg.noise(0.1f, 4); // invalid type, throws\n// after\nif (noiseType >= 0 && noiseType <= 3) img.noise(0.1f, noiseType);","handlingStrategy":"validation","validationCode":"bool isValidNoiseType(int t) { return t >= 0 && t <= 3; }\nif (isValidNoiseType(noiseType)) img.noise(sigma, noiseType);","typeGuard":null,"tryCatchPattern":"try {\n  img.noise(sigma, noiseType);\n} catch (const CImgArgumentException& e) {\n  std::fprintf(stderr, \"noise type %d unsupported, defaulting to gaussian\\n\", noiseType);\n  img.noise(sigma, CImg::noise_gaussian);\n}","preventionTips":["Use CImg's named constants (noise_gaussian, noise_uniform, noise_salt_pepper, noise_poisson) instead of raw ints.","Validate any user/config-supplied noise type against 0..3.","Double-check argument order: noise(sigma, type).","Clamp or map external enums to CImg's values explicitly."],"tags":["cimg","invalid-enum","image-processing","argument-validation"],"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-14T05:17:10.506Z"}