How to read PGM images with Java?
I think I'm missing something simple here (as usual)
I'm trying to read PGM images using Java Matlab does a good job – output image pixels in MATLAB (for example, a small 32 × 32) give me something like this:
1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133
However, my java reader output:
1 0 11 49 94 118 118 106 95 88 85 96 124 65533 65533 65533
Pixel values that appear to be greater than 127 are filled with 65533, although it does get some random values that are incorrect, and even almost the entire bottom row is assigned to the value - 1
This is the code I'm using:
filePath = 'imagepath.pgm'; FileInputStream fileInputStream = new FileInputStream(filePath); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); // read the header information ... int [][] data2D = new int [picWidth] [picHeight]; for (int row = 0; row < picHeight; row++) { for (int col = 0; col < picWidth; coL++) { data2D[row][col] = bufferedReader.read(); System.out.print(data2D[row][col] + " "); } System.out.println(); } fileInputStream.close();
Any ideas would be appreciated
Edit the following unsigned PGM values:
1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133 30 26 29 57 96 122 125 114 102 94 91 101 127 146 145 136 96 85 70 75 101 128 136 126 111 106 106 112 131 149 153 147 163 147 114 93 99 120 132 123 110 113 124 129 137 154 166 168 215 195 149 105 88 99 114 111 106 123 148 158 160 174 191 197 245 224 173 115 81 82 100 109 117 144 179 194 194 205 222 230 235 217 170 115 78 78 113 117 100 83 80 212 214 226 244 253 178 167 135 93 68 78 123 129 106 77 69 202 204 222 244 255 114 110 92 64 54 81 107 105 83 59 56 182 184 201 222 231 79 80 71 52 55 97 67 55 41 33 42 184 179 181 185 183 62 66 65 52 63 115 29 16 12 17 30 209 197 174 150 132 40 47 52 44 55 109 171 196 188 186 208 229 218 179 136 107 31 38 44 37 43 89 145 167 158 159 191 223 219 179 133 105 48 52 56 51 57 91 128 133 117 120 157 196 200 168 128 105 64 67 70 73 87 114 127 107 79 81 118 159 173 154 123 104 63 67 73 83 107 132 129 91 54 54 88 130 153 146 123 106
The title looks like this:
P5 # MatLab PGMWRITE file,saved 27-Jun-2002 16 16 255
Edit #2
The following is the complete output of the proof of concept Code:
Skipping unkNow token: "" Skipping unkNow token: "1^vvj_XU`|���" Skipping unkNow token: "" Skipping unkNow token: "9`z}rf^[e���`UFKe��~ojjp������r]cx�{nq|������ÕiXcroj{��������sQRdmu��������٪sNNqudSP�����]DN{�jME�����rn\@6QkiS;8�����OPG47aC7)!*�����>BA4?s" Skipping unkNow token: "" Skipping unkNow token: "" Skipping unkNow token: "�Ů��(/4,7m�ļ���ڳ�k" Skipping unkNow token: "&,%+Y������۳�i04839[��ux��Ȩ�i@CFIWrkOQv���{h?CISk��[66X���{j" Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at Test.main(Test.java:49)
Line 49 referenced in the exception thrown is:
System.out.println(String.format("Skipping unkNow token: \"%s\"",scan.next()));
I'm sure this problem is related to the fact that these image files contain ASCII text / numbers and binary image data However, if Java has no problem reading PNG, why does it lack PGM support?
Edit 3
Well, I found an effective implementation... Unfortunately, it was deprecated:
filePath = "imagepath.pgm" FileInputStream fileInputStream = new FileInputStream(filePath); DataInputStream dis = new DataInputStream(fileInputStream); StreamTokenizer streamTokenizer = new StreamTokenizer(dis); // read header text using StreamTokenizer.nextToken() data2D = new int [picWidth] [picHeight]; for (int row = 0; row < picHeight; row++) { for (int col = 0; col < picWidth; coL++) { data2D[row][col] = dis.readUnsignedByte(); System.out.print(data2D[row][col] + " "); } System.out.println(); }
According to the Java documentation, the streamtokenizer (InputStream) constructor is not recommended because datainputstream The readline() method did not correctly convert raw bytes to characters However, it seems to work in this particular case on the title and is obviously applicable to subsequent binary image data
Unfortunately, it is still deprecated, as if by mixing bufferedreaders, because the document recommends that eofexceptions be caused only after reading the header and trying to read the original bytes using datainputstream Still looking for a solution
Solution
The problem with your code is that you use the wrong class to read the raw data from the file As the BufferedReader document says:
Therefore, each call to BufferedReader's read () method actually consumes one or two bytes (based on character encoding) from the input stream, which is not what you want This also explains why you get a lot - 1: the flow is much earlier than you think
Because PGM contains ASCII decimal values, it is easy to parse using the scanner class
This is an almost untested code that shows how to read PGM images, assuming:
>It contains a single comment after the magic number (i.e. it has no line beginning with # except the second) > the PGM file is exactly 4 lines
This is the code:
String filePath = "image.pgm"; fileInputStream = new FileInputStream(filePath); Scanner scan = new Scanner(fileInputStream); // Discard the magic number scan.nextLine(); // Discard the comment line scan.nextLine(); // Read pic width,height and max value int picWidth = scan.nextInt(); int picHeight = scan.nextInt(); int maxvalue = scan.nextInt(); fileInputStream.close(); // Now parse the file as binary data fileInputStream = new FileInputStream(filePath); DataInputStream dis = new DataInputStream(fileInputStream); // look for 4 lines (i.e.: the header) and discard them int numnewlines = 4; while (numnewlines > 0) { char c; do { c = (char)(dis.readUnsignedByte()); } while (c != '\n'); numnewlines--; } // read the image data int[][] data2D = new int[picHeight][picWidth]; for (int row = 0; row < picHeight; row++) { for (int col = 0; col < picWidth; coL++) { data2D[row][col] = dis.readUnsignedByte(); System.out.print(data2D[row][col] + " "); } System.out.println(); }
Need to implement: support comment lines, the value of each element should be divided by maxvalue, error checking, malformed files, exception handling I tested it on a PGM file that uses UNIX at the end of the line, but it also works for windows
I want to emphasize that this is not a powerful and complete implementation of PGM parser This code is only used as a proof of concept to meet your needs
If you really need a powerful PGM parser, you can use the tools provided by netpbm