Yalantis/uCrop · error · CImgArgumentException

noise(): Invalid specified noise type %d (should be { 0=gaus

Error message

noise(): Invalid specified noise type %d (should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).

What it means

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.

Source

Thrown at ucrop/src/main/jni/CImg.h:35718

#if cimg_use_openmp!=0
          rng+=omp_get_thread_num();
#endif
          cimg_pragma_openmp(for)
          cimg_rofoff(*this,off) {
            const Tfloat
              val0 = (Tfloat)_data[off]/sqrt2,
              re = (Tfloat)(val0 + namplitude*cimg::grand(&rng)),
              im = (Tfloat)(val0 + namplitude*cimg::grand(&rng));
            Tfloat val = cimg::hypot(re,im);
            if (val>vmax) val = vmax;
            if (val<vmin) val = vmin;
            _data[off] = (T)val;
          }
          cimg::srand(rng);
        }
      } break;
      default :
        throw CImgArgumentException(_cimg_instance
                                    "noise(): Invalid specified noise type %d "
                                    "(should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).",
                                    cimg_instance,
                                    noise_type);
      }
      return *this;
    }

    //! Add random noise to pixel values \newinstance.
    CImg<T> get_noise(const double amplitude, const unsigned int noise_type=0) const {
      return (+*this).noise(amplitude,noise_type);
    }

    //! Linearly normalize pixel values.
    /**
       \param min_value Minimum desired value of the resulting image.
       \param max_value Maximum desired value of the resulting image.
       \param constant_case_ratio In case of instance image having a constant value, tell what ratio

View on GitHub (pinned to f788b534b4)

Solutions

  1. Clamp or validate noise_type against 0..3 (or the CImg::noise_* constants) before calling noise().
  2. Check argument order: the first parameter is sigma/amplitude, the second is noise_type.
  3. Map your application's noise enum to CImg's documented values explicitly instead of passing raw values.
  4. Pass CImg::noise_gaussian / noise_uniform / noise_salt_pepper / noise_poisson constants to avoid magic numbers.

Example fix

// before
img.noise(0.1f, 4); // invalid type, throws
// after
if (noiseType >= 0 && noiseType <= 3) img.noise(0.1f, noiseType);
Defensive patterns

Strategy: validation

Validate before calling

bool isValidNoiseType(int t) { return t >= 0 && t <= 3; }
if (isValidNoiseType(noiseType)) img.noise(sigma, noiseType);

Try / catch

try {
  img.noise(sigma, noiseType);
} catch (const CImgArgumentException& e) {
  std::fprintf(stderr, "noise type %d unsupported, defaulting to gaussian\n", noiseType);
  img.noise(sigma, CImg::noise_gaussian);
}

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/4e956540813c50c8. Report an issue: GitHub.