Use higher colour depth sprites in D2k.

This commit is contained in:
Paul Chote
2023-12-17 11:55:04 +00:00
committed by Gustas
parent f7f304a2e0
commit 34ff23d030
31 changed files with 2921 additions and 2348 deletions

View File

@@ -9,28 +9,104 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.SpriteLoaders;
using OpenRA.Primitives;
namespace OpenRA.Mods.D2k.SpriteLoaders
{
public class R8Loader : ISpriteLoader
{
sealed class R8Frame : ISpriteFrame
public sealed class RemappableFrame : ISpriteFrame
{
public SpriteFrameType Type => SpriteFrameType.Indexed8;
public SpriteFrameType Type => SpriteFrameType.Bgra32;
public Size Size => inner.Size;
public Size FrameSize => inner.FrameSize;
public float2 Offset => inner.Offset;
public bool DisableExportPadding => inner.DisableExportPadding;
readonly Frame inner;
readonly bool useShadow;
readonly bool convertShroudToFog;
readonly Color remap;
byte[] data;
public RemappableFrame(Frame inner, bool useShadow = true, bool convertShroudToFog = false, Color remap = default)
{
this.inner = inner;
this.useShadow = useShadow;
this.convertShroudToFog = convertShroudToFog;
this.remap = remap;
}
public byte[] Data
{
get
{
if (data == null)
{
var pixelCount = inner.Size.Width * inner.Size.Height;
data = new byte[4 * pixelCount];
var palette = inner.Palette;
if (useShadow || convertShroudToFog || remap != default)
{
palette = new uint[256];
Array.Copy(inner.Palette, palette, 256);
}
// Bit twiddling is equivalent to unpacking RGB channels, dividing them by 2, subtracting from 255, then repacking
if (convertShroudToFog)
for (var i = 0; i < Palette.Size; i++)
palette[i] = ~((palette[i] >> 1) & 0x007F7F7F);
// Remap index 1 to shadow
if (useShadow)
palette[1] = 140u << 24;
if (remap != default)
{
var r = new PlayerColorRemap(Enumerable.Range(240, 16).ToArray(), remap);
for (var i = 240; i < 256; i++)
palette[i] = (uint)r.GetRemappedColor(Color.FromArgb(palette[i]), i).ToArgb();
}
unsafe
{
fixed (byte* bd = &Data[0])
{
var data = (uint*)bd;
for (var i = 0; i < pixelCount; i++)
data[i] = palette[inner.Data[i]];
}
}
}
return data;
}
}
public RemappableFrame WithSequenceFlags(bool useShadow, bool convertShroudToFog, Color remap)
{
return new RemappableFrame(inner, useShadow, convertShroudToFog, remap);
}
}
public sealed class Frame : ISpriteFrame
{
public SpriteFrameType Type { get; }
public Size Size { get; }
public Size FrameSize { get; }
public float2 Offset { get; }
public byte[] Data { get; set; }
public byte[] Data { get; }
public bool DisableExportPadding => true;
public readonly uint[] Palette = null;
public uint[] Palette { get; }
public R8Frame(Stream s)
public Frame(Stream s, uint[] lastPalette)
{
// Scan forward until we find some data
var type = s.ReadUInt8();
@@ -45,11 +121,10 @@ namespace OpenRA.Mods.D2k.SpriteLoaders
Size = new Size(width, height);
Offset = new int2(width / 2 - x, height / 2 - y);
/*var imageOffset = */
s.ReadInt32();
var paletteOffset = s.ReadInt32();
s.ReadUInt32(); // imageHandle
var paletteHandle = s.ReadInt32();
var bpp = s.ReadUInt8();
if (bpp != 8)
if (bpp != 8 && bpp != 16)
throw new InvalidDataException($"Error: {bpp} bits per pixel are not supported.");
var frameHeight = s.ReadUInt8();
@@ -59,10 +134,32 @@ namespace OpenRA.Mods.D2k.SpriteLoaders
// Skip alignment byte
s.ReadUInt8();
Data = s.ReadBytes(width * height);
if (bpp == 16)
{
Data = new byte[width * height * 4];
Type = SpriteFrameType.Bgra32;
unsafe
{
fixed (byte* bd = &Data[0])
{
var data = (uint*)bd;
for (var i = 0; i < width * height; i++)
{
var packed = s.ReadUInt16();
data[i] = (uint)((0xFF << 24) | ((packed & 0x7C00) << 9) | ((packed & 0x3E0) << 6) | ((packed & 0x1f) << 3));
}
}
}
}
else
{
Data = s.ReadBytes(width * height);
Type = SpriteFrameType.Indexed8;
}
// Read palette
if (type == 1 && paletteOffset != 0)
if (type == 1 && paletteHandle != 0)
{
// Skip header
s.ReadUInt32();
@@ -72,9 +169,14 @@ namespace OpenRA.Mods.D2k.SpriteLoaders
for (var i = 0; i < 256; i++)
{
var packed = s.ReadUInt16();
Palette[i] = (uint)((255 << 24) | ((packed & 0xF800) << 8) | ((packed & 0x7E0) << 5) | ((packed & 0x1f) << 3));
Palette[i] = (uint)((0xFF << 24) | ((packed & 0x7C00) << 9) | ((packed & 0x3E0) << 6) | ((packed & 0x1f) << 3));
}
// Remap index 0 to transparent
Palette[0] = 0;
}
else if (type == 2)
Palette = lastPalette;
}
}
@@ -94,7 +196,7 @@ namespace OpenRA.Mods.D2k.SpriteLoaders
var d = s.ReadUInt8();
s.Position = start;
return d == 8;
return d == 8 || d == 16;
}
public bool TryParseSprite(Stream s, string filename, out ISpriteFrame[] frames, out TypeDictionary metadata)
@@ -107,21 +209,20 @@ namespace OpenRA.Mods.D2k.SpriteLoaders
}
var start = s.Position;
var tmp = new List<R8Frame>();
var palettes = new Dictionary<int, uint[]>();
var tmp = new List<Frame>();
uint[] lastPalette = null;
while (s.Position < s.Length)
{
var f = new R8Frame(s);
var f = new Frame(s, lastPalette);
if (f.Palette != null)
palettes.Add(tmp.Count, f.Palette);
lastPalette = f.Palette;
tmp.Add(f);
}
s.Position = start;
frames = tmp.ToArray();
if (palettes.Count > 0)
metadata = new TypeDictionary { new EmbeddedSpritePalette(framePalettes: palettes) };
frames = tmp.Select<Frame, ISpriteFrame>(f => f.Palette != null ? new RemappableFrame(f) : f).ToArray();
return true;
}