#include <string.h>
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<malloc.h>
#pragma pack (1)
#define
WIDTHBYTES(bits) (((bits)/8+3)/4*4)
/* bitmap file header */
typedef struct
{
unsigned short
type;
unsigned long fileSize;
unsigned short reserved1;
unsigned short reserved2;
unsigned long offbits;
} BITMAPFILEHEADER;
/* bitmap info header */
typedef struct
{
unsigned long dwSize;
unsigned long biwidth;
unsigned long biheight;
unsigned short planes;
unsigned short
bpp;
unsigned long compression;
unsigned long sizeImage;
unsigned long hResolution;
unsigned long vResolution;
unsigned long colors;
unsigned long importantColors;
} BITMAPINFOHEADER;
typedef struct
{
unsigned char rgbblue;
unsigned char rgbgreen;
unsigned char rgbred;
unsigned char rgbreserved;
}RGBQUAD;
/*struct bit
{
unsigned a:1;
};*/
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
RGBQUAD
rgbquad[2];
int width;
int height;
int bpp;
int *pColorData;
int *pColorData1;
void
readbmp()
{
FILE* fp;
// char fou[255];
int i,j,t=0;
// printf("resultfile name:");
//scanf("%s",fou);
fp=fopen("bw22.bmp","rb");
//打开图像文件
if(fp==NULL)
{
printf("open file error!\n");
exit(0);
//?
}
fseek(fp,0,0);
fread(&fileHeader,sizeof(fileHeader),1,fp);
fseek(fp,sizeof(fileHeader),0);
fread(&infoHeader,sizeof(infoHeader),1,fp);
printf("%d \n
到数据区的偏移量%d \n",fileHeader.type,fileHeader.offbits);
printf("信息头长%d \n 宽%d \n 高%d \n %d\n 位数%d \n %d\n 数据区大小%d \n %d \n ",infoHeader.dwSize,infoHeader.biwidth,infoHeader.biheight,infoHeader.planes,infoHeader.bpp, ,infoHeader.sizeImage,infoHeader.colors);
fseek(fp,sizeof(fileHeader)+sizeof(infoHeader),0);
fread(&rgbquad[0],sizeof(RGBQUAD),1,fp);
printf("索引号0:B-%d G-%d R-%d \n",rgbquad[0].rgbblue,rgbquad[0].rgbgreen,rgbquad[0].rgbred);
fread(&rgbquad[1],sizeof(RGBQUAD),1,fp);
printf("索引号1:B-%d G-%d R-%d \n",rgbquad[1].rgbblue,rgbquad[1].rgbgreen,rgbquad[1].rgbred);
bpp=infoHeader.bpp;
width = infoHeader.biwidth;
height = infoHeader.biheight;
//分配内存空间把源图存入内存
int l_width = WIDTHBYTES(width* infoHeader.bpp);//计算位图的实际宽度并确保它为32的倍数
printf("实际每行字节数%d \n ",l_width);
pColorData=(int *)malloc(height*l_width);
pColorData1=(int*)malloc(height*l_width);
memset(pColorData,0,height*l_width);
long nData = height*l_width;
unsigned char maxindex=0;
//把位图数据信息读到数组里
fseek(fp,fileHeader.offbits,0);
fread(pColorData,1,nData,fp);
printf("像素数据信息:\n");
if (infoHeader.bpp==1)
{
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
maxindex=*(pColorData+i*l_width+j);
t+=maxindex;
printf("%d ",maxindex);
*(pColorData1+i*l_width+j)=maxindex;
}
printf("\n");
}
t=t/(height*width);
printf("\n平均%d\n",t);
}
fclose(fp);
}
void savebmp(int *pColorData,int width,int height,int bpp)
{
FILE *wf;
wf=fopen("bw2重构.BMP","wb+");
//打开图像文件
int colorTablesize=0;
if(bpp==1)
colorTablesize=8;
int l_width = WIDTHBYTES(width* bpp);
fileHeader.type = 0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHeader.fileSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ colorTablesize + height*l_width;
fileHeader.reserved1 = 0;
fileHeader.reserved2 = 0;
fileHeader.offbits=54+colorTablesize;
fwrite(&fileHeader,sizeof(BITMAPFILEHEADER),1,wf);
infoHeader.bpp=bpp;
infoHeader.importantColors=0;
infoHeader.colors=0;
infoHeader.biheight=height;
infoHeader.planes=1;
infoHeader.dwSize=40;
infoHeader.sizeImage=l_width*height;
infoHeader.biwidth=width;
infoHeader.hResolution=0;
infoHeader.vResolution=0;
fwrite(&infoHeader,sizeof(BITMAPINFOHEADER),1,wf);
fwrite(rgbquad,sizeof(RGBQUAD),2,wf);
fwrite(pColorData,height*l_width,1,wf);
fclose(wf);
}
void main()
{
//readpath="bw2.BMP";
readbmp();
savebmp(pColorData1,width,height,bpp);
free(pColorData);
free(pColorData1);
}