Search code examples
javaprintingppm

Printing a PPM file - Wrong spaces


I need to print the a ppm picture but the output isn't organized as it should be, my code:

public static int[][][] read(String filename) {
        StdIn.setInput(filename);
        StdIn.readLine();
        int imgW = StdIn.readInt ();
        int imgH = StdIn.readInt ();
        int[][][] data = new int[imgH][imgW][3];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                for (int k = 0; k < data[i][j].length; k++) {
                    data[i][j][k] = StdIn.readInt();
                }
            }
        }
        return data;
    }

my output:

255 0 0 0 100 0 0 0 0 0 255 0
255 0 0 0 0 255 175 0 0 0 0 0
0 0 0 0 0 0 0 0 15 175 0 0
0 255 0 255 0 0 0 0 0 0 255 255
the correct output: (basically same like a matrix)

    0   0    0   100    0    0  0   0   0  255     0   255
    0   0    0     0  255  175  0   0   0    0     0     0
    0   0    0     0    0    0  0  15 175    0     0     0
  255   0  255     0    0    0  0   0   0  255   255   255

Solution

  • Ppm files also lists the maximum value appearing in the file, which is the 255 in the beginning of your output.

    You should add an extra StdIn.readInt(); before your loop.