当前位置:文档之家› bmp图像处理

bmp图像处理

#include
#include
#include

#define FILEPATH "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片\\Water lilies1.bmp"

/***************fwriteBMP*****************/
#define MAXWIDTH 2048
#define MAXHEIGHT 2048

typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} color;

typedef struct {
long height;
long width;
color data[MAXHEIGHT][MAXWIDTH];
} __img;

/*****************WriteBmp*************/
#define HEADERSIZE 54
#define MAXWIDTH 2048
#define MAXHEIGHT 2048
#define PALLETSIZE 1024

unsigned short Bmp_color;
unsigned int Bmp_header_size;
unsigned int Bmp_info_header_size;
unsigned short Bmp_planes;
long Bmp_xppm;
long Bmp_yppm;
long Bmp_image_size;
unsigned long Bmp_size;
unsigned char Bmp_headbuf[HEADERSIZE];
unsigned char Bmp_Pallet[PALLETSIZE];
long Bmp_height;
long Bmp_width;



typedef struct BMPFileInfo{
unsigned long Size;
unsigned long Width;
unsigned long Height;
unsigned long Level;
unsigned long ColorDepth;
unsigned long Compress;
unsigned long PixelBytes;
unsigned long horizon;
unsigned long vertical;
unsigned long offset1;
unsigned long offset2;
}BMPFileInfo;

typedef struct BMPFileHeader{
unsigned char B;
unsigned char M;
unsigned long FileSize;
unsigned long Reserved1;
unsigned long HeaderLength;
}BMPFileHeader;

BMPFileHeader MyHeader;

void PrintBMPHeader()
{
FILE *mybmp;
mybmp=fopen(FILEPATH,"r");

fread(&MyHeader.B,1,1,mybmp);
fread(&MyHeader.M,1,1,mybmp);
fread(&MyHeader.FileSize,1,4,mybmp);
fread(&MyHeader.Reserved1,1,4,mybmp);
fread(&MyHeader.HeaderLength,1,4,mybmp);

printf("FileType:\t\t%c %c P\n",MyHeader.B,MyHeader.M);
printf("FileSize:\t\t%ld bytes\n",MyHeader.FileSize);
printf("HeaderLength:\t\t%ld \n",MyHeader.HeaderLength);

fclose(mybmp);
}

BMPFileInfo MyInfo;

void PrintBMPInfo()
{
FILE *mybmp;
mybmp=fopen(FILEPATH,"r");

fseek(mybmp,14,0);
fread(&MyInfo.Size,1,4,mybmp);
fread(&MyInfo.Width,1,4,mybmp);
fread(&MyInfo.Height,1,4,mybmp);
fread(&MyInfo.Level,1,2,mybmp);
fread(&MyInfo.ColorDepth,1,2,mybmp);
fread(&https://www.doczj.com/doc/4414116447.html,press,1,4,mybmp);
fread(&MyInfo.PixelBytes,1,4,mybmp);
fread(&MyInfo.horizon,1,4,mybmp);
fread(&MyInfo.vertical,1,4,mybmp);
fread(&MyInfo.offset1,1,4,mybmp);
fread(&MyInfo.offset2,1,4,mybmp);

printf("Size:\t\t\t%ld\n",MyInfo.Size);
printf("Width:\t\t\t%ld\n",MyInfo.Width);
printf("Height:\t\t\t%ld\n",MyInfo.Height);
printf("Height*Width:\t\t%ld\n",MyInfo.Height*MyInfo.Width*3);
printf("Level:\t\t\t%ld\n",MyInfo.Level);
printf("ColorDepth:\t\t%ld\n",MyInfo.ColorDepth);
printf("PixelBytes:\t\t%ld\n",MyInfo.PixelBytes);

fclose(mybmp);
}

void ReadBidata(unsigned char*img)//read data part of bmp file
{
unsigned int i;
unsigned int data_size;

data_size=MyHeader.FileSize-54;


FILE *mybmp;
mybmp=fopen(FILEPATH,"rb");
fseek(mybmp,54,0);

for(i=0;iimg[i]=fgetc(mybmp);

fclose(mybmp);
}

void ReadBidata02(unsigned char* img,unsigned char* img2) // delete zeros
{
unsigned int i,_i=0;
unsigned _img_w;

_img_w=MyInfo.Width*3;
if((_img_w%4)!=0)_img_w=((MyInfo.Width*3)/4+1)*4;

for(i=0;i<(MyHeader.FileSize-54)/_img_w*_img_w;i++){
if(i%_img_wimg2[_i]=img[i];
_i++;
}
}
}


void SaveBipFile(unsigned char* img2)//save raw image as a raw
{
unsigned int i,data_size;
FILE *f=fopen("a.raw","wb");

data_size=MyInfo.Height*MyInfo.Width*3;
for(i=0;ifputc(img2[i],f); // b,g,r;

fclose(f);
}

void _Invert_img2(unsigned char* img)//ration img2
{
unsigned char temp;
unsigned int i,j,x,y;
x=MyInfo.Width*3;
y=MyInfo.Height;

for(j=0;jfor(i=0;itemp=img[i+x*j];
img[i+x*j]=img[i+x*(y-j-1)];
img[i+x*(y-j-1)]=temp;
}
}
}




void WriteBmp(char *filename, __img *tp)//write bmp
{
int i,j;
int Real_width;
FILE *Out_Fp = fopen(filename,"wb");
unsigned char *Bmp_Data;

if(Out_Fp==NULL){
fprintf(stderr,"Error: file %s couldn\'t open for write!\n",filename);
exit(1);
}

Bmp_color=24;
Bmp_header_size=HEADERSIZE;
Bmp_info_header_size=40;
Bmp_planes=1;

Real_width = tp->width*3 + tp->width%4;

if((Bmp_Data = (unsigned char *)calloc(Real_width,sizeof(unsigned char)))==NULL) {
fprintf(stderr,"Error: Memory allocation failed for Bmp_Data!\n");
exit(1);
}

Bmp_xppm=Bmp_yppm=0;
Bmp_image_size = tp->height*Real_width;
Bmp_size = Bmp_image_size + HEADERSIZE;
Bmp_headbuf[0]='B'; Bmp_headbuf[1]='M';
memcpy(Bmp_headbuf+2,&Bmp_size,sizeof(Bmp_size));
Bmp_headbuf[6]=Bmp_headbuf[7]=Bmp_headbuf[8]=Bmp_headbuf[9]=0;
memcpy(Bmp_headbuf+10,&Bmp_header_size,sizeof(Bmp_header_size));
Bmp_headbuf[11]=Bmp_headbuf[12]=Bmp_headbuf[13]=0;
memcpy(Bmp_headbuf+14,&Bmp_info_header_size,sizeof(Bmp_info_header_size));
Bmp_headbuf[15]=Bmp_headbuf[16]=Bmp_headbuf[17]=0;
memcpy(Bmp_headbuf+18,&tp->width,sizeof(Bmp_width));
memcpy(Bmp_headbuf+22,&tp->height,sizeof(Bmp_height));
memcpy(Bmp_headbuf+26,&Bmp_planes,sizeof(Bmp_planes));
memcpy(Bmp_headbuf+28,&Bmp_color,sizeof(Bmp_color));
memcpy(Bmp_headbuf+34,&Bmp_image_size,sizeof(Bmp_image_size));
memcpy(Bmp_headbuf+38,&Bmp_xppm,sizeof(Bmp_xppm));
memcpy(Bmp_headbuf+42,&Bmp_yppm,sizeof(Bmp_yppm));
Bmp_headbuf[46]=Bmp_headbuf[47]=Bmp_headbuf[48]=Bmp_headbuf[49]=0;
Bmp_headbuf[50]=Bmp_headbuf[51]=Bmp_headbuf[52]=Bmp_headbuf[53]=0;

fwrite(Bmp_headbuf,sizeof(unsigned char),HEADERSIZE,Out_Fp);

for (i=0;iheight;i++) {
for (j=0;jwidth;j++) {
Bmp_Data[j*3] = tp->data[tp->height-i-1][j].b;
Bmp_Data[j*3+1] = tp->data[tp->heigh

t-i-1][j].g;
Bmp_Data[j*3+2] = tp->data[tp->height-i-1][j].r;
}
for (j=tp->width*3;jBmp_Data[j]=0;
}
fwrite(Bmp_Data,sizeof(unsigned char),Real_width,Out_Fp);
}

free(Bmp_Data);
fclose(Out_Fp);
}




void fwriteBMP(unsigned char*img) // Write BMP file;
{
__img *tp;
tp=(__img*)malloc(sizeof(__img));
tp->width=MyInfo.Width;
tp->height=MyInfo.Height;
for(int y=0;yheight;y++){
for(int x=0;xwidth;x++){
tp->data[y][x].b=img[3*(y*tp->width+x)];
tp->data[y][x].g=img[3*(y*tp->width+x)+1];
tp->data[y][x].r=img[3*(y*tp->width+x)+2];
}
}
WriteBmp("BMPfile.bmp",tp); // Note:
free(tp);
}




int main(void){
unsigned char *img,*img2;
img=(unsigned char*)malloc(100000000);
img2=(unsigned char*)malloc(100000000);



PrintBMPHeader();
PrintBMPInfo();
ReadBidata(img);
ReadBidata02(img,img2);
_Invert_img2(img2);
SaveBipFile(img2);
fwriteBMP(img2);


/*for(int i=0;i<100;i++){
printf("%d\n",p[i]);
}*/
return 0;
}

相关主题
文本预览
相关文档 最新文档