Beispiel #1
0
        public void ImportToDST(Stream input)
        {
            var header = new DDSHeader();

            header.Parse(input);
            switch (header.pixelFormat.Fourcc)
            {
            case FourCC.DXT1:
                header.pixelFormat.Fourcc = FourCC.DST1;
                break;

            case FourCC.DXT3:
                throw new Exception("No sample yet");

            case FourCC.DXT5:
                header.pixelFormat.Fourcc = FourCC.DST5;
                break;

            case FourCC.DST1:
                this.isShuffled = true;
                break;

            case FourCC.DST3:
                this.isShuffled = true;
                break;

            case FourCC.DST5:
                this.isShuffled = true;
                break;

            default:
                throw new Exception("Not supported format. Read " + header.pixelFormat.Fourcc.ToString());
            }

            if (this.isShuffled)
            {
                input.Position = 0;
                BinaryReader r = new BinaryReader(input);
                this.data = r.ReadBytes((int)input.Length);
                return;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter w = new BinaryWriter(ms);
                w.Write(DDSHeader.Signature);
                header.UnParse(ms);
                this.header = header;
                this.Width  = this.header.Width;
                this.Height = this.header.Height;
                Shuffle(this.header, input, ms);
                this.data = ms.ToArray();
            }
        }
Beispiel #2
0
        private Stream Unshuffle(DDSHeader header, Stream s)
        {
            const int dataOffset = 128;
            var       dataSize   = (int)(s.Length - dataOffset);

            s.Position = dataOffset;
            BinaryReader r = new BinaryReader(s);

            MemoryStream result = new MemoryStream();
            BinaryWriter w      = new BinaryWriter(result);
            var          temp   = r.ReadBytes(dataSize);

            if (header.pixelFormat.Fourcc == FourCC.DST1)
            {
                header.pixelFormat.Fourcc = FourCC.DXT1;
                header.UnParse(result);

                var blockOffset2 = 0;
                var blockOffset3 = blockOffset2 + (dataSize >> 1);

                // probably a better way to do this
                var count = (blockOffset3 - blockOffset2) / 4;

                for (int i = 0; i < count; i++)
                {
                    result.Write(temp, blockOffset2, 4);
                    result.Write(temp, blockOffset3, 4);
                    blockOffset2 += 4;
                    blockOffset3 += 4;
                }
            }
            else if (header.pixelFormat.Fourcc == FourCC.DST3) // DST3
            {
                header.pixelFormat.Fourcc = FourCC.DXT3;
                header.UnParse(result);

                var blockOffset0 = 0;
                var blockOffset2 = blockOffset0 + (dataSize >> 1);
                var blockOffset3 = blockOffset2 + (dataSize >> 2);

                throw new NotImplementedException("no samples");
            }
            else if (header.pixelFormat.Fourcc == FourCC.DST5)  // DST5
            {
                header.pixelFormat.Fourcc = FourCC.DXT5;
                header.UnParse(result);

                var blockOffset0 = 0;
                var blockOffset2 = blockOffset0 + (dataSize >> 3);
                var blockOffset1 = blockOffset2 + (dataSize >> 2);
                var blockOffset3 = blockOffset1 + (6 * dataSize >> 4);

                // probably a better way to do this
                var count = (blockOffset2 - blockOffset0) / 2;

                for (int i = 0; i < count; i++)
                {
                    result.Write(temp, blockOffset0, 2);
                    result.Write(temp, blockOffset1, 6);
                    result.Write(temp, blockOffset2, 4);
                    result.Write(temp, blockOffset3, 4);

                    blockOffset0 += 2;
                    blockOffset1 += 6;
                    blockOffset2 += 4;
                    blockOffset3 += 4;
                }
            }
            result.Position = 0;
            return(result);
        }
Beispiel #3
0
        public void ImportToDST(Stream input)
        {
            var header = new DDSHeader();
            header.Parse(input);
            switch (header.pixelFormat.Fourcc)
            {
                case FourCC.DXT1:
                    header.pixelFormat.Fourcc = FourCC.DST1;
                    break;
                case FourCC.DXT3:
                    throw new Exception("No sample yet");
                case FourCC.DXT5:
                    header.pixelFormat.Fourcc = FourCC.DST5;
                    break;
                case FourCC.DST1:
                    this.isShuffled = true;
                    break;
                case FourCC.DST3:
                    this.isShuffled = true;
                    break;
                case FourCC.DST5:
                    this.isShuffled = true;
                    break;
                default:
                    throw new Exception("Not supported format. Read " + header.pixelFormat.Fourcc.ToString());
            }

            if(this.isShuffled)
            {
                input.Position = 0;
                BinaryReader r = new BinaryReader(input);
                this.data = r.ReadBytes((int)input.Length);
                return;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter w = new BinaryWriter(ms);
                w.Write(DDSHeader.Signature);
                header.UnParse(ms);
                this.header = header;
                this.Width = this.header.Width;
                this.Height = this.header.Height;
                Shuffle(this.header, input, ms);
                this.data = ms.ToArray();
            }
            
        }
Beispiel #4
0
        private Stream Unshuffle(DDSHeader header, Stream s)
        {
            const int dataOffset = 128;
            var dataSize = (int)(s.Length - dataOffset);

            s.Position = dataOffset;
            BinaryReader r = new BinaryReader(s);

            MemoryStream result = new MemoryStream();
            BinaryWriter w = new BinaryWriter(result);
            var temp = r.ReadBytes(dataSize);

            if (header.pixelFormat.Fourcc == FourCC.DST1)
            {
                header.pixelFormat.Fourcc = FourCC.DXT1;
                w.Write(0x20534444); // DDS header
                header.UnParse(result);

                var blockOffset2 = 0;
                var blockOffset3 = blockOffset2 + (dataSize >> 1);

                // probably a better way to do this
                var count = (blockOffset3 - blockOffset2) / 4;

                for (int i = 0; i < count; i++)
                {
                    result.Write(temp, blockOffset2, 4);
                    result.Write(temp, blockOffset3, 4);
                    blockOffset2 += 4;
                    blockOffset3 += 4;
                }
            }
            else if (header.pixelFormat.Fourcc == FourCC.DST3) // DST3
            {
                header.pixelFormat.Fourcc = FourCC.DXT3;
                w.Write(0x20534444);
                header.UnParse(result);

                var blockOffset0 = 0;
                var blockOffset2 = blockOffset0 + (dataSize >> 1);
                var blockOffset3 = blockOffset2 + (dataSize >> 2);

                throw new NotImplementedException("no samples");
            }
            else if (header.pixelFormat.Fourcc ==  FourCC.DST5) // DST5
            {
                header.pixelFormat.Fourcc = FourCC.DXT5;
                w.Write(0x20534444);
                header.UnParse(result);

                var blockOffset0 = 0;
                var blockOffset2 = blockOffset0 + (dataSize >> 3);
                var blockOffset1 = blockOffset2 + (dataSize >> 2);
                var blockOffset3 = blockOffset1 + (6 * dataSize >> 4);

                // probably a better way to do this
                var count = (blockOffset2 - blockOffset0) / 2;

                for (int i = 0; i < count; i++)
                {
                    result.Write(temp, blockOffset0, 2);
                    result.Write(temp, blockOffset1, 6);
                    result.Write(temp, blockOffset2, 4);
                    result.Write(temp, blockOffset3, 4);

                    blockOffset0 += 2;
                    blockOffset1 += 6;
                    blockOffset2 += 4;
                    blockOffset3 += 4;
                }
            }
            result.Position = 0;
            return result;
        }