数据压缩原理与应用实验一

数据压缩原理与应用实验一

一、实验要求
将YUV文件转换为RGB文件,将给定的实验数据用C语言转换为RGB文件,用YUVViewer播放转换后的图片,与原始文件进行比较,验证是否转换正确
二、实验原理
1,彩色空间转换的基本思想和转换公式
YUV与RGB空间的相互转换:
Y=0.2990R+0.5870G+0.1140B
R-Y=0.7010R-0.5870G-0.1140B
B-Y=-0.2990R-0.5870G+0.8860B
为了使色差信号的动态范围控制在0.5之间,需要进行归一化,对色差信号引入压缩
系数。归一化后的色差信号为:
U=-0.1684R-0.3316G+0.5B
V=0.5R-0.4187G-0.0813
由RGB转YUV的公式可推出YUV转RGB的公式如下:
R = Y + 1.4075(V - 128)
G = Y - 0.3455(U - 128) - 0.7169(V - 128)
B = Y + 1.779(U - 128)
2,色度格式
4:2:0格式是指色差信号U,V的取样频率为亮度信号取样频率的四分之一,在水平方 向和垂直方向上的取样点数均为Y的一半。
三、实验代码
①头文件

int YUV2RGB (int xx, int yy, void *rgb_out, void *y_in, void *u_in, void *v_in, int flip);
void InitLookupTable();

②yuv2rgb.cpp

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "yuv2rgb.h"

static float RGBYUV14075[256], RGBYUV03455[256];
static float RGBYUV1779[256], RGBYUV07169[256];


int YUV2RGB(int xx, int yy, void* rgb_out, void* y_in, void* u_in, void* v_in, int flip)
{
    static int init_done = 0;

    long i, j, size;
    unsigned char* r, * g, * b;
    unsigned char* y, * u, * v;
    unsigned char* pu1, * pu2, * pv1, * pv2, * psu, * psv;
    unsigned char* exp_u_buf, * exp_v_buf;
    unsigned char* u_buffer, * v_buffer, * y_buffer, * rgb_buffer;
    if (init_done == 0)
    {
        InitLookupTable();
        init_done = 1;
    }
    // check to see if x and yy are divisible by 2
    if ((xx % 2) || (yy % 2)) return 1;
    size = xx * yy;

    // allocate memory
    exp_u_buf = (unsigned char*)malloc(size * sizeof(unsigned char));
    exp_v_buf = (unsigned char*)malloc(size * sizeof(unsigned char));
    y_buffer = (unsigned char*)y_in;
    u_buffer = (unsigned char*)u_in;
    v_buffer = (unsigned char*)v_in;
    rgb_buffer = (unsigned char*)rgb_out;
    g = (unsigned char*)malloc(size * sizeof(unsigned char));
    r = (unsigned char*)malloc(size * sizeof(unsigned char));

    if (!(u_buffer && v_buffer))
    {
        if (u_buffer) free(u_buffer);
        if (v_buffer) free(v_buffer);
        return 2;
    }

    y = (unsigned char*)y_in;
    u = exp_u_buf;
    v = exp_v_buf;
    b = rgb_buffer;


    // convert yuv to rgb
  
    if (flip) {
        for (j = 0; j < yy; j++)
        {
            y = y_buffer + (yy - j - 1) * x;
            u = exp_u_buf + (yy - j - 1) * x;
            v = exp_v_buf + (yy - j - 1) * x;

            for (i = 0; i < x; i++) {
                g = b + 1;
                r = b + 2;


                float r0, g0, b0;
                r0 = ((*y) + RGBYUV14075[*v]);
                g0 = ((*y) - RGBYUV03455[*u] - RGBYUV07169[*v]);
                b0 = ((*y) + RGBYUV1779[*u]);
                *r = (r0 > 0 ? (r0 > 255 ? 255 : (unsigned char)r0) : 0);
                *g = (g0 > 0 ? (g0 > 255 ? 255 : (unsigned char)g0) : 0);
                *b = (b0 > 0 ? (b0 > 255 ? 255 : (unsigned char)b0) : 0);
                b += 3;
                y++;
                u++;
                v++;
            }
        }
    }
    else {
        for (i = 0; i < size; i++)
        {
            u = v + 1;
            y = v + 2;
            *r = (unsigned char)((*y) + RGBYUV14075[(*v) - 128]);
            *g = (unsigned char)((*y) - RGBYUV03455[(*u) - 128] - RGBYUV07169[(*v) - 128]);
            *b = (unsigned char)((*y) + RGBYUV1779[(*u) - 128]);
            v += 3;
            r++;
            g++;
            b++;
        }
    }

    for (j = 0; j < yy / 2; j++)
    {
        psu = u_buffer + j * xx / 2;
        psv = v_buffer + j * xx / 2;
        pu1 = exp_u_buf + 2 * j * xx;
        pu2 = exp_u_buf + (2 * j + 1) * xx;
        pv1 = exp_v_buf + 2 * j * xx;
        pv2 = exp_v_buf + (2 * j + 1) * xx;
        for (i = 0; i < xx / 2; i++)
        {
            *pu1 = *psu;
            *(pu1 + 1) = *psu;
            *pu2 = *psu,
                * (pu2 + 1) = *psu;
            *pv1 = *psv;*(pv1 + 1) = *psv;
            *pv2 = *psv, * (pv2 + 1) = *psv;
            psu++;
            psv++;
            pu1 += 2;
            pu2 += 2;
            pv1 += 2;
            pv2 += 2;
        }
    }

    free(exp_u_buf);
    free(exp_v_buf);


    return 0;
}

void InitLookupTable()
{
    int i;

    for (i = 0; i < 256; i++) RGBYUV14075[i] = (float)1.4075 * (i - 128);
    for (i = 0; i < 256; i++) RGBYUV03455[i] = (float)0.3455 * (i - 128);
    for (i = 0; i < 256; i++) RGBYUV07169[i] = (float)0.7169 * (i - 128);
    for (i = 0; i < 256; i++) RGBYUV1779[i] = (float)1.7790 * (i - 128);
}

③main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "yuv2rgb.h"

int main(int argc, char** argv)
{
    /* variables controlable from command line */
    unsigned __int32 frameWidth = 352;   /* --width=<uint> */
    unsigned __int32 frameHeight = 240;  /* --height=<uint> */
    bool flip = true;    /* --flip */
    unsigned char i;

    /* internal variables */
    char* rgbFileName = NULL;
    char* yuvFileName = NULL;
    FILE* rgbFile = NULL;
    FILE* yuvFile = NULL;
    unsigned char* yuvBuf = NULL;
    unsigned char* rgbBuf = NULL;
    unsigned char* yBuf = NULL;
    unsigned char* uBuf = NULL;
    unsigned char* vBuf = NULL;

    /* begin process command line */
    /* point to the specified file names */
    yuvFileName = argv[1];
    rgbFileName = argv[2];

    frameWidth = atoi(argv[3]);//把参数所指向的字符串转换为一个整数
    frameHeight = atoi(argv[4]);

    /* open the RGB file */
    yuvFile = fopen(yuvFileName, "rb");
    if (yuvFile == NULL)
    {
        printf("cannot find yuv file\n");
        exit(1);
    }
    else
    {
        printf("The input yuv file is %s\n", yuvFileName);
    }

    /* open the RAW file */
    rgbFile = fopen(rgbFileName, "wb");
    if (rgbFile == NULL)
    {
        printf("cannot find rgb file\n");
        exit(1);
    }
    else
    {
        printf("The output rgb file is %s\n", rgbFileName);
    }

    /* get an input buffer for a frame */
    yuvBuf = (unsigned char*)malloc(frameWidth * frameHeight * 1.5);

    /* get the output buffers for a frame */
    yBuf = (unsigned char*)malloc(frameWidth * frameHeight);
    uBuf = (unsigned char*)malloc((frameWidth * frameHeight) / 4);
    vBuf = (unsigned char*)malloc((frameWidth * frameHeight) / 4);
    rgbBuf = (unsigned char*)malloc(frameWidth * frameHeight * 3);
    fread(yuvBuf, 1, frameWidth * frameHeight * 1.5, yuvFile);


    for (int a = 0;a < frameWidth * frameHeight;a++)
    {
        yBuf[a] = yuvBuf[a];

    }
    for (int b = 0;b < (frameWidth * frameHeight) / 4;b++)
    {
        uBuf[b] = yuvBuf[(frameWidth * frameHeight) + b];

    }
    for (int c = 0;c < (frameWidth * frameHeight) / 4;c++)
    {
        vBuf[c] = yuvBuf[int((frameWidth * frameHeight) * 1.25) + c];

    }


    if (rgbBuf == NULL || yBuf == NULL || uBuf == NULL || vBuf == NULL || yuvBuf == NULL)
    {
        printf("no enought memory\n");
        exit(1);
    }

    if (!YUV2RGB(frameWidth, frameHeight, rgbBuf, yBuf, uBuf, vBuf, flip))
    {

        fwrite(rgbBuf, 1, frameWidth * frameHeight * 3, rgbFile);
    }

    /* cleanup */

    fclose(rgbFile);
    fclose(yuvFile);

    return(0);
}

四、实验结果
原down.yuv:在这里插入图片描述
转换出来的rgb_out.rgb:在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值