Computing.Net > Forums > Programming > Writing a BMP in C++ (manually)

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Writing a BMP in C++ (manually)

Reply to Message Icon

Original Message
Name: Nil_IQ
Date: November 7, 2006 at 09:45:36 Pacific
Subject: Writing a BMP in C++ (manually)
OS: Win XP
Comment:

Hi.

I've been trying to write a bitmap in C++, but so far the closest i've gotten is a blank bitmap file which says "drawing failed" when I open it. The code i've written below SHOULD make an entirely red bitmap, but no luck so far. Any ideas?


#include <iostream>
#include <fstream>

using namespace std; //keep this in or it breaks and dies horribly

struct BMFH { //declares BitMap File Header structure
unsigned short bfType; //might need to be a char
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
};

struct BMIH { //declares BitMap Info Header structure
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};

struct RGBQUAD{ //declares RGBQUAD
int rgbBlue;
int rgbGreen;
int rgbRed;
int rgbReserved;
};

struct pixel { //declares pixel structure

unsigned short R;
unsigned short G; //RGB values for each pixel
unsigned short B;
//unsigned short unused; //needed?
};

void setfileheader(BMFH& fileheader) //must pass by reference!
{ // Fills the file header with default values...

fileheader.bfType = 19778;
fileheader.bfSize = 108;
fileheader.bfReserved1 = 0;
fileheader.bfReserved2 = 0;
fileheader.bfOffBits = 1078;

}

void setinfoheader(BMIH& infoheader) //must pass by reference!
{ // Fills the file header with default values...

infoheader.biSize = sizeof(infoheader);
infoheader.biWidth = 128;
infoheader.biHeight = 128;
infoheader.biPlanes = 1;
infoheader.biBitCount = 24;
infoheader.biCompression = 0;
infoheader.biSizeImage = 0;
infoheader.biXPelsPerMeter = 0;
infoheader.biYPelsPerMeter = 0;
infoheader.biClrUsed = 0;
infoheader.biClrImportant = 0;

}

int main(){

//------------Setting up pixels and co-//


pixel PICTAR[128][128]; //declares an array of pixels

int i;
int j;

for (int i=0; i<128; i++){ //horizontal axis of pixels

for (int j=0; j<128; j++){ //vertical axis

PICTAR[i][j].R = 100; //make pixels red
PICTAR[i][j].G = 0;
PICTAR[i][j].B = 0;

}
}


-------------------//

ifstream::pos_type size; //special type used for file positioning; can treat as an integer
BMFH fileheader; //makes a header called "fileheader"
BMIH infoheader;

setfileheader(fileheader); //fills header with values
setinfoheader(infoheader);

ofstream myfile ("testbmp.bmp",ios::out | ios::binary); //creates an ofstream object called myfile
//and opens it for binary input

/////////////////////Writing////////////////////////////

//myfile.write ((char*) &fileheader,sizeof(fileheader)); //write header
//myfile.write ((char*) &infoheader,sizeof(infoheader)); //write header
myfile.write ((char*) &fileheader,14); //write header
myfile.write ((char*) &infoheader,40); //write header
myfile.write ((char*) &PICTAR,sizeof(pixel)*128*128);
myfile.close();


}

Thanks for your time,

Nil


Report Offensive Message For Removal


Response Number 1
Name: guest
Date: December 5, 2006 at 17:37:23 Pacific
Reply: (edit)

2 points
1) pixels should be char not unsigned short
2) for some unknown to me reason the struct BMFH caused the long bfSize (file size) to be incorrect and to have hex &hCC overwriting the first 2 bytes

guest


Report Offensive Follow Up For Removal







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








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software