|
| 1 | +using SixLabors.ImageSharp; |
| 2 | +using SixLabors.ImageSharp.PixelFormats; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +// this should really be in OpenLoco/OpenGraphics |
| 6 | + |
| 7 | +var baseDir = "Q:\\Games\\Locomotion\\Palettes"; |
| 8 | +var paletteFile = Path.Combine(baseDir, "palette.png"); |
| 9 | +var img = Image.Load<Rgba32>(paletteFile); |
| 10 | + |
| 11 | +MakeJascPal(baseDir, img); |
| 12 | +MakeMicrosoftPal(baseDir, img); |
| 13 | +MakePhotoshopAco(baseDir, img); |
| 14 | +MakePhotoshopAct(baseDir, img); |
| 15 | + |
| 16 | +static void MakeJascPal(string baseDir, Image<Rgba32> img) |
| 17 | +{ |
| 18 | + // header |
| 19 | + var lines = new List<string>() |
| 20 | + { |
| 21 | + "JASC-PAL", // header |
| 22 | + "0100", // version |
| 23 | + "256", // colours in palette file |
| 24 | + }; |
| 25 | + |
| 26 | + // colours |
| 27 | + for (var y = 0; y < img.Height; y++) |
| 28 | + { |
| 29 | + for (var x = 0; x < img.Width; x++) |
| 30 | + { |
| 31 | + var col = img[x, y]; |
| 32 | + lines.Add($"{col.R} {col.G} {col.B}"); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + File.WriteAllLines(Path.Combine(baseDir, "locopal.jasc.pal"), lines); |
| 37 | +} |
| 38 | + |
| 39 | +static void MakeMicrosoftPal(string baseDir, Image<Rgba32> img) |
| 40 | +{ |
| 41 | + // from https://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf |
| 42 | + |
| 43 | + var count = (uint)img.Width * (uint)img.Height; |
| 44 | + |
| 45 | + using var fs = new FileStream(Path.Combine(baseDir, "locopal.ms.pal"), FileMode.Create, FileAccess.Write, FileShare.None); |
| 46 | + using var bw = new BinaryWriter(fs); |
| 47 | + |
| 48 | + // RIFF header |
| 49 | + bw.Write(Encoding.ASCII.GetBytes("RIFF")); |
| 50 | + bw.Write(16 + (count * 4)); // exclude RIFF and filesize from the file size calc |
| 51 | + bw.Write(Encoding.ASCII.GetBytes("PAL ")); |
| 52 | + |
| 53 | + // data chunk |
| 54 | + bw.Write(Encoding.ASCII.GetBytes("data")); |
| 55 | + bw.Write(4 + (count * 4)); // chunk size |
| 56 | + bw.Write((ushort)0x0300); // palVersion |
| 57 | + bw.Write((ushort)count); // palNumEntries |
| 58 | + |
| 59 | + // colours |
| 60 | + for (var y = 0; y < img.Height; y++) |
| 61 | + { |
| 62 | + for (var x = 0; x < img.Width; x++) |
| 63 | + { |
| 64 | + var col = img[x, y]; |
| 65 | + bw.Write(col.R); // peRed |
| 66 | + bw.Write(col.G); // peGreen |
| 67 | + bw.Write(col.B); // peBlue |
| 68 | + bw.Write((byte)0x0); // peFlags (usually 0) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +static void MakePhotoshopAco(string baseDir, Image<Rgba32> img) |
| 74 | +{ |
| 75 | + var count = (uint)img.Width * (uint)img.Height; |
| 76 | + using var fs = new FileStream(Path.Combine(baseDir, "locopal.aco"), FileMode.Create, FileAccess.Write, FileShare.None); |
| 77 | + |
| 78 | + // Write the ACO file header |
| 79 | + WriteInt16(fs, 1); // Version 1 |
| 80 | + WriteInt16(fs, (short)count); |
| 81 | + |
| 82 | + // Write each colour |
| 83 | + for (var y = 0; y < img.Height; y++) |
| 84 | + { |
| 85 | + for (var x = 0; x < img.Width; x++) |
| 86 | + { |
| 87 | + var col = img[x, y]; |
| 88 | + WriteInt16(fs, 0); // Colour space: RGB |
| 89 | + WriteInt16(fs, (short)(col.R * 256)); // Red channel |
| 90 | + WriteInt16(fs, (short)(col.G * 256)); // Green channel |
| 91 | + WriteInt16(fs, (short)(col.B * 256)); // Blue channel |
| 92 | + WriteInt16(fs, 0); // Padding |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static void WriteInt16(Stream stream, short value) |
| 97 | + { |
| 98 | + stream.WriteByte((byte)(value >> 8)); |
| 99 | + stream.WriteByte((byte)value); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +static void MakePhotoshopAct(string baseDir, Image<Rgba32> img) |
| 104 | +{ |
| 105 | + var count = (uint)img.Width * (uint)img.Height; |
| 106 | + |
| 107 | + // ACT files expect 256 colors |
| 108 | + if (count != 256) |
| 109 | + { |
| 110 | + throw new ArgumentException("ACT files require exactly 256 colours."); |
| 111 | + } |
| 112 | + |
| 113 | + using var fs = new FileStream(Path.Combine(baseDir, "locopal.act"), FileMode.Create, FileAccess.Write, FileShare.None); |
| 114 | + |
| 115 | + // Write each colour |
| 116 | + for (var y = 0; y < img.Height; y++) |
| 117 | + { |
| 118 | + for (var x = 0; x < img.Width; x++) |
| 119 | + { |
| 120 | + var col = img[x, y]; |
| 121 | + fs.WriteByte(col.R); |
| 122 | + fs.WriteByte(col.G); |
| 123 | + fs.WriteByte(col.B); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments