473,473 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Create 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("bannerdump.bmp", std::ios::out | std::ios::binary);
bDump.write((char*)&b, 58);
bDump.write((char*)&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 5365

"Nhwk" <n_*****@hotmail.com> wrote in message
news:c1**************************@posting.google.c om...
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("bannerdump.bmp", std::ios::out | std::ios::binary);
bDump.write((char*)&b, 58);
but you write 58!!
bDump.write((char*)&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((char*)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_*****@hotmail.com> wrote in message
news:c1**************************@posting.google.c om...
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("bannerdump.bmp", std::ios::out | std::ios::binary);
bDump.write((char*)&b, 58);


but you write 58!!
bDump.write((char*)&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((char*)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("bannerdump.bmp", std::ios::out | std::ios::binary);
bDump.write((char*)&b, 58);
bDump.write((char*)&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.programmer.win32 know the exact details.

--
Peter van Merkerk
peter.van.merkerk(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
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...
5
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...
102
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...
16
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...
6
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
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...
89
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...
2
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++...
17
by: mrcw | last post by:
private void timer1_Tick(object sender, EventArgs e) { Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image); if (Invert(m_Undo)) ...
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
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,...
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...
1
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...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.