Thread: Howto read a bitmap?

  1. #1

    Howto read a bitmap?

    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:

    LoadBitmapMap(const char *filename)

    HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,

    if (file == INVALID_HANDLE_VALUE)
    return false;

    char buffer[48];
    unsigned long nBytesRead;
    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;


    return true;

  2. #2
    Registered User
    Join Date
    Dec 2001
    Make sure the PS and Paint are not compressing the files.
    Try saving the exact same picture in each application, including PSP, then open the files with a hex editor and find the difference

  3. #3
    Yes, I've done that, and the bit's look totally different, but the headers are almost the same. But I've saved them as RGB, 1 bit in all programs, but they're still different...

  4. #4
    Hmm... After some thinking I did figure it out...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Please help with my game
    By jjj93421 in forum Game Programming
    Replies: 10
    Last Post: 04-12-2004, 11:38 PM
  4. No one can seem to help me, loading bitmap
    By Shadow12345 in forum C++ Programming
    Replies: 7
    Last Post: 12-28-2002, 01:22 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM