using Ultima;
namespace Server.Custom.Bridge
{
///
/// **Hue application without GDI+** (servuo-plugins #39). The vendored
/// Ultima.Hue.ApplyTo(Bitmap, onlyHueGrayPixels) locks a 16bpp Bitmap and
/// repaints it in place, so the catalogue's body.def-hued creatures (162 Body.def
/// entries carry a non-zero hue) go back through libgdiplus after
/// has already escaped it. This reproduces ApplyTo over a
/// contiguous ARGB1555 ushort[] instead — same arithmetic, no Bitmap.
///
/// The colour table is still the vendored one: Ultima.Hues.List[index].Colors is a
/// short[32] read from hues.mul with no System.Drawing, so reusing it
/// guarantees the managed path and the vendored path tint a pixel to the identical value.
///
public static class BridgeHue
{
///
/// Repaints each non-transparent pixel to colors[(pixel >> 10) & 0x1F]
/// — the pixel's own red channel chooses the entry on the 32-step ramp, exactly as
/// Hue.ApplyTo. In gray-only mode only pixels whose three channels are equal are
/// touched; everything else is left alone. The buffer is contiguous (no stride), so
/// there is no row padding to skip.
///
public static void Apply(ushort[] pixels, short[] colors, bool onlyGray)
{
if (pixels == null || colors == null)
return;
for (int i = 0; i < pixels.Length; i++)
{
int c = pixels[i];
if (c == 0)
continue;
if (onlyGray)
{
int r = (c >> 10) & 0x1F;
int g = (c >> 5) & 0x1F;
int b = c & 0x1F;
if (r == g && r == b)
pixels[i] = (ushort)colors[(c >> 10) & 0x1F];
}
else
{
pixels[i] = (ushort)colors[(c >> 10) & 0x1F];
}
}
}
///
/// The hue-object selection GetAnimation does (Animations.cs 474-487) before it
/// applies one: the 0x8000 bit of the raw hue is the "only gray pixels" flag, the low
/// 14 bits minus one are the Hues.List index, and an out-of-range index means no
/// hue at all. Returns the colour table to hand to .
///
public static bool Resolve(int rawHue, out short[] colors, out bool onlyGray)
{
colors = null;
onlyGray = (rawHue & 0x8000) != 0;
int index = (rawHue & 0x3FFF) - 1;
if (index >= 0 && index < Hues.List.Length)
{
Hue hue = Hues.List[index];
if (hue != null)
{
colors = hue.Colors;
return colors != null;
}
}
return false;
}
}
}