This is a function I wrote a long time ago about ppm image reading (ie ascii images )
I use a CharArrayWriterGetbuf to get the data and a BufferManipulator
public void read() throws IOException {
int c;
int i; // colonne
int j; // ligne
int[] results;
int beforeDynamique = 0;
int[][] pixels;
ImageBNW_ppm image = this;
CharArrayWriterGetbuf data = new CharArrayWriterGetbuf();
There I read the file, ie I transfer to a Buffer
while ((c = fr_inputFile.read()) != -1) {
data.write(c);
}
I manipulate the buffer
BufferManipulator buf = new BufferManipulator(data.getBuf());
I get the header (entete in French ;-) )
ct_entete = buf.getCharData (0, '\n');
beforeDynamique = image.getDimension(buf);
results = buf.getIntData(beforeDynamique, '\n');
super.setDynamique(results[0]);
I read the pixels array
pixels = new int[super.getWidth()][super.getHeight()];
for (j=0; j<super.getHeight(); j++) {
for (i=0; i<super.getWidth(); i++) {
getData read depending up to the last character, \n in this example
results = buf.getIntData(results[1], '\n');
pixels[i][j] = results[0];
}
}
Junk stuff for my application
image.mc_pixels = new MatrixCalcul(pixels, super.getWidth(), super.getHeight());
data.close();
fr_inputFile.close();
}
I hope it was helpful. I don't remember exactrly everything but it might give you some idea and some classes to try. If you want further details, I could send you the classes to open an image for you to play with.
Cheers,
Pierre-Yves
|