bmp转yuv_Nvl函数

bmp转yuv_Nvl函数voidSaveBMP(BYTE*data,intw,inth) //data指代的是解码后的原始数据,格式为:NV12{FILE*f;unsignedchar*img=NULL;intfilesize=54+3*w*h;     //wisyourimagewidth,hisimageheight,bothi


void SaveBMP ( BYTE* data, int w, int h )  // data指代的是解码后的原始数据,格式为:NV12
{

FILE *f;
unsigned char *img = NULL;
int filesize = 54 + 3*w*h;          //w is your image width, h is image height, both int
if( img )  { free( img ); }


img = (unsigned char *)malloc(3*w*h);
memset(img,0,sizeof(img));

for(int j=0; j<h; j++)
{

for(int i=0; i<w; i++)
{

unsigned char r,g,b;
r = data[(((j*w)+i)*4)+2];
g = data[(((j*w)+i)*4)+1];
b = data[(((j*w)+i)*4)+0];
img[(((j*w)+i)*3)+2] = (r);
img[(((j*w)+i)*3)+1] = (g);
img[(((j*w)+i)*3)+0] = (b);
}
}

unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
unsigned char bmppad[3] = {0,0,0};

bmpfileheader[ 2] = (unsigned char)(filesize    );
bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
bmpfileheader[ 4] = (unsigned char)(filesize>>16);
bmpfileheader[ 5] = (unsigned char)(filesize>>24);

bmpinfoheader[ 4] = (unsigned char)(       w    );
bmpinfoheader[ 5] = (unsigned char)(       w>> 8);
bmpinfoheader[ 6] = (unsigned char)(       w>>16);
bmpinfoheader[ 7] = (unsigned char)(       w>>24);
bmpinfoheader[ 8] = (unsigned char)(       h    );
bmpinfoheader[ 9] = (unsigned char)(       h>> 8);
bmpinfoheader[10] = (unsigned char)(       h>>16);
bmpinfoheader[11] = (unsigned char)(       h>>24);

        g_FrameCapFile[] = "framecap.bmp"; //保存的图片名称
f = fopen(g_FrameCapFile,"wb");   

fwrite(bmpfileheader,1,14,f);
fwrite(bmpinfoheader,1,40,f);
for(int i=0; i<h; i++)
{

fwrite(img+(w*(h-i-1)*3),3,w,f);
fwrite(bmppad,1,(4-(w*3)%4)%4,f);
}
fclose(f);
printf("saved frame capture file width = %d, height = %d\n", w, h);
}





本文来源tkp2014,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/222592
0
 

发表评论