Search code examples
javaarrays2dnoisepgm

Adding Noise to a PGM image using 2D arrays and for loops?


I have an assignment and I am trying to create noise within an image similarly to how I did here:

public static short[][] brighten(short[][] orig, short amount) {

    short[][] returnArray = new short[orig.length][orig[0].length];
    for(int i = 0; i < orig.length; ++i){
        for(int j = 0; j < orig[0].length; ++j){
            returnArray[i][j] = (short)(orig[i][j]+amount);
        }

    }
    return returnArray;
}

The instructions: public short[][] Noise(short[][], short) - Is passed a 2D array of shorts which represents an image and another short which is the quantity of noise to add or subtract from the image. -Returns a 2D array of shorts which is the darkened image -For each item in the array, randomly either add or subtract a random value up to the short parameter


Solution

  • Apart from the randomness missing, I don't see any question, so I assume that it's about this:

    public static short[][] brighten(short[][] orig, short amount) {
        Random random = new Random();
        short[][] returnArray = new short[orig.length][orig[0].length];
        for(int i = 0; i < orig.length; ++i){
            for(int j = 0; j < orig[0].length; ++j){
                int randomValue = -amount + random.nextInt(amount+amount);
                returnArray[i][j] = (short)(orig[i][j]+randomValue);
            }
        }
        return returnArray;
    }