I want to read a simple , monocrome (1 bit), RGB encoded bitmap to a bool array, but I don't get it to work. It works for some bitmaps. It depends in which program they're saved. For example bitmaps saved in PSP do work, but from PS or Paint don't. What's the difference?
Can anyone help me how I can make a function which can read all 1 bit, rgb bitmaps, or at least tell me what's the problem is. Is there any class out there which I can use instead to load it to a bool array? I only need to load and then save it back to a bitmap.
Here's how I read the bitmap:
bool
Pathfind::
LoadBitmapMap(const char *filename)
{
HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (file == INVALID_HANDLE_VALUE)
return false;
char buffer[48];
unsigned long nBytesRead;
BITMAPFILEHEADER *header;
BITMAPINFO *bmpinfo;
ReadFile(file, buffer, sizeof(BITMAPFILEHEADER), &nBytesRead, NULL);
header = (BITMAPFILEHEADER*)buffer;
int offset = header->bfOffBits;
if (header->bfType != 0x4d42)
return false;
ReadFile(file, buffer, sizeof(BITMAPINFO), &nBytesRead, NULL);
bmpinfo = (BITMAPINFO*)buffer;
if (bmpinfo->bmiHeader.biBitCount != 1)
return false;
MAPX = (unsigned short)bmpinfo->bmiHeader.biWidth;
MAPY = (unsigned short)bmpinfo->bmiHeader.biHeight;
Resize(MAPX, MAPY); //Resize _map
SetFilePointer(file, offset, NULL, FILE_BEGIN);
int p;
unsigned int value;
for (int i = MAPY - 1; i >= 0; --i) {
p = i * MAPX;
int pos = 0;
while (true) {
ReadFile(file, buffer, sizeof(int), &nBytesRead, NULL);
value = *((unsigned int*)buffer);
for (int j = 0; j < 32; ++j) {
if (++pos > MAPX)
goto bye;
_map[p] = (value & (1 << (j^7))) == 1 ? true : false;
p++;
}
}
bye:
;
}
CloseHandle(file);
return true;
}