コード例 #1
0
ファイル: AudioWWA.cs プロジェクト: tenyuhuang/GARbro
        public WwaInput(Stream file, int count, int dir_size) : base(null)
        {
            file.Position = 0x10;
            var header = new byte[count * dir_size];

            if (header.Length != file.Read(header, 0, header.Length))
            {
                throw new InvalidFormatException();
            }

            var section = WpxSection.Find(header, 0x20, count, dir_size);

            if (null == section || section.UnpackedSize < 0x10 || section.DataFormat != 0x80)
            {
                throw new InvalidFormatException();
            }
            file.Position = section.Offset;
            Format        = ReadFormat(file);

            section = WpxSection.Find(header, 0x21, count, dir_size);
            if (null == section)
            {
                throw new InvalidFormatException();
            }

            var reader = new WwaReader(file, section);
            var data   = reader.Unpack(section.DataFormat);

            if (null == data)
            {
                throw new InvalidFormatException();
            }
            Source = new MemoryStream(data);
            file.Dispose();
        }
コード例 #2
0
        }                                                                 // 'WPX'

        public override ImageMetaData ReadMetaData(Stream stream)
        {
            byte[] header = new byte[0x10];
            if (header.Length != stream.Read(header, 0, header.Length))
            {
                return(null);
            }
            if (!Binary.AsciiEqual(header, 4, "BMP"))
            {
                return(null);
            }
            int count    = header[0xE];
            int dir_size = header[0xF];

            if (1 != header[0xC] || 0 == count || 0 == dir_size)
            {
                return(null);
            }
            header = new byte[count * dir_size];
            if (header.Length != stream.Read(header, 0, header.Length))
            {
                return(null);
            }
            var section = WpxSection.Find(header, 0x10, count, dir_size);

            if (null == section)
            {
                return(null);
            }
            if (section.UnpackedSize < 0x10)
            {
                return(null);
            }

            stream.Seek(section.Offset, SeekOrigin.Begin);
            byte[] data = new byte[section.UnpackedSize];
            if (data.Length != stream.Read(data, 0, data.Length))
            {
                return(null);
            }

            return(new WbmMetaData
            {
                Width = LittleEndian.ToUInt16(data, 4),
                Height = LittleEndian.ToUInt16(data, 6),
                BPP = data[0xC],
                EntryCount = count,
                EntrySize = dir_size,
                Header = header,
            });
        }
コード例 #3
0
        }                                                                 // 'WPX'

        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            var header = stream.ReadHeader(0x10);

            if (!header.AsciiEqual(4, "BMP"))
            {
                return(null);
            }
            int count    = header[0xE];
            int dir_size = header[0xF];

            if (1 != header[0xC] || 0 == count || 0 == dir_size)
            {
                return(null);
            }
            var dir     = stream.ReadBytes(count * dir_size);
            var section = WpxSection.Find(dir, 0x10, count, dir_size);

            if (null == section)
            {
                return(null);
            }
            if (section.UnpackedSize < 0x10)
            {
                return(null);
            }

            stream.Position = section.Offset;
            var data = stream.ReadBytes(section.UnpackedSize);

            if (data.Length != section.UnpackedSize)
            {
                return(null);
            }

            return(new WbmMetaData
            {
                Width = LittleEndian.ToUInt16(data, 4),
                Height = LittleEndian.ToUInt16(data, 6),
                BPP = data[0xC],
                EntryCount = count,
                EntrySize = dir_size,
                Header = dir,
            });
        }
コード例 #4
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var meta = (WbmMetaData)info;

            var section = WpxSection.Find(meta.Header, 0x11, meta.EntryCount, meta.EntrySize);

            if (null == section)
            {
                throw new InvalidFormatException();
            }

            PixelFormat format;
            int         pixel_size;

            switch (meta.BPP)
            {
            case 24:
                format     = PixelFormats.Bgr24;
                pixel_size = 3;
                break;

            case 32:
                format     = PixelFormats.Bgr32;
                pixel_size = 4;
                break;

            case 16:
                format     = PixelFormats.Bgr555;
                pixel_size = 2;
                break;

            case 8:
                format     = PixelFormats.Indexed8;
                pixel_size = 1;
                break;

            default:
                throw new NotSupportedException("Not supported WBM bitdepth");
            }
            int stride = ((int)meta.Width * pixel_size + 3) & -4;
            var reader = new WbmReader(stream, section);
            var pixels = reader.Unpack(stride, pixel_size, section.DataFormat);

            if (null == pixels)
            {
                throw new InvalidFormatException();
            }

            if (8 == meta.BPP)
            {
                section = WpxSection.Find(meta.Header, 0x12, meta.EntryCount, meta.EntrySize);
                if (null == section)
                {
                    return(ImageData.Create(info, PixelFormats.Gray8, null, pixels, stride));
                }
                reader = new WbmReader(stream, section);
                var palette_data = reader.Unpack(48, 3, section.DataFormat);
                var palette      = CreatePalette(palette_data);
                return(ImageData.Create(info, PixelFormats.Indexed8, palette, pixels, stride));
            }

            if (meta.BPP < 24)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }
            section = WpxSection.Find(meta.Header, 0x13, meta.EntryCount, meta.EntrySize);
            if (null == section)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }

            int alpha_stride = ((int)meta.Width + 3) & -4;

            byte[] alpha = null;
            try
            {
                reader = new WbmReader(stream, section);
                alpha  = reader.Unpack(alpha_stride, 1, section.DataFormat);
            }
            catch { }
            if (null == alpha)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }

            byte[] alpha_image = new byte[4 * meta.Width * meta.Height];
            int    dst         = 0;

            for (int y = 0; y < meta.Height; ++y)
            {
                int alpha_src = y * alpha_stride;
                int src       = y * stride;
                for (int x = 0; x < meta.Width; ++x)
                {
                    alpha_image[dst++] = pixels[src];
                    alpha_image[dst++] = pixels[src + 1];
                    alpha_image[dst++] = pixels[src + 2];
                    alpha_image[dst++] = alpha[alpha_src + x];
                    src += pixel_size;
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, alpha_image, (int)meta.Width * 4));
        }
コード例 #5
0
ファイル: AudioWWA.cs プロジェクト: uboaa/GARbro
        }                                                                 // 'WPX'

        public override SoundInput TryOpen(IBinaryStream file)
        {
            var header = file.ReadHeader(0x10);

            if (!header.AsciiEqual(4, "WAV"))
            {
                return(null);
            }
            int count    = header[0xE];
            int dir_size = header[0xF];

            if (1 != header[0xC] || 0 == count || 0 == dir_size)
            {
                return(null);
            }

            file.Position = 0x10;
            var index = file.ReadBytes(count * dir_size);

            var section = WpxSection.Find(index, 0x20, count, dir_size);

            if (null == section || section.UnpackedSize < 0x10 || section.DataFormat != 0x80)
            {
                throw new InvalidFormatException();
            }
            file.Position = section.Offset;
            var fmt = file.ReadBytes(section.UnpackedSize);

            section = WpxSection.Find(index, 0x21, count, dir_size);
            if (null == section)
            {
                throw new InvalidFormatException();
            }

            var reader = new WwaReader(file.AsStream, section);
            var data   = reader.Unpack(section.DataFormat);

            if (null == data)
            {
                throw new InvalidFormatException();
            }

            int total_size = 20 + fmt.Length + data.Length;

            using (var wav_file = new MemoryStream(20 + fmt.Length))
                using (var wav = new BinaryWriter(wav_file))
                {
                    wav.Write(Wav.Signature);
                    wav.Write(total_size);
                    wav.Write(0x45564157); // 'WAVE'
                    wav.Write(0x20746d66); // 'fmt '
                    wav.Write(fmt.Length);
                    wav.Write(fmt);
                    wav.Write(0x61746164); // 'data'
                    wav.Write(data.Length);
                    var wav_header  = wav_file.ToArray();
                    var data_stream = new MemoryStream(data);
                    var source      = new PrefixStream(wav_header, data_stream);
                    var sound       = new WaveInput(source);
                    file.Dispose();
                    return(sound);
                }
        }