// 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