/// <summary> /// Loads this BgfFile instance from XML/BMP /// </summary> /// <param name="Filename"></param> public void LoadXml(string Filename) { string path = Path.GetDirectoryName(Filename); // save raw filename without path or extensions Filename = Path.GetFileNameWithoutExtension(Filename); // init XML reader XmlReader reader = XmlReader.Create(Filename); // rootnode reader.ReadToFollowing("bgf"); uint version = Convert.ToUInt32(reader["version"]); if (version >= BgfFile.VERSION9) { ShrinkFactor = Convert.ToUInt32(reader["shrink"]); // skip maxindices, we use dynamic getter // MaxIndices = Convert.ToUInt32(reader["maxindices"]); // frames reader.ReadToFollowing("frames"); int FrameCount = Convert.ToInt32(reader["count"]); Frames.Capacity = FrameCount; for (int i = 0; i < FrameCount; i++) { reader.ReadToFollowing("frame"); uint width = Convert.ToUInt32(reader["width"]); uint height = Convert.ToUInt32(reader["height"]); int xoffset = Convert.ToInt32(reader["xoffset"]); int yoffset = Convert.ToInt32(reader["yoffset"]); string file = reader["file"]; // hotspots reader.ReadToFollowing("hotspots"); byte hotspotcount = Convert.ToByte(reader["count"]); List<BgfBitmapHotspot> hotspots = new List<BgfBitmapHotspot>(hotspotcount); for (int j = 0; j < hotspotcount; j++) { reader.ReadToFollowing("hotspot"); sbyte index = Convert.ToSByte(reader["index"]); int x = Convert.ToInt32(reader["x"]); int y = Convert.ToInt32(reader["y"]); BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y); hotspots.Add(hotspot); } // load bitmap from file Bitmap bmp = (Bitmap)Image.FromFile(path + "/" + file); byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp); BgfBitmap bgfBitmap = new BgfBitmap( (uint)i + 1, version, width, height, xoffset, yoffset, hotspots, false, 0, pixelData); // cleanp temporary bitmap bmp.Dispose(); Frames.Add(bgfBitmap); } // framesets reader.ReadToFollowing("framesets"); int FrameSetCount = Convert.ToInt32(reader["count"]); FrameSets.Capacity = FrameSetCount; for (int i = 0; i < FrameSetCount; i++) { reader.ReadToFollowing("frameset"); string[] indices = reader["indices"].Split(' '); List<int> intIndices = new List<int>(); foreach (string index in indices) intIndices.Add(Convert.ToInt32(index)); BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices); FrameSets.Add(bgfFrameSet); } } else throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ")."); }
public int ReadFrom(byte[] Buffer, int StartIndex = 0) { int cursor = StartIndex; uint signature = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; if (signature == SIGNATURE) { Version = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; // version has to be V9 or higher if (version >= VERSION9) { // name string has always fixed length Name = Encoding.Default.GetString(Buffer, cursor, NAMELENGTH); cursor += NAMELENGTH; uint frameCount = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; uint frameSetCount = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; // skip max indices, we use dynamic getter cursor += TypeSizes.INT; ShrinkFactor = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; Frames.Clear(); Frames.Capacity = (int)frameCount; for (uint i = 0; i < frameCount; i++) { BgfBitmap bgfBitmap = new BgfBitmap(i + 1, version, Buffer, cursor); cursor += bgfBitmap.ByteLength; Frames.Add(bgfBitmap); } FrameSets.Clear(); FrameSets.Capacity = (int)frameSetCount; for (uint i = 0; i < frameSetCount; i++) { BgfFrameSet frameSet = new BgfFrameSet(i + 1, Buffer, cursor); cursor += frameSet.ByteLength; FrameSets.Add(frameSet); } } else throw new Exception("Wrong file version: " + version + " (expected " + VERSION9 + " or higher)."); } else throw new Exception("Wrong file signature: " + signature + " (expected " + SIGNATURE + ")."); return cursor - StartIndex; }
public int ReadFrom(byte[] Buffer, int StartIndex = 0) { int cursor = StartIndex; uint signature = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; if (signature == SIGNATURE) { Version = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; // version has to be V9 or higher if (version >= VERSION9) { // name string has always fixed length Name = Encoding.Default.GetString(Buffer, cursor, NAMELENGTH); cursor += NAMELENGTH; uint frameCount = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; uint frameSetCount = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; // skip max indices, we use dynamic getter cursor += TypeSizes.INT; ShrinkFactor = BitConverter.ToUInt32(Buffer, cursor); cursor += TypeSizes.INT; Frames.Clear(); Frames.Capacity = (int)frameCount; for (uint i = 0; i < frameCount; i++) { BgfBitmap bgfBitmap = new BgfBitmap(i + 1, version, Buffer, cursor); cursor += bgfBitmap.ByteLength; Frames.Add(bgfBitmap); } FrameSets.Clear(); FrameSets.Capacity = (int)frameSetCount; for (uint i = 0; i < frameSetCount; i++) { BgfFrameSet frameSet = new BgfFrameSet(i + 1, Buffer, cursor); cursor += frameSet.ByteLength; FrameSets.Add(frameSet); } } else { throw new Exception("Wrong file version: " + version + " (expected " + VERSION9 + " or higher)."); } } else { throw new Exception("Wrong file signature: " + signature + " (expected " + SIGNATURE + ")."); } return(cursor - StartIndex); }
/// <summary> /// Loads this BgfFile instance from XML/BMP /// </summary> /// <param name="Filename"></param> public void LoadXml(string Filename) { string path = Path.GetDirectoryName(Filename); // save raw filename without path or extensions Filename = Path.GetFileNameWithoutExtension(Filename); // init XML reader XmlReader reader = XmlReader.Create(Filename); // rootnode reader.ReadToFollowing("bgf"); uint version = Convert.ToUInt32(reader["version"]); if (version >= BgfFile.VERSION9) { ShrinkFactor = Convert.ToUInt32(reader["shrink"]); // skip maxindices, we use dynamic getter // MaxIndices = Convert.ToUInt32(reader["maxindices"]); // frames reader.ReadToFollowing("frames"); int FrameCount = Convert.ToInt32(reader["count"]); Frames.Capacity = FrameCount; for (int i = 0; i < FrameCount; i++) { reader.ReadToFollowing("frame"); uint width = Convert.ToUInt32(reader["width"]); uint height = Convert.ToUInt32(reader["height"]); int xoffset = Convert.ToInt32(reader["xoffset"]); int yoffset = Convert.ToInt32(reader["yoffset"]); string file = reader["file"]; // hotspots reader.ReadToFollowing("hotspots"); byte hotspotcount = Convert.ToByte(reader["count"]); List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount); for (int j = 0; j < hotspotcount; j++) { reader.ReadToFollowing("hotspot"); sbyte index = Convert.ToSByte(reader["index"]); int x = Convert.ToInt32(reader["x"]); int y = Convert.ToInt32(reader["y"]); BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y); hotspots.Add(hotspot); } // load bitmap from file Bitmap bmp = (Bitmap)Image.FromFile(path + "/" + file); byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp); BgfBitmap bgfBitmap = new BgfBitmap( (uint)i + 1, version, width, height, xoffset, yoffset, hotspots, false, 0, pixelData); // cleanp temporary bitmap bmp.Dispose(); Frames.Add(bgfBitmap); } // framesets reader.ReadToFollowing("framesets"); int FrameSetCount = Convert.ToInt32(reader["count"]); FrameSets.Capacity = FrameSetCount; for (int i = 0; i < FrameSetCount; i++) { reader.ReadToFollowing("frameset"); string[] indices = reader["indices"].Split(' '); List <int> intIndices = new List <int>(); foreach (string index in indices) { intIndices.Add(Convert.ToInt32(index)); } BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices); FrameSets.Add(bgfFrameSet); } } else { throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ")."); } }
/// <summary> /// Import a storyboard given a full path to the storyboard bmp. The XML file for /// the storyboard must have a matching filename with the XML extension, and be in /// the same directory. /// </summary> /// <param name="Filename"></param> /// <returns></returns> public bool ImportStoryboard(string Filename) { string path = Path.GetDirectoryName(Filename); // raw filename without path or extensions string xmlFilename = Path.GetFileNameWithoutExtension(Filename); // index for reading bmp data, incremented as we split the giant bmp uint readoffset = 0; // open bmp into one giant bitmap Bitmap bmp = (Bitmap)Image.FromFile(Filename); uint totalWidth = (uint)bmp.Width; uint totalHeight = (uint)bmp.Height; byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp); // cleanp temporary bitmap bmp.Dispose(); // init XML reader XmlReader reader = XmlReader.Create(Path.Combine(path, xmlFilename) + FileExtensions.XML); // read in xml file // rootnode reader.ReadToFollowing("bgf"); uint version = Convert.ToUInt32(reader["version"]); if (version >= BgfFile.VERSION9) { ShrinkFactor = Convert.ToUInt32(reader["shrink"]); // skip maxindices, we use dynamic getter // MaxIndices = Convert.ToUInt32(reader["maxindices"]); // frames reader.ReadToFollowing("frames"); int FrameCount = Convert.ToInt32(reader["count"]); uint savedTotalHeight = Convert.ToUInt32(reader["totalheight"]); uint savedTotalWidth = Convert.ToUInt32(reader["totalwidth"]); // Saved dimensions must match those loaded for the storyboard bmp. if (savedTotalWidth != totalWidth || savedTotalHeight != totalHeight) { return(false); } Frames.Capacity = FrameCount; for (int i = 0; i < FrameCount; i++) { reader.ReadToFollowing("frame"); uint width = Convert.ToUInt32(reader["width"]); uint height = Convert.ToUInt32(reader["height"]); int xoffset = Convert.ToInt32(reader["xoffset"]); int yoffset = Convert.ToInt32(reader["yoffset"]); // hotspots reader.ReadToFollowing("hotspots"); byte hotspotcount = Convert.ToByte(reader["count"]); List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount); for (int j = 0; j < hotspotcount; j++) { reader.ReadToFollowing("hotspot"); sbyte index = Convert.ToSByte(reader["index"]); int x = Convert.ToInt32(reader["x"]); int y = Convert.ToInt32(reader["y"]); BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y); hotspots.Add(hotspot); } // get pixels for this frame from the single bmp byte[] pixels = new byte[height * width]; uint writeoffset = 0; for (int j = 0; j < height; ++j) { Wrapper.CopyMem(pixelData, (int)readoffset, pixels, (int)writeoffset, width); readoffset += totalWidth; writeoffset += width; } BgfBitmap bgfBitmap = new BgfBitmap( (uint)i + 1, version, width, height, xoffset, yoffset, hotspots, false, 0, pixels); Frames.Add(bgfBitmap); } // framesets reader.ReadToFollowing("framesets"); int FrameSetCount = Convert.ToInt32(reader["count"]); FrameSets.Capacity = FrameSetCount; for (int i = 0; i < FrameSetCount; i++) { reader.ReadToFollowing("frameset"); string[] indices = reader["indices"].Split(' '); List <int> intIndices = new List <int>(); foreach (string index in indices) { intIndices.Add(Convert.ToInt32(index)); } BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices); FrameSets.Add(bgfFrameSet); } } else { throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ")."); } return(true); }