AoMBrgEditor.DdtFile.ConvertToRGB C# (CSharp) Method

ConvertToRGB() private static method

private static ConvertToRGB ( byte data, int startData, System.Pixel palette, int rOff, int rCnt, int gOff, int gCnt, int bOff, int bCnt, int stride ) : void
data byte
startData int
palette System.Pixel
rOff int
rCnt int
gOff int
gCnt int
bOff int
bCnt int
stride int
return void
        private static void ConvertToRGB(byte[] data, int startData, Pixel[] palette, int rOff, int rCnt, int gOff, int gCnt, int bOff, int bCnt, int stride)
        {
            int rMask = ((1 << rCnt) - 1);
            int gMask = ((1 << gCnt) - 1);
            int bMask = ((1 << bCnt) - 1);
            for (int c = 0; c < palette.Length; ++c)
            {
                int cBit = startData * 8 + c * stride;
                int dByte = cBit / 8;
                // Will be such that the color starts at 24th bit
                int b1 = data[dByte + 0];
                int b2 = (dByte + 1 < data.Length ? data[dByte + 1] : 0);
                int b3 = 0;// (dByte + 2 < data.Length ? data[dByte + 2] : 0);
                if (rCnt == 4)
                {
                    palette[c] = new Pixel()
                    {
                        R = (byte)((b1 << 4) & 0xf0),
                        G = (byte)((b1 << 0) & 0xf0),
                        B = (byte)((b2 << 4) & 0xf0),
                        A = 255,
                    };
                }
                else
                {
                    int dataV = ((((b3 << 8) + b2) << 8) + b1) << (cBit - dByte * 8);
                    int r = (((dataV << rOff) >> (16 - rCnt)) & rMask) * 255 / rMask;
                    int g = (((dataV << gOff) >> (16 - gCnt)) & gMask) * 255 / gMask;
                    int b = (((dataV << bOff) >> (16 - bCnt)) & bMask) * 255 / bMask;
                    palette[c] = new Pixel()
                    {
                        R = (byte)r,
                        G = (byte)g,
                        B = (byte)b,
                        A = 255,
                    };
                    if (rCnt == 5 && gCnt == 5 && bCnt == 5)
                    {
                        palette[c].A = ((dataV & 0x8000) == 0 ? (byte)0 : (byte)255);
                    }
                }
            }
        }