public static MPQFont LoadFromStream(Stream fontStream, Stream tableStream) { var result = new MPQFont { FontImageSet = ImageSet.LoadFromStream(fontStream) }; var br = new BinaryReader(tableStream); var wooCheck = Encoding.UTF8.GetString(br.ReadBytes(4)); if (wooCheck != "Woo!") { throw new OpenDiablo2Exception("Error loading font. Missing the Woo!"); } br.ReadBytes(8); while (tableStream.Position < tableStream.Length) { br.ReadBytes(3); var size = new Size(br.ReadByte(), br.ReadByte()); br.ReadBytes(3); var charCode = (char)br.ReadByte(); result.CharacterMetric[charCode] = size; br.ReadBytes(5); } return(result); }
public static ImageSet LoadFromStream(Stream stream) { var br = new BinaryReader(stream); var result = new ImageSet { version = br.ReadUInt32(), unknown1 = br.ReadUInt32(), unknown2 = br.ReadUInt32(), termination = br.ReadUInt32(), Directions = br.ReadUInt32(), FramesPerDirection = br.ReadUInt32() }; result.framePointers = new uint[result.Directions * result.FramesPerDirection]; for (var i = 0; i < result.Directions * result.FramesPerDirection; i++) { result.framePointers[i] = br.ReadUInt32(); } result.Frames = new ImageFrame[result.Directions * result.FramesPerDirection]; for (var i = 0; i < result.Directions * result.FramesPerDirection; i++) { stream.Seek(result.framePointers[i], SeekOrigin.Begin); result.Frames[i] = new ImageFrame { Flip = br.ReadUInt32(), Width = br.ReadUInt32(), Height = br.ReadUInt32(), OffsetX = br.ReadInt32(), OffsetY = br.ReadInt32(), Unknown = br.ReadUInt32(), NextBlock = br.ReadUInt32(), Length = br.ReadUInt32() }; result.Frames[i].ImageData = new Int16[result.Frames[i].Width * result.Frames[i].Height]; for (int ty = 0; ty < result.Frames[i].Height; ty++) { for (int tx = 0; tx < result.Frames[i].Width; tx++) { result.Frames[i].ImageData[tx + (ty * result.Frames[i].Width)] = -1; } } int x = 0; int y = (int)result.Frames[i].Height - 1; while (true) { var b = br.ReadByte(); if (b == 0x80) { if (y == 0) { break; } y--; x = 0; continue; } if ((b & 0x80) > 0) { var transparentPixelsToWrite = b & 0x7F; Enumerable.Repeat <Int16>(-1, transparentPixelsToWrite).ToArray() .CopyTo(result.Frames[i].ImageData, x + (y * result.Frames[i].Width)); x += transparentPixelsToWrite; continue; } br.ReadBytes(b).CopyTo(result.Frames[i].ImageData, x + (y * result.Frames[i].Width)); x += b; } } return(result); }