首先,你可以不必把DIB轉換成DDB就可以讓它顯示在設備上。但是那樣的話,你都會看見會程序運行得有多慢了,而且你也不
能使用一些有關DDB操作的函數功能了,諸如BitBlt()....
以下是從DIB創建一個DDB的基本步驟:
1.從DIB顏色表信息中的信息可以創建一個邏輯調色板,如果設備支持的話,你只需要做這一步。爲了創建一個調色板,就需要
分配給邏輯調色板結構。初始化palversion和palnumentries,從DIB中的顏色表把顏色數拷貝過來。然後我們就可以使用createpalett
e()函數來使用我們初始化的邏輯調色板了。
2.把邏輯調色板選到設備中,然後實現它。
3.創建DDB,可以使用createdibbitmap()函數。
4.最後不要忘記釋放掉分給邏輯調色板結構的內存。
雖然這樣就可以創建一個邏輯調色板了,但它沒有返回信息給所調用的程序代碼。如果DIB描述的是一個256色的位圖的話,而
且設備也只支持256色,那麼DDB可能就不能正確的顯示在設備上。那是因爲系統使用的顏色數與位圖的顏色數不相同,因此,
你要修改一下,在我們畫位圖之前,就讓它返回邏輯調色板,然後我們選擇和實現它到設備的上下文。
//以下是程序代碼
HBITMAP CProcess::DIBtoDDB(HANDLE hdib )
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
CPalette Pal;
CPalette* pOldPal;
CClientDC dc(NULL);
if (hdib == NULL)
{
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hdib;
int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 1 << lpbi->biBitCount; //顏色表項的個數
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hdib;
LPVOID lpDIBits;
if(bmInfo.bmiHeader.biBitCount > 8 )
{
lpDIBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors
+ bmInfo.bmiHeader.biClrUsed)
+ ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
}
else{
lpDIBits = (LPVOID)(bmInfo.bmiColors + nColors);
}
// create and select a logical palette if needed
if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
UINT nSize=sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *plp=(LOGPALETTE *) new BYTE[nSize];
plp->palVersion = 0x300;
plp->palNumEntries = nColors;
for( int i=0; i<nColors;i++)
{
plp->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
plp->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
plp->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
plp->palPalEntry[i].peFlags = 0;
}
Pal.CreatePalette(plp);
delete[] plp; //要及時處理
// select and realize the palette
pOldPal = dc.SelectPalette(&Pal, false);
dc.RealizePalette();
}
hbm = CreateDIBitmap(dc.GetSafeHdc(), //設備上下文的句柄
(LPBITMAPINFOHEADER)lpbi, //位圖信息頭指針
(long)CBM_INIT, //初始化標誌
lpDIBits, //初始化數據指針
(LPBITMAPINFO)lpbi, //位圖信息指針
DIB_RGB_COLORS); //顏色數據的使用方式
if (Pal.GetSafeHandle())
{
dc.SelectPalette(pOldPal,false);
}
return hbm;
} --------------------------
DDB轉爲DIB
把DDB轉換成DIB
設備相關位圖(DDB)顯示方式是儘可能與顯示設備驅動程序相匹配,這樣,DDB不可能與其他顯示設備兼容;而設備無關位圖(DIB)能與所有顯示設備兼容,但是,其缺點在於顯示速度比較慢。
我們需要把DDB轉換爲DIB的一種情況是,需要將位圖保存到一個文件中。下面是其實現的代碼。
// DDBToDIB - Creates a DIB from a DDB
// bitmap - Device dependent bitmap(DDB)
// pPal - Logical palette
/*
HANDLE DDBToDIB(CBitmap& bitmap, DWORD dwCompression, CPalette* pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen; //DIB長度
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
ASSERT(bitmap.GetSafeHandle());
// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
{
return NULL;
}
// If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
{
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
}
// Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors> 256 )
{
nColors = 0;
}
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB)//如果內存申請失敗
{
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB; //使用hDIB申請的內存
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
//GetDIBits函數用來計算所需的內存空間的大小和得到DIB參數
bi = *lpbi;
// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0)
{
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) * bi.biHeight;
}
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
{
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
{
hDIB = handle;
}
else
{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits 獲取內存
lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)
DIB_RGB_COLORS ); // Use RGB for color table
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
2006-8-21
今天又寫了一個DDB轉DIB的函數,它是一個象素一個象素點的讀取的,相對較慢!而且只支持24位的!
HANDLE CProcess::DDBtoDIB(CBitmap &bitmap, CDC *pDC)
{
LPBITMAPINFO lpbmi;
DWORD dwHeader;
DWORD dwLen;
HANDLE hDIB;
LPSTR pDIB;
CDC memDC;
dwHeader = m_image->GetHeaderSize();
dwLen = dwHeader + m_image->GetImageSize();//獲取文件大小
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(bitmap);
pDIB = (LPSTR)new char[dwLen];//該內存在SaveLayer()函數中釋放
if (pDIB == NULL)//如果內存申請失敗
{
return NULL;
}
lpbmi = (LPBITMAPINFO)pDIB; //使用pDIB申請的內存
int width = m_image->GetWidth();
int height = m_image->GetHeight();
if (m_image->m_lpbmi->bmiHeader.biBitCount < 24)
{
m_image->ComputePaletteSize(24);
if (m_image->m_lpbmi != NULL)
{
m_image->m_lpbmi = NULL;
delete m_image->m_lpbmi;
}
m_image->m_lpbmi = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)];
if (NULL == m_image->m_lpbmi)
{
AfxMessageBox("m_lpbmi內存申請失敗");
}
m_image->m_lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_image->m_lpbmi->bmiHeader.biWidth = width;
m_image->m_lpbmi->bmiHeader.biHeight = height;
m_image->m_lpbmi->bmiHeader.biPlanes = 1;
m_image->m_lpbmi->bmiHeader.biBitCount = 24;
m_image->m_lpbmi->bmiHeader.biCompression = BI_RGB;
m_image->m_lpbmi->bmiHeader.biSizeImage = 0;
m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
m_image->m_lpbmi->bmiHeader.biClrUsed = 0;
m_image->m_lpbmi->bmiHeader.biClrImportant = 0;
m_image->ComputeImage();//重新計算圖像大小
m_image->m_lpbmi->bmiHeader.biSizeImage = m_image->GetImageSize();
}
memcpy(lpbmi, m_image->m_lpbmi, dwHeader);//獲取信息頭
int x = 0 ,y = 0;
DWORD i = 0;
COLORREF color;
for (y = height - 1; y >= 0; y--)
{
for (x = 0; x < width; x++)
{
i = (height - 1 - y)*width + x;
color = memDC.GetPixel(x, y);
pDIB[dwHeader + i*3] = (BYTE)((color&0x00ff0000)>>16);
pDIB[dwHeader + i*3 +1] = (BYTE)((color&0x0000ff00)>>8);
pDIB[dwHeader + i*3 +2] = (BYTE)(color&0x000000ff);
}
}
hDIB = (HANDLE)pDIB;
return hDIB;
}