Reworked internal palettes in video reader classes

This removes the need to pack & unpack color bytes as uints for no gain.
This commit is contained in:
penev92
2021-08-02 19:32:28 +03:00
committed by Matthias Mailänder
parent c4ab7041b8
commit cb8530fbae
2 changed files with 22 additions and 17 deletions

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
readonly byte chunkBufferParts; readonly byte chunkBufferParts;
readonly int2 blocks; readonly int2 blocks;
readonly uint[] offsets; readonly uint[] offsets;
readonly uint[] palette; readonly byte[] paletteBytes;
readonly uint videoFlags; // if 0x10 is set the video is a 16 bit hq video (ts and later) readonly uint videoFlags; // if 0x10 is set the video is a 16 bit hq video (ts and later)
readonly ushort totalFrameWidth; readonly ushort totalFrameWidth;
@@ -116,7 +116,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
origData = new byte[2 * blocks.X * blocks.Y]; origData = new byte[2 * blocks.X * blocks.Y];
} }
palette = new uint[numColors]; paletteBytes = new byte[numColors * 4];
var type = stream.ReadASCII(4); var type = stream.ReadASCII(4);
while (type != "FINF") while (type != "FINF")
{ {
@@ -396,7 +396,10 @@ namespace OpenRA.Mods.Cnc.FileFormats
var r = (byte)(s.ReadUInt8() << 2); var r = (byte)(s.ReadUInt8() << 2);
var g = (byte)(s.ReadUInt8() << 2); var g = (byte)(s.ReadUInt8() << 2);
var b = (byte)(s.ReadUInt8() << 2); var b = (byte)(s.ReadUInt8() << 2);
palette[i] = (uint)((255 << 24) | (r << 16) | (g << 8) | b); paletteBytes[i * 4] = b;
paletteBytes[i * 4 + 1] = g;
paletteBytes[i * 4 + 2] = r;
paletteBytes[i * 4 + 3] = 255;
} }
break; break;
@@ -492,15 +495,14 @@ namespace OpenRA.Mods.Cnc.FileFormats
{ {
var cbfi = (mod * 256 + px) * 8 + j * blockWidth + i; var cbfi = (mod * 256 + px) * 8 + j * blockWidth + i;
var colorIndex = (mod == 0x0f) ? px : cbf[cbfi]; var colorIndex = (mod == 0x0f) ? px : cbf[cbfi];
var color = palette[colorIndex];
var pixelX = x * blockWidth + i; var pixelX = x * blockWidth + i;
var pixelY = y * blockHeight + j; var pixelY = y * blockHeight + j;
var pos = pixelY * totalFrameWidth + pixelX; var pos = pixelY * totalFrameWidth + pixelX;
CurrentFrameData[pos * 4] = (byte)(color & 0xFF); CurrentFrameData[pos * 4] = paletteBytes[colorIndex * 4];
CurrentFrameData[pos * 4 + 1] = (byte)(color >> 8 & 0xFF); CurrentFrameData[pos * 4 + 1] = paletteBytes[colorIndex * 4 + 1];
CurrentFrameData[pos * 4 + 2] = (byte)(color >> 16 & 0xFF); CurrentFrameData[pos * 4 + 2] = paletteBytes[colorIndex * 4 + 2];
CurrentFrameData[pos * 4 + 3] = (byte)(color >> 24 & 0xFF); CurrentFrameData[pos * 4 + 3] = paletteBytes[colorIndex * 4 + 3];
} }
} }
} }

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
public int SampleRate => 0; public int SampleRate => 0;
readonly Stream stream; readonly Stream stream;
readonly uint[] palette; readonly byte[] paletteBytes;
readonly uint[] frameOffsets; readonly uint[] frameOffsets;
readonly ushort totalFrameWidth; readonly ushort totalFrameWidth;
@@ -60,8 +60,8 @@ namespace OpenRA.Mods.Cnc.FileFormats
if (flags == 1) if (flags == 1)
{ {
palette = new uint[256]; paletteBytes = new byte[1024];
for (var i = 0; i < palette.Length; i++) for (var i = 0; i < paletteBytes.Length;)
{ {
var r = (byte)(stream.ReadByte() << 2); var r = (byte)(stream.ReadByte() << 2);
var g = (byte)(stream.ReadByte() << 2); var g = (byte)(stream.ReadByte() << 2);
@@ -72,7 +72,10 @@ namespace OpenRA.Mods.Cnc.FileFormats
g |= (byte)(g >> 6); g |= (byte)(g >> 6);
b |= (byte)(b >> 6); b |= (byte)(b >> 6);
palette[i] = (uint)((255 << 24) | (r << 16) | (g << 8) | b); paletteBytes[i++] = b;
paletteBytes[i++] = g;
paletteBytes[i++] = r;
paletteBytes[i++] = 255;
} }
for (var i = 0; i < frameOffsets.Length; i++) for (var i = 0; i < frameOffsets.Length; i++)
@@ -138,11 +141,11 @@ namespace OpenRA.Mods.Cnc.FileFormats
{ {
for (var x = 0; x < Width; x++) for (var x = 0; x < Width; x++)
{ {
var color = palette[currentFramePaletteIndexData[c++]]; var colorIndex = currentFramePaletteIndexData[c++];
CurrentFrameData[position++] = (byte)(color & 0xFF); CurrentFrameData[position++] = paletteBytes[colorIndex * 4];
CurrentFrameData[position++] = (byte)(color >> 8 & 0xFF); CurrentFrameData[position++] = paletteBytes[colorIndex * 4 + 1];
CurrentFrameData[position++] = (byte)(color >> 16 & 0xFF); CurrentFrameData[position++] = paletteBytes[colorIndex * 4 + 2];
CurrentFrameData[position++] = (byte)(color >> 24 & 0xFF); CurrentFrameData[position++] = paletteBytes[colorIndex * 4 + 3];
} }
// Recalculate the position in the byte array to the start of the next pixel row just in case there is padding in the frame. // Recalculate the position in the byte array to the start of the next pixel row just in case there is padding in the frame.