最近在学习雷神的YUV生成420P的灰阶测试图
自己仿写了雷神的接口simplest_yuv420_graybar,在代码中加入了一些注释, 记录下来
【注】:
在Y分量取值方面, 雷神的程序将0~255分成了9份, 即最后一个色柱可以取到255
而我自己的程序将0~255分成了10份, 最后一个色柱取不到最大亮度255
代码的大概意思是malloc三块内存data_y,data_u,data_v,并分别将YUV值按照要求写入
开辟一块writeBuff用于将data_y,data_u,data_v装在一起, 最后将writeBuff的内容写入文件即可
#include"common_head.h"
#define MAX_LEN (1*1024*1024)
int simplest_yuv420_graybar(int width, int height, int ymin, int ymax, int barnum, char* url_out)
{
int barwidth = 0;//garybar width
float lum_inc = 0.0;//luma_increase
unsigned char lum_temp = '\0';//each column's luma value
int fd_ori = -1;//file descriptor
unsigned char* data_y = NULL;
unsigned char* data_u = NULL;
unsigned char* data_v = NULL;
int t = 0, i = 0, j = 0;//for circle
int s32Ret = 0;
unsigned char writeBuff[MAX_LEN] = {'\0'};
/*if width = 640;barnum = 10;then barwidth = 64*/
barwidth = width / barnum;
/*luma_increase number of each column*/
/*if ymax = 255;ymin = 0;barnum = 10;then lum_inc = 25.5*/
lum_inc = ((float)(ymax - ymin)) / ((float)(barnum));
/*malloc memories*/
data_y = (unsigned char*)malloc(width * height);
if(NULL == data_y)
{
printf("malloc memory for Y failed!\n");
return -1;
}
data_u = (unsigned char*)malloc(width * height / 4);
if(NULL == data_u)
{
printf("malloc memory for U failed!\n");
free(data_y);
return -1;
}
data_v = (unsigned char*)malloc(width * height / 4);
if(NULL == data_v)
{
printf("malloc memory for V failed!\n");
free(data_u);
free(data_y);
return -1;
}
printf("malloc success!\n");
/*open the source file*/
fd_ori = open(url_out, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 777);
if(fd_ori < 0)
{
printf("failed to open file!\n");
free(data_v);
free(data_u);
free(data_y);
return -1;
}
/*width:640; height:360; barnum:10; barwidth:64; lum_inc:25.5*/
printf("Y U V value from pic's left to right:\n");
for(t = 0; t < barnum; t++)
{
lum_temp = (unsigned char)(t * lum_inc);
printf("bar[%d]'s luma = %d\n",t, lum_temp);
}
/*process the Y data*/
memset(data_y, 0, width * height);
for(i = 0; i < height; i++)
{
for(j = 0; j < barnum; j++)
{
memset(data_y + j * barwidth + i * width, (unsigned char)(j * lum_inc), barwidth);
}
}
/*process the U data*/
memset(data_u, 0, (width * height / 4));
memset(data_u, 128, (width * height / 4));
/*process the V data*/
memset(data_v, 0, (width * height / 4));
memset(data_v, 128, (width * height / 4));
/*write the data back*/
memset(writeBuff, 0, sizeof(writeBuff));
memcpy(writeBuff, data_y, width * height);
memcpy(writeBuff + width * height, data_u, width * height / 4);
memcpy(writeBuff + width * height + width * height / 4, data_v, width * height / 4);
s32Ret = write(fd_ori, writeBuff, width * height * 3 / 2);
if(s32Ret < 0)
{
printf("write the whole file failed!\n");
close(fd_ori);
free(data_v);
free(data_u);
free(data_y);
return -1;
}
printf("write success!\n");
/*close and free memory...*/
close(fd_ori);
free(data_v);
free(data_u);
free(data_y);
printf("process success!\n");
return 0;
}
效果如下:
源博客地址:
https://blog.csdn.net/leixiaohua1020/article/details/50534150