Esempio n. 1
0
        /// <summary>
        /// <see cref="PsbResHelper.CollectResources"/> for packed-texture specs, like <see cref="PsbSpec.win"/>
        /// </summary>
        /// <param name="psb"></param>
        /// <returns></returns>
        public static List <ImageMetadata> CollectSplitResources(this PSB psb)
        {
            List <ImageMetadata> resList = new List <ImageMetadata>();
            var source = (PsbDictionary)psb.Objects["source"];

            foreach (var tex in source)
            {
                if (tex.Value is PsbDictionary texDic)
                {
                    var typeStr = (PsbString)texDic["texture"].Children("type");
                    typeStr = PsbPixelFormat.WinRGBA8.ToStringForPsb().ToPsbString();
                    var bmps  = SplitTexture(texDic, psb.Platform);
                    var icons = (PsbDictionary)texDic["icon"];
                    foreach (var iconPair in icons)
                    {
                        var res = new PsbResource
                        {
                            Data =
                                RL.GetPixelBytesFromImage(bmps[iconPair.Key], typeStr.Value.ToPsbPixelFormat(psb.Platform))
                        };
                        var icon = (PsbDictionary)iconPair.Value;
                        var md   = PsbResHelper.GenerateImageMetadata(icon, res);
                        md.Spec       = psb.Platform;
                        md.TypeString = typeStr;
                        resList.Add(md);
                    }
                }
            }

            return(resList);
        }
Esempio n. 2
0
        /// <summary>
        /// Combine Image texture parts
        /// </summary>
        /// <param name="psb">Image (image) type PSB</param>
        /// <returns></returns>
        public static Dictionary <string, Bitmap> CombineTachie(PSB psb)
        {
            if (psb.Type != PsbType.Tachie)
            {
                throw new NotSupportedException("PSB is not image type");
            }

            Dictionary <string, Bitmap> images = new Dictionary <string, Bitmap>();
            PsbList imageList = psb.Objects["imageList"] as PsbList;

            if (imageList == null)
            {
                return(images);
            }

            foreach (var psbValue in imageList)
            {
                var imageItem = psbValue as PsbDictionary;

                var texture = imageItem?["texture"] as PsbList;
                if (texture == null)
                {
                    continue;
                }

                var height = imageItem["height"].GetInt();
                var width  = imageItem["width"].GetInt();
                var label  = imageItem["label"].ToString();

                Bitmap img = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                using (var f = img.FastLock())
                {
                    foreach (var texObj in texture)
                    {
                        var tex = (PsbDictionary)texObj;
                        var md  = PsbResHelper.GenerateImageMetadata(tex.Children("image") as PsbDictionary);
                        md.Spec = psb.Platform;

                        var left    = tex["left"].GetInt();
                        var top     = tex["top"].GetInt();
                        var tHeight = tex["height"].GetInt();
                        var tWidth  = tex["width"].GetInt();

                        f.CopyRegion(md.ToImage(), new Rectangle(0, 0, md.Width, md.Height), new Rectangle(left, top, tWidth, tHeight));
                    }
                }

                images.Add(label, img);
            }

            return(images);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public List <ImageMetadata> FindFontResources(PSB psb, bool deDuplication = true)
        {
            List <ImageMetadata> resList = new List <ImageMetadata>(psb.Resources.Count);

            if (psb.Objects == null || !psb.Objects.ContainsKey(Source) || !(psb.Objects[Source] is PsbList list))
            {
                return(resList);
            }

            foreach (var item in list)
            {
                if (!(item is PsbDictionary obj))
                {
                    continue;
                }

                //TODO: deDuplication for resource (besides pal)
                var md = PsbResHelper.GenerateImageMetadata(obj, null);
                resList.Add(md);
            }

            return(resList);
        }
Esempio n. 5
0
        /// <summary>
        /// Split textures into parts and save to files
        /// </summary>
        /// <param name="psb">PSB</param>
        /// <param name="path">Save directory</param>
        /// <param name="option">Save option</param>
        /// <param name="imageFormat">Save format</param>
        /// <param name="pixelFormat">When save to PSB special formats, specific pixel format to use</param>
        public static void SplitTextureToFiles(this PSB psb, string path, PsbExtractOption option = PsbExtractOption.Extract,
                                               PsbImageFormat imageFormat = PsbImageFormat.png, PsbPixelFormat pixelFormat = PsbPixelFormat.None)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var source = (PsbDictionary)psb.Objects["source"];

            foreach (var texPair in source)
            {
                if (!(texPair.Value is PsbDictionary tex))
                {
                    continue;
                }

                var name = texPair.Key;
                if (!Directory.Exists(Path.Combine(path, name)))
                {
                    Directory.CreateDirectory(Path.Combine(path, name));
                }

                var icon    = (PsbDictionary)tex["icon"];
                var texture = (PsbDictionary)tex["texture"];

                //var mipmap = (PsbDictionary)texture["mipMap"]; //TODO: Mipmap?

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

                var md = PsbResHelper.GenerateImageMetadata(texture, pixelRes);
                md.Spec = psb.Platform; //Important
                Bitmap bmp = md.ToImage();

                foreach (var iconPair in icon)
                {
                    var    savePath = Path.Combine(path, name, iconPair.Key);
                    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"];
                    var    attr     = (int)(PsbNumber)info["attr"];
                    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

                    switch (option)
                    {
                    case PsbExtractOption.Decompress:
                        File.WriteAllBytes(savePath + ".raw", RL.GetPixelBytesFromImage(b, pixelFormat));
                        break;

                    case PsbExtractOption.Compress:
                        File.WriteAllBytes(savePath + ".rl", RL.CompressImage(b, pixelFormat));
                        break;

                    case PsbExtractOption.Original:
                    case PsbExtractOption.Extract:
                    default:
                        switch (imageFormat)
                        {
                        case PsbImageFormat.bmp:
                            b.Save(savePath + ".bmp", ImageFormat.Bmp);
                            break;

                        case PsbImageFormat.png:
                        default:
                            b.Save(savePath + ".png", ImageFormat.Png);
                            //b.Save(savePath + $"_{attr}.png", ImageFormat.png);
                            break;
                        }

                        break;
                    }
                }

                bmp.Dispose();
            }
        }