コード例 #1
0
ファイル: PsbHelper.cs プロジェクト: jszhtian/FreeMote
        /// <summary>
        /// Get <see cref="PsbPixelFormat"/> from string and <see cref="PsbSpec"/>
        /// </summary>
        /// <param name="typeStr"></param>
        /// <param name="spec"></param>
        /// <returns></returns>
        public static PsbPixelFormat ToPsbPixelFormat(this string typeStr, PsbSpec spec)
        {
            if (string.IsNullOrEmpty(typeStr))
            {
                return(PsbPixelFormat.None);
            }
            switch (typeStr.ToUpperInvariant())
            {
            case "DXT5":
                return(PsbPixelFormat.DXT5);

            case "RGBA8":
                if (spec == PsbSpec.common || spec == PsbSpec.ems)
                {
                    return(PsbPixelFormat.CommonRGBA8);
                }
                else
                {
                    return(PsbPixelFormat.WinRGBA8);
                }

            case "RGBA4444":
                return(PsbPixelFormat.WinRGBA4444);

            default:
                return(PsbPixelFormat.None);
            }
        }
コード例 #2
0
        public static Dictionary <string, Bitmap> SplitTexture(PsbDictionary tex, PsbSpec spec)
        {
            var icon    = (PsbDictionary)tex["icon"];
            var texture = (PsbDictionary)tex["texture"];

            //var mipmap = (PsbDictionary)texture["mipMap"]; //TODO: Mipmap?
            Dictionary <string, Bitmap> textures = new Dictionary <string, Bitmap>(icon.Count);

            var pixel = texture[Consts.ResourceKey];

            if (pixel == null || !(pixel is PsbResource pixelRes))
            {
                throw new PsbBadFormatException(PsbBadFormatReason.Resources,
                                                "External Texture PSB is not supported. Please Link textures into PSB.");
                return(textures);
            }

            var md = PsbResHelper.GenerateImageMetadata(texture, pixelRes);

            md.Spec = spec; //Important
            Bitmap bmp = md.ToImage();

            foreach (var iconPair in icon)
            {
                var    info   = (PsbDictionary)iconPair.Value;
                var    width  = (int)(PsbNumber)info["width"];
                var    height = (int)(PsbNumber)info["height"];
                var    top    = (int)(PsbNumber)info["top"];
                var    left   = (int)(PsbNumber)info["left"];
                Bitmap b      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
#if USE_FASTBITMAP
                using (FastBitmap f = b.FastLock())
                {
                    f.CopyRegion(bmp, new Rectangle(left, top, width, height), new Rectangle(0, 0, b.Width, b.Height));
                }
#else
                Graphics g = Graphics.FromImage(b);
                //g.InterpolationMode = InterpolationMode.NearestNeighbor;
                //g.PixelOffsetMode = PixelOffsetMode.Half;
                g.DrawImage(bmp, new Rectangle(0, 0, b.Width, b.Height), new Rectangle(left, top, width, height),
                            GraphicsUnit.Pixel);
                g.Dispose();
#endif
                textures.Add(iconPair.Key, b);
            }

            bmp.Dispose();
            return(textures);
        }
コード例 #3
0
        /// <summary>
        /// If this spec uses RL
        /// </summary>
        /// <param name="spec"></param>
        /// <returns></returns>
        public static PsbCompressType CompressType(this PsbSpec spec)
        {
            switch (spec)
            {
            case PsbSpec.krkr:
                return(PsbCompressType.RL);

            case PsbSpec.ems:
            case PsbSpec.common:
            case PsbSpec.win:
            case PsbSpec.other:
            default:
                return(PsbCompressType.None);
            }
        }
コード例 #4
0
        /// <summary>
        /// Get <see cref="PsbPixelFormat"/> from string and <see cref="PsbSpec"/>
        /// </summary>
        /// <param name="typeStr"></param>
        /// <param name="spec"></param>
        /// <returns></returns>
        public static PsbPixelFormat ToPsbPixelFormat(this string typeStr, PsbSpec spec)
        {
            if (string.IsNullOrEmpty(typeStr))
            {
                return(PsbPixelFormat.None);
            }

            switch (typeStr.ToUpperInvariant())
            {
            case "L8_SW":
                return(PsbPixelFormat.L8_SW);

            case "CI8_SW":
                return(PsbPixelFormat.CI8_SW);

            case "DXT5":
                return(PsbPixelFormat.DXT5);

            case "RGBA8":
                if (spec == PsbSpec.common || spec == PsbSpec.ems || spec == PsbSpec.vita)
                {
                    return(PsbPixelFormat.CommonRGBA8);
                }
                else
                {
                    return(PsbPixelFormat.WinRGBA8);
                }

            case "RGBA8_SW":
                return(spec == PsbSpec.ps4 ? PsbPixelFormat.TileRGBA8_SW : PsbPixelFormat.RGBA8_SW);

            case "A8_SW":
                return(spec == PsbSpec.ps4 ? PsbPixelFormat.TileA8_SW : PsbPixelFormat.A8_SW);

            case "RGBA4444":
                if (spec == PsbSpec.common || spec == PsbSpec.ems)
                {
                    return(PsbPixelFormat.CommonRGBA4444);
                }
                return(PsbPixelFormat.WinRGBA4444);

            case "A8L8":
                return(PsbPixelFormat.A8L8);

            default:
                return(Enum.TryParse(typeStr, true, out PsbPixelFormat pixelFormat) ? pixelFormat : PsbPixelFormat.None);
            }
        }
コード例 #5
0
        /// <summary>
        /// Get <see cref="PsbSpec"/>'s default <see cref="PsbPixelFormat"/>
        /// </summary>
        /// <param name="spec"></param>
        /// <returns></returns>
        public static PsbPixelFormat DefaultPixelFormat(this PsbSpec spec)
        {
            switch (spec)
            {
            case PsbSpec.common:
            case PsbSpec.ems:
                return(PsbPixelFormat.CommonRGBA8);

            case PsbSpec.krkr:
            case PsbSpec.win:
                return(PsbPixelFormat.WinRGBA8);

            case PsbSpec.other:
            default:
                return(PsbPixelFormat.None);
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: mailwl/FreeMote
        private static void Port(string s, PsbSpec portSpec)
        {
            var name = Path.GetFileNameWithoutExtension(s);
            var ext  = Path.GetExtension(s);

            Console.WriteLine($"Converting {name} to {portSpec} platform...");
            PSB psb = new PSB(s);

            if (psb.Platform == portSpec)
            {
                Console.WriteLine("Already at the same platform, Skip.");
            }
            else
            {
                psb.SwitchSpec(portSpec);
                psb.Merge();
                File.WriteAllBytes(Path.ChangeExtension(s, $".{portSpec}.psb"), psb.Build());
                Console.WriteLine($"Convert {name} succeed.");
            }
        }
コード例 #7
0
        public static Dictionary <string, Bitmap> SplitTexture(PsbDictionary tex, PsbSpec spec)
        {
            var icon    = (PsbDictionary)tex["icon"];
            var texture = (PsbDictionary)tex["texture"];

            //var mipmap = (PsbDictionary)texture["mipMap"]; //TODO: Mipmap?
            Dictionary <string, Bitmap> textures = new Dictionary <string, Bitmap>(icon.Count);

            var md = PsbResCollector.GenerateMotionResMetadata(texture, (PsbResource)texture["pixel"]);

            md.Spec = spec; //Important
            Bitmap bmp = md.ToImage();

            foreach (var iconPair in icon)
            {
                var    info   = (PsbDictionary)iconPair.Value;
                var    width  = (int)(PsbNumber)info["width"];
                var    height = (int)(PsbNumber)info["height"];
                var    top    = (int)(PsbNumber)info["top"];
                var    left   = (int)(PsbNumber)info["left"];
                Bitmap b      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
#if USE_FASTBITMAP
                using (FastBitmap f = b.FastLock())
                {
                    f.CopyRegion(bmp, new Rectangle(left, top, width, height), new Rectangle(0, 0, b.Width, b.Height));
                }
#else
                Graphics g = Graphics.FromImage(b);
                //g.InterpolationMode = InterpolationMode.NearestNeighbor;
                //g.PixelOffsetMode = PixelOffsetMode.Half;
                g.DrawImage(bmp, new Rectangle(0, 0, b.Width, b.Height), new Rectangle(left, top, width, height),
                            GraphicsUnit.Pixel);
                g.Dispose();
#endif
                textures.Add(iconPair.Key, b);
            }
            bmp.Dispose();
            return(textures);
        }
コード例 #8
0
        /// <summary>
        /// Try to switch Spec
        /// </summary>
        /// <param name="psb"></param>
        /// <param name="targetSpec"></param>
        /// <param name="pixelFormat"></param>
        public static void SwitchSpec(this PSB psb, PsbSpec targetSpec, PsbPixelFormat pixelFormat = PsbPixelFormat.None)
        {
            if (targetSpec == PsbSpec.other)
            {
                return;
            }

            //Alternative //TODO: Alternative table?
            bool isAlternative  = false;
            var  realTargetSpec = PsbSpec.common;

            var original = psb.Platform;

            if (original == PsbSpec.ems)
            {
                original = PsbSpec.common;
            }

            if (targetSpec == PsbSpec.ems)
            {
                isAlternative  = true;
                realTargetSpec = targetSpec;
                targetSpec     = PsbSpec.common;
            }

            if (targetSpec == PsbSpec.krkr) //krkr can not select pixel format
            {
                switch (original)
                {
                case PsbSpec.win:
                {
                    Common2KrkrConverter winKrkr = new Common2KrkrConverter();
                    winKrkr.Convert(psb);
                    break;
                }

                case PsbSpec.common:
                {
                    Common2KrkrConverter commonKrkr = new Common2KrkrConverter();
                    commonKrkr.Convert(psb);
                    break;
                }

                default:
                    psb.Platform = targetSpec;
                    break;
                }
            }

            else if (targetSpec == PsbSpec.win)
            {
                switch (original)
                {
                case PsbSpec.krkr:
                    Krkr2CommonConverter krkr2Win = new Krkr2CommonConverter(true);
                    krkr2Win.Convert(psb);
                    break;

                case PsbSpec.common:
                    CommonWinConverter winCommon = new CommonWinConverter();
                    winCommon.Convert(psb);
                    break;

                default:
                    psb.Platform = targetSpec;
                    break;
                }
            }

            else if (targetSpec == PsbSpec.common || targetSpec == PsbSpec.ems)
            {
                switch (original)
                {
                case PsbSpec.krkr:
                    Krkr2CommonConverter krkr2Common = new Krkr2CommonConverter();
                    krkr2Common.Convert(psb);
                    break;

                case PsbSpec.win:
                    CommonWinConverter commonWin = new CommonWinConverter();
                    commonWin.Convert(psb);
                    break;

                case PsbSpec.common:
                case PsbSpec.ems:
                    psb.Platform = targetSpec;
                    break;

                default:
                    psb.Platform = targetSpec;
                    break;
                }
            }

            else
            {
                psb.Platform = targetSpec;
            }

            if (isAlternative)
            {
                psb.Platform = realTargetSpec;
            }
        }