1

I have a c/c++ program running on my Linux box that loads up a 24bit bitmap, reads the two headers and then stores the image data into a char* variable. I have verified this works by dumping that variables contents into a raw binary file and compared it to the original bitmap+offset. I used the code from HERE and unmodified and takes care or reordering into RGB and bottom up.

Now if I have a list of coordinates like X, Y, Width, Height how the heck do I translate these into the byte offsets of my image?!

In MY CODE you see that I am calculating the width of one scanline and the glyph location to find Y and then adding a scanline for each y+1. Similarly for X I am iterating over by three bytes at a time. And finally I store those three bytes sequentially into my temporary character array.

In truth I do not need the pixel data as the glyph is a 0xFF or 0x00 with no smoothing. I included it to make sure my bits where accounted for.

HERE is the image I am using.

EDIT: --------------------------------------------

As mentioned below my math was a bit quarky. fixed the line in the i,j,k loop to:

tmpChar[i][j][k] = img.data[(((Y+j) * imgWidth) + (X + i)) * imgBPP + k];

As for my programs output HERE as you can see it loads the bitmap fine and the header info is proper but when I try to display the contents of the tmpChar array its all 0xFF (I used a signed int so 0xFF = -1 and 0x00 = +0)

4

2 回答 2

1

图像内存中的布局是(忽略我可能已经颠倒了 R、G 和 B):

[R of pixel 0]  [G of pixel 0] [B of pixel 0] ....... [B of (0, imgWidth-1)] [R of pixel (1, 0)] .....

所以要计算任何给定像素的偏移量offset = ((Y * imgWidth) + X) * imgBPP + colorByte

据我所知,为您的内部循环提供支持,并假设您的 X 和 Y 字符是正确的:

tmpChar[i][j][k] = img.data[(((Y+j) * imgWidth) + (x + i)) * imgBPP + k];
于 2012-12-08T20:14:58.983 回答
0

我猜像素在内存中以倒置的顺序存储,就像BMP 文件格式一样:

通常,像素相对于正常图像光栅扫描顺序“倒置”存储,从左下角开始,从左到右,然后从图像的底部到顶部逐行存储

因此,您的代码可能正在读取错误的像素块。

于 2012-12-08T21:39:14.140 回答