
代码及说明
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#pragma pack(1)
typedef struct BitmapFileHeader
{
unsigned char bfType[2];
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
}fileHeader;
typedef struct BitmapInfoHeader
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPixPerMeter;
long biYPixPerMeter;
unsigned long biClrUsed;
unsigned long biClrImporant;
}fileInfo;
int main(void)
{
FILE *fpaa;
if ((fpaa = fopen("aa.bmp", "rb") )== NULL)
{
printf("打开文件失败");
exit(0);
}
printf("fpaa偏移量: %d\n", ftell(fpaa));
fileHeader *fh;
printf("fileHeader结构体大小: %d字节\n", sizeof(fileHeader));
fh= (fileHeader *)malloc(sizeof(fileHeader));
fread(fh, sizeof(fileHeader), 1, fpaa);
printf("头文件格式: %c%cP\n", fh->bfType[0], fh->bfType[1]);
printf("头文件大小: %d字节\n", fh->bfSize);
printf("DIB数据在文件中的偏移量: %d字节\n", fh->bfOffBits);
printf("fpaa读取头文件后偏移量: %d\n",ftell(fpaa));
fileInfo * fi;
fi = (fileInfo *)malloc(sizeof(fileInfo));
fread(fi, sizeof(fileInfo), 1, fpaa);
printf("fpaa读取信息文件后偏移量: %d\n", ftell(fpaa));
printf("位图信息大小: %d\n",fi->biSize);
printf("文件宽度: %d\n", fi->biWidth);
printf("文件高度: %d\n", fi->biHeight);
printf("平面数: %d\n", fi->biPlanes);
printf("颜色位数: %d\n", fi->biBitCount);
printf("压缩类型: %d\n", fi->biCompression);
printf("DIB数据区大小: %d\n", fi->biCompression);
printf("颜色索引表: %d\n", fi->biClrUsed);
printf("重要颜色: %d\n", fi->biClrImporant);
printf("\n");
struct color
{
char r;
char g;
char b;
};
struct color *fc;
fc = (struct color *)malloc(sizeof(struct color));
for (int i = 1; i <=fi->biHeight; i ++)
{
fseek(fpaa, -((((fi->biBitCount * fi->biWidth) + 31) >> 5) << 2)*i, SEEK_END);
for (int j = 1; j <= fi->biWidth; j ++ )
{
fread(fc, sizeof(struct color), 1, fpaa);
if (fc->r == 0 & fc->g == 0 & fc->b == 0) printf("%c ",3);
else printf("%c ", 46);
}
printf("\n");
int k = ((((fi->biBitCount * fi->biWidth) + 31) >> 5) << 2) - ((fi->biBitCount * fi->biWidth) >> 3);
fseek(fpaa, k, SEEK_CUR);
}
/\*int m=10;
for (int l=1;l<100;l++)
{
printf("%d:%c ", l,l);
if (m==0)
{
m = 10;
printf("\n");
}
m--;
}
\*/
getchar();
**收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。**
,让我们一起学习成长!**