Strange problem with bmp

This is a discussion on Strange problem with bmp within the Linux Programming forums, part of the Platform Specific Boards category; Hi! I'm trying to program my bmp file loader, but I've got strange problem under Linux. I compiled 2 nearly ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    Strange problem with bmp

    Hi!
    I'm trying to program my bmp file loader, but I've got strange problem under Linux. I compiled 2 nearly the same codes. One under Window$ and another under Linux. The problem is that in Linux there is no BITMAPFILEHEADER - it's in windows.h. So i've written it on my own -> I downloaded the description of bmp file format and written my own structure. However it didn't work...

    Here are the codes:

    For Windows:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "stdio.h"
    #include "windows.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        FILE *bmp = fopen("test.bmp", "rb");
    
        if(!bmp)
                return 1;
    
        BITMAPFILEHEADER bfh;
    
        fread(&bfh, sizeof(BITMAPFILEHEADER), 1, bmp);
    
        printf("Sizeof Type: %d  ;  %d\n", sizeof(bfh.bfType), bfh.bfType);
        printf("Sizeof Size: %d  ;  %d\n", sizeof(bfh.bfSize), bfh.bfSize);
        printf("Sizeof reserved1: %d  ;  %d\n", sizeof(bfh.bfReserved1), bfh.bfReserved1);
        printf("Sizeof reserved2: %d  ;  %d\n", sizeof(bfh.bfReserved2), bfh.bfReserved2);
        printf("Sizeof OffsetBits: %d  ;  %d\n", sizeof(bfh.bfOffBits), bfh.bfOffBits);
    
        fclose(bmp);
    
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    It works under dev-c++

    Version for Linux:
    Code:
    #include "stdio.h"
    
    typedef struct BITMAPFILEHEADER {
      short bfType;
      int bfSize;
      short Reserved1;
      short Reserved2;
      int bfOffBits;
    };
    
    typedef struct BITMAPINFOHEADER {
      int biSize;
      long biWidth;
      long biHeight;
    };
    
    int main()
    {
      FILE *bmp = fopen("test.bmp", "rb");
      if(!bmp)
        {
          printf("Couldn't open file!\n");
          return 1;
        }
    
      BITMAPFILEHEADER myBmp;
      BITMAPINFOHEADER myH;
      fread(&myBmp, sizeof(BITMAPFILEHEADER), 1, bmp);
      fread(&myH, sizeof(BITMAPINFOHEADER), 1, bmp);
    
      printf("bfType sizeof %d : %d\n", sizeof(myBmp.bfType), myBmp.bfType);
      printf("bfSize sizeof %d: %d\n", sizeof(myBmp.bfSize), myBmp.bfSize);
      printf("r1 sizeof %d: %d\n", sizeof(myBmp.Reserved1), myBmp.Reserved1);
      printf("r2 sizeof %d: %d\n", sizeof(myBmp.Reserved2), myBmp.Reserved2);
      printf("bfOffbits sizeof %d: %ul\n", sizeof(myBmp.bfOffBits), myBmp.bfOffBits);
    
      printf("----------------------------\n");
      printf("biSize: %d\n", myH.biSize);
      printf("biWidth: %d\n", myH.biWidth);
      printf("biHeight: %d\n", myH.biHeight);
    
      fclose(bmp);
      return 0;
    }
    (BITMAPINFOHEADER is not ended yet but it's not important now)

    And the results for the same bmp file:
    Windows:
    Sizeof Type: 2 ; 19778
    Sizeof Size: 4 ; 1179702
    Sizeof reserved1: 2 ; 0
    Sizeof reserved2: 2 ; 0
    Sizeof OffsetBits: 4 ; 54

    And that's ok.

    But for Linux:
    [victor@localhost bmp_loader]$ ./bmp
    bfType sizeof 2 : 19778
    bfSize sizeof 4: 18
    r1 sizeof 2: 0
    r2 sizeof 2: 54
    bfOffbits sizeof 4: 2621440l

    First values are ok (these are shorts) but the rest is wrong.
    As you can see I also checked whether sizes of types in my structure are ok, and they are.

    So could anyone help me and explain why it doesn't work?
    I know it's a mistake somewhere in my structure but it should work if type sizes are same, shouldn't it?

    thx
    Victor

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    look here:
    Loading a BMP....it should work.

    You essentially need to #pragma pack(1)

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    23
    Thanks, it works!

    Now I must look for some info about #pragma pack(1) and learn what does it exactly do....

    btw. your website is really nice but you should upload your projects source codes so I could learn more :>

    Oh, and one more thing. Is/Should bitmap identifier (in structure it's type) always be equal to 19778?
    Last edited by Victor; 04-04-2005 at 04:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 10:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 03:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM

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