Skip to content

Commit

Permalink
squishlib optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro413 committed May 21, 2024
1 parent d1f76bb commit 3117cd1
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 166 deletions.
43 changes: 43 additions & 0 deletions TagTool/Bitmaps/Utils/BitmapDownsampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,49 @@ public static byte[] Downsample4x4BlockRgba(BitmapSampler sampler)
return result;
}

public static byte[] Downsample4x4BlockRgba(BitmapSampler sampler, int width, int height)
{
sampler.SetFilteringMode(BitmapSampler.FilteringMode.Bilinear); // must use bilinear

byte[] result = new byte[width * height * 4];

float pixelWidth = 1.0f / width;
float pixelHeight = 1.0f / height;
float halfPxWidth = pixelWidth * 0.5f;
float halfPxHeight = pixelHeight * 0.5f;

int rowBytes = width * 4;
int rowOffset = 0;

for (int i = 0; i < height; i++)
{
float yCoord = (pixelHeight * i) + halfPxHeight;

for (int j = 0; j < width; j++)
{
float xCoord = (pixelWidth * j) + halfPxWidth;

// 4x4 block
var sample0 = sampler.Sample2dOffsetF(xCoord, yCoord, -1, -1);
var sample1 = sampler.Sample2dOffsetF(xCoord, yCoord, 1, -1);
var sample2 = sampler.Sample2dOffsetF(xCoord, yCoord, -1, 1);
var sample3 = sampler.Sample2dOffsetF(xCoord, yCoord, 1, 1);
RealVector4d sample = (sample0 + sample1 + sample2 + sample3) / 4.0f;
sample *= 255; // to byte

int streamOffset = rowOffset + j * 4;
result[streamOffset] = (byte)sample.I;
result[streamOffset + 1] = (byte)sample.J;
result[streamOffset + 2] = (byte)sample.K;
result[streamOffset + 3] = (byte)sample.W;
}

rowOffset += rowBytes;
}

return result;
}

// try not to use this
public static byte[] DownsampleBilinearRgba(BitmapSampler sampler)
{
Expand Down
15 changes: 14 additions & 1 deletion TagTool/Bitmaps/Utils/BitmapSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TagTool.Bitmaps.Utils
public class BitmapSampler
{
private byte[] Rgba;
private uint StreamOffset;
private int Width;
private int Height;
private FilteringMode FilterMode;
Expand All @@ -20,6 +21,18 @@ public BitmapSampler(byte[] rgba, int width, int height, FilteringMode filter =
FilterMode = filter;
MipmapCount = mipCount;
CurrentLevel = 0;
StreamOffset = 0;
}

public BitmapSampler(byte[] rgba, uint streamOffset, int width, int height, FilteringMode filter = FilteringMode.Point, int mipCount = 0)
{
Rgba = rgba;
Width = width;
Height = height;
FilterMode = filter;
MipmapCount = mipCount;
CurrentLevel = 0;
StreamOffset = streamOffset;
}

public void SetData(byte[] d) => Rgba = d;
Expand Down Expand Up @@ -76,7 +89,7 @@ public RealVector4d Sample2dOffsetF(float x, float y, int offsetX, int offsetY)
private float PixelWidth() => 1.0f / Width;
private float PixelHeight() => 1.0f / Height;
private int UvToIndex(float x, float y) => 4 * ((int)(y * Height) * Width + (int)(x * Width));
private RGBAColor GetColour(int index) => new RGBAColor(Rgba[index], Rgba[index + 1], Rgba[index + 2], Rgba[index + 3]);
private RGBAColor GetColour(int index) => new RGBAColor(Rgba[StreamOffset + index], Rgba[StreamOffset + index + 1], Rgba[StreamOffset + index + 2], Rgba[StreamOffset + index + 3]);
private RGBAColor Sample2dPoint(int index) => GetColour(index);
private RGBAColor Sample2dPoint(float x, float y) => Sample2dPoint(UvToIndex(x, y));
private RGBAColor Sample2dPointBounded(float x, float y, bool hasX, bool hasY) => GetColourBounded(UvToIndex(x, y), x, y, hasX, hasY);
Expand Down
Loading

0 comments on commit 3117cd1

Please sign in to comment.