Ejemplo n.º 1
0
        public static byte[] DecodeDXT23(int Height, int Width, byte[] sourceData)
        {
            byte[]            destData = new byte[Height * Width * 4];
            Color.ColorRGBA[] color    = new Color.ColorRGBA[4];
            Color.ColorRGBA   CColor;
            int CData;
            int ChunksPerHLine = Width / 4;

            if (ChunksPerHLine == 0)
            {
                ChunksPerHLine += 1;
            }

            for (int i = 0; i <= (Width * Height) - 1; i += 16)
            {
                color[0] = Color.IntToRGBA(Convert.ToUInt32(sourceData[i + 8]) | Convert.ToUInt32(sourceData[i + 9]) << 8);
                color[1] = Color.IntToRGBA(Convert.ToUInt32(sourceData[i + 10]) | Convert.ToUInt32(sourceData[i + 11]) << 8);
                color[2] = Color.GradientColors(color[0], color[1]);
                color[3] = Color.GradientColors(color[1], color[0]);
                CData    = (Convert.ToInt32(sourceData[i + 12]) << 0) | (Convert.ToInt32(sourceData[i + 13]) << 8) | (Convert.ToInt32(sourceData[i + 14]) << 16) | (Convert.ToInt32(sourceData[i + 15]) << 24);
                int  ChunkNum = i / 16;
                long XPos     = ChunkNum % ChunksPerHLine;
                long YPos     = (ChunkNum - XPos) / ChunksPerHLine;
                long ttmp;
                int  alpha;
                int  sizeh = Height < 4 ? Height : 4;
                int  sizew = Width < 4 ? Width : 4;

                for (int x = 0; x <= sizeh - 1; x++)
                {
                    alpha = sourceData[i + (2 * x)] | Convert.ToInt32(sourceData[i + (2 * x) + 1]) << 8;
                    for (int y = 0; y <= sizew - 1; y++)
                    {
                        CColor             = color[CData & 3];
                        CData            >>= 2;
                        CColor.a           = (byte)((alpha & 15) * 16);
                        alpha            >>= 4;
                        ttmp               = ((YPos * 4 + x) * Width + XPos * 4 + y) * 4;
                        destData[ttmp]     = CColor.b;
                        destData[ttmp + 1] = CColor.g;
                        destData[ttmp + 2] = CColor.r;
                        destData[ttmp + 3] = CColor.a;
                    }
                }
            }
            return(destData);
        }
Ejemplo n.º 2
0
        public static byte[] DecodeDXT45(int Height, int Width, byte[] sourceData)
        {
            byte[]            destData = new byte[Height * Width * 4];
            Color.ColorRGBA[] color    = new Color.ColorRGBA[4];
            Color.ColorRGBA   CColor;
            int CData;
            int ChunksPerHLine = Width / 4;

            if (ChunksPerHLine == 0)
            {
                ChunksPerHLine += 1;
            }
            for (int i = 0; i <= (Width * Height) - 1; i += 16)
            {
                color[0] = Color.IntToRGBA(Convert.ToUInt32(sourceData[i + 8]) | Convert.ToUInt32(sourceData[i + 9]) << 8);
                color[1] = Color.IntToRGBA(Convert.ToUInt32(sourceData[i + 10]) | Convert.ToUInt32(sourceData[i + 11]) << 8);
                color[2] = Color.GradientColors(color[0], color[1]);
                color[3] = Color.GradientColors(color[1], color[0]);
                CData    = (Convert.ToInt32(sourceData[i + 12]) << 0) | (Convert.ToInt32(sourceData[i + 13]) << 8) | (Convert.ToInt32(sourceData[i + 14]) << 16) | (Convert.ToInt32(sourceData[i + 15]) << 24);
                byte[] alpha = new byte[8];
                alpha[0] = sourceData[i];
                alpha[1] = sourceData[i + 1];
                if ((alpha[0] > alpha[1]))
                {
                    alpha[2] = (byte)((6 * alpha[0] + 1 * alpha[1] + 3) / 7);
                    alpha[3] = (byte)((5 * alpha[0] + 2 * alpha[1] + 3) / 7);
                    alpha[4] = (byte)((4 * alpha[0] + 3 * alpha[1] + 3) / 7);
                    alpha[5] = (byte)((3 * alpha[0] + 4 * alpha[1] + 3) / 7);
                    alpha[6] = (byte)((2 * alpha[0] + 5 * alpha[1] + 3) / 7);
                    alpha[7] = (byte)((1 * alpha[0] + 6 * alpha[1] + 3) / 7);
                }
                else
                {
                    alpha[2] = (byte)((4 * alpha[0] + 1 * alpha[1] + 2) / 5);
                    alpha[3] = (byte)((3 * alpha[0] + 2 * alpha[1] + 2) / 5);
                    alpha[4] = (byte)((2 * alpha[0] + 3 * alpha[1] + 2) / 5);
                    alpha[5] = (byte)((1 * alpha[0] + 4 * alpha[1] + 2) / 5);
                    alpha[6] = 0;
                    alpha[7] = 255;
                }
                long tmpdword;
                int  tmpword;
                long alphaDat;
                tmpword  = sourceData[i + 2] | (Convert.ToInt32(sourceData[i + 3]) << 8);
                tmpdword = sourceData[i + 4] | (Convert.ToInt32(sourceData[i + 5]) << 8) | (sourceData[i + 6] << 16) | (Convert.ToInt32(sourceData[i + 7]) << 24);
                alphaDat = tmpword | (tmpdword << 16);
                int  ChunkNum = i / 16;
                long XPos     = ChunkNum % ChunksPerHLine;
                long YPos     = (ChunkNum - XPos) / ChunksPerHLine;
                long ttmp;
                int  sizeh = Height < 4 ? Height : 4;
                int  sizew = Width < 4 ? Width : 4;
                for (int x = 0; x <= sizeh - 1; x++)
                {
                    for (int y = 0; y <= sizew - 1; y++)
                    {
                        CColor             = color[CData & 3];
                        CData            >>= 2;
                        CColor.a           = alpha[alphaDat & 7];
                        alphaDat         >>= 3;
                        ttmp               = ((YPos * 4 + x) * Width + XPos * 4 + y) * 4;
                        destData[ttmp]     = CColor.b;
                        destData[ttmp + 1] = CColor.g;
                        destData[ttmp + 2] = CColor.r;
                        destData[ttmp + 3] = CColor.a;
                    }
                }
            }
            return(destData);
        }
Ejemplo n.º 3
0
        public static byte[] DecodeDXT1(int Height, int Width, byte[] sourceData)
        {
            byte[]            destData = new byte[Height * Width * 4];
            Color.ColorRGBA[] color    = new Color.ColorRGBA[4];
            int dptr = 0;

            Color.ColorRGBA CColor;
            int             CData;
            int             ChunksPerHLine = Width / 4;
            bool            trans;

            Color.ColorRGBA zeroColor = new Color.ColorRGBA();
            uint            c1;
            uint            c2;

            if (ChunksPerHLine == 0)
            {
                ChunksPerHLine += 1;
            }
            for (int i = 0; i < (Width * Height); i += 16)
            {
                c1 = (Convert.ToUInt32(sourceData[dptr + 1]) << 8) | (sourceData[dptr]);
                c2 = (Convert.ToUInt32(sourceData[dptr + 3]) << 8) | (sourceData[dptr + 2]);
                if (c1 > c2)
                {
                    trans = false;
                }
                else
                {
                    trans = true;
                }
                color[0] = Color.IntToRGBA(c1);
                color[1] = Color.IntToRGBA(c2);
                if (!(trans))
                {
                    color[2] = Color.GradientColors(color[0], color[1]);
                    color[3] = Color.GradientColors(color[1], color[0]);
                }
                else
                {
                    color[2] = Color.GradientColorsHalf(color[0], color[1]);
                    color[3] = zeroColor;
                }
                CData = (Convert.ToInt32(sourceData[dptr + 4]) << 0) | (Convert.ToInt32(sourceData[dptr + 5]) << 8) | (Convert.ToInt32(sourceData[dptr + 6]) << 16) | (Convert.ToInt32(sourceData[dptr + 7]) << 24);
                int  ChunkNum = i / 16;
                long XPos     = ChunkNum % ChunksPerHLine;
                long YPos     = (ChunkNum - XPos) / ChunksPerHLine;
                long ttmp;
                int  sizeh = Height < 4 ? Height : 4;
                int  sizew = Width < 4 ? Width : 4;
                for (int x = 0; x <= sizeh - 1; x++)
                {
                    for (int y = 0; y <= sizew - 1; y++)
                    {
                        CColor             = color[CData & 3];
                        CData            >>= 2;
                        ttmp               = ((YPos * 4 + x) * Width + XPos * 4 + y) * 4;
                        destData[ttmp]     = CColor.b;
                        destData[ttmp + 1] = CColor.g;
                        destData[ttmp + 2] = CColor.r;
                        destData[ttmp + 3] = CColor.a;
                    }
                }
                dptr += 8;
            }
            return(destData);
        }