473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Loading BMP problem.

I wroted this piece of code :
#include <stdio.h>
#include <malloc.h>

FILE * f;

typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
LONG width;
LONG height;
struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} h1;
struct BITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
};

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;
int main() {
// open file to read
f = fopen("1.bmp", "rb");

// read BITMAPFILEHEADER
fread( &h1 , sizeof(h1), 1, f);
char * string = & h1.bfType;
printf ("%s\n", string);

width = h1.bfSize;
printf("%ul \n", width);
}

it should print to stdout h1.bfType it's type of file: output is BM6;
and size of file. I see 91 or something like this, but should be 38386. If
anybody could help?

Thanks
Jul 23 '05 #1
7 3353
Pavlo Pelekh wrote:

// read BITMAPFILEHEADER
fread( &h1 , sizeof(h1), 1, f);
char * string = & h1.bfType;
printf ("%s\n", string);

width = h1.bfSize;
printf("%ul \n", width);
}

it should print to stdout h1.bfType it's type of file: output is BM6;
If you printf something with %s then you better should make sure
that the thing you output is indeed a C-style string. That is:
a sequence of charcaters, terminated with a '\0' character. The
bfType field does not quilify for that.
and size of file. I see 91 or something like this, but should be 38386. If
anybody could help?


In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
Karl Heinz Buchegger wrote:
In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.


I looked this file with editor in Hex and I definetlely can say, that there
was size in that field. I converted it to dec and it was the same as $file
1.bmp returned me.
Jul 23 '05 #3
Pavlo Pelekh wrote:

Karl Heinz Buchegger wrote:
In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.


I looked this file with editor in Hex and I definetlely can say, that there
was size in that field. I converted it to dec and it was the same as $file
1.bmp returned me.


You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )


--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 23 '05 #4
Karl Heinz Buchegger wrote:

Pavlo Pelekh wrote:

Karl Heinz Buchegger wrote:
In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.


I looked this file with editor in Hex and I definetlely can say, that there
was size in that field. I converted it to dec and it was the same as $file
1.bmp returned me.


You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )


Sorry, hit send accidently

#pragma pack( push, 1 )

struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;

....

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;

#pragma pack( pop )

Note that #pragma is compiler dependent by definition

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

"Karl Heinz Buchegger" <kb******@gascad.at> schrieb im Newsbeitrag
news:42***************@gascad.at...
Karl Heinz Buchegger wrote:

Pavlo Pelekh wrote:
>
> Karl Heinz Buchegger wrote:
>
> > In this structure, the bfSize member denotes the entire file
> > size. Most
> > bitmap files contain a wrong value or simply are 0. Thats
> > mainly because
> > no one really needs that value for anything.
>
> I looked this file with editor in Hex and I definetlely can say,
> that there
> was size in that field. I converted it to dec and it was the same
> as $file
> 1.bmp returned me.


You are fooled by padding.
Search for a compiler directive to turn of padding for that
structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )


Sorry, hit send accidently

#pragma pack( push, 1 )

struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;

....

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;

#pragma pack( pop )

Note that #pragma is compiler dependent by definition


Yep, had exaclty the same problem once... <remembers a horrible night
of debugging>
-Gernot
Jul 23 '05 #6
Karl Heinz Buchegger schrieb:
You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )


As the OP mentioned file (1), I assume he's probably on a POSIX system.
FWIW, for gcc the corresponding syntax is:

struct Foo __attribute__ ((__packed__))
{
//...
};

Cheers,
Malte
Jul 23 '05 #7

Thanks Karl for help. Now I see.:)
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Pavlo Pelekh wrote:

Karl Heinz Buchegger wrote:
In this structure, the bfSize member denotes the entire file size. Most bitmap files contain a wrong value or simply are 0. Thats mainly because no one really needs that value for anything.


I looked this file with editor in Hex and I definetlely can say, that there was size in that field. I converted it to dec and it was the same as $file 1.bmp returned me.


You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )


--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ram | last post by:
We are trying to load text tab delimited files into SQL server using informatica 5.1 version. The text files are FTPed to a local server and then files are moved to applicable folder before...
3
by: Roy Wang | last post by:
hi, My problem is how to determining when the XML file has loaded using javascript. I loaded an xml file using javascript in a web page. The code below is loading the xml file for IE:...
4
by: Adrian MacNair | last post by:
Hi, I created an image gallery which displays 63 images in a slideshow. The problem is that the show was slow because each image loaded one at a time during the show. No problem right? I just...
6
by: Curious George | last post by:
I have a page that takes about 10 seconds to load the first time it is run. I would like to first display a little animated gif telling the user that the page is loading. How do I do this with...
4
by: Matt Sawyer | last post by:
I am attempting to use an API (CxApiOem.dll) that has a large number of defines and complicated structs. It's just too much hassle to attempt to use DLLImport to make the desired API calls. ...
3
by: Holmes | last post by:
Hello Ran into a bit of a problem here and have now exhausted my resources to getting this working What I am trying to do is load and show a simple vb form with a listbox in it Dim...
2
by: nick.taylor | last post by:
Hi, I've been trying for weeks to figure out this problem. I'm developing a simple Javascript app that loads an XML file from a server, parses the contents, and displays them. But I am...
5
by: Pete Marsh | last post by:
Wondering if anyone can recomend some sample code for dynamically loading the GD module. I have tried setting the extension dir in php.ini, and loading the GD module from there when apache is...
4
Fary4u
by: Fary4u | last post by:
Hi i've found this problem for a long time but i thought i can live wd this but now it's giving me headack it's mostly happend when u got some problem on the network , after installation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.