Exemple #1
0
        RasterImage GetDXT1RasterImage()
        {
            //open stream and read file data
            MemoryStream mStream = new MemoryStream(FileData);

            mStream.ReadBytes(6);                   //skip
            byte compression = mStream.ReadByte();  //Compression Type

            if (compression == 4)                   //DXT1
            {
                int mipLevels = mStream.ReadByte(); //number of Miplevels
                //get header values
                int imageWidth  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int offset      = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int dataLenght  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);

                //Get pixeldata
                mStream.SetPosition(offset);
                RasterImage image   = new RasterImage(imageWidth, imageHeight);
                int         xOffset = 0;
                int         yOffset = 0;
                for (int i = 0; i < dataLenght; i += 8)
                {
                    //Get 4 colors
                    UInt16  iC0    = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c0 5:6:5 RGB
                    UInt16  iC1    = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c1 5:6:5 RGB
                    Color[] colors = GetColorsFromDXTFormat(iC0, iC1, true);         //Calculate the 4 colors

                    //Get 4x4 Pixelblock and set Pixels in bitmap
                    for (int y = 0; y < 4; y++)
                    {
                        List <byte> pointerBits = new List <byte>(ByteAnalyzer.GetBitsFromNumber((int)mStream.ReadByte()));//byte holding 4 2bit Color pointers
                        for (int x = 0; x < 4; x++)
                        {
                            int colorPointer = ByteAnalyzer.GetIntFromBits(pointerBits.GetRange(x * 2, 2).ToArray());//Pointer for pixel
                            image.SetPixel(xOffset + x, yOffset + y, colors[colorPointer]);
                        }
                    }

                    // set xOffset and yOffset values
                    if (xOffset + 4 < imageWidth)
                    {
                        xOffset += 4;
                    }
                    else
                    {
                        xOffset  = 0;
                        yOffset += 4;
                    }
                }
                return(image);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        RasterImage GetUncompressedRasterImage()
        {
            //open stream and read file data
            MemoryStream mStream = new MemoryStream(FileData);

            mStream.ReadBytes(6);                     //skip
            byte compression = mStream.ReadByte();    //Compression Type

            if (compression == 1)                     //uncompressed
            {
                int mipLevels   = mStream.ReadByte(); //number of Miplevels
                int imageWidth  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int offset      = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int dataLenght  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);

                //Get pixeldata
                mStream.SetPosition(offset);
                RasterImage image = new RasterImage(imageWidth, imageHeight);
                for (int y = 0; y < imageHeight; y++)
                {
                    for (int x = 0; x < imageWidth; x++)
                    {
                        int B = mStream.ReadByte();
                        int G = mStream.ReadByte();
                        int R = mStream.ReadByte();
                        int A = mStream.ReadByte();
                        image.SetPixel(x, y, Color.FromArgb(A, R, G, B));
                    }
                }
                return(image);
            }
            else
            {
                return(null);
            }
        }
Exemple #3
0
        RasterImage GetDXT3RasterImage()
        {
            //open stream and read file data
            MemoryStream mStream = new MemoryStream(FileData);

            mStream.ReadBytes(6);                   //skip
            byte compression = mStream.ReadByte();  //Compression Type

            if (compression == 8)                   //DXT3
            {
                int mipLevels = mStream.ReadByte(); //number of Miplevels
                //get header values
                int imageWidth  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int offset      = BitConverter.ToInt32(mStream.ReadBytes(4), 0);
                int dataLenght  = BitConverter.ToInt32(mStream.ReadBytes(4), 0);

                //Get pixeldata
                mStream.SetPosition(offset);
                RasterImage image   = new RasterImage(imageWidth, imageHeight);
                int         xOffset = 0;
                int         yOffset = 0;
                for (int i = 0; i < dataLenght; i += 16)
                {
                    //Get Alpha values
                    List <byte> alphaBytes = new List <byte>(mStream.ReadBytes(8));

                    //Get 4 colors
                    UInt16  iC0    = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c0 5:6:5 RGB
                    UInt16  iC1    = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c1 5:6:5 RGB
                    Color[] colors = GetColorsFromDXTFormat(iC0, iC1, false);        //Calculate the 4 colors

                    //Get 4x4 Pixelblock and set Pixels in bitmap
                    for (int y = 0; y < 4; y++)
                    {
                        List <byte> pointerBits = new List <byte>(ByteAnalyzer.GetBitsFromNumber((int)mStream.ReadByte())); //Convert byte holding 4 2bit Color pointers to bit list
                        List <byte> alphaBits   = new List <byte>(ByteAnalyzer.GetBitsFromNumber((alphaBytes[y * 2])));     //Convert bytes holding 2 4bit Alpha value each, to bit list
                        alphaBits.AddRange(ByteAnalyzer.GetBitsFromNumber(alphaBytes[y * 2 + 1]));                          //Add secon Byte-bits

                        for (int x = 0; x < 4; x++)
                        {
                            int   colorPointer = ByteAnalyzer.GetIntFromBits(pointerBits.GetRange(x * 2, 2).ToArray());                         //Pointer for pixel
                            byte  alpha        = (byte)((float)ByteAnalyzer.GetIntFromBits(alphaBits.GetRange(x * 4, 4).ToArray()) / 15 * 255); //Calulate alpha value byte
                            Color color        = Color.FromArgb(alpha, colors[colorPointer].R, colors[colorPointer].G, colors[colorPointer].B);

                            image.SetPixel(xOffset + x, yOffset + y, color);
                        }
                    }

                    // set xOffset and yOffset values
                    if (xOffset + 4 < imageWidth)
                    {
                        xOffset += 4;
                    }
                    else
                    {
                        xOffset  = 0;
                        yOffset += 4;
                    }
                }
                return(image);
            }
            else
            {
                return(null);
            }
        }