示例#1
0
        public static void ToTexture(this Wz_Png png, Texture2D texture, Point origin)
        {
            Rectangle rect = new Rectangle(origin, new Point(png.Width, png.Height));

            //检查大小
            if (rect.X < 0 || rect.Y < 0 || rect.Right > texture.Width || rect.Bottom > texture.Height)
            {
                throw new ArgumentException("Png rectangle is out of bounds.");
            }

            //检查像素格式
            var format = GetTextureFormatOfPng(png.Form);

            if (texture.Format == SurfaceFormat.Bgra32)
            {
                using (var bmp = png.ExtractPng())
                {
                    bmp.ToTexture(texture, origin);
                }
            }
            else if (texture.Format != format)
            {
                throw new ArgumentException($"Texture format({texture.Format}) does not fit the png form({png.Form}).");
            }
            else
            {
                byte[] plainData = png.GetRawData();
                if (plainData == null)
                {
                    throw new Exception("png decoding failed.");
                }

                switch (png.Form)
                {
                case 1:
                case 2:
                case 257:
                case 513:
                case 1026:
                case 2050:
                    texture.SetData(0, 0, rect, plainData, 0, plainData.Length);
                    break;

                case 3:
                    var pixel = Wz_Png.GetPixelDataForm3(plainData, png.Width, png.Height);
                    texture.SetData(0, 0, rect, pixel, 0, pixel.Length);
                    break;

                case 517:
                    pixel = Wz_Png.GetPixelDataForm517(plainData, png.Width, png.Height);
                    texture.SetData(0, 0, rect, pixel, 0, pixel.Length);
                    break;

                default:
                    throw new Exception($"unknown png form ({png.Form}).");
                }
            }
        }
示例#2
0
        public static GifFrame CreateFrameFromNode(Wz_Node frameNode, GlobalFindNodeFunction findNode)
        {
            if (frameNode == null || frameNode.Value == null)
            {
                return(null);
            }

            while (frameNode.Value is Wz_Uol)
            {
                Wz_Uol  uol     = frameNode.Value as Wz_Uol;
                Wz_Node uolNode = uol.HandleUol(frameNode);
                if (uolNode != null)
                {
                    frameNode = uolNode;
                }
            }
            if (frameNode.Value is Wz_Png)
            {
                var    linkNode = frameNode.GetLinkedSourceNode(findNode);
                Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)frameNode.Value;

                var gifFrame = new GifFrame(png.ExtractPng());
                foreach (Wz_Node propNode in frameNode.Nodes)
                {
                    switch (propNode.Text)
                    {
                    case "origin":
                        gifFrame.Origin = (propNode.Value as Wz_Vector);
                        break;

                    case "delay":
                        gifFrame.Delay = propNode.GetValue <int>();
                        break;

                    case "a0":
                        gifFrame.A0 = propNode.GetValue <int>();
                        break;

                    case "a1":
                        gifFrame.A1 = propNode.GetValue <int>();
                        break;
                    }
                }
                if (gifFrame.Delay == 0)
                {
                    gifFrame.Delay = 100;//给予默认delay
                }
                return(gifFrame);
            }
            return(null);
        }
示例#3
0
        public static BitmapOrigin CreateFromNode(Wz_Node node, GlobalFindNodeFunction findNode)
        {
            BitmapOrigin bp = new BitmapOrigin();
            Wz_Uol       uol;

            while ((uol = node.GetValue <Wz_Uol>(null)) != null)
            {
                node = uol.HandleUol(node);
            }

            //获取linkNode
            var    linkNode = node.GetLinkedSourceNode(findNode);
            Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)node.Value;

            bp.Bitmap = png?.ExtractPng();
            Wz_Node   originNode = node.FindNodeByPath("origin");
            Wz_Vector vec        = (originNode == null) ? null : originNode.GetValue <Wz_Vector>();

            bp.Origin = (vec == null) ? new Point() : new Point(vec.X, vec.Y);

            return(bp);
        }
示例#4
0
        public static PngInfo ToPngInfo(this Wz_Png wz_Png)
        {
            string base64;

            using (var bmp = wz_Png.ExtractPng())
            {
                using (var ms = new MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] data = ms.ToArray();
                    base64 = Convert.ToBase64String(data);
                }
            }
            return(new PngInfo
            {
                Width = wz_Png.Width,
                Height = wz_Png.Height,
                Base64Data = base64,
                DataLength = wz_Png.DataLength,
                Form = wz_Png.Form
            });
        }
示例#5
0
        public static Texture2D ToTexture(this Wz_Png png, GraphicsDevice graphicsDevice)
        {
            byte[] plainData = png.GetRawData();
            if (plainData == null)
            {
                return(null);
            }

            Texture2D t2d;

            switch (png.Form)
            {
            case 1:
                t2d = null;
                try
                {
                    t2d = MonogameUtils.CreateTexture_BGRA4444(graphicsDevice, png.Width, png.Height);
                    t2d.SetData(plainData);
                }
                catch      //monogame并不支持这个format 用gdi+转
                {
                    if (t2d != null)
                    {
                        t2d.Dispose();
                    }
                    goto default;
                }
                break;

            case 2:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Color);
                MonogameUtils.BgraToColor(plainData);
                t2d.SetData(plainData);
                break;

            case 513:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                t2d.SetData(plainData);
                break;

            case 517:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                byte[] texData = new byte[png.Width * png.Height * 2];
                for (int j0 = 0, j1 = png.Height / 16; j0 < j1; j0++)
                {
                    int idxTex = j0 * 16 * png.Width * 2;
                    for (int i0 = 0, i1 = png.Width / 16; i0 < i1; i0++)
                    {
                        int idx = (i0 + j0 * i1) * 2;

                        for (int k = 0; k < 16; k++)
                        {
                            texData[idxTex + i0 * 32 + k * 2]     = plainData[idx];
                            texData[idxTex + i0 * 32 + k * 2 + 1] = plainData[idx + 1];
                        }
                    }
                    for (int k = 1; k < 16; k++)
                    {
                        System.Buffer.BlockCopy(texData, idxTex, texData, idxTex + k * png.Width * 2, png.Width * 2);
                    }
                }
                t2d.SetData(texData);
                break;

            case 1026:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt3);
                t2d.SetData(plainData);
                break;

            case 2050:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt5);
                t2d.SetData(plainData);
                break;

            default:     //默认从bitmap复制 二次转换
                var bitmap = png.ExtractPng();
                t2d = bitmap.ToTexture(graphicsDevice);
                bitmap.Dispose();
                break;
            }

            return(t2d);
        }