c 生成任意尺寸YUV420P格式水平垂直彩条

因为先控制生成的是像素三基色,所以可以用此方法生成任何图案。如直线,圆,甚至汉字

可以把长宽围成的图案看成是一个大的二维数组,再从其中扣出一部分,再填充一部分就成了。可惜现在jpeg编码程序有错误,显示始终不正常。否则就可用此程序编写图片的扣图,拼图,加字,打码赛克等,甚至图片的缩放。

生成的yuv420p格式图片可直接用ffplay -s  长*宽  -i  文件名  直接播放


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>  
#include <string.h>
#include <sys/mman.h>
#include <linux/fb.h>

#define  pic_width  640
#define  pic_heigth  480

#define  file  "/home/wjs/Pictures/1280×720.rgb"
#define  file1 "/home/wjs/Pictures/sample.yuv"
int sp[pic_width*pic_heigth];
int sp1[pic_width*pic_heigth];


int main(void)
{
	
	int t=0;
	for(int a=0;a<pic_heigth/4;a++){
		for(int b=0;b<pic_width;b++){     
			sp1[t]=0xff<<16|0x0<<8|0x0;      
			t++;
		}
	}
	for(int a=0;a<pic_heigth/4;a++){
		for(int b=0;b<pic_width;b++){
			sp1[t]=0xff<<16|0xff<<8|0xff;     
			t++;
		}
	}
	for(int a=0;a<pic_heigth/4;a++){
		for(int b=0;b<pic_width;b++){       
			sp1[t]=0xff<<16|0xff<<8|0xff;
			t++;
		}
	}
	for(int a=0;a<(pic_heigth/4);a++){
		for(int b=0;b<pic_width;b++){
			sp1[t]=0xff<<16|0xff<<8|0xff;     
			t++;
		}
	
	}
	
/*	for(int a=0;a<pic_heigth;a++){            //生成垂直彩条
		for(int b=0;b<pic_width;b++){
			if(b<pic_width/2){
				sp1[t]=0xff<<16|0xff<<8|0xff;
			}else{
				sp1[t]=0xff<<16|0x0<<8|0x0;
			}
			t++;
		}
	}
	*/
	FILE *f=fopen(file,"w+b");           //生成RGB文件  
	fwrite(sp1,t*4,1,f);                   
	fclose(f);
	
	int sp2[pic_width*pic_heigth]={};   //读入RGB文件
	FILE *fo=fopen(file,"rb");             
	fread(sp2,t*4,1,fo);
	fclose(fo);
//--------------------------------------------------------
	/*	RGB ת»»³É YUV
	Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16              
	Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
	Cb = U = -( 0.148 * R) - (0.291 * G) + (0.439 * B) + 128
	YUV ת»»³É RGB
	B = 1.164(Y - 16) + 2.018(U - 128)
	G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
	R = 1.164(Y - 16) + 1.596(V - 128)
	*/
//-------------------------------------------
	unsigned char o_yuv[4*pic_width*pic_heigth]={};
	for(int a=0;a<pic_width*pic_heigth;a++){
		int Y=0,B=0,G=0,R=0;
		R=(sp2[a])>>16;
		G=(sp2[a]&0b1111111100000000)>>8;
		B=(sp2[a])&0b11111111;
		Y=(0.257*R)+(0.504*G)+(0.098*B)+16;
		if(Y>0xff)  Y=0xff;
		if(Y<0)     Y=0;
		o_yuv[a]=(unsigned char)Y;	                   
	}
	int nu=0;
	for(int a=0;a<pic_heigth;a=a+2){
		for(int c=0;c<pic_width;c=c+2){
			int U=0,B=0,G=0,R=0;
			R=(sp2[a*pic_width+c+1])>>16;
			G=(sp2[a*pic_width+c+1]&0b1111111100000000)>>8;
			B=(sp2[a*pic_width+c+1])&0b11111111;
			U=-(0.148*R)-(0.291*G)+(0.439*B)+128;
			if(U>0xff)  U=0xff;
			if(U<0)     U=0;
			o_yuv[pic_width*pic_heigth+nu]=(unsigned char)U;	 
			nu++;
		}
	}
	int nv=0;
	for(int a=0;a<pic_heigth;a=a+2){
		for(int c=0;c<pic_width;c=c+2){
			int V=0,B=0,G=0,R=0;
			R=(sp2[a*pic_width+c+1])>>16;
			G=(sp2[a*pic_width+c+1]&0b1111111100000000)>>8;
			B=(sp2[a*pic_width+c+1])&0b11111111;
			V=(0.439*R)-(0.368*G)-(0.071*B)+128;
			if(V>0xff)  V=0xff;
			if(V<0)     V=0;
			o_yuv[pic_width*pic_heigth*5/4+nv]=(unsigned char)V;	   
			nv++;
		}
	}
//-------------------------------------------------------

	FILE *of=fopen(file1,"w+b");
	fwrite(o_yuv,pic_width*pic_heigth*3/2,1,of);
	fclose(of);
	
	
	return 0;
}


 

 

 

 

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值