title: How to read PGM images in Java?
body: <p>I feel like I'm missing something simple here (as usual).</p>
  
  <p>I'm trying to read PGM images using Java. Matlab does it just fine - outputting the image pixels (for example, a small 32x32 image) in Matlab gives me something like this:</p>
  
  <pre>1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133</pre>
  
  <p>My Java reader, however, outputs this:</p>
  
  <pre>1 0 11 49 94 118 118 106 95 88 85 96 124 65533 65533 65533 </pre>
  
  <p>It seems like pixel values above 127 are filled in with 65533, though it does get some random values incorrect, and even assigns almost the entire bottom row to the value of -1.</p>
  
  <p>Here's the code I'm using:</p>
  
  <pre>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 &lt; picHeight; row++) {
    for (int col = 0; col &lt; picWidth; col++) {
      data2D[row][col] = bufferedReader.read();
      System.out.print(data2D[row][col] + " ");
    }
    System.out.println();
  }
  
  fileInputStream.close();</pre>
  
  <p>Any ideas would be greatly appreciated.</p>
  
  <p><strong>Edit</strong> Here are the unsigned PGM values:</p>
  
  <pre>     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</pre>
  
  <p>The header looks like this:</p>
  
  <pre>P5
  # MatLab PGMWRITE file, saved 27-Jun-2002
  16 16
  255</pre>
  
  <p><strong>Edit #2</strong></p>
  
  <p>Here's the full output to the proof of concept code below:</p>
  
  <pre>
  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)
  </pre>
  
  <p>Line 49 referred to in the thrown exception is:</p>
  
  <pre><code>System.out.println(String.format("Skipping unknow token: \"%s\"", scan.next()));
  </code></pre>
  
  <p>The problem, I'm sure, has something to do with the fact that these image files consist of both ASCII text/numbers as well as binary image data. But if Java has no problem reading PNGs, why the lack of support for PGMs?</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Ok, I found an implementation that works...unfortunately, it's deprecated:</p>
  
  <pre><code>  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 &lt; picHeight; row++) {
      for (int col = 0; col &lt; picWidth; col++) {
        data2D[row][col] = dis.readUnsignedByte();
        System.out.print(data2D[row][col] + " ");
      }
      System.out.println();
    }
  </code></pre>
  
  <p>According to the Java documentation, the <code>StreamTokenizer(InputStream)</code> constructor is deprecated, because the <code>DataInputStream.readLine()</code> method does not correctly convert raw bytes to characters. However, it seems to work in this specific case on the header, and obviously works for the ensuing binary image data.</p>
  
  <p>Unfortunately, it's still deprecated, and it seems that by intermixing a <code>BufferedReader</code> as the documentation suggests only results in <code>EOFException</code>s after reading the header and attempting to use the <code>DataInputStream</code> to read the raw bytes. Still looking for a solution...</p>