H
hz010c1877
Hi,
I've got PPM image files stored in raw bytes rather than ACSII which magic
identifiers are P6 and P3 respectively. I want to read them into memory and
return a BufferedImage instance. I know how to do it for ASCII PPM image
files but have no idea for raw bytes ones. Does anyone have any suggestions?
Any help will be appreciated.
Here is the mothed for reading ASCII PPM image as BufferedImage:
// some codes are not list here for simplicity
// width and height is the width and heigh of a image
// readHeader() reads the header of PPM image
// The following is supposed to run before the parsePPm() method
// private StreamTokenizer parser;
// reader = new BufferedReader(new InputStreamReader(in));
// parser = new StreamTokenizer(reader);
// parser.commentChar('#');
// readHeader();
private BufferedImage parsePPM() throws IOException, PPMDecoderException {
BufferedImage img =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
WritableRaster raster = img.getRaster();
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
for (int i = 0; i < 3; ++i) {
parser.nextToken();
if (parser.ttype == StreamTokenizer.TT_EOF)
throw new EOFException("image appears to be truncated");
if (parser.ttype != StreamTokenizer.TT_NUMBER)
throw new PPMDecoderException("non-numeric value for sample "
+ i + " of pixel at (" + x + "," + y + ")");
raster.setSample(x, y, i, (int) parser.nval);
}
return img;
}
I've got PPM image files stored in raw bytes rather than ACSII which magic
identifiers are P6 and P3 respectively. I want to read them into memory and
return a BufferedImage instance. I know how to do it for ASCII PPM image
files but have no idea for raw bytes ones. Does anyone have any suggestions?
Any help will be appreciated.
Here is the mothed for reading ASCII PPM image as BufferedImage:
// some codes are not list here for simplicity
// width and height is the width and heigh of a image
// readHeader() reads the header of PPM image
// The following is supposed to run before the parsePPm() method
// private StreamTokenizer parser;
// reader = new BufferedReader(new InputStreamReader(in));
// parser = new StreamTokenizer(reader);
// parser.commentChar('#');
// readHeader();
private BufferedImage parsePPM() throws IOException, PPMDecoderException {
BufferedImage img =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
WritableRaster raster = img.getRaster();
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
for (int i = 0; i < 3; ++i) {
parser.nextToken();
if (parser.ttype == StreamTokenizer.TT_EOF)
throw new EOFException("image appears to be truncated");
if (parser.ttype != StreamTokenizer.TT_NUMBER)
throw new PPMDecoderException("non-numeric value for sample "
+ i + " of pixel at (" + x + "," + y + ")");
raster.setSample(x, y, i, (int) parser.nval);
}
return img;
}