📄 dibapi.cpp
字号:
/*******************************************
* Function Name:
*
* GetSystemPalette()
*
* Parameters :
*
* HDC hDC -handle of device context to display
* palette
*
* Return Value:
*
* HPALETTE -destination palette handle
*
* Description:
*
* This function returns a handle to a palette which
* represents the system palette .The system RGB values
* are copied into our logical palette using the Get-
* systemPaletteEntries function
//******************************************************/
HPALETTE GetSystemPalette(HDC hDC)
{
static HPALETTE hPal=NULL;// handle of the palette
HANDLE hLogPal;//handle of a logical palette
LPLOGPALETTE lpLogPal;//pointer to a logical palette
int nColors;// number of colors
//find out how many palette entries we want
nColors=PalEntriesOnDevice(hDC);
//Number of the palette entries
//Allocate a room for the palette and lock it
hLogPal=::GlobalAlloc(GHND,
sizeof(LOGPALETTE)+nColors*sizeof(PALETTEENTRY));
// if we didn't get a logical palette ,return null
if (!hLogPal)
return NULL;
//get a pointer to the logical palette
lpLogPal =(LPLOGPALETTE)::GlobalLock (hLogPal);
//set some important fields
lpLogPal->palVersion =PALVERSION;
lpLogPal->palNumEntries =nColors;
//copy the current system palette into our logical palette
GetSystemPaletteEntries (hDC,0,nColors,
(LPPALETTEENTRY)(lpLogPal->palPalEntry ));
//Go ahead and create the palette. once it's create
// we no longer need the LOGPALETTE ,so free it
hPal=::CreatePalette (lpLogPal);
//clean up
GlobalUnlock(hLogPal);
GlobalFree (hLogPal);
return hPal;
}
/********************************************************
* Function Name:
*
* CreateDIBPalette()
*
* Parameters :
*
* LPBYTE lpbi -specifies the DIB
*
* Return Value:
*
* HPALETTE -destination palette handle
*
* Description:
*
* This function Create a Palette from the DIB color table
/************************************************************/
HPALETTE CreateDIBPalette(LPBYTE lpbi)
{
LPLOGPALETTE lpPal;//pointer to a logical palette
HANDLE hLogPal;// handle to a logical palette
HPALETTE hPal=NULL; // handle to palette
LPBITMAPINFO lpbmi;//pointer to bitmapinfo structure
int i,wNumColors;//loop index,number of colors in color table
// if handle to DIB is invalid,return NULL
if(!lpbi)
{
return NULL;
}
//get pointer to BITMAPINFO(WIN 30)
lpbmi=(LPBITMAPINFO)lpbi;
//get the number of colors in the DIB
wNumColors=DIBNumColors(lpbi);
//is this a win3.0 DIB?
if(wNumColors)
{
//allocates memory block for logical palette
hLogPal=GlobalAlloc(GHND,sizeof(LOGPALETTE)+
sizeof(PALETTEENTRY)*wNumColors);
//if not enough memory, clean up and return NULL
if(!hLogPal)
return NULL;
//lock memory block and get pointer to it
lpPal=(LPLOGPALETTE)GlobalLock(hLogPal);
//Set version and number of palette entries
lpPal->palVersion =PALVERSION;
lpPal->palNumEntries =wNumColors;
//store RGB triples(if(win 3.0DIB) or RGB quads(if OS/2DIB)
//into palette
for(i=0;i<wNumColors;i++)
{
lpPal->palPalEntry [i].peRed =
lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry [i].peGreen =
lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry [i].peBlue =
lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry [i].peFlags =0;
}
//create the palette and get handle to it
hPal=::CreatePalette(lpPal);
//if error getting handle to palette ,clean up and return
if(!hPal)
{
GlobalUnlock(hLogPal);
GlobalFree(hLogPal);
return NULL;
}
//clean up
GlobalUnlock(hLogPal);
GlobalFree(hLogPal);
}
return hPal;
}
/********************************************************
* Function Name:
*
* CreateDIBPalette()
*
* Parameters :
*
* HDIB -specifies the DIB
*
* Return Value:
*
* HPALETTE -destination palette handle
*
* Description:
*
* This function Create a Palette from the DIB color table
/************************************************************/
//根据图像数据块,创建调色板数据块
HPALETTE CreateDIBPalette(HDIB hDIB)
{
//锁定位图信息头
LPBITMAPINFOHEADER lpbi;
lpbi=(LPBITMAPINFOHEADER)GlobalLock(hDIB);
//VC自带的逻辑调色板数据结构
LPLOGPALETTE lpPal;//pointer to a logical palette
HANDLE hLogPal;// handle to a logical palette
HPALETTE hPal=NULL; // handle to palette
//位图信息数据结构指针
LPBITMAPINFO lpbmi;//pointer to bitmapinfo structure
int i,wNumColors;//loop index,number of colors in color table
// if handle to DIB is invalid,return NULL
if(!lpbi)
{
return NULL;
}
//get pointer to BITMAPINFO(WIN 30)
//锁定位图信息(包括位图信息头和颜色表)
lpbmi=(LPBITMAPINFO)lpbi;
//get the number of colors in the DIB
//计算当前图像数据块的颜色数目
wNumColors=DIBNumColors(lpbi);
//根据颜色数目,计算颜色表的大小,创建颜色表内存块
if(wNumColors)
{
//allocates memory block for logical palette
//分配内存
int nPal=sizeof(PALETTEENTRY);
int nPalSize=nPal*wNumColors;
int nLogPal=sizeof(LOGPALETTE);
hLogPal=GlobalAlloc(GHND,nLogPal+nPalSize);
//if not enough memory, clean up and return NULL
if(!hLogPal)
return NULL;
//lock memory block and get pointer to it
//锁定调色板数据块内存
lpPal=(LPLOGPALETTE)GlobalLock(hLogPal);
//Set version and number of palette entries
//设置相应的字段值
lpPal->palVersion =PALVERSION;
lpPal->palNumEntries =wNumColors;
//store RGB triples(if(win 3.0DIB) or RGB quads(if OS/2DIB)
//into palette
//将图像中读出的颜色表信息放入调色板数据结构中
for(i=0;i<wNumColors;i++)
{
lpPal->palPalEntry [i].peRed =
lpbmi->bmiColors[i].rgbRed;
int n=lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry [i].peGreen =
lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry [i].peBlue =
lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry [i].peFlags =0;
}
//create the palette and get handle to it
//根据调色板指针返回调色板内存块句柄(WinAPI函数)
hPal=::CreatePalette(lpPal);
//if error getting handle to palette ,clean up and return
if(!hPal)
{
GlobalUnlock(hLogPal);
GlobalFree(hLogPal);
return NULL;
}
//clean up
//释放逻辑调色板内存
//(以后使用Windows的调色板句柄控制调色板)
GlobalUnlock(hLogPal);
GlobalFree(hLogPal);
}
GlobalUnlock(hDIB);
return hPal;
}
/*************************************************************
* Function Name:
*
* DIBToDIBSection()
*
* Parameters :
*
* LPBYTE lpDIB -pointer to DIB data buffer
*
* Return Value:
*
* HBITMAP - handle of DIBSECTION or NULL for failure
*
* Description:
*
* This function create DIBSECTION from DIB
*
**************************************************************/
HBITMAP DIBToDIBSection(LPBYTE lpDIB)
{
LPBYTE lpSourceBits;
HDC hDC=NULL,hSourceDC;
HBITMAP hSourceBitmap,hOldSourceBitmap;
DWORD dwSourceBitSize;
LPBITMAPINFO lpSrcDIB=(LPBITMAPINFO)lpDIB;
if(!lpSrcDIB)
return NULL;
//go to use DIBSections and BitBlt() to do the conversion,
hDC=GetDC(NULL);
hSourceBitmap=CreateDIBSection(hDC,lpSrcDIB,DIB_RGB_COLORS,
(void**)&lpSourceBits,NULL,0);
hSourceDC=CreateCompatibleDC(hDC);
//flip the bits on the source DIBSection to match the source DIB
dwSourceBitSize=lpSrcDIB->bmiHeader .biHeight *
BytesPerLine((LPBYTE)&(lpSrcDIB->bmiHeader ));
memcpy(lpSourceBits,FindDIBBits((LPBYTE)lpSrcDIB),dwSourceBitSize);
//select DIBSections int DCs
hOldSourceBitmap=(HBITMAP)SelectObject(hSourceDC,hSourceBitmap);
//set the color tables for the DIBSections
if(lpSrcDIB->bmiHeader .biBitCount <=8)
SetDIBColorTable(hSourceDC,0,
1<<lpSrcDIB->bmiHeader .biBitCount ,
lpSrcDIB->bmiColors );
//clean up and delete the DCs
SelectObject(hSourceDC,hOldSourceBitmap);
DeleteDC(hSourceDC);
ReleaseDC(NULL,hDC);
//Flush the GDI batch,so we can play with the bits
GdiFlush();
return hSourceBitmap;
}
/*************************************************************
* Function Name:
*
* DIBToDIBSection()
*
* Parameters :
*
* HDIB hDIB -pointer to DIB data buffer
*
* Return Value:
*
* HBITMAP - handle of DIBSECTION or NULL for failure
*
* Description:
*
* This function create DIBSECTION from DIB
*
**************************************************************/
HBITMAP DIBToDIBSection(HDIB hDIB)
{
//锁定位图信息头
LPBITMAPINFOHEADER lpDIB;
lpDIB=(LPBITMAPINFOHEADER)GlobalLock(hDIB);
LPBYTE lpSourceBits;
//设备上下文句柄
HDC hDC=NULL,hSourceDC;
//位图句柄
HBITMAP hSourceBitmap,hOldSourceBitmap;
DWORD dwSourceBitSize;
//位图信息数据结构
LPBITMAPINFO lpSrcDIB=(LPBITMAPINFO)lpDIB;
if(!lpSrcDIB)
return NULL;
//go to use DIBSections and BitBlt() to do the conversion,
//获得当前的设备上下文
hDC=GetDC(NULL);
//创建位图数据句柄,调用WinAPI函数
hSourceBitmap=CreateDIBSection(hDC,lpSrcDIB,DIB_RGB_COLORS,
(void**)&lpSourceBits,NULL,0);
//创建兼容的设备上下文备用
hSourceDC=CreateCompatibleDC(hDC);
//flip the bits on the source DIBSection to match the source DIB
//计算位图数据需要的字节数
dwSourceBitSize=lpSrcDIB->bmiHeader .biHeight *
BytesPerLine((LPBYTE)&(lpSrcDIB->bmiHeader ));
//将实际位图数据复制到lpSourceBits中
memcpy(lpSourceBits,FindDIBBits((LPBYTE)lpSrcDIB),dwSourceBitSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -