Esempio n. 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}).");
                }
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        /// <summary>
        /// 比较两个节点绑定的值是否相同。
        /// </summary>
        /// <param Name="dataNew">新的值。</param>
        /// <param Name="dataOld">旧的值。</param>
        /// <returns></returns>
        public virtual bool CompareData(object dataNew, object dataOld)
        {
            // skip virtual dir
            {
                if (dataNew is Wz_File fileNew && fileNew.IsSubDir)
                {
                    dataNew = null;
                }
                if (dataOld is Wz_File fileOld && fileOld.IsSubDir)
                {
                    dataNew = null;
                }
            }

            if (dataNew == null && dataOld == null)
            {
                return(true);
            }
            if (dataNew == null ^ dataOld == null)
            {
                return(false);
            }

            Type type = dataNew.GetType();

            if (type != dataOld.GetType())
            {
                return(false);
            }

            if (type.IsClass)
            {
                switch (dataNew)
                {
                case string str:
                    return(str == (string)dataOld);

                case Wz_Image img:
                    Wz_Image imgOld = (Wz_Image)dataOld;
                    return(img.Size == imgOld.Size && img.Checksum == imgOld.Checksum);

                case Wz_File file:
                    Wz_File fileOld = (Wz_File)dataOld;
                    return(file.Type == fileOld.Type);

                case Wz_Png png:
                    Wz_Png pngOld = (Wz_Png)dataOld;
                    switch (this.PngComparison)
                    {
                    case WzPngComparison.SizeOnly:
                        return(png.Width == pngOld.Width && png.Height == pngOld.Height);

                    case WzPngComparison.SizeAndDataLength:
                        return(png.Width == pngOld.Width &&
                               png.Height == pngOld.Height &&
                               png.DataLength == pngOld.DataLength);

                    case WzPngComparison.Pixel:
                        if (!(png.Width == pngOld.Width && png.Height == pngOld.Height && png.Form == pngOld.Form))
                        {
                            return(false);
                        }
                        byte[] pixelNew = png.GetRawData();
                        byte[] pixelOld = pngOld.GetRawData();
                        if (pixelNew == null || pixelOld == null || pixelNew.Length != pixelOld.Length)
                        {
                            return(false);
                        }
                        for (int i = 0, i1 = pixelNew.Length; i < i1; i++)
                        {
                            if (pixelNew[i] != pixelOld[i])
                            {
                                return(false);
                            }
                        }
                        return(true);

                    default:
                        goto case WzPngComparison.SizeAndDataLength;
                    }
                    break;

                case Wz_Vector vector:
                    Wz_Vector vectorOld = (Wz_Vector)dataOld;
                    return(vector.X == vectorOld.X && vector.Y == vectorOld.Y);

                case Wz_Uol uol:
                    return(uol.Uol == ((Wz_Uol)dataOld).Uol);

                case Wz_Sound sound:
                    Wz_Sound soundOld = (Wz_Sound)dataOld;
                    return(sound.Ms == soundOld.Ms && sound.DataLength == soundOld.DataLength);
                }
            }

            return(object.Equals(dataNew, dataOld));
        }