// load pixels into an image
this.image = new BufferedImage(this.width,
this.height,
BufferedImage.TYPE_INT_RGB);
// get actual image data for easier pixel loading
byte[] iData = new byte[this.size - 54];
for(int i = 0; i < this.size - 54; i++) {
iData[i] = this.data[i+54];
}
// start from bottom row
for(int y = this.height-1; y >= 0; y--) {
for(int x = 0; x < this.width; x++) {
int index = (this.width*y + x) * 3;
int b = iData[index];
int g = iData[index+1];
int r = iData[index+2];
//System.out.format("R: %s\nG: %s\nB: %s\n\n", r, g, b);
// merge rgb values to single int
int rgb = ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);
// build image from bottom up
this.image.setRGB(x, this.height-1-y, rgb);
}
}
我正在讀取位圖中的RGB值。我的iData字節數組是正確的,因爲我使用十六進制編輯器對其進行了檢查。但是,當我運行這個循環時,我的輸出圖像被扭曲(見圖片)。爲了解決這個問題,我一直在困擾我的大腦幾個小時,爲什麼會這樣呢?扭曲的位圖圖像
輸入圖像是加拿大國旗。
輸出圖像:
您的輸入文件是否有4字節對齊的行?有些格式將填充添加到每行的末尾,以使每行長度爲4個字節的倍數。 – immibis 2014-10-09 04:25:58
是的,它的確如此!爲什麼? – Tetramputechture 2014-10-09 04:29:22
因爲你可能沒有考慮到這一點。 – immibis 2014-10-09 04:30:09