Use libyuv to convert yuv420p to RGB24, and the converted image turns blue

1.2k Views Asked by At

I want to convert the yuv420p image into RGB24 through libyuv, but the converted image is blue as a whole. I want to know why and go and correct it, thank you! Converted image

Original image

my code:

 const int width = 1280, height = 720;
FILE *src_file = fopen("1280x720.yuv", "rb");
FILE *dst_file = fopen("1280x720.rgb", "wb");

int size_src = width * height * 3 / 2;
int size_dest = width * height * 4;
char *buffer_src = (char *)malloc(size_src);
char *buffer_dest = (char *)malloc(size_dest);

uint64_t start_time = os_gettime_ns();
while (1)
{
    if (fread(buffer_src, 1, size_src, src_file) != size_src)
    {
        break;
    }
    libyuv::I420ToRGB24((const uint8*)buffer_src, width,
        (const uint8*)(buffer_src + width * height), width / 2,
        (const uint8*)(buffer_src + width * height * 5 / 4), width / 2,
        (uint8*)buffer_dest, width * 3,
        width, height);
    fwrite(buffer_dest, 1, size_dest, dst_file);
    fflush(dst_file);
}
uint64_t stop_time = os_gettime_ns();
printf("------ %ld \n", stop_time - start_time);

free(buffer_src);
free(buffer_dest);
//fclose(dst_file);
fclose(src_file);
return 0;
1

There are 1 best solutions below

2
On

Try it: Replace libyuv::I420ToRGB24 with libyuv::I420ToRAW.

Because the color layout in libyuv: RGB24 is B,G,R in memory. RAW is R,G,B in memory.