From 246c5e45263597e9396fa0a168d8dc154ba1fe4d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 30 Nov 2013 18:28:29 +1300 Subject: [PATCH] Update Utility --shp to match. --- OpenRA.Utility/Command.cs | 36 +++++++++++++++--------------------- OpenRA.Utility/Program.cs | 2 +- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/OpenRA.Utility/Command.cs b/OpenRA.Utility/Command.cs index b32f2393d8..0a2fc742a2 100644 --- a/OpenRA.Utility/Command.cs +++ b/OpenRA.Utility/Command.cs @@ -43,38 +43,32 @@ namespace OpenRA.Utility public static void ConvertPngToShp(string[] args) { - var src = args[1]; - var dest = Path.ChangeExtension(src, ".shp"); - var width = int.Parse(args[2]); + var dest = args[1].Split('-').First() + ".shp"; + var frames = args.Skip(1).Select(a => PngLoader.Load(a)); - var srcImage = PngLoader.Load(src); - - if (srcImage.Width % width != 0) - throw new InvalidOperationException("Bogus width; not a whole number of frames"); + var size = frames.First().Size; + if (frames.Any(f => f.Size != size)) + throw new InvalidOperationException("All frames must be the same size"); using (var destStream = File.Create(dest)) - ShpReader.Write(destStream, width, srcImage.Height, - srcImage.ToFrames(width)); + ShpReader.Write(destStream, size.Width, size.Height, frames.Select(f => f.ToBytes())); Console.WriteLine(dest + " saved."); } - static IEnumerable ToFrames(this Bitmap bitmap, int width) + static byte[] ToBytes(this Bitmap bitmap) { - for (var x = 0; x < bitmap.Width; x += width) - { - var data = bitmap.LockBits(new Rectangle(x, 0, width, bitmap.Height), ImageLockMode.ReadOnly, - PixelFormat.Format8bppIndexed); + var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, + PixelFormat.Format8bppIndexed); - var bytes = new byte[width * bitmap.Height]; - for (var i = 0; i < bitmap.Height; i++) - Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + i * data.Stride), - bytes, i * width, width); + var bytes = new byte[bitmap.Width * bitmap.Height]; + for (var i = 0; i < bitmap.Height; i++) + Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + i * data.Stride), + bytes, i * bitmap.Width, bitmap.Width); - bitmap.UnlockBits(data); + bitmap.UnlockBits(data); - yield return bytes; - } + return bytes; } public static void ConvertShpToPng(string[] args) diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index 7ebbc0425b..37ef9407d5 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -56,7 +56,7 @@ namespace OpenRA.Utility Console.WriteLine("Usage: OpenRA.Utility.exe [OPTION] [ARGS]"); Console.WriteLine(); Console.WriteLine(" --settings-value KEY Get value of KEY from settings.yaml"); - Console.WriteLine(" --shp PNGFILE FRAMEWIDTH Convert a single PNG with multiple frames appended after another to a SHP"); + Console.WriteLine(" --shp PNGFILE [PNGFILE ...] Combine a list of PNG images into a SHP"); Console.WriteLine(" --png SPRITEFILE PALETTE [--noshadow] [--nopadding] Convert a shp/tmp/R8 to a series of PNGs, optionally removing shadow"); Console.WriteLine(" --fromd2 DUNE2SHP C&CSHP Convert a Dune II SHP (C&C mouse cursor) to C&C SHP format."); Console.WriteLine(" --extract MOD[,MOD]* FILES [--userdir] Extract files from mod packages to the current (or user) directory");