View Single Post
Old 4th March 2011, 05:28   #21
Smelter
Major Dude
 
Smelter's Avatar
 
Join Date: Jan 2004
Posts: 1,139
code:

#pragma once
#include"stdafx.h"


class CBitmapHandler
{
private:

char * buffer;
public:
int width,height,depth;
CBitmapHandler(void)
{
buffer=NULL;
depth=32;

}

int getSize()
{
return width*height*(depth/8);
}
char * getBits()
{
return buffer;
}
int readBitmap(char * path)
{
HANDLE m_hFile = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

if (m_hFile == INVALID_HANDLE_VALUE)
return -1;

DWORD dwFileSize = GetFileSize(m_hFile, NULL);
if (dwFileSize == INVALID_FILE_SIZE)
return -1;
BYTE* m_pFile = new BYTE[dwFileSize];
DWORD nBytesRead = 0;
if(!ReadFile(m_hFile, m_pFile, dwFileSize, &nBytesRead, NULL))
{
return -1;
}
int cbFileHeader = sizeof(BITMAPFILEHEADER);
BITMAPFILEHEADER *pBm = (BITMAPFILEHEADER*)m_pFile;

int m_cbBitmapInfo = pBm->bfOffBits - cbFileHeader;

BITMAPINFO* m_pBmi = (BITMAPINFO*)(m_pFile + cbFileHeader);
height=m_pBmi->bmiHeader.biHeight;
width=m_pBmi->bmiHeader.biWidth;
depth=m_pBmi->bmiHeader.biBitCount;


if(buffer)
delete[] buffer;

buffer=new char[height * width * 4];

char * dataSrc=(char *) m_pFile + cbFileHeader + m_cbBitmapInfo;
char * dataDst=(char *) buffer;


for(int kk=0;kk< width * height ; kk++)
{


if(depth==32)
{
*dataDst++=*dataSrc++;
*dataDst++=*dataSrc++;
*dataDst++=*dataSrc++;
*dataDst++=*dataSrc++;
}
else if(depth==24)
{
*dataDst++=*dataSrc++;
*dataDst++=*dataSrc++;
*dataDst++=*dataSrc++;
*dataDst++=0xFF;
}else
{
return -1;
}



}

CloseHandle(m_hFile);

m_hFile = INVALID_HANDLE_VALUE;


return 1;

}

void makeBitmap(char * name, int Width,int Height,int bitCount,void * pBuffer,int BufferSize)
{
TCHAR szFilename[MAX_PATH];
wsprintf(szFilename, TEXT("%s\0"),name);

HANDLE hf = CreateFile(szFilename, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, NULL, NULL );

BITMAPFILEHEADER bfh;
memset( &bfh, 0, sizeof( bfh ) );
bfh.bfType = 'MB';
bfh.bfSize = sizeof( bfh ) + BufferSize + sizeof( BITMAPINFOHEADER );
bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );

DWORD Written = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &Written, NULL );

BITMAPINFOHEADER bih;
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = Width;
bih.biHeight = Height;
bih.biPlanes = 1;
bih.biBitCount = bitCount;
bih.biSizeImage=Width*Height*bitCount/8;
bih.biCompression=0;
bih.biClrImportant=0;

Written = 0;
WriteFile( hf, &bih, sizeof( bih ), &Written, NULL );

// Write the bitmap bits
//
Written = 0;
WriteFile( hf, pBuffer, BufferSize, &Written, NULL );

CloseHandle( hf );





}

~CBitmapHandler(void)
{
if(buffer)
delete[] buffer;
}
};




Smelter is offline   Reply With Quote