bitmap speichern



  • hallo!
    ich hab problems bei abspeichern einer bitmap, ich übergebe der methode SetPixel col...dh R,G,B sind immer gleich und haben den wert col! dh ich werde graubilder speichern...
    hab auch schon verucht R,G,B in ein array zu tun hat aber auch nicht geklappt...
    hab mir das BMP Format noch mal angeschaut:
    http://www.wotsit.org/search.asp?s=graphics

    vielleicht kann jemand mir da weiter helfen...im endeffekt will ich in einer bitmap die einzelnen pixel setzen...mit dem offset muss man noch aufpassen hab ich wo gelesen

    cu

    #ifndef Bitmap_H
    #define Bitmap_H
    
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    #include <math.h>
    #include <ctime>
    #include <fstream>
    using namespace std;
    
    #include "Exception.h"
    
    class Bitmap
    {
    private:
    	struct BMPInfoHeader {
    			unsigned long    nSize;
    			long             nWidth;
    			long             nHeight;
    			unsigned short   nPlanes;
    			unsigned short   nBitCount;
    			unsigned long    nCompression;
    			unsigned long    nSizeImage;
    			long             nXPelsPerMeter;
    			long             nYPelsPerMeter;
    			unsigned long    nClearUsed;
    			unsigned long    nClearImportant;
    		};
    
    	struct BMPFileHeader {
    			unsigned short   nType;
    			UINT     nSize;
    			unsigned short   Reserved1;
    			unsigned short   Reserved2;
    			UINT     nOffBits;
    		};
    
       	ofstream stream;
    	BMPInfoHeader  infoheader;
    	BMPFileHeader  fileheader;
    
    public:
    	Bitmap(long Width, long Height) 
    	{
    		// open stream
    		stream.open ("PerlinNoise.bmp", ios::binary);
    
    		// fill the headers with image data
    		fileheader.nType     = 0x4D42;
    		fileheader.nSize     = sizeof (BMPInfoHeader) + sizeof (BMPFileHeader) +
    			                       3 * Width * Height;
    		fileheader.Reserved1 = 0;
    		fileheader.Reserved2 = 0;
    		fileheader.nOffBits  = sizeof (BMPInfoHeader) + sizeof (BMPFileHeader);
    
    		infoheader.nSize           = sizeof (BMPInfoHeader);
    		infoheader.nWidth          = Width;
    		infoheader.nHeight         = Height;
    		infoheader.nPlanes         = 1;
    		infoheader.nBitCount       = 24;
    		infoheader.nCompression    = 0;
    		infoheader.nSizeImage      = Width * Height;
    		infoheader.nXPelsPerMeter  = 2835;
    		infoheader.nYPelsPerMeter  = 2835;
    		infoheader.nClearUsed      = 0;
    		infoheader.nClearImportant = 0;
    
    		stream.write (reinterpret_cast <char*> (&fileheader), sizeof (fileheader));
    		stream.write (reinterpret_cast <char*> (&infoheader), sizeof (infoheader));
    	}
    	~Bitmap()
    	{
    		stream.close();
    	}
    	void SetPixel(int line, int pos, int col);
    };
    
    void Bitmap::SetPixel(int pos, int line, int col)
    {
    	stream << col; // R
    	stream << col; // G
    	stream << col; // B
    }
    
    #endif
    


  • Was genau hat nicht geklappt?



  • ich brauch noch ein RGBQUAD array und
    Color-inex array
    mit welchen daten befülle ich die?

    cu



  • hi leute! ich hab nun mal mit hilfe eines freundes die methode setpixel neu geschrieben....

    class Bitmap
    {
    private:
    	struct BMPInfoHeader { 
    		unsigned long    nSize; 
    		long             nWidth; 
    		long             nHeight; 
    		unsigned short   nPlanes; 
    		unsigned short   nBitCount; 
    		unsigned long    nCompression; 
    		unsigned long    nSizeImage; 
    		long             nXPelsPerMeter; 
    		long             nYPelsPerMeter; 
    		unsigned long    nClearUsed; 
    		unsigned long    nClearImportant; 
    	}; 
    
    	struct BMPFileHeader { 
    		unsigned short   nType; 
    		UINT     nSize; 
    		unsigned short   Reserved1; 
    		unsigned short   Reserved2; 
    		UINT     nOffBits; 
    	}; 
    
    	struct Color
        {
    		unsigned char byRed;
    		unsigned char byGreen;
    		unsigned char byBlue;
    	};
    
    	ofstream m_stream; 
    	BMPInfoHeader  m_infoheader; 
    	BMPFileHeader  m_fileheader;
    
    	int m_nWidth;
    	int m_nHeight;
    
    	Color* m_pPixeldata;
    
    public:
    	Bitmap(int nWidth, int nHeight);
    	void SetPixel(int x, int y, Color col);
    	void SetPixel(int x, int y, int red, int green, int blue);
    	void SaveBitmap(string filename);
    }; 
    
    Bitmap::Bitmap(int nWidth, int nHeight)
    {
    	m_pPixeldata = new Color[nWidth*nHeight];
    	this->m_nWidth = nWidth;
    	this->m_nHeight = nHeight;
    }
    
    void Bitmap::SetPixel(int x, int y, Color col)
    {
    	if((x>=0 && x <this->m_nWidth) && (y>=0 && y <this->m_nHeight))
    		this->m_pPixeldata[x+y*this->m_nWidth] = col;
    }
    
    void Bitmap::SetPixel(int x, int y, int red, int green, int blue)
    {
    	if((x>=0 && x <this->m_nWidth) && (y>=0 && y <this->m_nHeight))
    	{
    		Color col;
    		col.byRed = static_cast<char>(blue);
    		col.byGreen = static_cast<char>(red);
    		col.byBlue = static_cast<char>(green);
    		this->m_pPixeldata[x+y*this->m_nWidth] = col;
    	}
    }
    
    void Bitmap::SaveBitmap(string filename)
    {
    	int nLineWidth = this->m_nWidth;
        while(nLineWidth%4!=0)
    		nLineWidth++;
    
    	unsigned char ucDummy = 0;
    
    	memset(&m_fileheader,0,sizeof(m_fileheader));
    	// fill the he aders with image data 
    	m_fileheader.nType=0x4d42;//"BM"
    	m_fileheader.nSize=(unsigned int)(sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + (((this->m_nWidth*3+3)&0xFFFFFC)*this->m_nHeight));
    	m_fileheader.nOffBits=sizeof(BMPFileHeader)+sizeof(BMPInfoHeader);
    
    	memset(&m_infoheader,0,sizeof(m_infoheader));
    	m_infoheader.nSize=sizeof(BMPInfoHeader);
    	m_infoheader.nWidth=this->m_nWidth;
    	m_infoheader.nHeight=this->m_nHeight;
    	m_infoheader.nPlanes=1;
    	m_infoheader.nBitCount=24;
    	m_infoheader.nCompression=BI_RGB;
    	m_infoheader.nXPelsPerMeter=2834;//72 DPI
    	m_infoheader.nYPelsPerMeter=2834;//72 DPI
    	m_infoheader.nSizeImage=this->m_nWidth*this->m_nHeight*3;
    	m_infoheader.nClearUsed      = 0; 
    	m_infoheader.nClearImportant = 0; 
    
    	this->m_stream.open(filename.c_str(), ios::binary);
    
    	m_stream.write (reinterpret_cast <char*> (&m_fileheader), sizeof (BMPFileHeader));
    	m_stream.write (reinterpret_cast <char*> (&m_infoheader), sizeof (BMPInfoHeader)); 
    
    	for(int y = 0; y < this->m_nHeight; y++)
    	{
    		for(int x = 0; x < nLineWidth; x++)
    		{
    			// writing fillbytes?
    			if(x >= this->m_nWidth)
    			{
    				m_stream.write (reinterpret_cast <char*> (&ucDummy), 1);
    			}
    			else
    			{
    				// write colordata
    				m_stream.write (reinterpret_cast <char*> (&this->m_pPixeldata[x+y*this->m_nWidth].byBlue), 1);
    				m_stream.write (reinterpret_cast <char*> (&this->m_pPixeldata[x+y*this->m_nWidth].byGreen), 1);
    				m_stream.write (reinterpret_cast <char*> (&this->m_pPixeldata[x+y*this->m_nWidth].byRed), 1);
    				x+=2; // 3 bytes written already! -> not only one
    			}
    		}
    	}
    
    	this->m_stream.close();
    }
    
    #endif
    

    mir fällt gerade auf, dass der fileheader struktur 2 bytes größer ist, als sie sein sollte die sollte 14 bytes groß sein, ist aber 16 bytes groß........
    warum liegt das...? die bitmap wird daher nicht korrekt dargestellt;-(
    vielleicht weiß jemand woran es liegt!?

    cu


Anmelden zum Antworten