📄 colormap.c
字号:
/* set color masks: */
if (lpcidd->dwFlags & _BITMASK) {
/* get offset of color masks in the bitmap: */
ULONG32 *lpColors = (ULONG32*)&lpHXbi->un.dwBitMask[0];
lpColors[0] = lpcidd->dwBitMask[0];
lpColors[1] = lpcidd->dwBitMask[1];
lpColors[2] = lpcidd->dwBitMask[2];
}
/* success: */
return 0;
}
/*
* Calculates the size of an image with a given color format.
*/
static int ImageSize (int cid, ULONG32 dwWidth, ULONG32 dwHeight)
{
int pitch, size;
/* do it in a lazy way: */
switch (cid) {
/* planar YUV 4:2:0 formats: */
case CID_I420:
case CID_YV12:
size = dwHeight * dwWidth * 3 / 2;
break;
/* YUV 9 format: */
case CID_YUVA:
size = (dwHeight * dwWidth) * 5 / 2;
break;
case CID_YVU9:
size = dwHeight * dwWidth * 9 / 8;
break;
/* packet YUV 4:2:2 formats: */
case CID_YUY2:
case CID_UYVY:
size = dwHeight * dwWidth * 2;
break;
/* RGB formats: */
case CID_RGB32:
case CID_ARGB32:
case CID_RGB24:
case CID_RGB565:
case CID_RGB555:
case CID_RGB8:
case CID_BGR32:
case CID_BGR24:
case CID_RGB32S:
case CID_RGB444:
pitch = dwWidth * ciddTbl[cid].nBPP;
pitch = (pitch + 3) & ~3;
size = dwHeight * pitch;
break;
/* the other formats: */
default:
size = 0; /* no idea what size should be??? */
}
return size;
}
/*
* Get pitch of the bitmap image.
* Use:
* int GetBitmapPitch (LPBITMAPINFO lpbi);
* Input:
* lpbi - pointer to BITMAPINFO structure containing image format
* Returns:
* !0 -- pitch of the bitmap image; <0 if bottom-up bitmap
* 0 - unrecognized bitmap format
*/
#ifdef _WIN32
int GetBitmapPitch (LPBITMAPINFO lpbi)
#else
int GetBitmapPitch (HXBitmapInfo* lpbi)
#endif
{
register int cid, pitch;
/* check bitmap pointer & format: */
cid = GetBitmapColor (lpbi);
if (cid == CID_UNKNOWN || !(ciddTbl[cid].dwFlags & _BITMAP))
return 0;
if (cid == CID_XING)
return 768;
/* calculate image pitch: */
pitch = lpbi->bmiHeader.biWidth * ciddTbl[cid].nBPP;
if (ciddTbl[cid].dwFlags & (_RGB|_BGR))
#if defined(_MACINTOSH) || defined(_UNIX)
pitch = ((pitch + 3) & ~3);
#else
pitch = -((pitch + 3) & ~3);
#endif
/* return pitch: */
return pitch;
}
/*
* Get size of the bitmap image.
* Use:
* int GetBitmapImageSize (LPBITMAPINFO lpbi);
* Input:
* lpbi - pointer to BITMAPINFO structure containing image format
* Returns:
* !0 -- size of the bitmap image
* 0 - unrecognized bitmap format
*/
#ifdef _WIN32
int GetBitmapImageSize (LPBITMAPINFO lpbi)
#else
int GetBitmapImageSize (HXBitmapInfo* lpbi)
#endif
{
/* check bitmap pointer & format: */
register int cid = GetBitmapColor (lpbi);
if (cid == CID_UNKNOWN ||
!(ciddTbl[cid].dwFlags & _BITMAP) ||
lpbi->bmiHeader.biWidth <= 0 ||
lpbi->bmiHeader.biHeight <= 0 ||
lpbi->bmiHeader.biPlanes != 1)
return 0;
return ImageSize (cid, lpbi->bmiHeader.biWidth, lpbi->bmiHeader.biHeight);
}
/*
* Build a bitmap structure.
* Use:
* int MakeBitmap (LPBITMAPINFO lpbi, int nBISize,
* int cid, ULONG32 dwWidth, ULONG32 dwHeight, LPPALETTEENTRY lppe, int nColors);
* Input:
* lpbi - pointer to BITMAPINFO structure to contain image format
* nBISize - size of memory block allocated for BITMAPINFO structure
* cid - color format to use for bitmap
* dwWidth, dwHeight - image width/height
* lppe, nColors - palette info
* Returns:
* !0 - bitmap has been successfully created
* 0 - invalid bitmap parameters
*/
#ifdef _WIN32
int MakeBitmap (LPBITMAPINFO lpbi, int nBISize,
int cid, ULONG32 dwWidth, ULONG32 dwHeight, LPPALETTEENTRY lppe, int nColors)
#else
int MakeBitmap(HXBitmapInfo* lpbi, int nBISize, int cid, ULONG32 dwWidth,
ULONG32 dwHeight, void* lppe, int nColors)
#endif
{
const struct _greg* lpcidd;
int setPalette, bitmapinfoSize;
/* check input parameters: */
if (lpbi == NULL ||
cid < 0 || cid > NFORMATS || !(ciddTbl[cid].dwFlags & _BITMAP) ||
(lpcidd = ciddTbl[cid].lpBitmapCIDD) == NULL ||
(int)dwWidth <= 0 || (int)dwHeight <= 0)
return 0;
/* calculate bitmapinfo size: */
setPalette = 0;
#ifdef _WIN32
bitmapinfoSize = sizeof(BITMAPINFOHEADER);
#else
bitmapinfoSize = sizeof(HXBitmapInfoHeader);
#endif
if (lpcidd->dwFlags & _BITMASK)
bitmapinfoSize += 3 * sizeof(ULONG32);
else
if ((lpcidd->dwFlags & (_FOURCC|_BITCOUNT)) == (_FOURCC|_BITCOUNT) &&
lpcidd->dwFourCC == BI_RGB && lpcidd->dwBitCount <= 8 &&
nColors) {
/* check palette parameters: */
if (lppe == NULL || nColors < 0 || nColors > 256)
return 0;
#ifdef _WIN32
bitmapinfoSize += nColors * sizeof(PALETTEENTRY);
setPalette = 1;
#endif
}
/* check if we have sufficient amount of memory: */
if (nBISize < bitmapinfoSize)
return 0;
/* initialize bitmapinfo structure: */
memset((void *)lpbi, 0, bitmapinfoSize);
#ifdef _WIN32
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
#else
lpbi->bmiHeader.biSize = sizeof(HXBitmapInfoHeader);
#endif
/* set image parameters: */
lpbi->bmiHeader.biWidth = dwWidth;
lpbi->bmiHeader.biHeight = dwHeight;
lpbi->bmiHeader.biPlanes = 1;
lpbi->bmiHeader.biSizeImage = ImageSize (cid, dwWidth, dwHeight);
/* set color format: */
SetBitmapColor (lpbi, cid);
#ifdef _WIN32
/* set palette: */
if (setPalette)
SetBitmapPalette (lpbi, lppe, nColors);
#endif
return bitmapinfoSize; /* the number of bytes written */
}
#ifdef _WIN32
#ifndef WINCE
int SetDirectDrawColor (LPDDPIXELFORMAT lpddpf, int cid)
{
LPCIDD lpcidd;
/* check input parameters: */
if (lpddpf == NULL ||
lpddpf->dwSize < sizeof(DDPIXELFORMAT) ||
cid < 0 || cid > NFORMATS || !(ciddTbl[cid].dwFlags & _DIRECTDRAW) ||
(lpcidd = ciddTbl[cid].lpDirectDrawCIDD) == NULL)
return -1;
/* set color-format releated fields in DDPIXELFORMAT: */
lpddpf->dwFourCC = lpcidd->dwFourCC;
lpddpf->dwRGBBitCount = lpcidd->dwBitCount;
lpddpf->dwRBitMask = lpcidd->dwBitMask[0];
lpddpf->dwGBitMask = lpcidd->dwBitMask[1];
lpddpf->dwBBitMask = lpcidd->dwBitMask[2];
/* set control flags: */
lpddpf->dwFlags = (lpddpf->dwFourCC == BI_RGB)? DDPF_RGB: DDPF_FOURCC;
/* success: */
return 0;
}
#endif
/*
* Get bitmap palette.
* Use:
* int GetBitmapPalette (LPBITMAPINFO lpbi, LPPALETTEENTRY lppe);
* Input:
* lpbi - pointer to BITMAPINFO structure
* lppe - pointer to a buffer to contain palette entries
* Returns:
* the number of colors in palette
* 0, if bitmap does not use palette.
*/
int GetBitmapPalette (LPBITMAPINFO lpbi, LPPALETTEENTRY lppe)
{
int i, n = 0;
/* check input parameters: */
if (lpbi != NULL && lppe != NULL &&
lpbi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) &&
lpbi->bmiHeader.biCompression == BI_RGB &&
lpbi->bmiHeader.biBitCount <= 8) {
/* get pointer to palette entries: */
RGBQUAD *prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->bmiHeader.biSize);
/* get number of entries to process: */
n = lpbi->bmiHeader.biClrUsed;
if (!n) n = 1U << lpbi->bmiHeader.biBitCount; /* !!! */
/* a DIB color table has its colors stored BGR not RGB
* so flip them around: */
for (i = 0; i < n; i ++) {
lppe[i].peRed = prgb[i].rgbRed;
lppe[i].peGreen = prgb[i].rgbGreen;
lppe[i].peBlue = prgb[i].rgbBlue;
lppe[i].peFlags = 0;
}
}
/* return # of colors extracted */
return n;
}
/*
* Set bitmap palette.
* Use:
* int SetBitmapPalette (LPBITMAPINFO lpbi, LPPALETTEENTRY lppe, int n);
* Input:
* lpbi - pointer to BITMAPINFO structure
* lppe - pointer to a buffer containing palette entries
* n - the total number of colors in palette
* Returns:
* the number of colors set
* 0, if bitmap does not use palette.
*/
int SetBitmapPalette (LPBITMAPINFO lpbi, LPPALETTEENTRY lppe, int nColors)
{
int i, m, n = 0;
/* check input parameters: */
if (lpbi != NULL && lppe != NULL && nColors > 0 && nColors <= 256 &&
lpbi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) &&
lpbi->bmiHeader.biCompression == BI_RGB &&
lpbi->bmiHeader.biBitCount <= 8) {
/* get pointer to palette entries: */
RGBQUAD *prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->bmiHeader.biSize);
/* check the number of entries to copy: */
m = 1U << lpbi->bmiHeader.biBitCount; /* !!! */
n = nColors; if (n > m) n = m;
/* a DIB color table has its colors stored BGR not RGB
* so flip them around: */
for (i = 0; i < n; i ++) {
prgb[i].rgbRed = lppe[i].peRed;
prgb[i].rgbGreen = lppe[i].peGreen;
prgb[i].rgbBlue = lppe[i].peBlue;
prgb[i].rgbReserved = 0;
}
/* set number of palette entries copied: */
if (i == m) i = 0; /* !!! */
lpbi->bmiHeader.biClrUsed = i;
lpbi->bmiHeader.biClrImportant = i;
}
/* return # of colors set */
return n;
}
/*
* Check the validity of a bitmap structure.
* Use:
* int CheckBitmap (LPBITMAPINFO lpbi);
* Input:
* lpbi - pointer to BITMAPINFO structure to check
* Returns:
* !0 - bitmap parameters are correct
* 0 - otherwise
*/
int CheckBitmap (LPBITMAPINFO lpbi)
{
/* check bitmap pointer & format: */
register int cid = GetBitmapColor (lpbi);
if (cid == CID_UNKNOWN ||
!(ciddTbl[cid].dwFlags & _BITMAP) ||
lpbi->bmiHeader.biWidth <= 0 ||
lpbi->bmiHeader.biHeight <= 0 ||
lpbi->bmiHeader.biPlanes != 1 ||
lpbi->bmiHeader.biSizeImage != ImageSize (cid, lpbi->bmiHeader.biWidth, lpbi->bmiHeader.biHeight))
return 0;
/* check if image should contain a palette: */
if (lpbi->bmiHeader.biCompression == BI_RGB &&
lpbi->bmiHeader.biBitCount <= 8) {
/* check ## of palette entries: */
unsigned int m = 1U << lpbi->bmiHeader.biBitCount;
if (lpbi->bmiHeader.biClrUsed > m ||
lpbi->bmiHeader.biClrImportant > lpbi->bmiHeader.biClrUsed)
return 0;
} else /* no palette: */
if (lpbi->bmiHeader.biClrUsed != 0 ||
lpbi->bmiHeader.biClrImportant != 0)
return 0;
/* all tests passed... */
return 1;
}
#endif //_WIN32
/* colormap.c -- end of file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -