/**
*2014/9/24 02:23
*读取bmp图片信息.cpp
*author:王炳华
*运行此文件,在同目录下存放一个名为dog.bmp的图片,格式必须为bmp
*/
#include<stdio.h>
#include<malloc.h>
#define BMPTYPE 19778
bool isBMP(FILE *F);
int multiply_2(int i);
void HeaderPartLenght(FILE *f);
void widthHeight(FILE *f);
void fileHeader(FILE* f);
void infoHeader(FILE* f);
void dataPart(FILE *f);
void colorData(FILE *f);
void dataData(FILE *f);
short biBitCount = 0;
int clrTable = 0;//需要多少个颜色表项
long width = 0;
long height = 0;
int main()
{
FILE *f;
f = fopen("dog.bmp","rb");
if(f == NULL)
{
fprintf(stderr,"Open File Error!");
}
else
{
if(isBMP(f))
{
printf("************Image MainInfo***********\n");
HeaderPartLenght(f);
widthHeight(f);
fileHeader(f);
infoHeader(f);
colorData(f);
dataPart(f);
}
else
{
printf("The type of the image is not BMP!");
}
}
return 0;
}
int multiply_2(int i)
{
int n;
int result = 1;
for(n = 0;n < i;n++)
{
result *= 2;
}
return result;
}
bool isBMP(FILE *f)
{
unsigned short type;
fread(&type,sizeof(char),2,f);
return (type==BMPTYPE)?1:0;
}
void HeaderPartLenght(FILE *f)
{
unsigned int length;
fseek(f,10L,SEEK_SET);
fread(&length,sizeof(char),4,f);
printf("The length of head part is %d\n",length);
}
void widthHeight(FILE *f)
{
long width = 0;
long height = 0;
fseek(f,18,SEEK_SET);
fread(&width,sizeof(char),4,f);
fread(&height,sizeof(char),4,f);
printf("The width of this image is %d\n",width);
printf("The height of this image is %d\n",height);
}
void fileHeader(FILE* f)
{
int biSize = 0;
short bifReserved1 = 0;
short bifReserved2 = 0;
int bifOffBits = 0;
printf("*************************************\n");
printf("**************FILE Header************\n");
printf("*************************************\n");
fseek(f,2,SEEK_SET);
fread(&biSize,sizeof(char),4,f);
fread(&bifReserved1,sizeof(char),2,f);
fread(&bifReserved2,sizeof(char),2,f);
fread(&bifOffBits,sizeof(char),4,f);
printf("biSize=%d\n",biSize);
printf("bifReserved1=%d\n",bifReserved1);
printf("bifReserved2=%d\n",bifReserved2);
printf("bifOffBits=%d\n",bifOffBits);
}
void infoHeader(FILE* f)
{
unsigned int bisize = 0;
short biplanes = 0;
unsigned int biCompression = 0;
unsigned int bixpelsPerMeter = 0;
unsigned int biypelsPerMeter = 0;
unsigned int biClrUsed = 0;
unsigned int biClrImportant = 0;
fseek(f,14,SEEK_SET);
fread(&bisize,sizeof(char),4,f);
fread(&width,sizeof(char),4,f);
fread(&height,sizeof(char),4,f);
fread(&biplanes,sizeof(char),2,f);
fread(&biBitCount,sizeof(char),2,f);
fread(&biCompression,sizeof(char),4,f);
fread(&bixpelsPerMeter,sizeof(char),4,f);
fread(&biypelsPerMeter,sizeof(char),4,f);
fread(&biClrUsed,sizeof(char),4,f);
fread(&biClrImportant,sizeof(char),4,f);
printf("*************************************\n");
printf("**************FILE Header************\n");
printf("*************************************\n");
printf("bisize=%d\n",bisize);
printf("biwidth=%d\n",width);
printf("biheigth=%d\n",height);
printf("biBitCount=%d\n",biBitCount);
printf("biCompression=%d\n",biCompression);
printf("bixpelsPerMeter=%d\n",bixpelsPerMeter);
printf("biypelsPerMeter=%d\n",biypelsPerMeter);
printf("biClrUsed=%d\n",biClrUsed);
printf("biClrImportant=%d\n",biClrImportant);
}
void colorData(FILE *f)
{
//计算需要的表项数目 一个表需要4个字节
int i,j;
clrTable = (biBitCount==24)?0:multiply_2(biBitCount);
int *tables = (int *)malloc(sizeof(char)*clrTable*4);
printf("*************************************\n");
printf("**************Color Info*************\n");
printf("*************************************\n");
printf("clrTable=%d\n",clrTable);
for(i = 0,j = 0;i < clrTable;i++,j+=4)
{
fseek(f,54+j,SEEK_SET);
fread(&tables[i],sizeof(char),4,f);
printf("tables[%d]=%d\n",i,tables[i]);
}
}
void dataPart(FILE *f)
{
fseek(f,54+clrTable*4,SEEK_SET);
int i;
int m;
int n;
//每一个行的字节数
int byteHeight = (width*biBitCount/8+3)/4*4;
int lineBytes = width;
//将所有的像素存到字符串中
unsigned char *count = (unsigned char *)malloc(sizeof(char)*byteHeight*lineBytes);
//windows扫描 图像时 从下到上 从左到右
fread(count,sizeof(char),lineBytes*byteHeight,f);
//字符数组来记录每一个像素
unsigned char **ch = (unsigned char **)malloc(sizeof(unsigned char *)*byteHeight);
for(i = 0;i < byteHeight;i++)
{
*(ch+i) = (unsigned char *)malloc(sizeof(unsigned char *)*lineBytes);
}
//将一个像素点写入到文件中
FILE *w = fopen("1.txt","w+");
for(m = 0;m < byteHeight;m++)
{
for(n = 0;n < lineBytes;n++)
{
ch[m][n] = count[(byteHeight-1-m)*(lineBytes)+n];
fprintf(w,"%-5d",ch[m][n]);
if((n+1)%8 == 0)
{
fprintf(w,"\n");
}
}
}
printf("*************************************\n");
printf("**************Data Area**************\n");
printf("*************************************\n");
printf("The data is written into the file successfully!\n");
free(count);
free(ch);
fclose(f);
fclose(w);
}
使用C语言读取bmp文件数据
最新推荐文章于 2023-10-13 15:12:45 发布