?
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