C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-04-2005, 02:35 PM   #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
Victor is offline   Reply With Quote
Old 04-04-2005, 02:40 PM   #2
Crazy Fool
 
Perspective's Avatar
 
Join Date: Jan 2003
Location: Canada
Posts: 2,596
look here:
Loading a BMP....it should work.

You essentially need to #pragma pack(1)
Perspective is offline   Reply With Quote
Old 04-04-2005, 02:48 PM   #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 03:20 PM.
Victor is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem with GETLINE wco5002 C++ Programming 13 07-07-2008 09:57 AM
Strange problem G4B3 C Programming 6 05-14-2008 02:07 PM
Strange problem with classes in header files samGwilliam C++ Programming 2 02-29-2008 04:55 AM
Strange problem ~Kyo~ Game Programming 0 02-14-2006 10:35 PM


All times are GMT -6. The time now is 12:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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