private void DumpTexture(TexturePack texturePack, CMerlinTexture texture, string directoryPath) { int mipmapIndex = 0; foreach (var mipmap in texture.Mipmaps) { Console.WriteLine("Dumping decal mipmap " + mipmapIndex); using (Bitmap bitmap = new Bitmap(mipmap.ImageDimensions.Width, mipmap.ImageDimensions.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed)) { var palette = bitmap.Palette; for (int i = 0; i < palette.Entries.Length; i++) { palette.Entries.SetValue(texturePack.Palette[i], i); } // Need to set the palette as we were given a clone of the original bitmap.Palette = palette; int imageDataPtr = 0; for (int y = 0; y < mipmap.ImageDimensions.Height; y++) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, y, mipmap.ImageDimensions.Width, 1), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); byte[] row = new byte[mipmap.ImageDimensions.Width]; foreach (var span in mipmap.PixelSpans[y]) { for (int x = span.StartIndex >> mipmapIndex; x < (span.EndIndex + 1) >> mipmapIndex; x++) { row[x] = mipmap.ImageData[imageDataPtr++]; } } Marshal.Copy(row, 0, bitmapData.Scan0, row.Length); bitmap.UnlockBits(bitmapData); } if (imageDataPtr != mipmap.ImageData.Length) { throw new InvalidOperationException(); } // Convert from column-major bottom-up Hover format to human-editable foramt bitmap.RotateFlip(RotateFlipType.Rotate90FlipX); bitmap.Save(directoryPath + "\\" + texture.Name + "_MIPMAP_" + mipmap.Level + ".gif", ImageFormat.Gif); mipmapIndex++; } } }
private void CombineTexturesToFile(string xmlFile, string texturePackFile) { Console.WriteLine("Creating {0} from XML file {1}:", texturePackFile, xmlFile); XmlDocument xmlDocument = new XmlDocument(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable); xmlNamespaceManager.AddNamespace("t", NAMESPACE); xmlDocument.Load(xmlFile); Console.WriteLine("Processing palette..."); TexturePack texturePack = new TexturePack(); Color[] palette = new Color[TexturePack.PALETTE_ENTRIES]; foreach (var paletteEntry in xmlDocument.SelectNodes("/t:TexturePack/t:Palette/t:Entry", xmlNamespaceManager).OfType <XmlNode>()) { palette[int.Parse(paletteEntry.Attributes["Index"].Value)] = Color.FromArgb( int.Parse(paletteEntry.Attributes["Red"].Value), int.Parse(paletteEntry.Attributes["Green"].Value), int.Parse(paletteEntry.Attributes["Blue"].Value)); } texturePack.Palette = palette.ToArray(); List <CMerlinTexture> textures = new List <CMerlinTexture>(); foreach (var textureDef in xmlDocument.SelectNodes("/t:TexturePack/t:Textures/t:Texture", xmlNamespaceManager).OfType <XmlNode>()) { string textureName = textureDef.Attributes["Name"].Value; Console.WriteLine("Processing texture {0}...", textureName); var mipmapDef = textureDef.SelectSingleNode("t:MipMap", xmlNamespaceManager); string bitmapPath = Path.Combine(Path.GetDirectoryName(xmlFile), mipmapDef.Attributes["Data"].Value); using (Bitmap bitmap = new Bitmap(bitmapPath)) { CMerlinTexture texture = new CMerlinTexture(); texture.Name = textureName; texture.UpdateImage( texturePack, bool.Parse(textureDef.Attributes["HasTransparency"].Value), bitmap); textures.Add(texture); } } texturePack.Textures = textures; Console.WriteLine("Saving to {0}", texturePackFile); SaveHoverFile(texturePackFile, texturePack); }