/// <summary> /// Extract resource info /// </summary> /// <param name="d">PsbObject which contains "pixel"</param> /// <param name="r">Resource</param> /// <param name="duplicatePalette">When set to true, Pal.Data may not be set!</param> /// <returns></returns> internal static ImageMetadata GenerateImageMetadata(PsbDictionary d, PsbResource r = null, bool duplicatePalette = false) { if (r == null) { if (d.ContainsKey(Consts.ResourceKey) && d[Consts.ResourceKey] is PsbResource rr) { r = rr; } else //this may find Pal { r = d.Values.FirstOrDefault(v => v is PsbResource) as PsbResource; } } bool is2D = false; var part = GetPartName(d); var name = d.GetName(); RectangleF clip = RectangleF.Empty; if (d["clip"] is PsbDictionary clipDic && clipDic.Count > 0) { is2D = true; clip = RectangleF.FromLTRB( left: clipDic["left"] == null ? 0f : (float)(PsbNumber)clipDic["left"], top: clipDic["top"] == null ? 0f : (float)(PsbNumber)clipDic["top"], right: clipDic["right"] == null ? 1f : (float)(PsbNumber)clipDic["right"], bottom: clipDic["bottom"] == null ? 1f : (float)(PsbNumber)clipDic["bottom"] ); } var compress = PsbCompressType.None; if (d["compress"] is PsbString sc) { is2D = true; if (sc.Value.ToUpperInvariant() == "RL") { compress = PsbCompressType.RL; } } int width = 1, height = 1; float originX = 0, originY = 0; if (d["width"] is PsbNumber nw) { is2D = true; width = (int)nw; } if (d["height"] is PsbNumber nh) { is2D = true; height = (int)nh; } if (d["originX"] is PsbNumber nx) { is2D = true; originX = (float)nx; } if (d["originY"] is PsbNumber ny) { is2D = true; originY = (float)ny; } PsbString typeString = null; if (d["type"] is PsbString typeStr) { typeString = typeStr; } int top = 0, left = 0; if (d["top"] is PsbNumber nt) { is2D = true; top = (int)nt; } if (d["left"] is PsbNumber nl) { is2D = true; left = (int)nl; } PsbResource palResource = null; PsbString palTypeString = null; if (d["pal"] is PsbResource palRes) { if (duplicatePalette) { palResource = new PsbResource(palRes.Index); d["pal"] = palResource; } else { palResource = palRes; } palTypeString = d["palType"] as PsbString; } var md = new ImageMetadata() { Index = r.Index ?? int.MaxValue, Compress = compress, Name = name, Part = part, Clip = clip, Is2D = is2D, OriginX = originX, OriginY = originY, Top = top, Left = left, Width = width, Height = height, TypeString = typeString, Resource = r, Palette = palResource, PaletteTypeString = palTypeString }; return(md); }
private void Add(PSB psb) { //add `easing` if (!psb.Objects.ContainsKey("easing")) { psb.Objects.Add("easing", new PsbCollection(0)); } //add `/object/*/motion/*/bounds` //add `/object/*/motion/*/layerIndexMap` var obj = (PsbDictionary)psb.Objects["object"]; foreach (var o in obj) { //var name = o.Key; foreach (var m in (PsbDictionary)((PsbDictionary)o.Value)["motion"]) { if (m.Value is PsbDictionary mDic) { if (!mDic.ContainsKey("bounds")) { var bounds = new PsbDictionary(4) { { "top", PsbNumber.Zero }, { "left", PsbNumber.Zero }, { "right", PsbNumber.Zero }, { "bottom", PsbNumber.Zero } }; mDic.Add("bounds", bounds); } if (!(mDic["layer"] is PsbCollection col)) { continue; } if (!mDic.ContainsKey("layerIndexMap")) { var layerIndexList = new List <string>(); LayerTravel(col, layerIndexList); var layerIndexMap = new PsbDictionary(layerIndexList.Count); int index = 0; foreach (var layerName in layerIndexList) { if (layerIndexMap.ContainsKey(layerName)) { continue; } layerIndexMap.Add(layerName, new PsbNumber(index)); index++; } mDic.Add("layerIndexMap", layerIndexMap); } } } } void LayerTravel(PsbCollection collection, List <string> indexList) { foreach (var col in collection) { if (col is PsbDictionary dic && dic.ContainsKey("children")) { if (dic["label"] is PsbString str) { indexList.Add(str.Value); } if (dic["children"] is PsbCollection childrenCollection) { LayerTravel(childrenCollection, indexList); } } } } }