I have done the conversion of 24 bit or 8bit Bmp to 32 bit bmp.
For this create a dialog application with
3 - Buttons and 1 text box.
1. Browse button for browse the target bmp.
2. Convert button for convert to 32 bit bmp
3. Exit button
4. Text box to show brows path.
Code
the following members and functions should add in .h file
CString m_strPath;
CString m_strNewPath;
HANDLE m_hFile2, m_hFile;
BITMAPFILEHEADER m_bmfHeader, m_bmfHeader2;
BITMAPINFOHEADER m_bi, m_bi2;
int m_nSizeImage;
char* lpbitmap2;
DWORD dwBytesWritten2;
CString m_strEdit1;
CString m_strLabel;
void Convert8bit();
void Convert24bit();
in Cpp File Following functions are added.
void BitmapConverterDlg::onButtonBrowse()
CFileDialog dlg(1);
if(dlg.DoModal()==IDOK)
{
m_strLabel="";
m_strPath=dlg.GetPathName();
m_strEdit1=m_strPath;
UpdateData(0);
m_strPath.Replace("\\","\\\\");
}
void BitmapConverterDlg::OnButtonConvert()
{
m_strNewPath=m_strPath;
m_strNewPath.TrimRight(".bmp");
m_strNewPath+="_New.bmp";
m_hFile2=CreateFile(m_strPath,
GENERIC_READ,
0,
NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hFile2==INVALID_HANDLE_VALUE)
{
AfxMessageBox("Cannot Open a New File");
return;
}
dwBytesWritten2 = 0;
ReadFile(m_hFile2, (LPSTR)&m_bmfHeader2, sizeof(BITMAPFILEHEADER), &dwBytesWritten2, NULL);
ReadFile(m_hFile2, (LPSTR)&m_bi2, sizeof(BITMAPINFOHEADER), &dwBytesWritten2, NULL);
if(0x18==m_bi2.biBitCount)
{
AfxMessageBox("This is 24bit BmP");
Convert24bit();
}
else if(0x08==m_bi2.biBitCount)
{
AfxMessageBox("This is 8 bit bmp");
Convert8bit();
}
else
{
AfxMessageBox("This is Not 8 bit or 24 bit bmp");
}
CloseHandle( m_hFile2 );
m_strNewPath = "";
}
the Following Functions do the convertion of bitmap from 8 bit to 32 bit
void BitmapConverterDlg::Convert8bit()
{
m_nSizeImage=m_bi2.biSizeImage;
char* table=new char[1024];
ReadFile(m_hFile2, (LPSTR)table, 1024, &dwBytesWritten2, NULL);
lpbitmap2=new char[m_nSizeImage];
ReadFile(m_hFile2, (LPSTR)lpbitmap2, m_nSizeImage, &dwBytesWritten2, NULL);
int width=m_bi2.biWidth;
int height=m_bi2.biHeight;
int size=height*width*4;
HANDLE hFile = CreateFile(m_strNewPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
AfxMessageBox("Cannot Open File");
}
char* lpbitmap=new char[size];
int padding=0;
if(width%4)
padding=4-(width%4);
int shif=0;
int index;
int m_nNewIndex=0;
for(int i=0;i<m_nSizeImage;i++)
{
index=(int)lpbitmap2[i];
index=4*index;
lpbitmap[m_nNewIndex]=table[index];
lpbitmap[m_nNewIndex+1]=table[index+1];
lpbitmap[m_nNewIndex+2]=table[index+2];
lpbitmap[m_nNewIndex+3]=table[index+3];
m_nNewIndex+=4;
shif++;
if(shif==width)
{
i+=padding;
shif=0;
}
}
m_bmfHeader=m_bmfHeader2;
m_bi=m_bi2;
m_bmfHeader.bfOffBits=0x00000036;
m_bmfHeader.bfSize=52+size;
m_bi.biBitCount=0x20;
m_bi.biSizeImage=size;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&m_bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&m_bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, size, &dwBytesWritten, NULL);
if(dwBytesWritten)
{
m_strLabel="File Converted"+m_strNewPath;
m_strEdit1="";
UpdateData(0);
CloseHandle(hFile);
}
}
the Following Functions do the convertion of bitmap from 24 bit to 32 bit
void BitmapConverterDlg::Convert24bit()
{
m_nSizeImage=m_bi2.biSizeImage;
lpbitmap2=new char[m_nSizeImage];
ReadFile(m_hFile2, (LPSTR)lpbitmap2, m_nSizeImage, &dwBytesWritten2, NULL);
int width=m_bi2.biWidth;
int height=m_bi2.biHeight;
int size=height*width*4;
HANDLE hFile = CreateFile(m_strNewPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
AfxMessageBox("Cannot Open File");
}
char* lpbitmap=new char[size];
int padding=0;
padding=width%4;
int d=width*3;
int m_nNewIndex=0;
for(int i=0;i<m_nSizeImage;i++)
{
lpbitmap[m_nNewIndex]=lpbitmap2[i];
if((0!=padding)&&(0==(i+1)%d))
{
i=i+padding;
d=width*3+d+padding;
}
m_nNewIndex++;
if(0==(m_nNewIndex+1)%4)
{ lpbitmap[m_nNewIndex]=0x00;
m_nNewIndex++;
}
}
m_bmfHeader=m_bmfHeader2;
m_bi=m_bi2;
m_bmfHeader.bfOffBits=0x00000036;
m_bmfHeader.bfSize=52+size;
m_bi.biBitCount=0x20;
m_bi.biSizeImage=size;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&m_bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&m_bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, size, &dwBytesWritten, NULL);
if(dwBytesWritten)
{
m_strLabel="File Converted"+m_strNewPath;
m_strEdit1="";
UpdateData(0);
CloseHandle(hFile);
}
}
Thank you..
ShibiN_b : shibisaketh@gmail.com