I am writing a simple program that is creating a bmp file. Unfortunately it seems that my Bitmap does not contain any color data.
Where is my mistake ?
Expand|Select|Wrap|Line Numbers
- //
- #pragma once
- #include "stdafx.h"
- #include <windows.h>
- #include <fstream>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- void wait ()
- {
- std::cin.clear();
- std::cin.ignore(std::cin.rdbuf()->in_avail());
- std::cin.get();
- }
- int main(){
- int i,j,r;
- ofstream bmpdata("BITMAP.bmp", ios::out|ios::binary); //begin stream
- if (!bmpdata) return 1;
- BITMAPINFOHEADER bmpinfo; //Infoheader Data
- bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
- bmpinfo.biWidth = 256;
- bmpinfo.biHeight = 256;
- bmpinfo.biPlanes = 1;
- bmpinfo.biBitCount = 24;
- bmpinfo.biCompression = BI_RGB;
- bmpinfo.biSizeImage = 0;
- bmpinfo.biXPelsPerMeter = 0;
- bmpinfo.biYPelsPerMeter = 0;
- bmpinfo.biClrUsed = 0;
- bmpinfo.biClrImportant = 0;
- BITMAPFILEHEADER bmpfile; //Fileheader Data
- bmpfile.bfType = 'Mb';
- bmpfile.bfSize = (bmpinfo.biWidth * bmpinfo.biHeight * (bmpinfo.biBitCount / 8)) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //aggregate - bmpfile
- bmpfile.bfReserved1 = 0;
- bmpfile.bfReserved2 = 0;
- bmpfile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
- bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
- RGBQUAD bmp;;
- for(r=0;r < 256; r++) {
- bmp.rgbBlue = r;
- bmp.rgbGreen = r;
- bmp.rgbRed = r;
- bmp.rgbReserved=0;
- //writing directly to the file:
- bmpdata.write((char*)&(bmp.rgbBlue), sizeof(bmp.rgbBlue));
- bmpdata.write((char*)&(bmp.rgbGreen), sizeof(bmp.rgbGreen));
- bmpdata.write((char*)&(bmp.rgbRed), sizeof(bmp.rgbRed));
- bmpdata.write((char*)&(bmp.rgbReserved), sizeof(bmp.rgbReserved));
- }
- if(bmpinfo.biBitCount == 24) //fill image
- {
- for (i=0; i < bmpinfo.biHeight ; i++){
- for (j=0; j < bmpinfo.biWidth; j++){
- bmpdata.write((char*)&bmp, sizeof(bmp));
- }
- }
- }
- bmpdata.close();
- cout << "Size: " << bmpfile.bfSize << "byte" <<
- " Width: " << bmpinfo.biWidth <<
- " Height: " << bmpinfo.biHeight <<
- " Blue: " << bmp.rgbBlue <<
- "\nBitmap created successfully" << endl;
- wait();
- return 1;
- }