Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
PGM_Reader.java
1 package ij.plugin;
2 import java.awt.*;
3 import java.awt.image.*;
4 import java.io.*;
5 import ij.*;
6 import ij.io.*;
7 import ij.process.*;
8 
56 public class PGM_Reader extends ImagePlus implements PlugIn {
57 
58  private int width, height;
59  private boolean rawBits;
60  private boolean sixteenBits;
61 
62  public void run(String path) {
63 
64  IJ.showStatus("Opening: " + path);
65  File fp = new File(path);
66  ImageProcessor ip;
67  try {
68  ip = openFile(path);
69  }
70  catch (IOException e) {
71  String msg = e.getMessage();
72  IJ.showMessage("PGM Reader", msg.equals("")?""+e:msg);
73  return;
74  }
75 
76  setProcessor(fp.getName(), ip);
77  if (path.equals(""))
78  show();
79  }
80 
81  public ImageProcessor openFile(String path) throws IOException {
82 
83  // updated this to no longer used deprecated StringTokenizer constructor
84  // rumored not to work
85  FileInputStream is = new FileInputStream(path);
86  Reader r = new BufferedReader(new InputStreamReader(is));
87  StreamTokenizer tok = new StreamTokenizer(r);
88 
89  // used to be
90  //InputStream is = new BufferedInputStream();
91  //StreamTokenizer tok = new StreamTokenizer(is); //deprecated, but it works
92 
93  tok.resetSyntax();
94  tok.wordChars(33, 255);
95  tok.whitespaceChars(0, ' ');
96  tok.parseNumbers();
97  tok.eolIsSignificant(true);
98  tok.commentChar('#');
99  openHeader(tok);
100  if (sixteenBits)
101  if(rawBits)
102  return open16bitRawImage(is, width, height);
103  else
104  return open16bitAsciiImage(tok, width, height);
105  else {
106  byte[] pixels = new byte[width*height];
107  ImageProcessor ip = new ByteProcessor(width, height, pixels, null);
108  if (rawBits)
109  openRawImage(is, width*height, pixels);
110  else
111  openAsciiImage(tok, width*height, pixels);
112  return ip;
113  }
114  }
115 
116  public void openHeader(StreamTokenizer tok) throws IOException {
117  String magicNumber = getWord(tok);
118  if (magicNumber.equals("P5"))
119  rawBits = true;
120  else if (!magicNumber.equals("P2"))
121  throw new IOException("PGM files must start with \"P2\" or \"P5\"");
122  width = getInt(tok);
123  height = getInt(tok);
124  int maxValue = getInt(tok);
125  if (width==-1 || height==-1 || maxValue==-1)
126  throw new IOException("Error opening PGM header..");
127  if(maxValue > 255)
128  sixteenBits = true;
129  else
130  sixteenBits = false;
131  String msg = "The maximum gray value is larger than ";
132  if (sixteenBits && maxValue>65535)
133  throw new IOException(msg + "65535.");
134  }
135 
136  public void openAsciiImage(StreamTokenizer tok, int size, byte[] pixels) throws IOException {
137  int i = 0;
138  int inc = size/20;
139  while (tok.nextToken() != tok.TT_EOF) {
140  if (tok.ttype==tok.TT_NUMBER) {
141  pixels[i++] = (byte)(((int)tok.nval)&255);
142  if (i%inc==0)
143  IJ.showProgress(0.5+((double)i/size)/2.0);
144  }
145  }
146  IJ.showProgress(1.0);
147  }
148 
149  public void openRawImage(InputStream is, int size, byte[] pixels) throws IOException {
150  int count = 0;
151  while (count<size && count>=0)
152  count = is.read(pixels, count, size-count);
153  }
154 
155  public ImageProcessor open16bitRawImage(InputStream is, int width, int height) throws IOException {
156  int size = width*height*2;
157  byte[] bytes = new byte[size];
158  int count = 0;
159  while (count<size && count>=0)
160  count = is.read(bytes, count, size-count);
161  short[] pixels = new short[size/2];
162  for (int i=0,j=0; i<size/2; i++,j+=2)
163  pixels[i] = (short)(((bytes[j]&0xff)<<8) | (bytes[j+1]&0xff)); //big endian
164  return new ShortProcessor(width, height, pixels, null);
165  }
166 
167  public ImageProcessor open16bitAsciiImage(StreamTokenizer tok,
168  int width, int height) throws IOException {
169  int i = 0;
170  int size = width * height;
171  int inc = size/20; // Progress update interval
172  short[] pixels = new short[size];
173  while (tok.nextToken() != tok.TT_EOF) {
174  if (tok.ttype==tok.TT_NUMBER) {
175  pixels[i++] = (short)(((int)tok.nval)&65535);
176  if (i%inc==0)
177  IJ.showProgress(0.5+((double)i/size)/2.0);
178  }
179  }
180  IJ.showProgress(1.0);
181  return new ShortProcessor(width, height, pixels, null);
182  }
183 
184  String getWord(StreamTokenizer tok) throws IOException {
185  while (tok.nextToken() != tok.TT_EOF) {
186  if (tok.ttype==tok.TT_WORD)
187  return tok.sval;
188  }
189  return null;
190  }
191 
192  int getInt(StreamTokenizer tok) throws IOException {
193  while (tok.nextToken() != tok.TT_EOF) {
194  if (tok.ttype==tok.TT_NUMBER)
195  return (int)tok.nval;
196  }
197  return -1;
198  }
199 
200 }
201 
202