/** Adds Gaussian/normal i.i.d noise to each pixel in the image. */
  public static void addGaussian(ImageSInt8 img, Random rand, double sigma, int min, int max) {
    final int h = img.getHeight();
    final int w = img.getWidth();

    byte[] data = img.data;

    for (int y = 0; y < h; y++) {
      int index = img.getStartIndex() + y * img.getStride();
      for (int x = 0; x < w; x++) {
        int value = (data[index]) + (int) (rand.nextGaussian() * sigma);
        if (value < min) value = min;
        if (value > max) value = max;

        data[index++] = (byte) value;
      }
    }
  }
  /** Adds uniform i.i.d noise to each pixel in the image. Noise range is min <= X < max. */
  public static void addUniform(ImageSInt8 img, Random rand, int min, int max) {
    final int h = img.getHeight();
    final int w = img.getWidth();

    int range = max - min;

    byte[] data = img.data;

    for (int y = 0; y < h; y++) {
      int index = img.getStartIndex() + y * img.getStride();
      for (int x = 0; x < w; x++) {
        int value = (data[index]) + rand.nextInt(range) + min;
        if (value < -128) value = -128;
        if (value > 127) value = 127;

        data[index++] = (byte) value;
      }
    }
  }