public Reader(Stream stream, CmMetaData info) { m_input = stream; m_width = (int)info.Width; m_height = (int)info.Height; m_pixel_size = info.BPP / 8; m_compressed = info.IsCompressed; m_data_length = (int)info.DataLength; switch (m_pixel_size) { case 1: Format = PixelFormats.Indexed8; break; case 3: Format = PixelFormats.Bgr24; break; case 4: Format = PixelFormats.Bgr32; break; default: throw new InvalidFormatException("Invalid color depth"); } if (info.Colors > 0) { m_input.Position = 0x20; Palette = RleDecoder.ReadPalette(m_input, info.Colors, 3); } m_input.Position = info.DataOffset; int size = info.IsCompressed ? m_width * m_height * m_pixel_size : (int)info.DataLength; m_pixels = new byte[size]; }
public SourceFile Run(EncodedFile encoded, string outputPath) { var rleDecoder = new RleDecoder(); var huffmanDecoder = new HuffmanDecoder(encoded.Header.HuffmanMetadata); var bytes = encoded.Content.ToArray(); var decoded = huffmanDecoder.Decode(bytes); var outRle = rleDecoder.Decode(decoded); return(new SourceFile(outputPath, outRle, encoded.Header.SourceExtension)); }
public void Unpack() { if (m_compressed) { RleDecoder.Unpack(m_input, m_data_length, m_pixels, m_pixel_size); } else { m_input.Read(m_pixels, 0, m_pixels.Length); } }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var meta = (CmMetaData)info; stream.Position = meta.DataOffset; var palette = ReadPalette(stream.AsStream, 0x100, PaletteFormat.BgrX); var pixels = new byte[info.Width * info.Height]; if (meta.IsCompressed) { int packed_size = (int)(stream.Length - meta.DataOffset); RleDecoder.Unpack(stream.AsStream, packed_size, pixels, 1); } else if (pixels.Length != stream.Read(pixels, 0, pixels.Length)) { throw new InvalidFormatException(); } return(ImageData.Create(info, PixelFormats.Indexed8, palette, pixels)); }
public void Unpack() { if (m_info.Colors > 0) { m_input.Position = 0x30; Palette = ImageFormat.ReadPalette(m_input, m_info.Colors, PaletteFormat.Bgr); } m_input.Position = m_info.DataOffset; if (m_info.IsCompressed) { RleDecoder.Unpack(m_input, (int)m_info.DataLength, m_output, m_pixel_size); } else { m_input.Read(m_output, 0, m_output.Length); } m_input.Position = m_info.MaskOffset; if (m_info.IsMaskCompressed) { RleDecoder.Unpack(m_input, (int)m_info.MaskLength, m_alpha, 1); } else { m_input.Read(m_alpha, 0, m_alpha.Length); } Action <int, int, byte> copy_pixel; if (m_pixel_size > 1) { copy_pixel = (src, dst, alpha) => { m_pixels[dst] = m_output[src]; m_pixels[dst + 1] = m_output[src + 1]; m_pixels[dst + 2] = m_output[src + 2]; m_pixels[dst + 3] = alpha; } } ; else { copy_pixel = (src, dst, alpha) => { var color = Palette.Colors[m_output[src]]; m_pixels[dst] = color.B; m_pixels[dst + 1] = color.G; m_pixels[dst + 2] = color.R; m_pixels[dst + 3] = alpha; } }; int src_stride = m_width * m_pixel_size; for (int y = 0; y < m_height; ++y) { int dst_line = y * m_width * 4; int src_line = (m_height - 1 - y) * src_stride;; for (int x = 0; x < m_width; ++x) { copy_pixel(src_line, dst_line, m_alpha[y * m_width + x]); src_line += m_pixel_size; dst_line += 4; } } } }