打印
[研电赛技术支持]

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

[复制链接]
132|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2024-1-21 18:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
因为先控制生成的是像素三基色,所以可以用此方法生成任何图案。如直线,圆,甚至汉字

可以把长宽围成的图案看成是一个大的二维数组,再从其中扣出一部分,再填充一部分就成了。可惜现在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;
}



————————————————
版权声明:本文为CSDN博主「乐山劲松」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_59802969/article/details/135471758

使用特权

评论回复
沙发
申小林一号| | 2024-4-30 17:05 | 只看该作者
非常不错的帖子,值得推广扩散!!!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1364

主题

13994

帖子

8

粉丝