private static void ReadPngFile(CommandLineOptions opts, ParseContext context, IcoFrame frame, byte[] sourceFile, out bool canSourceBePreserved) { using (var stream = new MemoryStream(sourceFile)) { var decoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder(); frame.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream); } var encoding = PngDecoder.GetPngFileEncoding(new Memory <byte>(sourceFile)); frame.Encoding.Type = IcoEncodingType.Png; frame.Encoding.ClaimedBitDepth = 32; frame.Encoding.ActualBitDepth = 32; canSourceBePreserved = encoding.ColorType == PngColorType.RGBA && encoding.BitsPerChannel == 8; }
public static void DoPngEntry(ByteReader bitmapHeader, ParseContext context, IcoFrame source) { if (source.Encoding.ClaimedBitDepth != 32) { context.Reporter.WarnLine(IcoErrorCode.PngNot32Bit, $"PNG-encoded image with bit depth {source.Encoding.ClaimedBitDepth} (expected 32).", context.DisplayedPath, context.ImageDirectoryIndex.Value); } using (var stream = new MemoryStream(bitmapHeader.Data.ToArray())) { var decoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder(); source.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream); } source.Encoding.Type = IcoEncodingType.Png; source.Encoding.PixelFormat = BmpUtil.IsAnyPartialTransparency(source.CookedData) ? BitmapEncoding.Pixel_argb32 : BitmapEncoding.Pixel_0rgb32; source.Mask = GenerateMaskFromAlpha(source.CookedData); // Conservatively assume that the output wouldn't have used palette trimming, if it had been a bmp frame. if (source.Encoding.ClaimedBitDepth < 16) { source.Encoding.PaletteSize = 1u << (int)source.Encoding.ClaimedBitDepth; } var encoding = GetPngFileEncoding(bitmapHeader.Data); if (encoding.ColorType != PngColorType.RGBA) { context.Reporter.WarnLine(IcoErrorCode.PngNotRGBA32, $"ICO files require the embedded PNG image to be encoded in RGBA32 format; this is {encoding.ColorType}", context.DisplayedPath, context.ImageDirectoryIndex.Value); } else if (encoding.BitsPerChannel != 8) { context.Reporter.WarnLine(IcoErrorCode.PngNotRGBA32, $"ICO files require the embedded PNG image to be encoded in RGBA32 format; this is RGBA{encoding.BitsPerChannel * 4}", context.DisplayedPath, context.ImageDirectoryIndex.Value); } uint numChannels = 0; switch (encoding.ColorType) { case PngColorType.Grayscale: numChannels = 1; break; case PngColorType.RGB: numChannels = 3; break; case PngColorType.GrayscaleAlpha: numChannels = 2; break; case PngColorType.RGBA: numChannels = 4; break; case PngColorType.Palette: default: break; } source.Encoding.ActualHeight = encoding.Height; source.Encoding.ActualWidth = encoding.Width; source.Encoding.ActualBitDepth = encoding.BitsPerChannel * numChannels; }