【发布时间】:2015-05-27 13:30:01
【问题描述】:
我想获取光栅坐标上的像素颜色,例如:
[0,0] - 第一行第一列的像素(左上角)
[0,1] - 第一行第二列的像素,依此类推。
我正在像这样加载我的位图:
BitsPerPixel = FileInfo[28];
width = FileInfo[18] + (FileInfo[19] << 8);
height = FileInfo[22] + (FileInfo[23] << 8);
int PixelsOffset = FileInfo[10] + (FileInfo[11] << 8);
int size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
Pixels.resize(size);
hFile.seekg(PixelsOffset, ios::beg);
hFile.read(reinterpret_cast<char*>(Pixels.data()), size);
hFile.close();
还有我的 GetPixel 函数:
void BITMAPLOADER::GetPixel(int x, int y, unsigned char* pixel_color)
{
y = height - y;
const int RowLength = 4 * ((width * BitsPerPixel + 31) / 32);
pixel_color[0] = Pixels[RowLength * y * BitsPerPixel / 8 + x * BitsPerPixel / 8];
pixel_color[1] = Pixels[RowLength * y * BitsPerPixel / 8 + x * BitsPerPixel / 8 + 1];
pixel_color[2] = Pixels[RowLength * y * BitsPerPixel / 8 + x * BitsPerPixel / 8 + 2];
pixel_color[3] = Pixels[RowLength * y * BitsPerPixel / 8 + x * BitsPerPixel / 8 + 3];
}
我知道位图中的数据是倒置存储的,所以我想使用y = height - y; 反转它,但是通过这一行,我只能得到一些甚至不在图像数据数组中的值。在不反转图像的情况下,我得到了数组中的一些值,但它们从不与给定的坐标相对应。我的位图可以是 24 位或 32 位。
【问题讨论】:
-
应该是
height - 1 - y。 -
我也试过了,但它仍然返回错误的值。