Reputation: 21542
I have the following algorithm for converting a 24-bit bitmap to a hexadecimal string representation of the pixels it is composed of:
// *data = previously returned data from a call to GetDIBits
// width = width of bmp
// height = height of bmp
void BitmapToString(BYTE *data, int width, int height)
{
int total = 4*width*height;
int i;
CHAR buf[3];
DWORD dwWritten = 0;
HANDLE hFile = CreateFile(TEXT("out.txt"), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
for(i = 0; i < total; i++)
{
SecureZeroMemory(buf, 3);
wsprintfA(buf, "%.2X", data[i]);
// only write the 2 characters and not the null terminator:
WriteFile(hFile, buf, 2, &dwWritten, NULL);
}
WriteFile(hFile, "\0", 2, &dwWritten, NULL);
CloseHandle(hFile);
}
The problem is, I want it to ignore the padding at the end of each row. For example, with a 2x2 bitmap where all pixels are of the value #7f7f7f, the contents of out.txt also contain the padding bytes:
7F7F7F7F7F7F00007F7F7F7F7F7F0000
How would I adjust the loop to avoid inclusion of the padding zeroes?
Upvotes: 0
Views: 634
Reputation: 12515
Change your project setting to not pad to 16 bytes on write is one idea. The other is to know what amount of bytes the pad is set at (16 in your example), and use modulus (or and) to determine how many bytes you need to skip per row:
int offset = 0;
for(i = 0; i < height; i++)
{
// only read 3 sets of bytes as the 4th is padding.
for(j = 0; j < width*3; j++)
{
SecureZeroMemory(buf, 3);
wsprintfA(buf, "%.2X", data[offset]);
// only write the 2 characters and not the null terminator:
WriteFile(hFile, buf, 2, &dwWritten, NULL);
offset++;
}
// offset past pad bytes
offset += offset % 8;
}
This solution should work, though I won't guarentee it without further understanding hwo your pad bytes are occuring.
Upvotes: 1