C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-23-2009, 11:24 AM   #1
Registered User
 
Join Date: May 2008
Posts: 17
BMP file header

Hi,

Can i know how to read in a bmp header using C which is needed to create a new BMP image.

Do i need to define a new structure for the header? Or just write a function to read it in?
YongzZ is offline   Reply With Quote
Old 04-23-2009, 11:31 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Either will work - depends on what you want to do. Note also that some of the alignment normally provided by the compiler doesn't match what the actual header in the file has, so you need to do some special trickery one way or another.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 04-23-2009, 11:48 AM   #3
Registered User
 
Join Date: May 2008
Posts: 17
Thanks for the tip.

I decided to read them in by using the fopen function to open the bmp header. Then use for loops to assign each pixel in the header to my BMP image. Still working on it. Is there a problem with this approach?
YongzZ is offline   Reply With Quote
Old 04-23-2009, 12:40 PM   #4
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
No, that sounds fine.
Sebastiani is offline   Reply With Quote
Old 04-23-2009, 02:42 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,258
Jump over to Wotsit.org and look up the file format for .BMP. It should give you all you need to know.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-24-2009, 11:23 AM   #6
Registered User
 
Join Date: May 2008
Posts: 17
Thanks people.
YongzZ is offline   Reply With Quote
Old 04-24-2009, 11:32 AM   #7
Registered User
 
Join Date: May 2008
Posts: 17
After reading them, what is the next step? Im aware i hv to write the raw data. The raw data are the colours, the pixels etc. Am i right?
YongzZ is offline   Reply With Quote
Old 04-24-2009, 11:36 AM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by YongzZ View Post
After reading them, what is the next step? Im aware i hv to write the raw data. The raw data are the colours, the pixels etc. Am i right?
What did you intend to do with it? If all you are doing is copying the file, you do not need to use a structure or understand the header -- just fread the file into an unsigned char array. You can fwrite the same buffer to (eg) "picture.bmp" and it will be identical to the original.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 04-24-2009, 11:39 AM   #9
Registered User
 
Join Date: May 2008
Posts: 17
Im not copying, I am trying to create a bitmap file from a bmp header. And a bmp file should contain the header data, colours, and sizes, shouldn't it?
YongzZ is offline   Reply With Quote
Old 04-24-2009, 11:44 AM   #10
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
In your Original Post you say "read in"; where is that header from?

Or do you really mean you want to create the header properly?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 04-24-2009, 11:47 AM   #11
Registered User
 
Join Date: May 2008
Posts: 17
The header is provided, i need to use it to create a BMP file by reading it in. The header if im not mistaken is part of the data in creating the BMP image. Sorry if it isn't clear. I just want to know whether im on the right track. After reading the header which is provided, my next step would be going on to write the other data for the BMP file such as colours, size, etc. Correct?
YongzZ is offline   Reply With Quote
Old 04-24-2009, 11:49 AM   #12
Registered User
 
Join Date: Sep 2006
Posts: 2,502
Quote:
Originally Posted by YongzZ View Post
The header is provided, i need to use it to create a BMP file by reading it in. The header if im not mistaken is part of the data in creating the BMP image. Sorry if it isn't clear. I just want to know whether im on the right track. After reading the header which is provided, my next step would be going on to write the other data for the BMP file such as colours, size, etc. Correct?
Yes.
Adak is offline   Reply With Quote
Old 04-24-2009, 12:04 PM   #13
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
The header is part of the file. I just found this description:
BMP (Windows) Header Format
So the header is the first 54 bytes. The image data begins immediately after the header.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 04-24-2009, 04:27 PM   #14
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
You must understand the data in the header to correctly read in the file. However there are Windows functions to do this and I'm assuming you are on Windows since BMPs are decidedly a Microsoft file format.

If I remember correctly each line in a BMP is byte padded to the nearest 4 byte boundary and they are stored bottom right to top left. The RGB's are also in reverse order (BGR). The size of the header, image width, image height, and image byte size are all in the header. You will need these to display the BMP correctly.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 04-25-2009, 10:46 AM   #15
Registered User
 
Join Date: May 2008
Posts: 17
Yes, I already defined the size, width, depth etc. for the image in my BMPimage structure. So I am now going to write another function (my first one was reading the header in) to write colours. Since I already defined the size, depth, width, etc., do I need a function to write those now? Or can I now go and write in the colours?

Lastly, thanks for the replies.
YongzZ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie homework help fossage C Programming 3 04-30-2009 04:27 PM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
Problem reading BMP Mortissus C Programming 4 02-02-2006 06:32 AM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
Include a header file, but where does the header file look in? Nutshell C Programming 6 04-02-2002 05:46 AM


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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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