关于YUV图像转换问题
现在要做个个从yuv422转换成BMP565格式的图片
- static void cvt_422p_to_rgb565(int width, int height, const unsigned char *src, char * file)
- {
- FILE *fp;
- int Y0POS = 0,UPOS = 1,VPOS = 3,Y1POS = 2;
- int u,v,r,g,b;
- int rdif,invgdif,bdif;
- unsigned char *YUVdata;
- unsigned char RGBdata[2];
- BITMAPFILEHEADER *file_head = (BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER));
- BITMAPINFOHEADER *file_info = (BITMAPINFOHEADER *)malloc(sizeof(BITMAPINFOHEADER));
- RGBMASK *bitmask =(RGBMASK *)malloc(sizeof(RGBMASK));
- if (file_head == NULL || file_info == NULL || bitmask == NULL )
- perror("lost malloc");
- file_head->bfType = 19778;
- file_head->bfSize = 0; //这个大小
- file_head->bfReserved1 = 0;
- file_head->bfReserved2 = 0;
- file_head->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBMASK);
- printf("%s %d\n",__func__,__LINE__);
- file_info->biSize = sizeof(BITMAPINFOHEADER);
- file_info->biWidth = width;
- file_info->biHeight = height;
- file_info->biPlanes = 1;
- file_info->biBitCount = 16;
- file_info->biCompression = 0;
- printf("%s %d\n",__func__,__LINE__);
- file_info->biSizeImage = (file_info->biWidth* file_info->biBitCount+31)/8 /4 * 4 *height;
- file_info->biXPelsPerMeter = 0;
- file_info->biYPelsPerMeter = 0;
- file_info->biClrUsed = 0;
- file_info->biClrImportant = 0;
- printf("%s %d\n",__func__,__LINE__);
- bitmask->rgbRed = 0xF800;
- bitmask->rgbGreen = 0x07E0;
- bitmask->rgbBlue = 0x001F;
- fp=fopen(file, "wb");
- {
- if ( fp == NULL )
- {
- perror("open");
- return;
- }
- }
- fwrite(file_head,sizeof(BITMAPFILEHEADER),1,fp);
- fwrite(file_info,sizeof(BITMAPINFOHEADER),1,fp);
- fwrite(bitmask,sizeof(RGBMASK),1,fp);
- while ( src )
- {
- YUVdata = (unsigned char *)src ; ///
- u = YUVdata[UPOS] - 128;
- v = YUVdata[VPOS] - 128;
- rdif = v + ((v * 103) >> 8);
- invgdif = ((u * 88) >> 8) +((v * 183) >> 8);
- bdif = u +( (u*198) >> 8);
- r = YUVdata[Y0POS] + rdif;
- g = YUVdata[Y0POS] - invgdif;
- b = YUVdata[Y0POS] + bdif;
- RGBdata[1] =( (r & 0xF8) | ( g >> 5) );
- RGBdata[0] =( ((g & 0x1C) << 3) | ( b >> 3) );
- fwrite(RGBdata,sizeof(RGBdata),1,fp);
- r = YUVdata[Y1POS] + rdif;
- g = YUVdata[Y1POS] - invgdif;
- b = YUVdata[Y1POS] + bdif;
- RGBdata[1] =( (r & 0xF8) | ( g >> 5) );
- RGBdata[0] =( ((g & 0x1C) << 3) | ( b >> 3) );
- fwrite(RGBdata,sizeof(RGBdata),1,fp);
- src += 4;
- }
- file_head->bfSize = ftell(fp) - file_head->bfOffBits;
- fseek(fp,sizeof(file_head->bfType),SEEK_SET);
- fwrite(&file_head->bfSize,sizeof(file_head->bfSize),1,fp);
- free(file_head);
- free(file_info);
- free(bitmask);
- fclose(fp);
- }
复制代码转换后图片老是显示不出来,不知道是哪里错了,请高手们帮忙看下
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论