0

I am converting a YUV422 frame to a PPM file - (RGB888).

The image is converting mostly. Colors are fine. But each seems row is askew, as if extra

Here is the frame conversion function:

BYTE* CYUV::toRGB()
{
    BYTE*   rgb = new BYTE[m_width*m_height*3];
    BYTE*   rgb_ret = rgb;
    BYTE*   p_yuv = m_yuv_frame;

    for (int i=m_width*m_height/2; i--;)
    {
        BYTE    y, u, v, y2;
        BYTE    r,g, b;

        y2 = *p_yuv++;
        v = *p_yuv++;
        y = *p_yuv++;
        u = *p_yuv++;

        r = (y + 1.370705 * (v - 128));
        g = (y - (0.698001 * (v - 128)) - (0.337633 * (u - 128)));
        b = (y + (1.732446 * (u - 128)));
        *rgb++ = b < 0 ? 0 : b > 255 ? 255 : b;
        *rgb++ = g < 0 ? 0 : g > 255 ? 255 : g;
        *rgb++ = r < 0 ? 0 : r > 255 ? 255 : r;


        r = (y2 + 1.370705 * (v - 128));
        g = (y2 - (0.698001 * (v - 128)) - (0.337633 * (u - 128)));
        b = (y2 + (1.732446 * (u - 128)));
        *rgb++ = b < 0 ? 0 : b > 255 ? 255 : b;
        *rgb++ = g < 0 ? 0 : g > 255 ? 255 : g;
        *rgb++ = r < 0 ? 0 : r > 255 ? 255 : r;


    }
    return rgb_ret;
}

I believe the actual yuv-to-rgb pixel conversion is correct, because I had tried many other formulae, with color distorted results.

As for the PPM file, that is good also, since all image readers handle it.

As for the original YUV4222 frame, it to is fine - I display it using SDL without this distortion.

Any suggestions?

TIA

ken

user3182551
  • 343
  • 2
  • 9

1 Answers1

2

OK: Solved this.

It was simple enough: write out the y converted pixel BEFORE the y2.

There were SO many solutions out there, but few seem completely correct.

FYI: This DOES work, but only for a yuv 4:2:2 format. This seems to be the most common one with the fourcc label of YUYV. yuv 4:2:0 is much more complex to convert. yuv 4:4:4 should be the same as this solution fetching only 1 y component, and writing out only one rgb triplet. (can't verify that, but I that was the comment along with this solution.

Hope this helps someone.

ken

user3182551
  • 343
  • 2
  • 9