public void Unpack() { m_input.Position = m_info.DataOffset; if (0 == m_info.CompressionMode) { if (1 == m_info.ColorMode) { int dst = 0; for (int count = m_info.DataSize / 3; count > 0; --count) { m_input.Read(m_output, dst, 3); dst += 4; } } else { m_input.Read(m_output, 0, m_info.DataSize); } } else { Action <int, int> unpacker; if (1 == m_info.CompressionMode) { unpacker = UnpackRle; } else { unpacker = UnpackLz; } if (0 == m_info.ColorMode) { for (int channel = 0; channel < 4; ++channel) { unpacker(channel, 4); } } else if (1 == m_info.ColorMode) { for (int channel = 0; channel < 3; ++channel) { unpacker(channel, 4); } } else { unpacker(0, 1); } } }
void Unpack24bpp() { int pixel_size = 3; var pixel = new byte[4]; var block = new byte[64]; for (int y = 0; y < m_info.BlocksHeight; ++y) { for (int x = 0; x < m_info.BlocksWidth; ++x) { byte ctl = m_control[x + m_info.BlocksWidth * (m_info.BlocksHeight - y - 1)]; if (0x80 == ctl) { int dst = pixel_size * 8 * (x + 8 * y * m_info.BlocksWidth); for (int i = 0; i < 8; ++i) { for (int j = 0; j < 24; ++j) { m_output[dst + j] = 0; } dst += m_src_stride; } } else { m_input.Read(pixel, 0, pixel_size); for (int channel = 0; channel < pixel_size; ++channel) { DecodeBlock(ctl & 3, pixel[channel], block); int dst = pixel_size * 8 * (x + 8 * y * m_info.BlocksWidth) + channel; int src = 0; for (int i = 0; i < 8; ++i) { m_output[dst] = block[src++]; m_output[dst + 3] = block[src++]; m_output[dst + 6] = block[src++]; m_output[dst + 9] = block[src++]; m_output[dst + 12] = block[src++]; m_output[dst + 15] = block[src++]; m_output[dst + 18] = block[src++]; m_output[dst + 21] = block[src++]; dst += m_src_stride; } ctl >>= 2; } } } } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (RbpMetaData)info; file.Position = meta.DataOffset; int stride = 4 * (int)meta.Width; var pixels = new byte[stride * (int)meta.Height]; if (24 == meta.BPP) { for (int i = 0; i < pixels.Length; i += 4) { int pixel = file.ReadInt24(); pixels[i] = (byte)(pixel << 3); pixels[i + 1] = (byte)((pixel >> 3) & 0xFC); pixels[i + 2] = (byte)((pixel >> 8) & 0xF8); pixels[i + 3] = (byte)((pixel >> 16) * 0xFF / 0x3F); } } else { file.Read(pixels, 0, pixels.Length); for (int i = 3; i < pixels.Length; i += 4) { byte alpha = pixels[i]; if (alpha != 0) { pixels[i] = (byte)(alpha * 0xFF / 0x3F); } } } return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, stride)); }
byte[] ReadChunks() { var buffer = new byte[0x10000 * m_info.ChunkCount + m_info.LastChunkSize]; int dst = 0; for (int i = 0; i <= m_info.ChunkCount; ++i) { int length = Binary.BigEndian(m_input.ReadInt32()); bool is_compressed = length < 0; length &= 0x7FFFFFFF; if (is_compressed) { int unpacked_size = Math.Min(0x10000, buffer.Length - dst); using (var region = new StreamRegion(m_input.AsStream, m_input.Position, length, true)) using (var zinput = new ZLibStream(region, CompressionMode.Decompress, true)) { dst += zinput.Read(buffer, dst, unpacked_size); } } else { dst += m_input.Read(buffer, dst, length); } } return(buffer); }
void ReadGms(IBinaryStream input) { var header = new byte[0x10]; if (0x10 != input.Read(header, 0, 0x10)) { throw new EndOfStreamException(); } byte swap = header[13]; header[13] = header[9]; header[9] = swap; swap = header[15]; header[15] = header[11]; header[11] = swap; swap = header[4]; header[4] = header[8]; header[8] = swap; swap = header[6]; header[6] = header[10]; header[10] = swap; int unpacked_size = LittleEndian.ToInt32(header, 12); var gms_info = new byte[unpacked_size]; LzssUnpack(input, gms_info); // foreach (byte in gms_info) byte ^= 0xFF // decrypted array contains image meta data (see InfoReader class) }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (GgdMetaData)info; file.Position = meta.HeaderSize + 4; var palette_data = new byte[0x400]; if (palette_data.Length != file.Read(palette_data, 0, palette_data.Length)) { throw new InvalidFormatException(); } var colors = new Color[256]; for (int i = 0; i < 256; ++i) { colors[i] = Color.FromRgb(palette_data[i * 4 + 2], palette_data[i * 4 + 1], palette_data[i * 4]); } file.Seek(4, SeekOrigin.Current); int input_size = (int)(file.Length - file.Position); using (var reader = new LzssReader(file.AsStream, input_size, (int)meta.BitmapSize)) { reader.Unpack(); var palette = new BitmapPalette(colors); int stride = ((int)meta.Width + 3) & ~3; return(ImageData.Create(info, PixelFormats.Indexed8, palette, reader.Data, stride)); } }
byte[] ReadAlphaChannel() { var header = new byte[0x24]; if (0x24 != m_input.Read(header, 0, header.Length)) { return(null); } if (!Binary.AsciiEqual(header, 0, "ACIF")) { return(null); } int unpacked_size = LittleEndian.ToInt32(header, 0x1C); int packed_size = LittleEndian.ToInt32(header, 0x20); if (m_width * m_height != unpacked_size) { return(null); } var alpha = new byte[unpacked_size]; using (var unpacked = OpenSection(m_input.AsStream, unpacked_size, packed_size)) if (unpacked_size != unpacked.Read(alpha, 0, unpacked_size)) { return(null); } return(alpha); }
public override SoundInput TryOpen(IBinaryStream file) { var header = file.ReadHeader(10).ToArray(); long start_offset = SkipId3Tag(header); int sync_pos = 0; if (0 != start_offset) { file.Position = start_offset; if (4 != file.Read(header, 0, 4)) { return(null); } } else if (0xFF != header[0]) { file.Position = 1; header = file.ReadBytes(SyncSearchThreshold); sync_pos = System.Array.IndexOf <byte> (header, 0xFF, 1, SyncSearchThreshold - 4); if (-1 == sync_pos) { return(null); } } if (0xFF != header[sync_pos] || 0xE2 != (header[sync_pos + 1] & 0xE6) || 0xF0 == (header[sync_pos + 2] & 0xF0)) { return(null); } file.Position = 0; return(new Mp3Input(file.AsStream)); }
public Reader(IBinaryStream input, Pt1MetaData info) { m_type = info.Type; m_input = new byte[info.PackedSize + 8]; input.Position = 0x20; if (info.PackedSize != input.Read(m_input, 0, info.PackedSize)) { throw new InvalidFormatException("Unexpected end of file"); } m_width = (int)info.Width; m_height = (int)info.Height; m_output = new byte[info.UnpackedSize]; m_stride = m_width * 3; if (3 == m_type) { Format = PixelFormats.Bgra32; int packed_size = input.ReadInt32(); m_alpha_packed = input.ReadBytes(packed_size); if (m_alpha_packed.Length != packed_size) { throw new EndOfStreamException(); } } else { Format = PixelFormats.Bgr24; } }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var pixels = new byte[info.Width * info.Height * 4]; stream.Position = 0x18; int dst = 0; for (uint y = 0; y < info.Height; ++y) { while (dst < pixels.Length) { byte a = stream.ReadUInt8(); if (0 == a) { byte count = stream.ReadUInt8(); if (0 == count) { break; } dst += count * 4; } else { stream.Read(pixels, dst, 3); pixels[dst + 3] = (byte)a; dst += 4; } } } return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels)); }
void UnpackStream(byte[] output, int packed_size) { if (0 == packed_size) { m_input.Read(output, 0, output.Length); return; } var input = m_input.AsStream; var input_pos = m_input.Position; if (m_info.IsEncrypted) { input = new StreamRegion(input, input_pos, packed_size, true); input = new InputCryptoStream(input, new SjTransform(m_info.Key)); } try { UnpackRle(input, output); } finally { if (input != m_input.AsStream) { input.Dispose(); } m_input.Position = input_pos + packed_size; } }
public byte[] Unpack() { if (99 == m_info.Version) { return(ReadGrayScale()); } m_input.Position = m_info.DataOffset; if (10 == m_info.Version) { Palette = ImageFormat.ReadPalette(m_input.AsStream, 0x100, PaletteFormat.Bgr); } m_input.Read(m_output, 0, m_output.Length); if (!m_info.HasAlpha) { return(m_output); } m_input.Position = m_info.AlphaOffset; int plane_size = (int)m_info.Width * (int)m_info.Height; var alpha = new byte[plane_size]; RleUnpack(m_input, m_info.AlphaRleCount, alpha); var pixels = new byte[4 * plane_size]; if (8 == m_info.BPP) { ApplyAlpha8bpp(alpha, pixels); } else { ApplyAlpha24bpp(alpha, pixels); } return(pixels); }
void UnpackStream8(IBinaryStream input, byte[] output, byte[] alpha) { int alpha_dst = 0; int dst = 0; while (dst < output.Length) { byte rle = input.ReadUInt8(); if (0 == rle) { int skip = input.ReadUInt8(); dst += skip; alpha_dst += skip; } else if (rle != 0xFF) { output[dst++] = input.ReadUInt8(); alpha[alpha_dst++] = rle; } else { int count = input.ReadUInt8(); input.Read(output, dst, count); dst += count; for (int i = 0; i < count; ++i) { alpha[alpha_dst++] = 0xFF; } } } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { file.Position = 0x1C; var palette = ReadPalette(file.AsStream); if (info.BPP != 32) { var pixels = new byte[info.iWidth * info.iHeight]; file.Read(pixels, 0, pixels.Length); return(ImageData.CreateFlipped(info, PixelFormats.Indexed8, palette, pixels, info.iWidth)); } else { int stride = info.iWidth * 4; var pixels = new byte[stride * info.iHeight]; var colormap = palette.Colors; for (int dst = 0; dst < pixels.Length; dst += 4) { byte c = file.ReadUInt8(); pixels[dst] = colormap[c].B; pixels[dst + 1] = colormap[c].G; pixels[dst + 2] = colormap[c].R; pixels[dst + 3] = file.ReadUInt8(); } return(ImageData.CreateFlipped(info, PixelFormats.Bgra32, null, pixels, stride)); } }
void UnpackChannel(IBinaryStream input, byte[] output) { int dst = 0; while (dst < output.Length) { int count = input.ReadByte(); if (count < 0) { break; } if (count <= 0x32) { input.Read(output, dst, count); dst += count; } else { count -= 0x32; byte v = input.ReadUInt8(); while (count-- > 0) { output[dst++] = v; } } } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var output = new byte[info.Width * info.Height]; var table = new int[info.Height * 4]; file.Position = 12; for (int i = 0; i < table.Length; ++i) { table[i] = file.ReadInt32(); } int stride = (int)info.Width; int height = (int)info.Height; int row = 0; int data_pos = height * 16 + 12; int dst = 0; for (int y = 0; y < height; ++y) { if (table[row] != 0) { file.Position = table[row + 3] + data_pos; file.Read(output, dst + table[row + 1], table[row + 2]); } row += 4; dst += stride; } return(ImageData.Create(info, PixelFormats.Gray8, null, output)); }
void Unpack24() { int dst = 0; int bits = 0; int mask = 0; while (dst < m_output.Length) { mask >>= 1; if (0 == mask) { bits = m_input.ReadByte(); mask = 0x80; } if (0 != (bits & mask)) { m_input.Read(m_output, dst, 3); dst += 4; } else { int offset = m_input.ReadUInt16(); int count = (1 + (offset & 0xF)) * 4; offset = (1 + (offset >> 4)) * 4; Binary.CopyOverlapped(m_output, dst - offset, dst, count); dst += count; } } }
void UnpackKeyFrame(IBinaryStream input, byte[] output) { int last = -1; int dst = 0; while (dst < output.Length) { if (input.Read(output, dst, 3) < 3) { break; } int color = output[dst] | output[dst + 1] << 8 | output[dst + 2] << 16; dst += 3; if (color == last) { int count = ReadInteger(input); if (-1 == count) { break; } if (count > 2) { count = Math.Min((count - 2) * 3, output.Length - dst); Binary.CopyOverlapped(output, dst - 3, dst, count); dst += count; } } last = color; } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (Bm_MetaData)info; file.Position = 0x18; var palette = ReadPalette(file.AsStream); var pixels = new byte[info.Width * info.Height]; file.Position = 0x424; if (meta.IsCompressed) { ReadRle(file, pixels, meta.RleFlag); } else { file.Read(pixels, 0, pixels.Length); } if (meta.IsCompressed) { return(ImageData.Create(info, PixelFormats.Indexed8, palette, pixels)); } else { return(ImageData.CreateFlipped(info, PixelFormats.Indexed8, palette, pixels, info.iWidth)); } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (MfgMetaData)info; file.Position = 0x14; if ('_' != meta.Type) { for (uint i = 0; i < meta.Height; ++i) { uint n = file.ReadUInt32(); file.Seek(n * 8, SeekOrigin.Current); } } byte[] pixels = new byte[meta.Stride * info.Height]; if (pixels.Length != file.Read(pixels, 0, pixels.Length)) { throw new InvalidFormatException("Unexpected end of file"); } PixelFormat format; if (24 == meta.BPP) { format = PixelFormats.Bgr24; } else { format = PixelFormats.Bgra32; } return(ImageData.Create(info, format, null, pixels, meta.Stride)); }
/// <summary> /// Reads an instance from stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="position">The position.</param> /// <returns>Instance of the header.</returns> public static unsafe BinaryObjectHeader Read(IBinaryStream stream, int position) { Debug.Assert(stream != null); Debug.Assert(position >= 0); stream.Seek(position, SeekOrigin.Begin); BinaryObjectHeader hdr; if (BitConverter.IsLittleEndian) { stream.Read((byte *)&hdr, Size); Debug.Assert(hdr.Version == BinaryUtils.ProtoVer); Debug.Assert(hdr.SchemaOffset <= hdr.Length); Debug.Assert(hdr.SchemaOffset >= Size || !hdr.HasSchema); } else { hdr = new BinaryObjectHeader(stream); } // Only one of the flags can be set var f = hdr.Flags; Debug.Assert((f & (Flag.OffsetOneByte | Flag.OffsetTwoBytes)) != (Flag.OffsetOneByte | Flag.OffsetTwoBytes)); return(hdr); }
public byte[] Unpack() { m_input.Position = m_info.DataOffset; if ('b' == m_info.Compression) { m_input.Read(m_output, 0, m_output.Length); } else { using (var bits = new MsbBitStream(m_input.AsStream, true)) { if ('l' == m_info.Compression) { UnpackL(bits); } else if ('p' == m_info.Compression) { UnpackP(bits); } else { throw new InvalidFormatException(); } } } return(m_output); }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { PixelFormat format; if (24 == info.BPP) { format = PixelFormats.Bgr24; } else if (32 == info.BPP) { format = PixelFormats.Bgra32; } else { format = PixelFormats.Gray8; } int stride = (int)info.Width * ((info.BPP + 7) / 8); var pixels = new byte[stride * info.Height]; stream.Position = 0x10; int read = stream.Read(pixels, 0, pixels.Length); if (read != pixels.Length) { throw new InvalidFormatException(); } return(ImageData.Create(info, format, null, pixels, stride)); }
byte[] UnpackStream24(IBinaryStream input, byte[] output, int total_length) { int dst = 0; while (dst < total_length) { int v = input.ReadUInt8(); if (0 == v) { int count = input.ReadUInt8(); if (0 == count) { continue; } dst += count; } else if (0xff == v) { int count = input.ReadUInt8(); if (0 == count) { continue; } count = Math.Min(count, total_length - dst); input.Read(output, dst, count); dst += count; } else { output[dst++] = input.ReadUInt8(); } } return(output); }
public PcmDecoder(IBinaryStream input, int pcm_size, int extra, XpcmCompression mode) { if (extra < 0 || extra > 3) { throw new InvalidFormatException(); } int packed_size = input.ReadInt32(); m_extra = extra; m_pcm_size = pcm_size; m_pcm_data = new byte[pcm_size + 8192]; m_encoded = new byte[(pcm_size / 0xFE0 << 12) + 16386]; if (XpcmCompression.Lzss == mode) { if (packed_size != input.Read(m_pcm_data, 0, packed_size)) { throw new InvalidFormatException("Unexpected end of file"); } UnpackV1(m_pcm_data, packed_size, m_encoded); } else if (XpcmCompression.Zlib == mode) { using (var z = new ZLibStream(input.AsStream, CompressionMode.Decompress, true)) z.Read(m_encoded, 0, m_encoded.Length); } else { throw new InvalidFormatException("Unknown PCM compression mode"); } }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { var key = new byte[0x10]; stream.Position = 4; if (key.Length != stream.Read(key, 0, key.Length)) { return(null); } using (var enc = new InputProxyStream(stream.AsStream, true)) using (var crypto = new InputCryptoStream(enc, new GaxTransform(key))) using (var input = new BinaryStream(crypto, stream.Name)) { var info = Png.ReadMetaData(input); if (null == info) { return(null); } return(new GaxMetaData { OffsetX = info.OffsetX, OffsetY = info.OffsetY, Width = info.Width, Height = info.Height, BPP = info.BPP, Key = key, }); } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { file.Position = 0xE; int total = (int)info.Width * (int)info.Height; var pixels = new byte[total + 15]; int dst = 0; while (dst < total) { int count = file.ReadUInt16(); if (count > 0x7FFF) { count &= 0x7FFF; file.Read(pixels, dst, count); dst += count; } else { byte c = file.ReadUInt8(); while (count-- > 0) { pixels[dst++] = c; } } } return(ImageData.Create(info, PixelFormats.Gray8, null, pixels)); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (Kgd1MetaData)info; file.Position = 0x18; byte[] alpha = null; if (meta.AlphaLength != 0) { alpha = file.ReadBytes(meta.AlphaLength); } BitmapPalette palette = null; PixelFormat format; if (8 == info.BPP) { palette = ReadPalette(file.AsStream); format = PixelFormats.Indexed8; } else { format = PixelFormats.Bgr24; } var pixels = new byte[(int)info.Width * (int)info.Height * (info.BPP / 8)]; file.Read(pixels, 0, pixels.Length); if (alpha != null) { pixels = ApplyAlphaChannel(meta, pixels, palette, alpha); format = PixelFormats.Bgra32; } return(ImageData.Create(info, format, palette, pixels)); }
internal static byte[] LzDecompress(IBinaryStream input) { int unpacked_size = input.ReadInt32(); var data = new byte[unpacked_size]; int dst = 0; while (dst < unpacked_size) { int ctl = input.ReadByte(); if (-1 == ctl) { throw new EndOfStreamException(); } if (0 != (ctl & 0x80)) { byte lo = input.ReadUInt8(); int offset = ((ctl << 3 | lo >> 5) & 0x3FF) + 1; int count = (lo & 0x1F) + 1; Binary.CopyOverlapped(data, dst - offset, dst, count); dst += count; } else { int count = ctl + 1; if (input.Read(data, dst, count) != count) { throw new EndOfStreamException(); } dst += count; } } return(data); }
private void ReadPalette(int colors) { int color_size = 0x102 == colors ? 4 : 3; if (colors > 0x100) { colors = 0x100; } int palette_size = colors * color_size; var palette_data = new byte[palette_size]; if (palette_size != m_input.Read(palette_data, 0, palette_size)) { throw new InvalidFormatException(); } var palette = new Color[colors]; int color_pos = 0; for (int i = 0; i < palette.Length; ++i) { byte r = palette_data[color_pos]; byte g = palette_data[color_pos + 1]; byte b = palette_data[color_pos + 2]; if (0xff == b && 0 == g && 0xff == r) { g = 0xff; } palette[i] = Color.FromRgb(r, g, b); color_pos += color_size; } Palette = new BitmapPalette(palette); }
/// <summary> /// Tests the stream. /// </summary> private static unsafe void TestStream(IBinaryStream stream, bool sameArr, Action flush) { Action seek = () => Assert.AreEqual(0, stream.Seek(0, SeekOrigin.Begin)); Action<Action, Func<object>, object> check = (write, read, expectedResult) => { seek(); write(); flush(); seek(); Assert.AreEqual(expectedResult, read()); }; // Arrays. Assert.AreEqual(sameArr, stream.IsSameArray(stream.GetArray())); Assert.IsFalse(stream.IsSameArray(new byte[1])); Assert.IsFalse(stream.IsSameArray(stream.GetArrayCopy())); // byte* byte* bytes = stackalloc byte[10]; *bytes = 1; *(bytes + 1) = 2; stream.Write(bytes, 2); Assert.AreEqual(2, stream.Position); flush(); seek(); Assert.AreEqual(sameArr ? 256 : 2, stream.Remaining); byte* bytes2 = stackalloc byte[2]; stream.Read(bytes2, 2); Assert.AreEqual(1, *bytes2); Assert.AreEqual(2, *(bytes2 + 1)); // char* seek(); char* chars = stackalloc char[10]; *chars = 'a'; *(chars + 1) = 'b'; Assert.AreEqual(2, stream.WriteString(chars, 2, 2, Encoding.ASCII)); flush(); seek(); stream.Read(bytes2, 2); Assert.AreEqual('a', *bytes2); Assert.AreEqual('b', *(bytes2 + 1)); // Others. check(() => stream.Write(new byte[] {3, 4, 5}, 1, 2), () => stream.ReadByteArray(2), new byte[] {4, 5}); check(() => stream.WriteBool(true), () => stream.ReadBool(), true); check(() => stream.WriteBoolArray(new[] {true, false}), () => stream.ReadBoolArray(2), new[] {true, false}); check(() => stream.WriteByte(4), () => stream.ReadByte(), 4); check(() => stream.WriteByteArray(new byte[] {4, 5, 6}), () => stream.ReadByteArray(3), new byte[] {4, 5, 6}); check(() => stream.WriteChar('x'), () => stream.ReadChar(), 'x'); check(() => stream.WriteCharArray(new[] {'a', 'b'}), () => stream.ReadCharArray(2), new[] {'a', 'b'}); check(() => stream.WriteDouble(4), () => stream.ReadDouble(), 4d); check(() => stream.WriteDoubleArray(new[] {4d}), () => stream.ReadDoubleArray(1), new[] {4d}); check(() => stream.WriteFloat(4), () => stream.ReadFloat(), 4f); check(() => stream.WriteFloatArray(new[] {4f}), () => stream.ReadFloatArray(1), new[] {4f}); check(() => stream.WriteInt(4), () => stream.ReadInt(), 4); check(() => stream.WriteInt(0, 4), () => stream.ReadInt(), 4); check(() => stream.WriteIntArray(new[] {4}), () => stream.ReadIntArray(1), new[] {4}); check(() => stream.WriteLong(4), () => stream.ReadLong(), 4L); check(() => stream.WriteLongArray(new[] {4L}), () => stream.ReadLongArray(1), new[] {4L}); check(() => stream.WriteShort(4), () => stream.ReadShort(), (short)4); check(() => stream.WriteShortArray(new short[] {4}), () => stream.ReadShortArray(1), new short[] {4}); }