typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
如何调用者windows.h中的结构体
操作与图像相关的动作,使用Windows Image Component(WIC)功能强大,方便,不仅是bmp,jpg,tiff,icon等轻松进行解码,颜色数据操作,重新编码,转码,像素格式转换等等
biBitCount:
The number of bits-per-pixel. The biBitCount member of the BITMAPINFOHEADER structure determines the number of bits that define each pixel and the maximum number of colors in the bitmap. This member must be one of the following values.
.....
8: The bitmap has a maximum of 256 colors, and the bmiColors member of BITMAPINFO contains up to 256 entries. In this case, each byte in the array represents a single pixel.
.....
以上来自msdn:BITMAPINFOHEADER structure
因此判断biBitCount是不是8即可
tagBITMAPINFOHEADER{
DWORD biSize; // 本结构所占用字节数
LONGbiWidth; // 位图的宽度,以像素为单位
LONGbiHeight; // 位图的高度,以像素为单位
WORD biPlanes; // 目标设备的级别,必须为1
WORD biBitCount// 每个像素所需的位数,必须是1(双色), 4(16色),8(256色)或24(真彩色)之一
那么怎么动态分配图片的二维数组的动态内存呢?
#include
#include
typedef struct{
BYTE b;
BYTE g;
BYTE r;
}RGB;
int main(void)
{
int i=0;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
FILE*pfin=fopen("3.bmp","rb");
FILE*pfout=fopen("4.bmp","wb");
//ReadtheBitmapfileheader;
fread(&fileHeader,sizeof(BITMAPFILEHEADER),1,pfin);
//ReadtheBitmapinfoheader;
fread(&infoHeader,sizeof(BITMAPINFOHEADER),1,pfin);
//为简化代码,只处理24位彩色
if(infoHeader.biBitCount == 24)
{
printf("位图的宽度为%d\n", infoHeader.biWidth);
printf("位图的高度为%d\n", infoHeader.biHeight);
int size=infoHeader.biWidth*infoHeader.biHeight;
RGB img[infoHeader.biHeight][infoHeader.biWidth] = (RGB*)malloc(sizeof*sizeof(*RGB));
fread(img,sizeof(RGB),size,pfin);
//把第50行染成黑色
for(i= 0; i < infoHeader.biWidth;i++)
{
img[50][i].b=img[50][i].g=img[50][i].r=0;
}
//将修改后的图片保存到文件
fwrite(&fileHeader,sizeof(fileHeader),1,pfout);
fwrite(&infoHeader,sizeof(infoHeader),1,pfout);
fwrite(img,sizeof(RGB),size,pfout);
}
fclose(pfin);
fclose(pfout);
}