⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dibapi.cpp

📁 vc++实现简单图像编辑器。。。。。。。。。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
*                                                   
*   WORD -size of palette                  
*                                                   
*   Description:                                  
*                                                   
*   This function  calculates the size of the palette
*
************************************************************/
WORD PaletteSize(LPBYTE lpDIB)
{
	WORD nNumColors;
	nNumColors=DIBNumColors(lpDIB);
   	return nNumColors*sizeof(RGBQUAD);
}
/************************************************************
*                                                   
*   Function Name:                                 
*                                                   
*   PaletteSize()                                  
*                                                 
*   Parameters :                                  
*                                                  
*   HDIB hDIB:  -handle of DIB 
*                                       
*   Return Value:                                 
*                                                   
*   WORD -size of palette                  
*                                                   
*   Description:                                  
*                                                   
*   This function  calculates the size of the palette
*
************************************************************/
WORD PaletteSize(HDIB hDIB)
{
	WORD nNumColors;
	nNumColors=DIBNumColors(hDIB);
   	return nNumColors*sizeof(RGBQUAD);
}
/************************************************************
*                                                   
*   Function Name:                                 
*                                                   
*   PaintDIB()                                  
*                                                 
*   Parameters :                                  
*                                                  
*   HDC hDC:             -DC to output to
    LPRECT lpDCRect:     -rectangle on DC to output to
	HDIB hDIB:           -handle to global memory with DIB
	LPRECT lpDIBRect:    -rectangle of DIB to output 
	HPALETTE hPal:       -palette used display DIB if NULL
	                      use DIB palette to display
	DWORD dwRop:         -ROP mode to display DIB
*                                       
*   Return Value:                                 
*                                                   
*   BOOL -TRUE if successful , FALSE if unsuccessful                  
*                                                   
*   Description:                                  
*                                                   
*   This function  calls StretchDIBBits to paint the DIB.DIB
*   is output to the specified DC, ar the coordinated given 
*   in lpDCRect.the area of the DIB to be output is given by 
*   lpDIBRect.
*
************************************************************/
BOOL PaintDIB(HDC hDC, LPRECT lpDCRect, HDIB hDIB, 
			  LPRECT lpDIBRect, HPALETTE hPal, DWORD dwRop)
{
	LPBYTE lpDIBHdr;//pointer to BITMAPINFOHEADER
	LPBYTE lpDIBBits;//pointer to DIB bits
	BOOL bSuccess=FALSE;//success/fail flag
	HPALETTE hOldPal=NULL;//previous palette

	//check for valid DIB handle

	if(!hDIB)
		return FALSE;

	//Lock down the DIB, and get a pointer to the beginning of
	//the bit buffer

	lpDIBHdr=(LPBYTE)GlobalLock(hDIB);
	lpDIBBits=lpDIBHdr+sizeof(BITMAPINFOHEADER)+
		      DIBNumColors(lpDIBHdr)*sizeof(RGBQUAD);
	//if no palette provided, create one from DIB

	if(!hPal)
		hPal=CreateDIBPalette(lpDIBHdr);

	//select and realize our palette as background

	if(hPal)
	{
		hOldPal=SelectPalette(hDC,hPal,TRUE);
		RealizePalette(hDC);
	}

	//Make sure to use the stretching mode best for color pictures

	SetStretchBltMode(hDC,COLORONCOLOR);

	//call stretchDIBits() with dwRop

	bSuccess=StretchDIBits(hDC,
		                   lpDCRect->left ,
						   lpDCRect->top ,
						   RECTWIDTH(lpDCRect), 
		               	   RECTHEIGHT(lpDCRect),
						   lpDIBRect->left ,
						   ((LPBITMAPINFOHEADER)lpDIBHdr)->biHeight 
						   -lpDIBRect->top -(lpDIBRect->bottom-lpDIBRect->top),
						   lpDIBRect->right -lpDIBRect->left,
						   lpDIBRect->bottom-lpDIBRect->top,
						   lpDIBBits,
						   (LPBITMAPINFO)lpDIBHdr,
						   DIB_RGB_COLORS,
						   SRCCOPY);
	//unlock the memory block

	GlobalUnlock(hDIB);

	//Reselect old palette
    if(hOldPal)
		SelectPalette(hDC,hOldPal,FALSE);

	GlobalUnlock(hDIB);

	//return with success/fail flag

	return bSuccess;

}

/************************************************************
*                                                   
*   Function Name:                                 
*                                                   
*   PaintDDB()                                  
*                                                 
*   Parameters :                                  
*                                                  
*   HDC hDC:             -DC to output to
*   LPRECT lpDCRect:     -rectangle on DC to output to
*	HDIB hDIB:           -handle to global memory with DIB
*   LPRECT lpDDBRect:    -rectangle of DDB to output 
*	HPALETTE hPal:       -palette used display DDB if NULL
*	                      use DIB palette to display
*	DWORD dwRop:         -ROP mode to display DDB
*                                       
*   Return Value:                                 
*                                                   
*   BOOL -TRUE if successful , FALSE if unsuccessful                  
*                                                   
*   Description:                                  
*                                                   
*   This function  calls BitBlt() and stretchBlt() to paint
*   the DDB.DDB  is output to the specified DC, ar the 
*   coordinated given  in lpDCRect.the area of the DIB to 
*   be output is given by  lpDDBRect.
*
************************************************************/
BOOL PaintDDB(HDC hDC,LPRECT lpDCRect,HBITMAP hDDB,
			  LPRECT lpDDBRect,HPALETTE hPal,DWORD dwRop)
{
	HDC hMemDC;//handle of memory DC
	HBITMAP hOldBitmap;//handle to previous bitmap
	HPALETTE hOldPal1=NULL;//handle to previous palette
	HPALETTE hOldPal2=NULL;//handle to previous palette
	BOOL bSuccess=FALSE;//Success/fail flag

	//create a memory DC

	hMemDC=::CreateCompatibleDC(hDC);

	//if this failed,return FALSE

	if(!hMemDC)
		return FALSE;
	//if we have a palette,select and realize it
	if(hPal)
	{
		hOldPal1=SelectPalette(hMemDC,hPal,TRUE);
		hOldPal2=SelectPalette(hMemDC,hPal,TRUE);
		RealizePalette(hDC);
	}
	//select bitmap into the memory DC
	hOldBitmap=(HBITMAP)SelectObject(hMemDC,hDDB);
	//make sure to use the stretching mode best for color pictures
	SetStretchBltMode(hDC,COLORONCOLOR);
	//determine whether to call stretchBlt or BitBlt
	if((RECTWIDTH(lpDCRect)==RECTWIDTH(lpDCRect))&&
		(RECTHEIGHT(lpDCRect)==RECTHEIGHT(lpDDBRect)))
		bSuccess=BitBlt(hDC,lpDCRect->left ,lpDCRect->top ,
		                RECTWIDTH(lpDCRect), 
		               	RECTHEIGHT(lpDCRect),
						hMemDC,lpDDBRect->left ,lpDDBRect->top ,
						dwRop);
	else
		bSuccess=StretchBlt(hDC,lpDCRect->left ,lpDCRect->top ,
		                RECTWIDTH(lpDCRect), 
		               	RECTHEIGHT(lpDCRect),
						hMemDC,lpDDBRect->left ,lpDDBRect->top,
						RECTWIDTH(lpDDBRect),
						RECTHEIGHT(lpDDBRect),
						dwRop);
	//clean up
	SelectObject(hMemDC,hOldBitmap);
	if(hOldPal1)
		SelectPalette(hMemDC,hOldPal1,FALSE);
	if(hOldPal2)
		SelectPalette(hDC,hOldPal2,FALSE);
	DeleteDC(hMemDC);

	//return with success/fail flag
	return bSuccess;

}

/*************************************************
// Check the system Palette status 
// return the num of the color in palette
// and  the reserve num of the color in palette
//**************************************************/

void CheckSystemPalette(CDC *pDC)
{
	if(!pDC->GetDeviceCaps ((RASTERCAPS) & RC_PALETTE))
	{
		AfxMessageBox("The current system doesn't surport palette");
	    return;
	}
	CString str;
	int nColorNum=pDC->GetDeviceCaps (SIZEPALETTE);
	str.Format ("The current system 's number of the palette color is : %d",nColorNum);
	AfxMessageBox(str);
	int nColorReserved=pDC->GetDeviceCaps (NUMRESERVED);
	str.Format ("The current system 's reserve number of the palette color is : %d",nColorReserved);
	AfxMessageBox(str);
    
}

/*************************************************
*   Function Name:                                 
*                                                   
*   CopyPalette()                                  
*                                                 
*   Parameters :                                  
*                                                  
*   HPALETTE hPalSrc      -palette used display DDB if NULL
*	                      use DIB palette to display
*	                                       
*   Return Value:                                 
*                                                   
*   HPALETTE -destination palette handle                  
*                                                   
*   Description:                                  
*                                                   
*   This function  copys the source palette to a new 
*   palette to a new palette handle
*
//***********************************************************/

HPALETTE CopyPalette(HPALETTE hPalSrc)
{
	PLOGPALETTE pLogPal;
	int nNumEntries=0;
	HPALETTE hPal;
	HANDLE h;
	
	nNumEntries=::GetPaletteEntries(hPalSrc,0,nNumEntries,NULL);
	if(nNumEntries==0)
		return(HPALETTE) NULL;
	h=GlobalAlloc(GHND,
		            sizeof(DWORD)+sizeof(PALETTEENTRY)*nNumEntries);
	if(!h)
        return(HPALETTE) NULL;
	pLogPal=(PLOGPALETTE) GlobalLock(h);
	if(!pLogPal)
		return(HPALETTE) NULL;

	pLogPal->palVersion =0x300;
	pLogPal->palNumEntries =(WORD) nNumEntries;
	::GetPaletteEntries (hPalSrc,0,nNumEntries,pLogPal->palPalEntry );
	hPal=::CreatePalette (pLogPal);

	GlobalUnlock (h);
	GlobalFree(h);

	return hPal;

}

/*****************************************************
*   Function Name:                                 
*                                                   
*   DisplayPalette()                                  
*                                                 
*   Parameters :                                  
*                                                  
*   HDC hDC               -handle of device context to display
*                          palette
*   LPRECT                -rect range to show palette
*   HPALETTE hPal         -palette used display 	                      use DIB palette to display
*	                                       
*   Return Value:                                 
*                                                   
*   BOOL -True,if success ,else FALSE                  
*                                                   
*   Description:                                  
*                                                   
*   This function  display the color of Palette to the 
*   color dot
//*********************************************************/

BOOL DisplayPalette(HDC hDC, LPRECT lpRect, HPALETTE hPal)
{
	if(!hPal)
	{

		return FALSE;
	}
	int nEntries; // entries number of palette
	PALETTEENTRY pe[256]; // color matrix of palette
    //获得调色板的索引数目
	nEntries=::GetPaletteEntries(hPal,0,256,pe);
	//计算每行显示调色板矩形块的数目
	int nSqr=(int)sqrt((double)nEntries);

	//number of color in every row
    //计算每个矩形的宽度
	int nWidth=(lpRect->right -lpRect->left )/nSqr;
	// width of row
	//计算每个矩形的高度
	int nHeight=(lpRect->bottom -lpRect->top )/nSqr;
	//Height of column
    //计算矩形块的精确尺寸
	lpRect->right =lpRect->left +nWidth*nSqr;
	lpRect->bottom =lpRect->top +nHeight*nSqr;
    //获得和实现当前调色板
	HPALETTE hOldPal=(HPALETTE)SelectPalette (hDC,hPal,FALSE);
	RealizePalette (hDC);
	//开始使用画刷画矩形块
	HBRUSH hBrush,hOldBrush;
	int x,y;
	for(int i=0;i<nEntries;++i)
	{
		x=i%nSqr;
		y=i/nSqr;
		//设置画刷颜色
		hBrush=CreateSolidBrush( RGB(pe[i].peRed ,
								     pe[i].peGreen ,
									 pe[i].peBlue ));
		//选择画刷
		hOldBrush=(HBRUSH) SelectObject(hDC,hBrush);
		//用此画刷画矩形
		Rectangle(hDC, lpRect->left +x*nWidth,
					   lpRect->top +y*nHeight,
					   lpRect->left +(x+1)*nWidth,
					   lpRect->top +(y+1)*nHeight);
		//恢复画刷状态
		SelectObject(hDC,hOldBrush);
		DeleteObject(hBrush);
	}
    //恢复调色板状态
	SelectPalette (hDC,hOldPal,FALSE);
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -