예제 #1
0
        private Color[] GetPalette()
        {
            using (var reader = new EndiannessAwareBinaryReader(GetReadStream(), Endianness))
            {
                MagicBytes = reader.ReadBytes(4, ByteOrder.LittleEndian);

                if (!IsValidHPL)
                {
                    return(null);
                }

                if (!endiannessChecked)
                {
                    CheckEndianness(reader.ReadBytes(4));
                    reader.ChangeEndianness(Endianness);
                }
                else
                {
                    reader.BaseStream.Seek(4, SeekOrigin.Current);
                }

                FileLength = reader.ReadUInt32();
                ColorRange = reader.ReadUInt32();
                reader.BaseStream.Seek(16, SeekOrigin.Current);

                var colors = new Color[ColorRange];

                for (var i = 0; i < colors.Length; i++)
                {
                    var bytes = reader.ReadBytes(4);
                    colors[i] = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
                }

                return(colors);
            }
        }
예제 #2
0
        private void InitGetHeader(bool onlyHeader = false)
        {
            var stream = GetReadStream(onlyHeader);

            if (stream == null)
            {
                return;
            }
            using (stream)
            {
                if (FileLength < 32)
                {
                    return;
                }

                try
                {
                    using (var reader = new EndiannessAwareBinaryReader(stream, Endianness))
                    {
                        MagicBytes = reader.ReadBytes(4, ByteOrder.LittleEndian);
                        if (MagicBytes.SequenceEqual(new byte[] { 0x42, 0x43, 0x53, 0x4D }))
                        {
                            reader.BaseStream.Seek(12, SeekOrigin.Current);
                            Offset += 16;
                        }
                        else
                        {
                            reader.BaseStream.Seek(-4, SeekOrigin.Current);
                        }

                        if (Endianness == ByteOrder.LittleEndian && !endiannessChecked)
                        {
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var headersize = reader.ReadUInt32();
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var filecount = reader.ReadUInt32();
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var fileentrysize = reader.ReadUInt32() + 12;
                            var remainder     = fileentrysize % 16;
                            fileentrysize = fileentrysize + (remainder == 0 ? remainder : 16 - remainder);
                            var calcheadersize  = fileentrysize * filecount + 0x20;
                            var calcheadersize2 = (fileentrysize + 16) * filecount + 0x20;
                            if (calcheadersize != headersize && calcheadersize2 != headersize)
                            {
                                Endianness = ByteOrder.BigEndian;
                                reader.ChangeEndianness(Endianness);
                            }

                            stream.Seek(-24, SeekOrigin.Current);

                            endiannessChecked = true;
                        }

                        ReadHeaderInfo(reader);
                    }
                }
                catch
                {
                }
            }
        }