![]() |
| ||
| Help with manually creating a BMP image Hello, im a noob with C++ at the moment and Im trying to create a BMP image from pixel data stored in a array. Everything seems to work except that when i look at the file generated in a hex editor i get an extra '00 00' in between the BM identifier and the bytes reserved for the file size. It is as though the member short BM within the bmp_header struct is taking 4 bytes where i think i it is suppose to take 2 bytes. Could someone please explain why this is happening. Thanks in advance. (there are probably much more efficient ways of doing this but this is the way i want 2 do it at the moment) Template is from http://en.wikipedia.org/wiki/BMP_file_format (the 2 by 2 pixel example) #include <fstream> |
| ||
| Re: Help with manually creating a BMP image The short answer is padding and alignment. > short BM; > long size_of_file; To ensure efficient access to size_of_file, the compiler inserts a pair of padding bytes after BM. The common way to "fix" this is to use something like #pragma pack(1), but there is no standard way to tell a compiler how to do this (or indeed how well your request for packing the struct will be met). Also look up "endian-ess" Unfortunately, the only good long-term answer (which is pretty horrible) is to read/write bytes one at a time, and assign them to the struct in the correct order. |
| ||
| Re: Help with manually creating a BMP image Thanks 4 your help. Ill experiment with what you told me today |
| ||
| Re: Help with manually creating a BMP image Yet another common and portable approach: use ordinar structures to create BMP info then pack them into the final char buffer (use memcpy(buffer+offset,&member,sizeof member), move offset forward and so on). Of course, it's possible to write data to a file stream directly (no need in current write pos forwarding). Add some syntax sugar, for example: struct bmp_headerA nasty thing but it works ;) |
| ||
| Re: Help with manually creating a BMP image Can i suggest you store the value of BM outside of the structure and store it in a char array of size 2 then write each one to the file before calling write() on the structure. Chris |
| ||
| Re: Help with manually creating a BMP image I did what freaky_chris suggested and created the value of BM outside of the bmp_header struct and it seems to work. Final code is now #include <fstream> |
| ||
| Re: Help with manually creating a BMP image Thanks 4 all of ur help |
| All times are GMT -4. The time now is 4:28 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC