BMP Loading Structs

This is a discussion on BMP Loading Structs within the C++ Programming forums, part of the General Programming Boards category; I'm trying to load a BMP in Linux using the Windows structures. It's loading, but a lot of the values ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    BMP Loading Structs

    I'm trying to load a BMP in Linux using the Windows structures. It's loading, but a lot of the values are just junk. First, heres the code:

    Code:
    #include <iostream>
    #include <cstdio>
    
    #define BYTE  unsigned char
    #define DWORD  unsigned int
    #define LONG  int
    #define UINT  unsigned int
    #define WORD  unsigned short int
    
    #define LPSTR  char*
    
    
    typedef struct tagBITMAPFILEHEADER {
      WORD    bfType;
      DWORD   bfSize;
      WORD    bfReserved1;
      WORD    bfReserved2;
      DWORD   bfOffBits;
    } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
    
    typedef struct tagBITMAPINFOHEADER{
      DWORD  biSize;
      LONG   biWidth;
      LONG   biHeight;
      WORD   biPlanes;
      WORD   biBitCount;
      DWORD  biCompression;
      DWORD  biSizeImage;
      LONG   biXPelsPerMeter;
      LONG   biYPelsPerMeter;
      DWORD  biClrUsed;
      DWORD  biClrImportant;
    } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
    
    typedef struct tagRGBQUAD {
      BYTE    rgbBlue;
      BYTE    rgbGreen;
      BYTE    rgbRed;
      BYTE    rgbReserved;
    } RGBQUAD;
    
    void loadBMP(LPSTR filename, BITMAPFILEHEADER *tbmfh, BITMAPINFOHEADER *tbmih)
    {
        FILE *in;
        in=fopen(filename,"rb");
    
        fread(tbmfh,sizeof(BITMAPFILEHEADER),1,in);
        fread(tbmih,sizeof(BITMAPINFOHEADER),1,in);
        return;
    }
    
    int main()
    {
        BITMAPFILEHEADER bmfh;
        BITMAPINFOHEADER bmih;
        loadBMP("/home/david/DavidsStuff/Pictures/somebmp.bmp", &bmfh, &bmih);
        if(bmih.biBitCount != 24)
    
        std::cout << bmih.biSize << std::endl;
        std::cin.get();
    }
    bmfh.bfType will give me 19778, like it's supposed to. bfOffBits returns a good value too, I think. As for most of the other values, then seem bad. Here's some examples:

    biBitCount: 0 (Should be 1, 4, 8, or 14)
    biWidth: 39976960
    biSize: 44892160 (Should be 40)
    biCompression: 5588251136

    This article explains what the values are: http://www.gamedev.net/reference/art...rticle1966.asp

    The rest are more of the same. I also get similar results with different bmp's. Any clue why?

    Thanks,
    David

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Search: Key Word(s): padding, alignment
    There is no (good) way to guarantee the amount of internal padding within a structure.

    In addition, if your Linux is running on something other than an x86 processor, then you may have an endian problem as well.

    The only good way of getting consistent answers on all machines is to read the file one byte at a time and using bitwise ops (<<, >>, &, |) to move the byte to the appropriate part of the appropriate member of your struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Hey Salem,

    I've been working on this for a while and am really having some trouble with it. I just can't comprehend how alignment works. I read that an image that was something like 256x256 for example, would not have any padding, because it ends on a DWORD, or something of that nature.

    Basically I'm just trying to load the bitmap headers, but I guess the alignment is bad. Could you give me an example of how you would fix the alignment?

    Thanks
    David

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you're using g++ (gcc), you can do this:
    Code:
    #pragma pack(1)
    typedef struct tagBITMAPFILEHEADER {
      WORD    bfType;
      DWORD   bfSize;
      WORD    bfReserved1;
      WORD    bfReserved2;
      DWORD   bfOffBits;
    } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
    
    #pragma pack()

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well that's rather nifty. It worked fine...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how come... (bmp loading)
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 03-21-2006, 08:00 PM
  2. Loading a bmp file
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2005, 12:51 AM
  3. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 04:16 PM
  4. Loading BMP to a DirectDrawSurface
    By phatslug in forum Game Programming
    Replies: 1
    Last Post: 07-18-2002, 04:15 PM
  5. Loading BMP without WM_PAINT?
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 01-10-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21