NV12转化为BMP函数
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);
}
總結
以上是生活随笔為你收集整理的NV12转化为BMP函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CHIP下游分析(仅ChIPseeker
- 下一篇: 使用Python批量转换图片格式