From c0666a54044a46dab59955e3bb0108292361fd71 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 29 Aug 2024 16:58:20 +0100 Subject: [PATCH] More lightmap asbytes cleanup --- project/code/LGS/Database/Chunks/WorldRep.cs | 58 +++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/project/code/LGS/Database/Chunks/WorldRep.cs b/project/code/LGS/Database/Chunks/WorldRep.cs index 7f22d81..cc61d6f 100644 --- a/project/code/LGS/Database/Chunks/WorldRep.cs +++ b/project/code/LGS/Database/Chunks/WorldRep.cs @@ -154,41 +154,33 @@ public class WorldRep : IChunk ArgumentOutOfRangeException.ThrowIfLessThan(layer, 0, nameof(layer)); ArgumentOutOfRangeException.ThrowIfGreaterThan(layer, Layers, nameof(layer)); - var bIdx = 0; - var pIdx = layer * Bpp * Width * Height; - - var length = Width * Height * 4; - var bytes = new byte[length]; - for (var y = 0; y < Height; y++) + var pixelCount = Width * Height; + var pIdx = layer * Bpp * pixelCount; + var bytes = new byte[pixelCount * 4]; + for (var i = 0; i < pixelCount; i += 4, pIdx += Bpp) { - for (var x = 0; x < Width; x++) + switch (Bpp) { - switch (Bpp) - { - case 1: - var raw1 = Pixels[pIdx]; - bytes[bIdx] = raw1; - bytes[bIdx + 1] = raw1; - bytes[bIdx + 2] = raw1; - bytes[bIdx + 3] = 255; - break; - case 2: - var raw2 = Pixels[pIdx] + (Pixels[pIdx + 1] << 8); - bytes[bIdx] = (byte)(255 * (raw2 & 31) / 31.0f); - bytes[bIdx + 1] = (byte)(255 * ((raw2 >> 5) & 31) / 31.0f); - bytes[bIdx + 2] = (byte)(255 * ((raw2 >> 10) & 31) / 31.0f); - bytes[bIdx + 3] = 255; - break; - case 4: - bytes[bIdx] = Pixels[pIdx + 2]; - bytes[bIdx + 1] = Pixels[pIdx + 1]; - bytes[bIdx + 2] = Pixels[pIdx]; - bytes[bIdx + 3] = Pixels[pIdx + 3]; - break; - } - - bIdx += 4; - pIdx += Bpp; + case 1: + var raw1 = Pixels[pIdx]; + bytes[i] = raw1; + bytes[i + 1] = raw1; + bytes[i + 2] = raw1; + bytes[i + 3] = 255; + break; + case 2: + var raw2 = Pixels[pIdx] + (Pixels[pIdx + 1] << 8); + bytes[i] = (byte)(255 * (raw2 & 31) / 31.0f); + bytes[i + 1] = (byte)(255 * ((raw2 >> 5) & 31) / 31.0f); + bytes[i + 2] = (byte)(255 * ((raw2 >> 10) & 31) / 31.0f); + bytes[i + 3] = 255; + break; + case 4: + bytes[i] = Pixels[pIdx + 2]; + bytes[i + 1] = Pixels[pIdx + 1]; + bytes[i + 2] = Pixels[pIdx]; + bytes[i + 3] = Pixels[pIdx + 3]; + break; } }