定义几个辅助的宏,如下。
// 拆分合并BYTE
#define HIBITS(w) ((BYTE)((((BYTE)(w)) >> 4) & 0xf))
#define LOBITS(w) ((BYTE)(((BYTE)(w)) & 0xf))
#define MAKEBITS(a, b) ((BYTE)(((BYTE)(((BYTE)(a)) & 0xf)) | ((BYTE)((BYTE)(((BYTE)(b)) & 0xf))) << 4))
// 读写16色图像的演示操作,将每个像素的值加上20
BOOL CImgProcess::Test4bppIndexedBitmap(CImgProcess *pTo)
{// 首先检查图像是否是8位灰度图像if (m_pBMIH->biBitCount != 4) {AfxMessageBox(_T("fuck, 这不是4位的图像"));return FALSE;}// 小小测试一下BYTE grey = 0x4c; // 假设这个字节是16级灰度图像的两个像素BYTE hi = HIBITS(grey), // 0xflo = LOBITS(grey); // 0xcBYTE test = MAKEBITS(0x34, 0x56); // 把两个字节的地位部分组成一个新的字节,所以这里应该输出 0x64TRACE3("high=0x%x,low=0x%x, test=0x%x \n", hi, lo, test);//BYTE a =(lo + 20)%0xf;BYTE b = (hi + 20)%0xf;test = MAKEBITS(a, b);TRACE3("a=0x%x,b=0x%x, test add = 0x%d \n", a, b, test);//working................int nHeight = GetHeight(),nWidth = GetWidthPixel(); // 像素宽度TRACE2("width=%d,height=%d\n", nWidth, nHeight);int i,j;for(i=0; i<nHeight; i++){for(j=0; j < nWidth/2; j++) // nWidth是像素的个数,但是只有nWidth/2个字节{BYTE pixel2 = this->m_lpData[i][j]; //包含两个像素的 hi = HIBITS(pixel2); // 像素分解lo = LOBITS(pixel2);a =(lo + 20) % 0xf;b = (hi + 20) % 0xf;test = MAKEBITS(a, b); // 重新合并像素pTo->m_lpData[i][j] = test; // 写入数据}}return TRUE;
}