📄 dibapi.cpp
字号:
#include "stdafx.h"
#include "DibAPI.h"
#include "math.h"
/**************************************************
* *
* *
* This is the image process function set *
* *
* written by Judy Feng in May 2001 *
* *
* *
***************************************************/
void WaitCursorBegin()
{
SetCursor(LoadCursor(NULL,IDC_WAIT));
}
void WaitCursorEnd()
{
SetCursor(LoadCursor(NULL,IDC_ARROW));
}
/***************************************************
* Function Name:
*
* timeGetTime()
*
* Parameters :
*
* none
*
* Return Value:
*
* DWORD - ms to delay
*
* Description:
*
* This function return the system time of ms
**************************************************************/
DWORD timeGetTime(void)
{
DWORD ms=0;
SYSTEMTIME timeCurrent;
GetSystemTime(&timeCurrent);
ms+=timeCurrent.wMilliseconds+timeCurrent.wSecond *1000+
timeCurrent.wMinute *60*1000+timeCurrent .wHour
*60*60*1000;
return ms;
}
/***************************************************
* Function Name:
*
* Delay()
*
* Parameters :
*
* DWORD dwDelayTime- ms to delay
*
* Return Value:
*
* none
*
* Description:
*
* This function delay the specified ms before perform next
* instructions
**************************************************************/
void Delay(DWORD dwDelayTime)
{
DWORD dwTimeBegin,dwTimeEnd;
dwTimeBegin=timeGetTime();
do
{
dwTimeEnd=timeGetTime();
}
while (dwTimeEnd-dwTimeBegin<dwDelayTime);
}
/************************************************************
*
* Function Name:
*
* CreateDIB()
*
* Parameters :
*
* DWORD dwWidth:-width for new bitmap,in pixels
* DWORD dwHeight:-height for new bitmap
* WORD wBitCount:-Bit Count for new DIB(1,4,8,24)
*
* Return Value:
*
* HDIB -Handle of the new DIB
*
* Description:
*
* This function allocates memory for and initialize
* a new DIB by filling in the BITMAPINFOHEADER ,all-
* ocating memory for the color table, and allocate
* memory for the bitmap bits. As with all HDIBs,the
* header, colortable and bits are all in one contiguous
* memory block.This function is similar to the WinApi-
* CreateBitmap()
*
* The colortable and bitmap bits are left uninitialized
* (zeroed) in the returned HDIB
*
***********************************************************/
HDIB CreateDIB(DWORD dwWidth,DWORD dwHeight,WORD wBitCount)
{
BITMAPINFOHEADER bi;// bitmap header
LPBITMAPINFOHEADER lpbi;//pointer to bitmap header
DWORD dwLen;//Size of memory block
HDIB hDIB;
DWORD dwBytesPerLine;//Number of bytes per scan line
//make sure bits per pixel is valid
if(wBitCount<=1)
wBitCount=1;
else if (wBitCount<=4)
wBitCount=4;
else if (wBitCount<=8)
wBitCount=8;
else if (wBitCount<=24)
wBitCount=24;
else
wBitCount=4;//set defaule value to 4 if parameter is bogus
//initialize bitmapinfoheader
bi.biWidth =dwWidth;//fill in width from parameter
bi.biHeight =dwHeight;//fill in height from parameter
bi.biPlanes =1;//must be 1
bi.biBitCount =wBitCount;//from parameter
bi.biCompression =BI_RGB;
bi.biSizeImage =0;//0 means default
bi.biXPelsPerMeter =0;
bi.biYPelsPerMeter =0;
bi.biClrUsed =0;
bi.biClrImportant =0;
//calculate size of memory block required to store the DIB,
//This block should be big enough to hold the BITMAPINFOHEADER,the
//color table and the bits
dwBytesPerLine=WIDTHBYTES(wBitCount *dwWidth);
dwLen=bi.biSize +PaletteSize((LPBYTE)&bi)+(dwBytesPerLine*dwHeight);
//allocate memory block to store our bitmap
hDIB=GlobalAlloc(GHND,dwLen);
//major bummer if we couldn't get pointer to it
if(!hDIB)
return NULL;
//lock memory and ger pointer to it
lpbi=(LPBITMAPINFOHEADER)GlobalLock(hDIB);
//use out bitmap info structure to fill in first part of
//our DIB with the BITMAPINFOHEADER
*lpbi=bi;
//since we don't know what the colortable and bits should contain
//just leave these blank.unlock the DIB and return the HDIB
GlobalUnlock(hDIB);
//return handle to the DIB
return hDIB;
}
/************************************************************
*
* Function Name:
*
* CreateDefaultDIB()
*
* Parameters :
*
* DWORD dwWidth:-width for new bitmap,in pixels
* DWORD dwHeight:-height for new bitmap
*
* Return Value:
*
* HDIB -Handle of the new DIB
*
* Description:
*
* This function allocates memory and initialize
* a new DIB by filling in the BITMAPINFOHEADER with
* default system palette ,allocating memory for the
* color table, and allocate memory for the bitmap bits.
* As with all HDIBs,the header, colortable and bits are
* all in one contiguous memory block.This function is
* similar to the WinApi CreateBitmap()
* The colortable is initialized with system palette,
* but bitmap bits are left uninitialized
* (zeroed) in the returned HDIB
*
***********************************************************/
HDIB CreateDefaultDIB(DWORD dwWidth,DWORD dwHeight)
{
//Get DC
HDC hDC=GetDC(NULL);
if(!hDC)
return NULL;
//DC bits/pixel
int nDeviceBitsPixel=GetDeviceCaps(hDC,BITSPIXEL);
//create DIB according to DC
HDIB hDIB=CreateDIB(dwWidth,dwHeight,nDeviceBitsPixel);
//DIB buffer
LPBITMAPINFO lpbmi=(LPBITMAPINFO)GlobalLock(hDIB);
LPBYTE lpDIBBits=FindDIBBits((LPBYTE)lpbmi);
DWORD dwBitsSize=lpbmi->bmiHeader .biHeight *
BytesPerLine((LPBYTE)&(lpbmi->bmiHeader ));
//set DIB color to white
for(DWORD l=0;l<dwBitsSize;++l)
lpDIBBits[l]=0xff;
//if no palette, return DIB handle
if(nDeviceBitsPixel>8)
{
GlobalUnlock(hDIB);
ReleaseDC(NULL,hDC);
return hDIB;
}
//if there is palette, set system palette to DIB
//colors in system palette
int nColors=PalEntriesOnDevice(hDC);//number of palette entries
//copy the current system palette into our logical palette
PALETTEENTRY pe[256];
GetSystemPaletteEntries(hDC,0,nColors,pe);
//set color table
for(int i=0;i<nColors;++i)
{
lpbmi->bmiColors [i].rgbRed =pe[i].peRed ;
lpbmi->bmiColors [i].rgbGreen =pe[i].peGreen ;
lpbmi->bmiColors [i].rgbBlue =pe[i].peBlue ;
lpbmi->bmiColors [i].rgbReserved =0;
}
//clean up
GlobalUnlock(hDIB);
ReleaseDC(NULL,hDC);
return hDIB;
}
/************************************************************
*
* Function Name:
*
* DestroyDIB()
*
* Parameters :
*
* NULL
*
* Return Value:
*
* NULL
*
* Description:
*
* This function frees memory associated with a DIB
*
************************************************************/
void DestroyDIB(HDIB hDIB)
{
GlobalFree(hDIB);
}
/************************************************************
*
* Function Name:
*
* PalEntriesOnDevice()
*
* Parameters :
*
* HDC hDC:-device context
*
* Return Value:
*
* int -number of palette entries on Device
*
* Description:
*
* This function gets the number of palette entries on
* the specified device
*
************************************************************/
int PalEntriesOnDevice(HDC hDC)
{
int nColors;// number of colors
//find out the number of colors on this device
nColors=(1<<(GetDeviceCaps (hDC,BITSPIXEL)*
GetDeviceCaps (hDC,PLANES)));
ASSERT(nColors);
return nColors;
}
/************************************************************
*
* Function Name:
*
* LoadDIB()
*
* Parameters :
*
* LPTSTR lpFileName:-specifies the file to load a DIB from
*
* Return Value:
*
* A handle to DIB data buffer, or null if unsuccessful
*
* Description:
*
* This function loads the DIB bitmap to the memory
*
************************************************************/
HDIB LoadDIB(LPCTSTR lpFileName)
{
HDIB hDIB;
HANDLE hFile;
//set the cursor to a hourglass,in case the loading
//operation take more than a sec, the user will know
// what 's going on
SetCursor(LoadCursor(NULL,IDC_WAIT));
if ((hFile=CreateFile(lpFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL))!=INVALID_HANDLE_VALUE)
{
hDIB=ReadDIBFile(hFile);
CloseHandle(hFile);
SetCursor(LoadCursor(NULL,IDC_ARROW));
return hDIB;
}
else
{
SetCursor(LoadCursor(NULL,IDC_ARROW));
return NULL;
}
}
/************************************************************
*
* Function Name:
*
* ReadDIBFile()
*
* Parameters :
*
* HANDLE file:- the file handle to read from
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -