yiyou5318
yiyou5318
2017-11-29 01:38

二维数组数据ucValue[100][100],想存储为灰度图。(但是该图像存储后为彩色的)

  • c++
  • 图像处理
  • 位图
  • 存储

二维数组数据ucValue[100][100],存储为灰度图。(但是用我代码存储后该图像为彩色的)
代码如下:
width_p = 100;
height_p = 100;
bitCount = 8;
//存储为bitmap
FILE* f = fopen(save_fileName, "wb");//create or open file to be written
if (f == NULL) return false;
memset(&fileHeader, 0, sizeof(BITMAPFILEHEADER));
memset(&infoHeader, 0, sizeof(BITMAPINFOHEADER));
int colorTableSize = 0;//if bitcount is 24, there is no color table.
if (bitCount == 8)//if bitcount is 8 ,the size of color table is 256*4,4B is the size of RGBQUAD.
colorTableSize = sizeof(RGBQUAD)* 256;

int headerSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTableSize;//the size of the header of bmp file.
int lineSize = (width_p*bitCount / 8 + 3) / 4 * 4;//the size of each line in bmp file.
int dataSize = lineSize*height_p;//the size of the image data of bmp file.

fileHeader.bfType = (WORD)0x4D42;//set the attribute of BITMAPFILEHEADER
fileHeader.bfSize = (DWORD)(headerSize + dataSize);
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = (DWORD)headerSize;

infoHeader.biSize = sizeof(BITMAPINFOHEADER);//set the attribute of BITMAPINFOHEADER
infoHeader.biClrUsed = 0;
infoHeader.biWidth = width_p;
infoHeader.biHeight = height_p;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = bitCount;
infoHeader.biCompression = BI_RGB;
infoHeader.biSizeImage = dataSize;
infoHeader.biClrImportant = 0;
infoHeader.biXPelsPerMeter = 0;
infoHeader.biYPelsPerMeter = 0;

fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f);//write the data of BITFILEHEADER to bmp file
fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f);//write the data of BITINFOHEADER to bmp file
colorTable = new RGBQUAD[256];
int i;
for (i = 0; i<256; i++)
{
colorTable[i].rgbBlue = (BYTE)i;
colorTable[i].rgbGreen = (BYTE)i;
colorTable[i].rgbRed = (BYTE)i;
colorTable[i].rgbReserved = 0;
}
fwrite(&colorTable, sizeof(RGBQUAD), 256, f);
fwrite(radarData.ucValue, 1, dataSize, f);//write the image data to bmp file

fclose(f);//data writting is finished,close the bmp file.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换