I'm trying to mirror libswscale PIX_FMT_YUYV422-type image horizontally. Using simple loop for each line with 16-bits per pixel results in having colors wrong, for example blue objects are orange. Here is my code:
typedef unsigned short YUVPixel; // 16 bits per pixel
for (int y = 0; y < outHeight; y++)
{
YUVPixel *p1 = (YUVPixel*)pBits + y * outWidth;
YUVPixel *p2 = p1 + outWidth - 1;
for (int x = 0; x < outWidth/2; x++) // outWidth is image width in pixels
{
// packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
unsigned short tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
Then I tried redefining YUVPixel as 32-bit type and modifying my loop accordingly. This results in correct colors, but looks like neighboring pixels are swapped. Any ideas, I'm totally lost with this?
Your approach using a 32bit YUVPixel type was good, you only have to make sure you swap the two Y values inside that pixel structure after moving it around, e.g.:
Y0 U Y1 V
move to new position
-------------------------------->
Y0 U Y1 V
<swap>
Y1 U Y0 V
The U and V values are both valid for the whole 2-pixel-structure, the Y values have to be flipped.