473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing BMPs

I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.
So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.
Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::ofstream bDump;
bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
bDump.write((ch ar*)&b, 58);
bDump.write((ch ar*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.
Can anyone give me any pointers?
Jul 19 '05 #1
4 5381

"Nhwk" <n_*****@hotmai l.com> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.
So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
57 bytes
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.
Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::ofstream bDump;
bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
bDump.write((ch ar*)&b, 58);
but you write 58!!
bDump.write((ch ar*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.
Can anyone give me any pointers?


You seem confused about arrays and pointers. What you've written isn't
*necessarily* wrong, but without seeing all the definitions you have used
its hard to be sure. Nevertheless it is not necessary to take the address of
an array to convert it to a pointer. That conversion happens automatically.

bDump.write(b, 58);
bDump.write((ch ar*)p->banner, 96);

I'm not saying this will fix your problem, but make this change, fix the 57
vs. 58 problem, and if it still doesn't work post again but this time
remember to include all the variable declarations you use (p in particular).

john
Jul 19 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:bi******** ****@ID-196037.news.uni-berlin.de...

"Nhwk" <n_*****@hotmai l.com> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.
So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
57 bytes
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.
Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::ofstream bDump;
bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
bDump.write((ch ar*)&b, 58);


but you write 58!!
bDump.write((ch ar*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.
Can anyone give me any pointers?


You seem confused about arrays and pointers. What you've written isn't
*necessarily* wrong, but without seeing all the definitions you have used
its hard to be sure. Nevertheless it is not necessary to take the address

of an array to convert it to a pointer. That conversion happens automatically.
bDump.write(b, 58);
bDump.write((ch ar*)p->banner, 96);

I'm not saying this will fix your problem, but make this change, fix the 57 vs. 58 problem, and if it still doesn't work post again but this time
remember to include all the variable declarations you use (p in particular).
john


A quick note on the bitmap file format. It doesnt dump all the bytes in one
like you are doing, instead it dumps them in scanlines, and sometimes there
are padding bytes after each. I cannot remeber how many padding bytes there
are, but a quick search in Google should serve you well.
Allan
Jul 19 '05 #3
Nhwk wrote:
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.
So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.
Why are you using a separate assignment statement for each byte?

Easier way: use a string or array initialisation for b.

Much easier way: do away with b and actually define the struct.
http://astronomy.swin.edu.au/~pbourke/dataformats/bmp/
Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::ofstream bDump;
bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
bDump.write((ch ar*)&b, 58);
bDump.write((ch ar*)&p->banner, 96);
bDump.close();
I presume that the offset variable in the header correctly matches the
point in the file where you start writing out the data? And that you've
got your dimensions and colour depth correctly matched?
However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.

<snip>

Good job there isn't a cast of unsigned char to char* in the code you've
provided then.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #4
> A quick note on the bitmap file format. It doesnt dump all the bytes
in one
like you are doing, instead it dumps them in scanlines, and sometimes there are padding bytes after each. I cannot remeber how many padding bytes there are, but a quick search in Google should serve you well.


IIRC every scan-line should be a multiple of 4 bytes, but I'm sure the
people at comp.os.ms-windows.program mer.win32 know the exact details.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 19 '05 #5

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

Similar topics

48
8449
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during...
5
45644
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long *air_address;
102
7009
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality of its documentation, it is a impeccability. it has or possesses properties of:
16
7170
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
6
5252
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
5010
by: Craig | last post by:
Hi there, I'm trying to open colour BMPs using PIL and I'm getting the following errors. Opening a 16 colour BMP I get: Traceback (most recent call last): File "<pyshell#3>", line 1, in <module>
89
3791
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or record inheritance (records do have operator overloading) The idea is to write a generic integer class with derived integer classess for 8 bit, 16...
2
2855
by: DumRat | last post by:
Hi, I want to run an animation(in essence). I want to draw multiple images (possibly .bmps, but I'd like other formats as well.) on to the screen each frame. I tried to do this with some C++ code, but it isn't working as it should (As I'm expecting it to I guess.). Can anyone please help me? Here's the class definition. Just for the...
17
7996
by: mrcw | last post by:
private void timer1_Tick(object sender, EventArgs e) { Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image); if (Invert(m_Undo)) this.Invalidate(); pbLeftDetected.Image = m_Undo; }
0
7888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8185
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.