Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 functionsvoid 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!

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.)

![]() |
![]() |
![]() |

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