您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > C语言读取bmp位图文件(含bmp格式定义)
loadbmp.h#ifndef_LOADBMP_H_#define_LOADBMP_H_typedefunsignedcharBYTE;typedefunsignedshortWORD;typedefunsignedlongDWORD;typedefstruct{/*BITMAPFILEHEADER*/BYTEbfType[2];DWORDbfSize;WORDbfReserved1;WORDbfReserved2;DWORDbfOffBits;}BMPFILEHEAD;#defineFILEHEADSIZE14/*windowsstyle*/typedefstruct{/*BITMAPINFOHEADER*/DWORDBiSize;DWORDBiWidth;DWORDBiHeight;WORDBiPlanes;WORDBiBitCount;DWORDBiCompression;DWORDBiSizeImage;DWORDBiXpelsPerMeter;DWORDBiYpelsPerMeter;DWORDBiClrUsed;DWORDBiClrImportant;}BMPINFOHEAD;#defineINFOHEADSIZE40typedefstruct_BMP{BMPINFOHEADinfo;unsignedchar*rgba;unsignedchar*yuy2;unsignedchar*yv12;}BMP,*PBMP;intLoadBMP(char*name,PBMPpbmp);intReleaseBMP(PBMPpbmp);voidrgb_to_yuv(unsignedintr,unsignedintg,unsignedintb,unsignedint*y,unsignedint*u,unsignedint*v);#endif/*_LOADBMP_H_*/loadbmp.c#includestdio.h#includesys/types.h#includesys/stat.h#includefcntl.h#includeunistd.h#includestdlib.h#includeloadbmp.hvoidrgb_to_yuv(unsignedintr,unsignedintg,unsignedintb,unsignedint*y,unsignedint*u,unsignedint*v){doubledy,du,dv;dy=(0.257*(double)r)+(0.504*(double)g)+(0.098*(double)b)+16.0;dv=(0.439*(double)r)-(0.368*(double)g)-(0.071*(double)b)+128.0;du=-(0.148*(double)r)-(0.291*(double)g)+(0.439*(double)b)+128.0;*y=(unsignedint)dy&0xff;*v=(unsignedint)dv&0xff;*u=(unsignedint)du&0xff;}staticvoidconvert_to_yv12(PBMPpbmp){intx,y;unsignedchar*in_ptr,*out_ptr_y,*out_ptr_u,*out_ptr_v;unsignedintY,U,V;in_ptr=pbmp-rgba;out_ptr_y=pbmp-yv12;out_ptr_u=out_ptr_y+pbmp-info.BiWidth*pbmp-info.BiHeight;out_ptr_v=out_ptr_u+pbmp-info.BiWidth*pbmp-info.BiHeight/4;for(y=0;ypbmp-info.BiHeight;y++){for(x=0;xpbmp-info.BiWidth;x++){rgb_to_yuv(in_ptr[0],in_ptr[1],in_ptr[2],&Y,&U,&V);in_ptr+=pbmp-info.BiBitCount/8;*out_ptr_y++=Y;if(x%2==0&&y%2==0){*out_ptr_u++=V;*out_ptr_v++=U;}}}}staticvoidconvert_to_yuy2(PBMPpbmp){intx,y;unsignedchar*in,*out;unsignedintY,U,V;in=pbmp-rgba;out=pbmp-yuy2;for(y=0;ypbmp-info.BiHeight;y++){for(x=0;xpbmp-info.BiWidth;x++){staticintcnt=0;rgb_to_yuv(in[0],in[1],in[2],&Y,&U,&V);in+=pbmp-info.BiBitCount/8;*(out)=Y;if(cnt%2==0)*(out+1)=V;else*(out+1)=U;out+=2;cnt++;}}}intLoadBMP(char*name,PBMPpbmp){intfd;intheadsize;BMPFILEHEADbmpf;unsignedcharheadbuffer[INFOHEADSIZE];fd=open(name,O_RDONLY);if(fd0){perror(open);return-1;}read(fd,&headbuffer,FILEHEADSIZE);bmpf.bfType[0]=headbuffer[0];bmpf.bfType[1]=headbuffer[1];if(*(WORD*)&bmpf.bfType[0]!=0x4D42)/*'BM'*/return-2;/*notbmpimage*/bmpf.bfOffBits=*((DWORD*)&headbuffer[10]);if(read(fd,&headsize,sizeof(DWORD))!=sizeof(DWORD))return0;/*notbmpimage*/read(fd,&headbuffer,INFOHEADSIZE-sizeof(DWORD));pbmp-info.BiWidth=*(DWORD*)(&headbuffer[0]);pbmp-info.BiHeight=*(DWORD*)(&headbuffer[4]);pbmp-info.BiBitCount=*(DWORD*)(&headbuffer[10]);pbmp-info.BiSizeImage=*(DWORD*)(&headbuffer[16]);pbmp-rgba=(unsignedchar*)malloc(pbmp-info.BiSizeImage);if(!pbmp-rgba){perror(malloc);exit(EXIT_FAILURE);}//pbmp-yuy2=(unsignedchar*)malloc(pbmp-info.BiWidth*pbmp-info.BiHeight*2);//if(!pbmp-yuy2){//perror(malloc);//exit(EXIT_FAILURE);//}//pbmp-yv12=(unsignedchar*)malloc(pbmp-info.BiWidth*pbmp-info.BiHeight*3/2);//if(!pbmp-yv12){//perror(malloc);//exit(EXIT_FAILURE);//}lseek(fd,bmpf.bfOffBits,SEEK_SET);if(read(fd,pbmp-rgba,pbmp-info.BiSizeImage)!=pbmp-info.BiSizeImage){perror(read);return-4;}//convert_to_yv12(pbmp);//convert_to_yuy2(pbmp);printf(LoadBMP:%s%ldx%ldx%d\n,name,pbmp-info.BiWidth,pbmp-info.BiHeight,pbmp-info.BiBitCount);close(fd);return0;}intReleaseBMP(PBMPpbmp){if(pbmp-rgba){free(pbmp-rgba);pbmp-rgba=NULL;}//if(pbmp-yuy2){//free(pbmp-yuy2);//pbmp-yuy2=NULL;//}//if(pbmp-yv12){//free(pbmp-yv12);//pbmp-yv12=NULL;//}return0;}
本文标题:C语言读取bmp位图文件(含bmp格式定义)
链接地址:https://www.777doc.com/doc-7274054 .html