simplest_pic_gen。该工具可以生成视频测试时候常用的RGB/YUV格式的测试图片。包括灰阶测试图,彩条图,彩色条纹图,RGB渐变彩条图,YUV渐变彩条图,颜色视频等。
下面简单介绍一下生成YUV格式图片的方法
查看生成的图片需要使用RGB/YUV播放器。
亮度取值为16-235的灰阶测试图
下面这张图是一张灰阶测试图的示例。这张图的分辨率是1280x720,像素格式是YUV420P,亮度的取值范围是16-235,一共包含了10级的灰度。最左边的灰度竖条的YUV取值为(16,128,128),最右边的灰度竖条的YUV取值为(235,128,128)。
int gen_yuv420p_graybar(int width, int height,int barnum,unsigned char ymin,unsigned char ymax){
int barwidth;
float lum_inc;
unsigned char lum_temp;
int uv_width,uv_height;
FILE *fp=NULL;
unsigned char *data_y=NULL;
unsigned char *data_u=NULL;
unsigned char *data_v=NULL;
int t=0,i=0,j=0;
char filename[100]={0};
//Check
if(width<=0||height<=0||barnum<=0){
printf("Error: Width, Height or Bar Number cannot be 0 or negative number!\n");
printf("Default Param is used.\n");
width=640;
height=480;
barnum=10;
}
if(width%barnum!=0){
printf("Warning: Width cannot be divided by Bar Number without remainder!\n");
}
barwidth=width/barnum;
lum_inc=((float)(ymax-ymin))/((float)(barnum-1));
uv_width=width/2;
uv_height=height/2;
data_y=(unsigned char *)malloc(width*height);
data_u=(unsigned char *)malloc(uv_width*uv_height);
data_v=(unsigned char *)malloc(uv_width*uv_height);
sprintf(filename,"graybar_%dx%d_yuv420p.yuv",width,height);
if((fp=fopen(filename,"wb+"))==NULL){
printf("Error: Cannot create file!");
return -1;
}
//Output Info
printf("Y, U, V value from picture's left to right:\n");
for(t=0;t<(width/barwidth);t++){
lum_temp=ymin+(char)(t*lum_inc);
printf("%3d, 128, 128\n",lum_temp);
}
//Gen Data
//设置相应的Y值
for(j=0;j<height;j++){
for(i=0;i<width;i++){
t=i/barwidth;
lum_temp=ymin+(char)(t*lum_inc);
data_y[j*width+i]=lum_temp;
}
}
//设置相应的U值
for(j=0;j<uv_height;j++){
for(i=0;i<uv_width;i++){
//data_u[j*uv_width+i]=128+128%(i+1);
data_u[j*uv_width + i] = 128;
}
}
//设置相应的V值
for(j=0;j<uv_height;j++){
for(i=0;i<uv_width;i++){
//data_v[j*uv_width+i]=128+128/(i+1);
data_v[j*uv_width + i] = 128;
}
}
//写入相应的图片数据
fwrite(data_y,width*height,1,fp);
fwrite(data_u,uv_width*uv_height,1,fp);
fwrite(data_v,uv_width*uv_height,1,fp);
fclose(fp);
free(data_y);
free(data_u);
free(data_v);
printf("Finish generate %s!\n",filename);
return 0;
}
生成图片如下:
由代码可知,设置相应的u,v值为恒定值128,相应图片的色调,饱和度不变
修改相应的代码如下:
//设置相应的Y值
for(j=0;j<height;j++){
for(i=0;i<width;i++){
t=i/barwidth;
lum_temp=ymin+(char)(t*lum_inc);
data_y[j*width+i]=lum_temp;
}
}
//设置相应的U值
for(j=0;j<uv_height;j++){
for(i=0;i<uv_width;i++){
data_u[j*uv_width+i]=128+128%(i+1);
//data_u[j*uv_width + i] = 128;
}
}
//设置相应的V值
for(j=0;j<uv_height;j++){
for(i=0;i<uv_width;i++){
data_v[j*uv_width+i]=128+128/(i+1);
//data_v[j*uv_width + i] = 128;
}
}
函数调用如下:
int main(int argc, char* argv[])
{
//All picture's resolution is 1280x720
//Gray Bar, from 16 to 235
gen_yuv420p_graybar(1280,720,10,16,235);}
生成图片如下:
相应的U,V值进行渐变,图片色调,饱和度进行变化。