Hi.
First merry christmas to everyone
I've been struggling with some java code to read a pgm-file. I've looked searched a lot on the internet and looked at several different examples and my code is almost working, but i still experience some weird errors.
The PGM file format is a very simple one and the full specification can be seen here.
So i've written a method that generates and saves a pgm file in the root folder. To test it i generated an array containing image data for an image with size 24*16 and greytone values from 0 to 15. With each line of pixels i increment the greytone-value so that i get 16 lines with different greytone values. So the imagedata looks like this:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13
14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
When i open the file in an image-program that can display pgm-files it loads just fine and the image has no flaws or anything. However i get problems when trying to read the file from the root-folder back into an array of ints containing all the different pixel-values:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11
11 11 11 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12
12 12 12 12 12 12 12 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14
14 14 14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I've had different variations of errors, but so far this is the least bugged read i've been able to produce. The first line is fine. i get 24 0's which is what is supposed to be in the first line. But then it's like it skips one byte of data in every line of pixels and in the end it inserts 0's in for all those bytes that it missed earlier. But why oh why?? I know that the bytes are there! Here is my code:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.Scanner; public class testpgm { public testpgm() { } int width; int height; int[][] imgData; //ByteBuffer buffer; public void writePGM(int[] img,int w, int h, int r, String s) { String newline = new String("\n"); if(img.length==w*h) { try{ FileWriter fstream = new FileWriter(s+".pgm"); BufferedWriter out = new BufferedWriter(fstream); out.write("P5"+newline); out.write(Integer.toString(w)+" "+Integer.toString(h)+newline); out.write(Integer.toString(r-1)+newline); int i = 0; while(i<w*h) { out.write((byte)img[i]); i++; } out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } else { System.out.println("Error"); } } public static void main(String[] args) throws Exception { testpgm image = new testpgm(); int[] img = new int[24*16]; int w=0; int h=0; while(h<16) { if(w<24) { img[w+(h*24)]=h; w++; } else { h++; w=0; } } image.writePGM(img, 24, 16, 16, "billede"); try { BufferedReader d = new BufferedReader( new InputStreamReader( new DataInputStream( new FileInputStream("billede.pgm")))); String magic = d.readLine(); // first line contains P2 or P5 String line = d.readLine(); // second line contains height and width while (line.startsWith("#")) { line = d.readLine(); } Scanner s = new Scanner(line); int width = s.nextInt(); int height = s.nextInt(); line = d.readLine();// third line contains maxVal s = new Scanner(line); int maxVal = s.nextInt(); int[][] im = new int[width][height]; System.out.println(maxVal); int x=0; int y=0; int count = 0; int b = 0; try { while (count < height*width) { b = d.read(); //System.out.println(b); if ( b < 0 || b>=maxVal) break ; if ( "P5".equals(magic) ) { //Magic number p5, binary format if(x<width) { im[x][y] = b; //System.out.println("x: "+x+" y: "+y+" tone: "+b); x++; }else { y++; x=0; } } else { // Magic number p2, ASCII format im[count %width][count /width] = b ; count++; } } } catch (EOFException eof) { eof.printStackTrace(System.out) ; } System.out.println("Height=" + height); System.out.println("Width=" + height); System.out.println("Required elements=" + (height * width)); System.out.println("Obtained elements=" + count); System.out.println("value=" + im[22][14]); x = 0; y = 0; while(y < height) { if(x<width) { System.out.print(im[x][y]+" "); x++; } else { System.out.println(" "); y++; x=0; } } } catch(Throwable t) { t.printStackTrace(System.err) ; return ; } } }
I will only be working with pgm-files with magic number p5. I would be very thankful if anyone could help me figure out what is going wrong in this code.