实现APNG渲染功能

发布于 2024-11-13 12:14:27 字数 3072 浏览 4 评论 0原文

嘿大家, 因此,我目前正在尝试实现 APNG 规范,但在帧渲染方面遇到了一些问题。我的功能是

    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 画廊。但是,其中一些渲染不正确(此 存在伪影问题,并且 这个 没有始终保留左侧的红条)。请注意,您将可以在 Firefox 3 或更高版本中查看页面来查看动画。

我相信这个问题与我处理 DISPOSE_OP_PREVIOUS 的方式有关,但我无法弄清楚我做错了什么。谁能建议我可能缺少什么?

Hey everyone,
So, I'm currently trying the implement the APNG Specification, but am having some trouble with the frame rendering. My function is

    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;
            }
        }
    }

The APNGBox is

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

I think that this is mostly correct, as I've run it against all the images in the APNG Gallery. However, some of them are rendering incorrectly (This one has artifacting issues, and this one does not retain the red bar on the left consistently). Please note, you'll have the view the page in Firefox 3 or higher to view the animations.

I'm lead to believe the issue has to do with how I handle the DISPOSE_OP_PREVIOUS, but I can't figure out what I'm doing wrong. Can anyone suggest what I might be missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-11-20 12:14:27

所以,我最终解决了这个问题,并将在此发布,以防将来有人遇到类似的问题。事实证明,问题主要有三个方面:

  1. 如果帧类型为“上一个”,则不应更改“上一个缓冲区”;
  2. 应该使用上一帧的 dispose_op,而不是当前帧的 dispose_op,来确定它如何处置;
  3. 应该确定仅处理旧框架的区域,而不是整个框架

新代码是:

    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);
        }
    }

So, I was able to figure it out in the end, and will post here in case anyone in the future comes across a similar problem. Turns out the issue was largely threefold:

  1. One should not change the 'previous buffer' if the frame type is 'PREVIOUS'
  2. One should use the previous frame's dispose_op, not the dispose_op of the current frame, to determine how it disposes
  3. One should be sure to dispose only the old frame's region, not the whole frame

The new code is:

    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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文