实现APNG渲染功能

时间:2011-06-02 14:44:03

标签: c# winforms render apng

嘿大家, 所以,我目前正在尝试实现APNG Specification,但是在渲染方面遇到了一些麻烦。我的功能是

    private void UpdateUI()
    {
        foreach (PictureBox pb in pics)
        {
            APNGBox box = (APNGBox)pb.Tag;
            APNGLib.APNG png = box.png;
            if (box.buffer == null)
            {
                box.buffer = new Bitmap((int)png.Width, (int)png.Height);
            }
            APNGLib.Frame f = png.GetFrame(box.frameNum);
            using (Graphics g = Graphics.FromImage(box.buffer))
            {
                switch (f.DisposeOp)
                {
                    case APNGLib.Frame.DisposeOperation.NONE:
                        break;
                    case APNGLib.Frame.DisposeOperation.BACKGROUND:
                        g.Clear(Color.Transparent);
                        break;
                    case APNGLib.Frame.DisposeOperation.PREVIOUS:
                        if (box.prevBuffer != null)
                        {
                            g.DrawImage(box.prevBuffer, Point.Empty);
                        }
                        else
                        {
                            g.Clear(Color.Transparent);
                        }
                        break;
                    default:
                        break;
                }
                Bitmap read = png.ToBitmap(box.frameNum++);
                switch (f.BlendOp)
                {
                    case APNGLib.Frame.BlendOperation.OVER:
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                        break;
                    case APNGLib.Frame.BlendOperation.SOURCE:
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        break;
                    default:
                        break;
                }
                g.DrawImage(read, new Point((int)f.XOffset, (int)f.YOffset));
            }

            box.prevBuffer = box.buffer;
            pb.Image = box.buffer;

            if (box.frameNum >= box.png.FrameCount)
            {
                box.frameNum = 0;
                box.buffer = null;
                box.prevBuffer = null;
            }
        }
    }

APNGBox

internal class APNGBox
    {
        public int frameNum = 0;
        public APNGLib.APNG png;
        public Bitmap buffer;
        public Bitmap prevBuffer;
    }

我认为这大多是正确的,因为我已经针对APNG Gallery中的所有图片运行它。但是,其中一些渲染不正确(This一个具有伪像问题,this一个不保持左侧的红色条一致)。请注意,您可以在Firefox 3或更高版本中查看该页面以查看动画。

我认为这个问题与我如何处理DISPOSE_OP_PREVIOUS有关,但我无法弄清楚我做错了什么。任何人都可以建议我可能缺少的东西吗?

1 个答案:

答案 0 :(得分:5)

所以,我最终能够弄明白,并且会在这里发布,以防将来有人遇到类似的问题。事实证明这个问题主要有三个方面:

  1. 如果帧类型为'PREVIOUS'
  2. ,则不应更改'上一个缓冲区'
  3. 应该使用前一帧的dispose_op而不是当前帧的dispose_op来确定它是如何处置的
  4. 应该确保只处理旧框架的区域,而不是整个框架
  5. 新代码是:

        internal class APNGBox
        {
            public int frameNum { get; set; }
            public APNGLib.APNG apng { get; set; }
            public Bitmap buffer { get; set; }
            public Bitmap prevBuffer { get; set; }
    
            public APNGBox(APNGLib.APNG png)
            {
                frameNum = 0;
                apng = png;
                buffer = apng.ToBitmap(0);
                prevBuffer = null;
            }
        }
    
        private void UpdateUI()
        {
            foreach (PictureBox pb in pics)
            {
                APNGBox box = (APNGBox)pb.Tag;
                APNGLib.APNG png = box.apng;
                if (!png.IsAnimated)
                {
                    if (pb.Image == null)
                    {
                        pb.Image = png.ToBitmap();
                    }
                }
                else
                {
                    if (box.frameNum != png.FrameCount - 1)
                    {
                        Bitmap prev = box.prevBuffer == null ? null : new Bitmap(box.prevBuffer);
                        APNGLib.Frame f1 = png.GetFrame(box.frameNum);
                        if (f1.DisposeOp != APNGLib.Frame.DisposeOperation.PREVIOUS)
                        {
                            box.prevBuffer = new Bitmap(box.buffer);
                        }
                        DisposeBuffer(box.buffer, new Rectangle((int)f1.XOffset, (int)f1.YOffset, (int)f1.Width, (int)f1.Height), f1.DisposeOp, prev);
                        box.frameNum++;
                        APNGLib.Frame f2 = png.GetFrame(box.frameNum);
                        RenderNextFrame(box.buffer, new Point((int)f2.XOffset, (int)f2.YOffset), png.ToBitmap(box.frameNum), f2.BlendOp);
                    }
                    else
                    {
                        box.frameNum = 0;
                        box.prevBuffer = null;
                        ClearFrame(box.buffer);
                        RenderNextFrame(box.buffer, Point.Empty, png.ToBitmap(box.frameNum), APNGLib.Frame.BlendOperation.SOURCE);
                    }
                    pb.Invalidate();
                }
            }
        }
    
        private void DisposeBuffer(Bitmap buffer, Rectangle region, APNGLib.Frame.DisposeOperation dispose, Bitmap prevBuffer)
        {
            using (Graphics g = Graphics.FromImage(buffer))
            {
                g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    
                Brush b = new SolidBrush(Color.Transparent);
                switch (dispose)
                {
                    case APNGLib.Frame.DisposeOperation.NONE:
                        break;
                    case APNGLib.Frame.DisposeOperation.BACKGROUND:
                        g.FillRectangle(b, region);
                        break;
                    case APNGLib.Frame.DisposeOperation.PREVIOUS:
                        if(prevBuffer != null)
                        {
                            g.FillRectangle(b, region);
                            g.DrawImage(prevBuffer, region, region, GraphicsUnit.Pixel);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    
        private void RenderNextFrame(Bitmap buffer, Point point, Bitmap nextFrame, APNGLib.Frame.BlendOperation blend)
        {
            using(Graphics g = Graphics.FromImage(buffer))
            {
                switch(blend)
                {
                    case APNGLib.Frame.BlendOperation.OVER:
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                        break;
                    case APNGLib.Frame.BlendOperation.SOURCE:
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        break;
                    default:
                        break;
                }
                g.DrawImage(nextFrame, point);
            }
        }
    
        private void ClearFrame(Bitmap buffer)
        {
            using(Graphics g = Graphics.FromImage(buffer))
            {
                g.Clear(Color.Transparent);
            }
        }