YUV444转RGB

#include<stdio.h>
#include<stdlib.h>
typedef struct RGBPixel {
    unsigned char R;
    unsigned char G;
    unsigned char B;
} RGBPixel;

typedef struct YUVPixel {
    int Y;
    int U;
    int V;
} YUVPixel;

unsigned char clip_sh_0_255(int in) {
    unsigned char out;
    if (in >= 0 && in <= 255) {
        out = (unsigned char) in;
    } else if (in > 255) {
        out = (unsigned char) 255;
    } else {
        out = 0;
    }
    return out;
}

void YUV444_TO_RGB(const YUVPixel *yuv, RGBPixel *rgb) {
    rgb->R = clip_sh_0_255((298 * (yuv->Y - 16) + 409 * (yuv->V - 128) + 128) >> 8);
    rgb->G = clip_sh_0_255((298 * (yuv->Y - 16) - 208 * (yuv->V - 128) - 100 * (yuv->U - 128) + 128) >> 8);
    rgb->B = clip_sh_0_255((298 * (yuv->Y - 16) + 516 * (yuv->U - 128) + 128) >> 8);
}

int main()
{
        FILE *orignal_data = fopen("/home/gaopeng/Test_Sequence/app_JDshopping_1080p_30fps_444_295.yuv","r");
        FILE *f_r = fopen("r_data","w");
        if(orignal_data == NULL)
            printf("open file error:\n");
        unsigned int width = 1920, height = 1080;    
        unsigned char *raw_8bit_pixels = (unsigned char *)malloc(width*height*3*sizeof(unsigned char));
        fread(raw_8bit_pixels,1,width*height*3,orignal_data); //暂时忽略其返回值
        YUVPixel yuv_pixel;
        RGBPixel rgb_pixel;
        for(int j = 0;j < height;j++)
            for(int i = 0;i < width;i++)            
            {
                  yuv_pixel.Y = raw_8bit_pixels[1920*j+i];
                  yuv_pixel.U = raw_8bit_pixels[1920*j+i+1920*1080];
                  yuv_pixel.V = raw_8bit_pixels[1920*j+i+1920*1080*2];

            YUV444_TO_RGB(&yuv_pixel, &rgb_pixel);
            fputc(rgb_pixel.R,f_r);
            fputc(rgb_pixel.G,f_r);
            fputc(rgb_pixel.B,f_r);
            }

        fclose(f_r);
        return 0;    
}

猜你喜欢

转载自blog.csdn.net/gaopeng1111/article/details/94471944
今日推荐