コード例 #1
0
ファイル: DdsImage.cs プロジェクト: skwasjer/skwas.Drawing
        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);
            }
        }