yuenshome / yuenshome.github.io

https://yuenshome.github.io
MIT License
81 stars 15 forks source link

yuv #126

Open ysh329 opened 3 years ago

ysh329 commented 3 years ago

    unsigned int mapSize = srcWidth * srcHeight;

    for (int posX = 0; posX < width; ++posX) {
        for (int posY = 0; posY < height; ++posY) {
            unsigned int posSrc = posY * srcWidth * 3 + posX * 3;
            unsigned int posDst = posY / 2 * srcWidth + posX;

            int B = sourceImage[posSrc];
            int G = sourceImage[posSrc + 1];
            int R = sourceImage[posSrc + 2];

            int Y = (77 * R + 150 * G + 29 * B) >> 8;
            int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128;
            int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128;

            destImage[posY * srcWidth + posX] = Y;
            destImage[mapSize + posDst / 2] = U;
            destImage[mapSize * 5 / 4 + posDst / 2] = V;
        }
    }