Exemple #1
0
        public void Deserialize(Stream input)
        {
            if (input.ReadValueU32() != 0x39444350)
            {
                throw new FormatException();
            }

            this.Format = input.ReadValueEnum <PCD9.Format>();
            var dataSize = input.ReadValueU32();

            this.Unknown0C = input.ReadValueU32();
            this.Width     = input.ReadValueU16();
            this.Height    = input.ReadValueU16();
            this.BPP       = input.ReadValueU8();
            var mipMapCount = 1 + input.ReadValueU8();

            this.Unknown16 = input.ReadValueU16();
            if ((this.Unknown16 & 0x8000) != 0)
            {
                throw new NotSupportedException();
                this.unknownFlag = true;
            }

            this.Mipmaps.Clear();
            using (var data = input.ReadToMemoryStream(dataSize))
            {
                var mipWidth  = this.Width;
                var mipHeight = this.Height;

                for (int i = 0; i < mipMapCount; i++)
                {
                    if (mipWidth == 0)
                    {
                        mipWidth = 1;
                    }

                    if (mipHeight == 0)
                    {
                        mipHeight = 1;
                    }

                    int size;
                    switch (this.Format)
                    {
                    case PCD9.Format.A8R8G8B8:
                    {
                        size = mipWidth * mipHeight * 4;
                        break;
                    }

                    case PCD9.Format.DXT1:
                    case PCD9.Format.DXT3:
                    case PCD9.Format.DXT5:
                    {
                        int blockCount = ((mipWidth + 3) / 4) * ((mipHeight + 3) / 4);
                        int blockSize  = this.Format == PCD9.Format.DXT1 ? 8 : 16;
                        size = blockCount * blockSize;
                        break;
                    }

                    default:
                    {
                        throw new NotSupportedException();
                    }
                    }

                    var buffer = new byte[size];
                    if (data.Read(buffer, 0, buffer.Length) != buffer.Length)
                    {
                        throw new EndOfStreamException();
                    }

                    this.Mipmaps.Add(new PCD9.Mipmap()
                    {
                        Width  = mipWidth,
                        Height = mipHeight,
                        Data   = buffer,
                    });

                    mipWidth  >>= 1;
                    mipHeight >>= 1;
                }

                if (data.Position != data.Length)
                {
                    throw new InvalidOperationException();
                }
            }
        }
Exemple #2
0
        public void Deserialize(Stream input)
        {
            if (input.ReadValueU32() != 0x39444350)
            {
                throw new FormatException();
            }

            this.Format = input.ReadValueEnum<PCD9.Format>();
            var dataSize = input.ReadValueU32();
            this.Unknown0C = input.ReadValueU32();
            this.Width = input.ReadValueU16();
            this.Height = input.ReadValueU16();
            this.BPP = input.ReadValueU8();
            var mipMapCount = 1 + input.ReadValueU8();
            this.Unknown16 = input.ReadValueU16();
            if ((this.Unknown16 & 0x8000) != 0)
            {
                throw new NotSupportedException();
                this.unknownFlag = true;
            }

            this.Mipmaps.Clear();
            using (var data = input.ReadToMemoryStream(dataSize))
            {
                var mipWidth = this.Width;
                var mipHeight = this.Height;

                for (int i = 0; i < mipMapCount; i++)
                {
                    if (mipWidth == 0)
                    {
                        mipWidth = 1;
                    }

                    if (mipHeight == 0)
                    {
                        mipHeight = 1;
                    }

                    int size;
                    switch (this.Format)
                    {
                        case PCD9.Format.A8R8G8B8:
                        {
                            size = mipWidth * mipHeight * 4;
                            break;
                        }

                        case PCD9.Format.DXT1:
                        case PCD9.Format.DXT3:
                        case PCD9.Format.DXT5:
                        {
                            int blockCount = ((mipWidth + 3) / 4) * ((mipHeight + 3) / 4);
                            int blockSize = this.Format == PCD9.Format.DXT1 ? 8 : 16;
                            size = blockCount * blockSize;
                            break;
                        }

                        default:
                        {
                            throw new NotSupportedException();
                        }
                    }

                    var buffer = new byte[size];
                    if (data.Read(buffer, 0, buffer.Length) != buffer.Length)
                    {
                        throw new EndOfStreamException();
                    }

                    this.Mipmaps.Add(new PCD9.Mipmap()
                        {
                            Width = mipWidth,
                            Height = mipHeight,
                            Data = buffer,
                        });

                    mipWidth >>= 1;
                    mipHeight >>= 1;
                }

                if (data.Position != data.Length)
                {
                    throw new InvalidOperationException();
                }
            }
        }