示例#1
0
        }                                                                 // 'MICO'

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

            if (!header.AsciiEqual(4, "CG01"))
            {
                return(null);
            }
            int length = header.ToInt32(8);

            using (var input = new Mg2EncryptedStream(file.AsStream, 0x10, length))
                using (var png = new BinaryStream(input, file.Name))
                {
                    var info = Png.ReadMetaData(png);
                    if (null == info)
                    {
                        return(null);
                    }
                    return(new Mg2MetaData
                    {
                        Width = info.Width,
                        Height = info.Height,
                        OffsetX = info.OffsetX,
                        OffsetY = info.OffsetY,
                        BPP = info.BPP,
                        ImageLength = length,
                        AlphaLength = header.ToInt32(12)
                    });
                }
        }
示例#2
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var          meta = (Mg2MetaData)info;
            BitmapSource frame;

            using (var input = new Mg2EncryptedStream(file.AsStream, 0x10, meta.ImageLength))
            {
                var decoder = new PngBitmapDecoder(input, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                frame = decoder.Frames[0];
                if (0 == meta.AlphaLength)
                {
                    frame.Freeze();
                    return(new ImageData(frame, info));
                }
            }
            if (frame.Format.BitsPerPixel != 32)
            {
                frame = new FormatConvertedBitmap(frame, PixelFormats.Bgr32, null, 0);
            }
            int stride = frame.PixelWidth * 4;
            var pixels = new byte[stride * (int)meta.Height];

            frame.CopyPixels(pixels, stride, 0);

            using (var input = new Mg2EncryptedStream(file.AsStream, 0x10 + meta.ImageLength, meta.AlphaLength))
            {
                var          decoder     = new PngBitmapDecoder(input, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                BitmapSource alpha_frame = decoder.Frames[0];
                if (alpha_frame.PixelWidth != frame.PixelWidth || alpha_frame.PixelHeight != frame.PixelHeight)
                {
                    return(ImageData.Create(info, PixelFormats.Bgr32, null, pixels, stride));
                }

                alpha_frame = new FormatConvertedBitmap(alpha_frame, PixelFormats.Gray8, null, 0);
                var alpha = new byte[alpha_frame.PixelWidth * alpha_frame.PixelHeight];
                alpha_frame.CopyPixels(alpha, alpha_frame.PixelWidth, 0);

                int src = 0;
                for (int dst = 3; dst < pixels.Length; dst += 4)
                {
                    pixels[dst] = alpha[src++];
                }
                return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, stride));
            }
        }
示例#3
0
 public Mg2EncryptedStream CreateStream(Stream main, int offset, int length)
 {
     return(Mg2EncryptedStream.CreateV2(main, offset, length));
 }