3 import java.awt.image.*;
58 private int width, height;
59 private boolean rawBits;
60 private boolean sixteenBits;
62 public void run(String path) {
65 File fp =
new File(path);
70 catch (IOException e) {
71 String msg = e.getMessage();
81 public ImageProcessor openFile(String path)
throws IOException {
85 FileInputStream is =
new FileInputStream(path);
86 Reader r =
new BufferedReader(
new InputStreamReader(is));
87 StreamTokenizer tok =
new StreamTokenizer(r);
94 tok.wordChars(33, 255);
95 tok.whitespaceChars(0,
' ');
97 tok.eolIsSignificant(
true);
102 return open16bitRawImage(is, width, height);
104 return open16bitAsciiImage(tok, width, height);
106 byte[] pixels =
new byte[width*height];
107 ImageProcessor ip =
new ByteProcessor(width, height, pixels, null);
109 openRawImage(is, width*height, pixels);
111 openAsciiImage(tok, width*height, pixels);
116 public void openHeader(StreamTokenizer tok)
throws IOException {
117 String magicNumber = getWord(tok);
118 if (magicNumber.equals(
"P5"))
120 else if (!magicNumber.equals(
"P2"))
121 throw new IOException(
"PGM files must start with \"P2\" or \"P5\"");
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..");
131 String msg =
"The maximum gray value is larger than ";
132 if (sixteenBits && maxValue>65535)
133 throw new IOException(msg +
"65535.");
136 public void openAsciiImage(StreamTokenizer tok,
int size, byte[] pixels)
throws IOException {
139 while (tok.nextToken() != tok.TT_EOF) {
140 if (tok.ttype==tok.TT_NUMBER) {
141 pixels[i++] = (byte)(((
int)tok.nval)&255);
143 IJ.showProgress(0.5+((
double)i/size)/2.0);
146 IJ.showProgress(1.0);
149 public void openRawImage(InputStream is,
int size, byte[] pixels)
throws IOException {
151 while (count<size && count>=0)
152 count = is.read(pixels, count, size-count);
155 public ImageProcessor open16bitRawImage(InputStream is,
int width,
int height)
throws IOException {
156 int size = width*height*2;
157 byte[] bytes =
new byte[size];
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));
164 return new ShortProcessor(width, height, pixels, null);
167 public ImageProcessor open16bitAsciiImage(StreamTokenizer tok,
168 int width,
int height)
throws IOException {
170 int size = width * height;
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);
177 IJ.showProgress(0.5+((
double)i/size)/2.0);
180 IJ.showProgress(1.0);
181 return new ShortProcessor(width, height, pixels, null);
184 String getWord(StreamTokenizer tok)
throws IOException {
185 while (tok.nextToken() != tok.TT_EOF) {
186 if (tok.ttype==tok.TT_WORD)
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;