Computing.Net > Forums > Programming > [C++] Writing data to a bitmap.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

[C++] Writing data to a bitmap.

Reply to Message Icon

Name: MH89
Date: December 19, 2008 at 10:58:58 Pacific
OS: Windows Vista
CPU/Ram: 2Ghz/1GB
Product: Compaq / PRESARIO C762NR
Comment:

Hello, I'm programming in C++ using VisualC++ 2008 Express; no APIs or libraries aside from the standard C++ ones.

I've been experimenting with bitmaps as a learning exercise for the past week. I coded a bitmap reader and copier using fstream.

When it comes to writing to a bitmap created out of whole cloth, I come across a problem.

Source code:

-----Loader.h-------------
#include "Preliminaries.h" //contains all //the major #includes (string, fstream, etc.)

#ifndef _BITMAPLOADER
#define _BITMAPLOADER

#pragma pack(push, 1)

using std::fstream;

struct Bitmap {
//data members
short x, y;
short h, w;
string filename;
fstream pic;

struct Header {
short type;
int size;
short reserved1,
reserved2;
int offset;
}header;
struct Info {
int size;
int width;
int height;
short planes;
short depth;
int compressn;
int imgsize;
int xbpm;
int ypbm;
int colors;
int palette;
}info;
struct Color {
unsigned char b;
unsigned char g;
unsigned char r;
/*unsigned char misc;*/
}color;


//member functions

void Create(string,int, int);
void Load(string f);
inline void Load() { Load(filename);}

//operators
operator const fstream&() { return pic;}

//constructors
Bitmap(string f);
Bitmap();

};

#pragma pack(pop)

#endif

--------Loader.cpp----------

<code>
#include "Loader.h"

using std::cout;
using std::cin;
using std::endl;
using std::ios;

void Bitmap::Create(string name, int h2, int w2) {
h = h2;
w = w2;
filename = name;

Header temp1 = {19778,sizeof(Color) *(w*h), 0, 0, 1078};
Info temp2 = {sizeof(Bitmap::Info), w, h, 1, sizeof(Color)*8, 0, header.size, 0, 0, 0, 0};
Color temp3 = {0,255,90};

header = temp1;
info = temp2;
color = temp3;


pic.open(filename.c_str(), ios::out|ios::binary);

pic.write(reinterpret_cast<char*>(&header), sizeof(header));
pic.write(reinterpret_cast<char*>(&info), sizeof(info));

for (int f = 0; f <= w*h; ++f) {
pic.put((char)&color);
cout<<(int)color.b<<","<<(int)color.g<<","<<(int)color.r<<"..."<<f<<endl;
}
cin.get();

}


void Bitmap::Load(string f) {
filename = f;
pic.open(filename.c_str(), ios::in|ios::binary);
pic.read(reinterpret_cast<char*>(&header), sizeof(Bitmap::Header));
pic.read(reinterpret_cast<char*>(&info), sizeof(Bitmap::Info));

h = info.height;
w = info.width;


}

Bitmap::Bitmap(string f) : x(0), y(0), filename(f) { pic.open(filename.c_str(), ios::in|ios::binary);}
Bitmap::Bitmap(){x = y = 0;}

int main() {
Bitmap a;
a.Load("italianpretties.bmp");
Bitmap b;
b.Create("new.bmp", 20, 20);

return 0;
}


I was trying to write a bitmap with a solid color. I'm not quite sure what I'm doing wrong. While it compiles and runs just fine, but the outputted bitmap never comes out right--it's either all black, black and grey, or a random jumble of pixels.

I hope someone can point me in the right direction!



Sponsored Link
Ads by Google

Response Number 1
Name: lucas999
Date: December 20, 2008 at 05:53:01 Pacific
Reply:

Why re-inventing the wheel ?!
Use win32 api, it's immediate (3 lines of code !)


0

Response Number 2
Name: Razor2.3
Date: December 20, 2008 at 18:27:44 Pacific
Reply:

lucas999: Why re-inventing the wheel ?!
'Cause he apparently didn't download the Win32 SDK.

MH89:
In Bitmap::Create(), you have:

pic.put((char)&color); 
Change that to:
pic.write(reinterpret_cast<char*>(&color), sizeof(color)); 
(Not writing enough per pixel.)

In Bitmap::Create(), you have:

Header temp1 = {19778,sizeof(Color) *(w*h), 0, 0, 1078};
Change that to:
Header temp1 = {19778,sizeof(Color) *(w*h), 0, 0, sizeof(Bitmap::Header) + sizeof(Bitmap::Info)};
(Offset is incorrect.)

0

Response Number 3
Name: MH89
Date: December 27, 2008 at 18:29:52 Pacific
Reply:

Thank you very much, I got it to work perfectly!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: [C++] Writing data to a bitmap.

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

Writing Data to a database using ADO www.computing.net/answers/programming/writing-data-to-a-database-using-ado/2699.html

writing data to file in java www.computing.net/answers/programming/writing-data-to-file-in-java/14129.html