如下代码展示如何生成一个像素占4bit的图片。
CImage oFourBit;
const RGBQUAD tab[16] = {
{0,0,0,0}, {36,36,36,0}, {72,72,72,0}, {108,108,108,0},
{144,144,144,0}, {180,180,180,0}, {216,216,216,0}, {255, 255, 255, 0},
{0,0,0,0}, {36,36,36,0}, {72,72,72,0}, {108,108,108,0},
{144,144,144,0}, {180,180,180,0}, {216,216,216,0}, {255, 255, 255, 0}
};
int width = 1600;int height = 1600;
oFourBit.Create(1600, 1600, 4); // Create and attach a bitmap
oFourBit.SetColorTable(0, 16, tab);
byte *pImg = (byte *)oFourBit.GetBits();
int step = oFourBit.GetPitch();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if(i < 200)
{
*(pImg + i*step + j) = 0;
}
else if( i > 200 && i < 400)
{
*(pImg + i*step + j) = 1;
}
else if( i > 400 && i < 600)
{
*(pImg + i*step + j) = 2;
}
else if( i > 600 && i < 800)
{
*(pImg + i*step + j) = 3;
}
else if( i > 800 && i < 1000)
{
*(pImg + i*step + j) = 4;
}
else if( i > 1000 && i < 1200)
{
*(pImg + i*step + j) = 5;
}
else if( i > 1200 && i < 1400)
{
*(pImg + i*step + j) = 6;
}
else
*(pImg + i*step + j) = 7;
}
}
oFourBit.Save(_T("c.bmp"),Gdiplus::ImageFormatBMP);
像素的值跟平时处理图片的灰度值有所不同,他的值是代表颜色标中的索引,解析的时候调色盘需要一致。