public override ImageMetaData ReadMetaData(IBinaryStream stream) { var header = stream.ReadHeader(20); if (!header.AsciiEqual("*Pola* ")) { return(null); } int unpacked_size = header.ToInt32(8); using (var reader = new PolaReader(stream, 64)) { reader.Unpack(); using (var temp = BinaryStream.FromArray(reader.Data, stream.Name)) { var info = base.ReadMetaData(temp); if (null == info) { return(null); } return(new PolaMetaData { Width = info.Width, Height = info.Height, BPP = info.BPP, UnpackedSize = unpacked_size, }); } } }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var meta = (ApsMetaData)info; stream.Position = meta.DataOffset; byte[] image_data; if (meta.IsPacked) { using (var reader = new LzReader(stream.AsStream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); image_data = reader.Data; } } else { image_data = stream.ReadBytes((int)meta.UnpackedSize); } using (var unpacked = BinaryStream.FromArray(image_data, stream.Name)) { var ap_info = base.ReadMetaData(unpacked); if (null == ap_info) { throw new InvalidFormatException(); } return(base.Read(unpacked, ap_info)); } }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var meta = (PolaMetaData)info; stream.Position = meta.DataOffset; var reader = new PolaReader(stream, meta.UnpackedSize); reader.Unpack(); using (var temp = BinaryStream.FromArray(reader.Data, stream.Name)) return(base.Read(temp, info)); }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { var header = stream.ReadHeader(20); if (!header.AsciiEqual("*Pola")) { return(null); } bool new_version = header.AsciiEqual(5, "* "); uint data_offset = new_version ? 20u : 13u; int unpacked_size = header.ToInt32(8); stream.Position = data_offset; var reader = new PolaReader(stream, 64); reader.Unpack(); using (var temp = BinaryStream.FromArray(reader.Data, stream.Name)) { var info = base.ReadMetaData(temp); if (null == info) { return(null); } if (!new_version) { unpacked_size = 0x10 + GetStride(info) * (int)info.Height; } return(new PolaMetaData { Width = info.Width, Height = info.Height, BPP = info.BPP, DataOffset = data_offset, UnpackedSize = unpacked_size, }); } }
public override void Write(Stream file, ImageData image) { var header = new byte[0x14]; var depth = (short)(24 == image.BPP ? 0 : 32 == image.BPP ? 1 : 2); var compression = (ushort)3; var flags = (ushort)17; var mode = (ushort)0; using (var memeStream = new MemoryStream(header)) { using (var binaryWriter = new BinaryWriter(memeStream)) { binaryWriter.Write(Signature); binaryWriter.Write((ushort)image.OffsetX); binaryWriter.Write((ushort)image.OffsetY); binaryWriter.Write((ushort)image.Width); binaryWriter.Write((ushort)image.Height); binaryWriter.Write(compression); binaryWriter.Write(flags); binaryWriter.Write(depth); binaryWriter.Write(mode); } } var metaData = ReadMetaData(BinaryStream.FromArray(header, "")); var bitmap = image.Bitmap; var pixelFormat = CheckFormat(image.BPP); int stride = (int)(image.Width * pixelFormat.BitsPerPixel / 8 + 3) & ~3; if (pixelFormat != bitmap.Format) { var converted_bitmap = new FormatConvertedBitmap(); converted_bitmap.BeginInit(); converted_bitmap.Source = image.Bitmap; converted_bitmap.DestinationFormat = pixelFormat; converted_bitmap.EndInit(); bitmap = converted_bitmap; } var data = new byte[image.Height * stride]; var row_data = new byte[stride]; var rect = new Int32Rect(0, 0, (int)image.Width, 1); for (uint row = 0; row < image.Height; ++row) { bitmap.CopyPixels(rect, row_data, stride, 0); rect.Y++; row_data.CopyTo(data, row * stride); } using (var binaryWriter = new BinaryWriter(file)) { binaryWriter.Write(header); using (var writer = new Writer(data, binaryWriter, (CrxMetaData)metaData)) { writer.Write(); } } }