void CreateNewTileset() { this.Show(); using (var formNew = new FormNew()) if (DialogResult.OK == formNew.ShowDialog()) { srcfile = formNew.ImageFile; this.size = formNew.TileSize; var bitmap = new Bitmap(srcfile); if (!formNew.PaletteFromImage) { var terrainPalette = new ImmutablePalette(formNew.PaletteFile, new int[0]); bitmap.Palette = terrainPalette.AsSystemPalette(); } InitializeSurface(bitmap); } }
public void CreateNewTileset() { this.Show(); using (var formNew = new FormNew { }) if (DialogResult.OK == formNew.ShowDialog()) { PaletteFromImage = formNew.PaletteFromImage; PaletteFile = formNew.PaletteFile; ImageFile = formNew.ImageFile; TileSize = formNew.TileSize; srcfile = ImageFile; this.size = TileSize; surface1.TileSize = TileSize; Bitmap rbitmap; using (var fbitmap = new Bitmap(ImageFile)) rbitmap = fbitmap.Clone(new Rectangle(0, 0, fbitmap.Width, fbitmap.Height), fbitmap.PixelFormat); int[] shadowIndex = { }; if (!PaletteFromImage) { TerrainPalette = new ImmutablePalette(PaletteFile, shadowIndex); rbitmap.Palette = TerrainPalette.AsSystemPalette(); } surface1.Image = (Bitmap)rbitmap; surface1.TilesPerRow = surface1.Image.Size.Width / surface1.TileSize; surface1.Image.SetResolution(96, 96); // people keep being noobs about DPI, and GDI+ cares. surface1.TerrainTypes = new int[surface1.Image.Width / size, surface1.Image.Height / size]; /* all passable by default */ surface1.Templates = new List<Template>(); surface1.Size = surface1.Image.Size; surface1.Enabled = true; Load(); } }
void IUtilityCommand.Run(Utility utility, string[] args) { // HACK: The engine code assumes that Game.modData is set. var modData = Game.ModData = utility.ModData; var src = args[1]; var shadowIndex = new int[] { }; if (args.Contains("--noshadow")) { Array.Resize(ref shadowIndex, shadowIndex.Length + 3); shadowIndex[shadowIndex.Length - 1] = 1; shadowIndex[shadowIndex.Length - 2] = 3; shadowIndex[shadowIndex.Length - 3] = 4; } var palette = new ImmutablePalette(args[2], shadowIndex); var frames = SpriteLoader.GetFrames(File.OpenRead(src), modData.SpriteLoaders); var usePadding = !args.Contains("--nopadding"); var count = 0; var prefix = Path.GetFileNameWithoutExtension(src); foreach (var frame in frames) { var frameSize = usePadding && !frame.DisableExportPadding ? frame.FrameSize : frame.Size; var offset = usePadding && !frame.DisableExportPadding ? (frame.Offset - 0.5f * new float2(frame.Size - frame.FrameSize)).ToInt2() : int2.Zero; // shp(ts) may define empty frames if (frameSize.Width == 0 && frameSize.Height == 0) { count++; continue; } using (var bitmap = new Bitmap(frameSize.Width, frameSize.Height, PixelFormat.Format8bppIndexed)) { bitmap.Palette = palette.AsSystemPalette(); var data = bitmap.LockBits(new Rectangle(0, 0, frameSize.Width, frameSize.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); // Clear the frame if (usePadding && !frame.DisableExportPadding) { var clearRow = new byte[data.Stride]; for (var i = 0; i < frameSize.Height; i++) Marshal.Copy(clearRow, 0, new IntPtr(data.Scan0.ToInt64() + i * data.Stride), data.Stride); } for (var i = 0; i < frame.Size.Height; i++) { var destIndex = new IntPtr(data.Scan0.ToInt64() + (i + offset.Y) * data.Stride + offset.X); Marshal.Copy(frame.Data, i * frame.Size.Width, destIndex, frame.Size.Width); } bitmap.UnlockBits(data); var filename = "{0}-{1:D4}.png".F(prefix, count++); bitmap.Save(filename); } } Console.WriteLine("Saved {0}-[0..{1}].png", prefix, count - 1); }
public static void ConvertSpriteToPng(string[] args) { var src = args[1]; var shadowIndex = new int[] { }; if (args.Contains("--noshadow")) { Array.Resize(ref shadowIndex, shadowIndex.Length + 3); shadowIndex[shadowIndex.Length - 1] = 1; shadowIndex[shadowIndex.Length - 2] = 3; shadowIndex[shadowIndex.Length - 3] = 4; } var palette = new ImmutablePalette(args[2], shadowIndex); ISpriteSource source; using (var stream = File.OpenRead(src)) source = SpriteSource.LoadSpriteSource(stream, src); // The r8 padding requires external information that we can't access here. var usePadding = !(args.Contains("--nopadding") || source is R8Reader); var count = 0; var prefix = Path.GetFileNameWithoutExtension(src); foreach (var frame in source.Frames) { var frameSize = usePadding ? frame.FrameSize : frame.Size; var offset = usePadding ? (frame.Offset - 0.5f * new float2(frame.Size - frame.FrameSize)).ToInt2() : int2.Zero; // shp(ts) may define empty frames if (frameSize.Width == 0 && frameSize.Height == 0) { count++; continue; } using (var bitmap = new Bitmap(frameSize.Width, frameSize.Height, PixelFormat.Format8bppIndexed)) { bitmap.Palette = palette.AsSystemPalette(); var data = bitmap.LockBits(new Rectangle(0, 0, frameSize.Width, frameSize.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); // Clear the frame if (usePadding) { var clearRow = new byte[data.Stride]; for (var i = 0; i < frameSize.Height; i++) Marshal.Copy(clearRow, 0, new IntPtr(data.Scan0.ToInt64() + i * data.Stride), data.Stride); } for (var i = 0; i < frame.Size.Height; i++) { var destIndex = new IntPtr(data.Scan0.ToInt64() + (i + offset.Y) * data.Stride + offset.X); Marshal.Copy(frame.Data, i * frame.Size.Width, destIndex, frame.Size.Width); } bitmap.UnlockBits(data); var filename = "{0}-{1:D4}.png".F(prefix, count++); bitmap.Save(filename); } } Console.WriteLine("Saved {0}-[0..{1}].png", prefix, count - 1); }