源码如下:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h> #define XSIZE 1920
#define YSIZE 1080 #define RGB_SIZE XSIZE * YSIZE * 3
typedef unsigned char byte; double YUV2RGB_CONVERT_MATRIX[3][3] = { { 1, 0, 1.4022 }, { 1, -0.3456, -0.7145 }, { 1, 1.771, 0 } }; void ConvertYUV2RGB(unsigned char *yuvFrame, unsigned char *rgbFrame, int width, int height)
{
int uIndex = width * height;
int vIndex = uIndex + ((width * height) >> 2);
int gIndex = width * height;
int bIndex = gIndex * 2;
int temp, x, y; for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
// R分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0][2]);
rgbFrame[y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
// G分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1][1] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1][2]);
rgbFrame[gIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
// B分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2][1]);
rgbFrame[bIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
}
}
} int main(){
int i, j, y, x;
long width, height;
unsigned char *data;
unsigned char *image; //用于存放读取的yuv数据
unsigned char *image_bmp; FILE *fp_r; width = XSIZE;
height = YSIZE;
long int bytePerLine = width * 3; //申请空间
image = (unsigned char *)malloc(width * height * 3 / 2);
image_bmp = (unsigned char *)malloc(width * height * 3);
data = (unsigned char*)malloc(bytePerLine * height); if ((NULL == image) || (image_bmp == NULL) || (data == NULL))
{
printf("faied to malloc the image\n");
return -1;
} /********读取yuv 文件***********/
fp_r = fopen("test.yuv", "rb"); //打开yuv 文件
if (NULL == fp_r)
{
printf("failed to open the fp_r\n");
return -1;
} fread(image, sizeof(unsigned char), XSIZE * YSIZE * 3 / 2, fp_r);
fclose(fp_r); for (i = 0; i<width *height * 3; i++)
*(image_bmp + i) = 255; //写bmp图片
int gIndex = width * height;
int bIndex = gIndex * 2; unsigned char header[54] = {
0x42, 0x4d, //WORD bfType----------- [0,1]
0, 0, 0, 0, //DWORD bfSize----------- [2,3,4,5]
0, 0, //WORD bfReserved1------ [6,7]
0, 0, //WORD bfReserved2------ [8,9]
54, 0, 0, 0, //WORD bfOffBits-------- [10,11,12,13] 40, 0, 0, 0, //DWORD biSize----------- [14,15,16,17]
0, 0, 0, 0, //LONG biWidth---------- [18,19,20,21]
0, 0, 0, 0, //LONG biHeight--------- [22,23,24,25]
1, 0, //WORD biplanes--------- [26,27]
24, 0, //WORD biCount---------- [28,29]
0, 0, 0, 0, //DWORD biCompression---- [30,31,32,33]
0, 0, 0, 0, //DWORD biSizeImage------ [34,35,36,37]
0, 0, 0, 0, //LONG biXPelsPerMeter-- [38,39,40,41]
0, 0, 0, 0, //LONG biYPelsPerMeter-- [42,43,44,45]
0, 0, 0, 0, //DWORD biClrUsed-------- [46,47,48,49]
0, 0, 0, 0 //DWORD biClrImportant--- [50,51,52,53]
};
long file_size = (long)width * (long)height * 3 + 54;
header[2] = (unsigned char)(file_size & 0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff; header[18] = width & 0x000000ff;
header[19] = (width >> 8) & 0x000000ff;
header[20] = (width >> 16) & 0x000000ff;
header[21] = (width >> 24) & 0x000000ff; header[22] = height & 0x000000ff;
header[23] = (height >> 8) & 0x000000ff;
header[24] = (height >> 16) & 0x000000ff;
header[25] = (height >> 24) & 0x000000ff; char filename[6] = "1.bmp"; ConvertYUV2RGB(image, image_bmp, width, height); for (y = height - 1, j = 0; y >= 0; y--, j++)
{
for (x = 0, i = 0; x < width; x++)
{
data[y * bytePerLine + i++] = image_bmp[bIndex + j * width + x];
// B data[y * bytePerLine + i++] = image_bmp[gIndex + j * width + x];
// G data[y * bytePerLine + i++] = image_bmp[j * width + x];
// R
}
}
//sprintf(filename, "%d.bmp",1); FILE *fp_w;
if (!(fp_w = fopen(filename, "wb")))
return -1; fwrite(header, sizeof(unsigned char), 54, fp_w); fwrite(data, sizeof(unsigned char), XSIZE * YSIZE * 3, fp_w); fclose(fp_w);
free(image);
return(0);
}

yuv420 转换成 bmp的更多相关文章

  1. 音视频入门-03-RGB转成BMP图片

    * 音视频入门文章目录 * BMP 文件格式解析 BMP 文件由文件头.位图信息头.颜色信息和图形数据四部分组成. 位图文件头(14个字节) 位图信息头(40个字节) 颜色信息 图形数据 文件头与信息 ...

  2. 通过FFmpeg将多媒体文件解码后保存成Bmp图像(YUV420 RGB32)

    /* g++ -o test test.cpp -lavformat -lavcodec -lavutil -lz -lm -lpthread -lswscale */ #include <st ...

  3. 实现同时将一批.bmp文件转换成.mat格式

    %% 功能:实现同时对一批.bmp文件的转换成.mat格式PicFormat = {'*.bmp','Bitmap image (*.bmp)';... '*.jpg','JPEG image (*. ...

  4. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

  5. webp怎么打开 webp怎么转换成jpg

    webp怎么打开 webp怎么转换成jpg   2 3 4 5 6 7 分步阅读 在使用google服务的时候(比如 google play),我们会发现保存的图都是webp格式. 那webp是什么东 ...

  6. Android网络图片转换成bitmap保存到本地指定文件夹

    下列代码,请求网络图片转换为bitmap,然后保存到指定文件夹,微信,QQ分享,要求缩略图不大于32kb 压缩图片代码,使用了Glide来进行图片压缩处理 Glide.get(ShopDetailsA ...

  7. data:image/png;base64 上传图像将图片转换成base64格式

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...

  8. delphi将图片转换成Base64编码函数

    {************************************************************************** 名称: BaseImage 参数: fn: TF ...

  9. android将drawable下的图片转换成bitmap

    将drawable下的图片转换成bitmap 1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xx ...

随机推荐

  1. K12

    K12,教育类专用名词(kindergarten through twelfth grade),是学前教育至高中教育的缩写,现在普遍被用来代指基础教育. K-12教育是美国基础教育的统称.“K12”中 ...

  2. B1005 继续(3n+1)猜想 (25 分)

    一.技术总结 一个是注意输出格式,也就是末尾没有空格,这个解决方法一般是使用一个计数,然后打印输出一个就减少一个. 就是在使用数组时,要注意段错误,也就是数组开辟的是否够大. 如果可以的话,可以多设置 ...

  3. c语言线程中传输多个参数

    前言:c语言中创建一条线程,但是需要传送多个参数给线程的话我们自然会想到通过传送数组或者结构体来实现,下面我们来看看如何在创建线程的时候传送结构体和数组. #include <stdio.h&g ...

  4. Linux性能优化实战学习笔记:第五十三讲

    一.上节回顾 在前面的内容中,我为你介绍了很多性能分析的原理.思路以及相关的工具.不过,在实际的性能分析中,一个很常见的现象是,明明发生了性能瓶颈,但当你登录到服务器中想要排查的时候,却发现瓶颈已经消 ...

  5. Lambda 表达式已阅

    Lambda 表达式已阅 君子之间淡如水,何谓淡如水?并不时常想起,但却无处不在. 背景:This is f**king why we code? 可以让Java 代码异常简洁的Lambda 表达式, ...

  6. DingTalk钉钉消息推送(.net core 3 WebApi尝鲜记)

    我发了个朋友圈,Swagger真他妈的牛B,解放了开发API的码农,麻麻再也不用担心我们写API文档耽误回家吃饭了. /// <summary> /// 发送钉钉消息 /// </s ...

  7. [原创]A/B测试系统调研思维导图

    [原创]A/B测试系统调研思维导图

  8. http与tcp,udp的区别

    1.网络协议的概念 (1)在学习网络课程的时候,老师会讲iso七层模型,有应用层 表示层 会话层 传输层 网络层 数据链路层 物理层,其中http就属于应用层,tcp与udp是属于传输层,如图1.1( ...

  9. 异步IO与回调

    最好了解 Java NIO 中 Buffer.Channel 和 Selector 的基本操作,主要是一些接口操作,比较简单. 本文将介绍非阻塞 IO 和异步 IO,也就是大家耳熟能详的 NIO 和 ...

  10. Java ReentrantLock中tryLock与lock的区别(非公平锁与公平锁)

    设置同步状态,利用CAS操作. // CAS操作:如果当前状态值等于期望值,则自动将同步状态设置为给定的更新值 protected final boolean compareAndSetState(i ...