Skip to content

Commit

Permalink
[WzLib] WzPngProperty -- speed up the performance of decompressing bg…
Browse files Browse the repository at this point in the history
…ra444 pixel data
  • Loading branch information
lastbattle committed Dec 13, 2022
1 parent 7318957 commit ed788a0
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions MapleLib/WzLib/WzProperties/WzPngProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using MapleLib.Converters;
using MapleLib.WzLib.Util;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Graphics.PackedVector;

namespace MapleLib.WzLib.WzProperties
{
Expand Down Expand Up @@ -556,25 +557,49 @@ public static Color RGB565ToColor(ushort val)
return c;
}

/// <summary>
/// For debugging: an example of this image may be found at "Effect.wz\\5skill.img\\character_delayed\\0"
/// </summary>
/// <param name="rawData"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="bmp"></param>
/// <param name="bmpData"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DecompressImage_PixelDataBgra4444(byte[] rawData, int width, int height, Bitmap bmp, BitmapData bmpData)
public unsafe static void DecompressImage_PixelDataBgra4444(byte[] rawData, int width, int height, Bitmap bmp, BitmapData bmpData)
{
int uncompressedSize = width * height * 2;
byte[] decoded = new byte[uncompressedSize * 2];

for (int i = 0; i < uncompressedSize; i++)
// Declare an "unsafe" pointer to the raw data
fixed (byte* dataPointer = rawData)
{
byte byteAtPosition = rawData[i];
// Create pointers for the B, G, and R values of each pixel
byte* bPointer = dataPointer;
byte* gPointer = dataPointer + 1;

int lo = byteAtPosition & 0x0F;
byte b = (byte)(lo | (lo << 4));
decoded[i * 2] = b;
// Iterate over the pixels
for (int i = 0; i < uncompressedSize; i += 2)
{
// Get the B and G values of the current pixel
byte b = *bPointer;
byte g = *gPointer;

// Decompress the B and G values
b = (byte)((b >> 4) | (b >> 4));
g = (byte)(g | (g << 4));

// Set the decompressed B and G values for the current pixel
*bPointer = b;
*gPointer = g;

int hi = byteAtPosition & 0xF0;
byte g = (byte)(hi | (hi >> 4));
decoded[i * 2 + 1] = g;
// Move the pointers to the next pixel
bPointer += 2;
gPointer += 2;
}
}
Marshal.Copy(decoded, 0, bmpData.Scan0, decoded.Length);

// Copy the decompressed data to the bitmap
Marshal.Copy(rawData, 0, bmpData.Scan0, rawData.Length);
bmp.UnlockBits(bmpData);
}

Expand Down Expand Up @@ -844,6 +869,7 @@ internal void CompressPng(Bitmap bmp)
format2 = 0;
width = bmp.Width;
height = bmp.Height;

//byte[] bmpBytes = bmp.BitmapToBytes();
/* if (SquishPNGWrapper.CheckAndLoadLibrary())
{
Expand Down

0 comments on commit ed788a0

Please sign in to comment.