예제 #1
0
        protected void Parse(Stream s)
        {
            if (s == null || s.Length == 0)
            {
                this.data = null; return;
            }
            s.Position  = 0;
            this.header = new DDSHeader();
            this.header.Parse(s);
            if (header.pixelFormat.Fourcc != FourCC.DST1 &&
                header.pixelFormat.Fourcc != FourCC.DST3 &&
                header.pixelFormat.Fourcc != FourCC.DST5)
            {
                this.isShuffled = false;
            }
            else
            {
                this.isShuffled = true;
            }
            this.Width  = header.Width;
            this.Height = header.Height;

            BinaryReader r = new BinaryReader(s);

            s.Position = 0;
            this.data  = r.ReadBytes((int)s.Length);
        }
예제 #2
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();
            }
        }
예제 #3
0
        private static void Shuffle(DDSHeader header, Stream input, Stream output)
        {
            BinaryWriter w = new BinaryWriter(output);
            BinaryReader r = new BinaryReader(input);

            input.Position = 128;
            if (header.pixelFormat.Fourcc == FourCC.DST1)
            {
                using (MemoryStream block1 = new MemoryStream(), block2 = new MemoryStream())
                {
                    BinaryWriter w1 = new BinaryWriter(block1), w2 = new BinaryWriter(block2);
                    int          count = ((int)input.Length - 128) / 8;

                    for (int i = 0; i < count; i++)
                    {
                        w1.Write(r.ReadBytes(4));
                        w2.Write(r.ReadBytes(4));
                    }

                    w.Write(block1.ToArray());
                    w.Write(block2.ToArray());
                }
            }
            else if (header.pixelFormat.Fourcc == FourCC.DST3)
            {
                throw new Exception("No sample yet");
            }
            else if (header.pixelFormat.Fourcc == FourCC.DST5) // dst5
            {
                using (MemoryStream ms0 = new MemoryStream(), ms1 = new MemoryStream(), ms2 = new MemoryStream(), ms3 = new MemoryStream())
                {
                    BinaryWriter w0 = new BinaryWriter(ms0);
                    BinaryWriter w1 = new BinaryWriter(ms1);
                    BinaryWriter w2 = new BinaryWriter(ms2);
                    BinaryWriter w3 = new BinaryWriter(ms3);

                    int count = (int)(input.Length - 128) / 16;
                    for (int i = 0; i < count; i++)
                    {
                        w0.Write(r.ReadBytes(2));
                        w1.Write(r.ReadBytes(6));
                        w2.Write(r.ReadBytes(4));
                        w3.Write(r.ReadBytes(4));
                    }

                    w.Write(ms0.ToArray());
                    w.Write(ms2.ToArray());
                    w.Write(ms1.ToArray());
                    w.Write(ms3.ToArray());
                }
            }
        }
예제 #4
0
 protected void Parse(Stream s)
 {
     this.header = new DDSHeader();
     this.header.Parse(s);
     if (header.pixelFormat.Fourcc != FourCC.DST1 &&
         header.pixelFormat.Fourcc != FourCC.DST3 &&
         header.pixelFormat.Fourcc != FourCC.DST5)
     {
         throw new InvalidDataException("Texture does not need to be un-shuffled");
     }
     this.data = new byte[s.Length];
     s.Read(this.data, 0, (int)s.Length);
 }
예제 #5
0
 protected void Parse(Stream s)
 {
     this.header = new DDSHeader();
     this.header.Parse(s);
     if (header.pixelFormat.Fourcc != FourCC.DST1 &&
                  header.pixelFormat.Fourcc != FourCC.DST3 &&
                  header.pixelFormat.Fourcc != FourCC.DST5)
     {
         throw new InvalidDataException("Texture does not need to be un-shuffled");
     }
     this.data = new byte[s.Length];
     s.Read(this.data, 0, (int)s.Length);
 }
예제 #6
0
        protected void Parse(Stream s)
        {
            if (s == null || s.Length == 0) { this.data = null; return; }
            s.Position = 0;
            this.header = new DDSHeader();
            this.header.Parse(s);
            if (header.pixelFormat.Fourcc != FourCC.DST1 &&
                         header.pixelFormat.Fourcc != FourCC.DST3 &&
                         header.pixelFormat.Fourcc != FourCC.DST5)
            {
                this.isShuffled = false;
            }
            else
            {
                this.isShuffled = true;
            }
            this.Width = header.Width;
            this.Height = header.Height;

            BinaryReader r = new BinaryReader(s);
            s.Position = 0;
            this.data = r.ReadBytes((int)s.Length);
        }
예제 #7
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);
        }
예제 #8
0
 private static void Shuffle(DDSHeader header, Stream input, Stream output)
 {
     throw new NotImplementedException();
 }
예제 #9
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();
            }
            
        }
예제 #10
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;
        }
예제 #11
0
        private static void Shuffle(DDSHeader header, Stream input, Stream output)
        {
            BinaryWriter w = new BinaryWriter(output);
            BinaryReader r = new BinaryReader(input);
            input.Position = 128;
            if(header.pixelFormat.Fourcc == FourCC.DST1)
            {
                using(MemoryStream block1 = new MemoryStream(), block2 = new MemoryStream())
                {
                    BinaryWriter w1 = new BinaryWriter(block1), w2 = new BinaryWriter(block2);
                    int count = ((int)input.Length - 128) / 8;

                    for(int i = 0; i < count; i++)
                    {
                        w1.Write(r.ReadBytes(4));
                        w2.Write(r.ReadBytes(4));
                    }

                    w.Write(block1.ToArray());
                    w.Write(block2.ToArray());
                }
            }
            else if(header.pixelFormat.Fourcc == FourCC.DST3)
            {
                throw new Exception("No sample yet");
            }
            else if(header.pixelFormat.Fourcc == FourCC.DST5) // dst5
            {
                using(MemoryStream ms0 = new MemoryStream(), ms1 = new MemoryStream(), ms2 = new MemoryStream(), ms3 = new MemoryStream())
                {
                    BinaryWriter w0 = new BinaryWriter(ms0);
                    BinaryWriter w1 = new BinaryWriter(ms1);
                    BinaryWriter w2 = new BinaryWriter(ms2);
                    BinaryWriter w3 = new BinaryWriter(ms3);

                    int count = (int)(input.Length - 128) / 16;
                    for (int i = 0; i < count; i++)
                    {
                        w0.Write(r.ReadBytes(2));
                        w1.Write(r.ReadBytes(6));
                        w2.Write(r.ReadBytes(4));
                        w3.Write(r.ReadBytes(4));
                    }

                    w.Write(ms0.ToArray());
                    w.Write(ms2.ToArray());
                    w.Write(ms1.ToArray());
                    w.Write(ms3.ToArray());

                }
                
            }

        }
예제 #12
0
 private static void Shuffle(DDSHeader header, Stream input, Stream output)
 {
     throw new NotImplementedException();
 }