PNG spritesheet support, along with PaletteFromPng.

Cursor palette loader can now be specified via yaml.
This commit is contained in:
Andre Mohren
2018-07-24 15:56:04 +02:00
committed by reaperrr
parent 48248266a8
commit 693b5a54af
9 changed files with 549 additions and 226 deletions

View File

@@ -35,14 +35,14 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
var inputFiles = GlobArgs(args).OrderBy(a => a).ToList();
var dest = inputFiles[0].Split('-').First() + ".shp";
var frames = inputFiles.Select(a => PngLoader.Load(a));
var size = frames.First().Size;
if (frames.Any(f => f.Size != size))
var frames = inputFiles.Select(a => new Png(File.OpenRead(a))).ToList();
var size = new Size(frames[0].Width, frames[0].Height);
if (frames.Any(f => f.Width != size.Width || f.Height != size.Height))
throw new InvalidOperationException("All frames must be the same size");
using (var destStream = File.Create(dest))
ShpTDSprite.Write(destStream, size, frames.Select(f => ToBytes(f)));
ShpTDSprite.Write(destStream, size, frames.Select(f => f.Data));
Console.WriteLine(dest + " saved.");
}
@@ -53,20 +53,5 @@ namespace OpenRA.Mods.Common.UtilityCommands
foreach (var path in Glob.Expand(args[i]))
yield return path;
}
static byte[] ToBytes(Bitmap bitmap)
{
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly,
PixelFormat.Format8bppIndexed);
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);
return bytes;
}
}
}

View File

@@ -0,0 +1,39 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.IO;
using System.Linq;
using OpenRA.FileFormats;
namespace OpenRA.Mods.Common.UtilityCommands
{
public class PngSheetExportMetadataCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--png-sheet-export"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length == 2;
}
[Desc("PNGFILE", "Export png metadata to yaml")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
using (var s = File.OpenRead(args[1]))
{
var png = new Png(s);
png.EmbeddedData.Select(m => new MiniYamlNode(m.Key, m.Value))
.ToList()
.WriteToFile(Path.ChangeExtension(args[1], "yaml"));
}
}
}
}

View File

@@ -0,0 +1,87 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.IO;
using System.Net;
using System.Text;
using ICSharpCode.SharpZipLib.Checksums;
namespace OpenRA.Mods.Common.UtilityCommands
{
public class PngSheetImportMetadataCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--png-sheet-import"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length == 2;
}
[Desc("PNGFILE", "Import yaml metadata to png")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: This could eventually be merged into the Png class (add a Write method), however this requires complex png writing algorythms.
var rs = File.OpenRead(args[1]);
var ws = new MemoryStream();
using (var bw = new BinaryWriter(ws))
{
bw.Write(rs.ReadBytes(8));
var crc32 = new Crc32();
for (;;)
{
var length = IPAddress.NetworkToHostOrder(rs.ReadInt32());
var type = Encoding.UTF8.GetString(rs.ReadBytes(4));
var content = rs.ReadBytes(length);
var crc = rs.ReadUInt32();
switch (type)
{
case "tEXt":
break;
case "IEND":
rs.Close();
foreach (var node in MiniYaml.FromFile(Path.ChangeExtension(args[1], "yaml")))
{
bw.Write(IPAddress.NetworkToHostOrder(node.Key.Length + 1 + node.Value.Value.Length));
bw.Write("tEXt".ToCharArray());
bw.Write(node.Key.ToCharArray());
bw.Write((byte)0x00);
bw.Write(node.Value.Value.ToCharArray());
crc32.Reset();
crc32.Update(Encoding.ASCII.GetBytes("tEXt"));
crc32.Update(Encoding.ASCII.GetBytes(node.Key + (char)0x00 + node.Value.Value));
bw.Write((uint)IPAddress.NetworkToHostOrder((int)crc32.Value));
}
bw.Write(0);
bw.Write(type.ToCharArray());
bw.Write(crc);
File.WriteAllBytes(args[1], ws.ToArray());
ws.Close();
return;
default:
bw.Write(IPAddress.NetworkToHostOrder(length));
bw.Write(type.ToCharArray());
bw.Write(content);
bw.Write(crc);
break;
}
}
}
}
}
}