Professional Documents
Culture Documents
(Solved) Converting 8 Bit, 24 Bit Bitmap To 32 Bit in MFC - CodeProject
(Solved) Converting 8 Bit, 24 Bit Bitmap To 32 Bit in MFC - CodeProject
Comments
‐ GDI+[^] distributed with XP and over and downloadable for previous Win OS.
‐ The Windows Imaging Component[^] distributed with Vista and over.
‐ CxImage[^] from CodeProject which is system independant.
With any of these read the library documentation and use the provided API ﴾should be few lines of code
in any of them﴿.
cheers,
AR
HBITMAP convert_8to32(HBITMAP hbmp)
{
BITMAP bmp;
if(GetObject(hbmp,sizeof(bmp),&bmp))
{
if(8==bmp.bmBitsPixel)
{
int cy = 0<bmp.bmheight?bmp.bmheight:‐bmp.bmheight;
unsigned int bpl8 = (bmp.bmWidth+3)&~3;
unsigned int bpl32 = 4*bmp.bmWidth;
unsigned char* lp8 = (unsigned char*)malloc(bpl8*cy);
unsigned char* lp32 = (unsigned char*)malloc(bpl32*cy);
HDC mdc = CreateCompatibleDC(0);
BITMAPINFO* pbmi = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+
(256*sizeof(RGBQUAD)));
int x,y;
unsigned char* lpdst = lp32;
unsigned char* lpsrc = lp8;
pbmi‐>bmiHeader.biSize = sizeof(BITMAPINFO)+(256*sizeof(RGBQUAD));
GetDIBits(mdc,hbmp,0,cy,lp8,pbmi,DIB_RGB_COLORS);
for(y=0;y<cy;y++)
{
for(x=0;x<bmp.bmwidth;x++)
{
lpdst[(x<<2)+0] = pbmi‐>bmiColors[lpsrc[x]].rgbBlue;
lpdst[(x<<2)+1] = pbmi‐>bmiColors[lpsrc[x]].rgbGreen;
lpdst[(x<<2)+2] = pbmi‐>bmiColors[lpsrc[x]].rgbRed;
lpdst[(x<<2)+3] = 0x00;
}
lpdst += bpl32;
lpsrc += bpl8;
}
pbmi‐>bmiHeader.biSize = sizeof(BITMAPINFO);
pbmi‐>bmiHeader.biPlanes = 1;
pbmi‐>bmiHeader.biBitCount = 32;
pbmi‐>bmiHeader.biCompression = 0;
pbmi‐>bmiHeader.biSizeImage = bpl32*cy;
pbmi‐>bmiHeader.biClrUsed = 0;
pbmi‐>bmiHeader.biClrImportant = 0;
HBITMAP hbmp32 = CreateDIBitmap(mdc,&pbmi‐
>bmiHeader,CBM_INIT,lp32,pbmi,DIB_RGB_COLORS);
DeleteDC(mdc);
free(pbmi);
free(lp8);
free(lp32);
return hbmp32;
}
}
return 0;
}
code reformatted.
‐Emilio‐
@‐Emilio‐: thanks, but there are some '>' too and i dont know how to remove. ps: hey i've got it.
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();
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) //Checking bits/pixel is 24
{
AfxMessageBox("This is 24bit BmP");
Convert24bit();
}
else if(0x08==m_bi2.biBitCount) //Checking bits/pixel is 8
{
AfxMessageBox("This is 8 bit bmp");
Convert8bit();
}
else
{
AfxMessageBox("This is Not 8 bit or 24 bit bmp");
}
CloseHandle( m_hFile2 );
m_strNewPath = "";
}
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; //Changing the offset value.
m_bmfHeader.bfSize=52+size; //Changing the Size
m_bi.biBitCount=0x20; //Changing bits/pixel to 32
m_bi.biSizeImage=size; //Changing the image 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);
// AfxMessageBox("File Converted\n New File : "+m_strNewPath);
if(dwBytesWritten)
{
m_strLabel="File Converted"+m_strNewPath;
m_strEdit1="";
UpdateData(0);
CloseHandle(hFile);
}
}
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; //Changing the offset value.
m_bmfHeader.bfSize=52+size; //Changing the Size
m_bi.biBitCount=0x20; //Changing bits/pixel to 32
m_bi.biSizeImage=size; //Changing the image 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);
// AfxMessageBox("File Converted\n New File : "+m_strNewPath);
if(dwBytesWritten)
{
m_strLabel="File Converted"+m_strNewPath;
m_strEdit1="";
UpdateData(0);
CloseHandle(hFile);
}
}
Thank you..
ShibiN_b : shibisaketh@gmail.com
This content, along with any associated source code and files, is licensed under The Code Project Open License ﴾CPOL﴿
CodeProject, 503‐250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416‐849‐8900 x 100