Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
See more:
converting 8 bit, 24 bit Bitmap to 32 bit in VC++
Posted
Comments
CPallini 26-Oct-10 6:06am    
That's a rather big topic. You cannot get full code in a quick answer: check out the documentation: http://msdn.microsoft.com/en-us/library/dd183377(VS.85).aspx
Sandeep Mewara 26-Oct-10 6:14am    
Any effort made by you to solve your problem? Currently, question formation by you lacks effort!

Hi,
Supposing your are asking about Windows(tm) bitmaps[^] the simplest is to use a Windows image library:

- 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).

Alternatively you can use the GDI[^] API:
Assuming you have a bmp0 HBITMAP with 8 or 24 bits color depth:
Create a dc0 memory DC,
Select bmp0 into dc0,
Use CreateDIBSection[^] with dc0 and a correctly initialized BITMAPINFO[^] with bmp0 size and 24 bits color depth. The result is a bmp1 HBITMAP,
Create a dc1 memory dc compatible with dc0,
Select bmp1 into dc1, keep track of the returned bmpDef HBITMAP,
BitBlt[^] dc0 into dc1,
Select bmpDef into dc1.
DeleteObject() everything except bmp1,

You are done :)

cheers,
AR
 
Share this answer
 
v2
I have some ideas:
- when converting 24 to 32 bit you can simply copy components of color, and put maximum value to alpha;
- when converting 8 to 32 bit you you can mutilply all components to value of maximum 32 bit color component divided to maximum 8 bit color component
 
Share this answer
 
v2
Comments
Sauro Viti 27-Oct-10 5:48am    
What are you proposing for 8 to 32 bit conversion doesn't work: if the 8 bit image is a grayscale one, you have to set for each pixel the alpha channel to 255 and red = green = blue = the value of the byte on the source. If the 8 bit image is a colour image with palette, you have to lookup each value in the palette to get the correspondant RGB values.
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;
}

have not tested yet - but should work.

code reformatted.
-Emilio-

@-Emilio-: thanks, but there are some '>' too and i dont know how to remove. ps: hey i've got it.
 
Share this answer
 
v13
converting 8 bit, 24 bit Bitmap to 32 bit in mfc
 
Share this answer
 
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)  //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 = "";

}



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;	//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);
	}
}


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;		//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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900