Example #1
0
        private void LoadBitmap(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException();
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream does not support seeking.");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("The stream does not support reading.");
            }

            var reader = new BinaryReader(stream, Encoding.ASCII);

            // Read the header.
            var header = (DDSHEADER)reader.ReadStruct(typeof(DDSHEADER));

            if (!header.IsValid())
            {
                throw new ArgumentException("Invalid header. Stream does not appear to be a valid DDS-file.", nameof(stream));
            }

            _fourcc = DXT_FOURCC.Unknown;
            if ((header.surfaceDesc.ddpfPixelFormat.dwFlags & DDPF.RGB) == DDPF.RGB)
            {
                _bitmap = LoadRGB(header.surfaceDesc, stream);
            }
            else if ((header.surfaceDesc.ddpfPixelFormat.dwFlags & DDPF.FOURCC) == DDPF.FOURCC)
            {
                _fourcc = header.surfaceDesc.ddpfPixelFormat.dwFourCC;
                IDXTDecoder decoder;
                switch (header.surfaceDesc.ddpfPixelFormat.dwFourCC)
                {
                case DXT_FOURCC.DXT1:
                    decoder = new DXT1Decoder();
                    break;

                case DXT_FOURCC.DXT3:
                    decoder = new DXT3Decoder();
                    break;

                case DXT_FOURCC.DXT5:
                    decoder = new DXT5Decoder();
                    break;

                default:
                    throw new NotSupportedException(string.Format("FOURCC {0} is not supported.", header.surfaceDesc.ddpfPixelFormat.dwFourCC));
                }
                _bitmap = decoder.Decode(header.surfaceDesc, stream);
            }
        }
Example #2
0
        public async Task LoadFromStream(Stream stream, bool lzma = false)
        {
            var br = new ExtendedBinaryReader(stream);

            // DTX Header

            iResType = br.ReadUInt32();

            iVersion = br.ReadInt32();

            width  = br.ReadUInt16();
            height = br.ReadUInt16();

            nMipmap  = br.ReadUInt16();
            nSection = br.ReadUInt16();

            flags     = br.ReadInt32();
            userFlags = br.ReadInt32();

            extra = br.ReadBytes(12);

            cmdStr = br.ReadBytes(128);

            if (iResType != 0 || iVersion != -5 || nMipmap == 0)
            {
                if (lzma)
                {
                    throw new Exception("Unsupported DTX Type");
                }
                else
                {
                    stream.Position = 0;

                    var lzmaStream = new LzmaDecodeStream(stream);
                    var ms         = new MemoryStream();

                    lzmaStream.CopyTo(ms);

                    await LoadFromStream(ms, true);

                    return;
                }
            }

            DXTDecoder decoder = null;

            if (storeType == 4)
            {
                decoder = new DXT1Decoder();
            }
            else if (storeType == 5)
            {
                decoder = new DXT3Decoder();
            }
            else if (storeType == 6)
            {
                decoder = new DXT5Decoder();
            }

            if (decoder != null)
            {
                rawBuffer = await decoder.DecodeFrame(stream, width, height);
            }
            else
            {
                rawBuffer = br.ReadBytes(width * height * 4);
            }

            br.Close();
        }