2005年9月13日 星期二

[Java] Read RGB values from raw image file


import java.io.*;

public class ReadRawImage {

private byte[] all;
/*
* Channel[0] for red channel, Channel[1] for green channel, Channel[2]
* for blue channel
*/
private int[][][] Channel;

private FileInputStream stream = null;

public ReadRawImage(File raw_image, int width, int height) {
try {
/* Read the raw image file into memory */
stream = new FileInputStream(raw_image);
all = new byte[width * height * 3];
Channel = new int[3][][];
stream.read(all);

/* dispatch pixel values to each array of channel */
ByteArrayInputStream bytestream = new ByteArrayInputStream(all);

byte [] temp = new byte[width * height];

bytestream.read(temp, 0, width * height);
Channel[0] = new int[height][width];
for(int i = 0; i < width * height; i++) {
/*
* extends the byte value to integer value by
* performing the "and" operation with 0x000000ff
*/
Channel[0][i / width][i%width] = temp[i] & 0x000000ff;
}

bytestream.read(temp, 0, width * height);
Channel[1] = new int[height][width];
for(int i = 0; i < width * height; i++) {
Channel[1][i / width][i%width] = temp[i] & 0x000000ff;
}

bytestream.read(temp, 0, width * height);
Channel[2] = new int[height][width];
for(int i = 0; i < width * height; i++) {
Channel[2][i / width][i%width] = temp[i] & 0x000000ff;
}

}catch(Exception e) {
e.printStackTrace();
}
}

public int[][][] getRGBValue() {
return Channel;
}
}



沒有留言: