1
0
mirror of synced 2024-11-15 03:27:38 +01:00
Switch-Toolbox/Switch_Toolbox_Library/FileFormats/DDS/RGBAPixelDecoder.cs
KillzXGaming ed78d46545 Tons more bflyt improvements
Opengl textures that are not power of 2 decode from ST decoder. This prevents those from not loading. (Common in SMM1, SMM2, and also BFLYT).
Bflyt displays bounding panes, and window panes display textures. Window panes still need more work for rendering.
Bflyt now uses custom shaders for more advancements with rendering. Legacy PCs should still work fine with this.
Fixed uv transforms for bflyt if they are negative, which flips them.
Fixed an issue loading bclyt layouts.
Fixed pane trnasformation issues. They are nearly perfect, but rotations for X and Y are off.
Parts now search for opened sarc archives.
Fixed an issue with some spaces not quite saving for txt1 panes. This may fix some saving issues, as most i've tried output back identically.
Fixed an issue displaying LA8 textures.
2019-09-08 15:15:42 -04:00

87 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Toolbox.Library
{
public class RGBAPixelDecoder
{
private static byte[] GetComponentsFromPixel(TEX_FORMAT format, int pixel)
{
byte[] comp = new byte[] { 0, 0xFF, 0, 0, 0, 0xFF };
switch (format)
{
case TEX_FORMAT.L8:
comp[2] = (byte)(pixel & 0xFF);
break;
case TEX_FORMAT.L4:
comp[2] = (byte)((pixel & 0xF) * 17);
comp[3] = (byte)(((pixel & 0xF0) >> 4) * 17);
break;
case TEX_FORMAT.LA8:
comp[2] = (byte)(pixel & 0xFF);
comp[3] = (byte)((pixel & 0xFF00) >> 8);
break;
case TEX_FORMAT.R5G5B5_UNORM:
comp[2] = (byte)((pixel & 0x1F) / 0x1F * 0xFF);
comp[3] = (byte)(((pixel & 0x7E0) >> 5) / 0x3F * 0xFF);
comp[4] = (byte)(((pixel & 0xF800) >> 11) / 0x1F * 0xFF);
break;
case TEX_FORMAT.B5G6R5_UNORM:
comp[2] = (byte)(((pixel & 0xF800) >> 11) / 0x1F * 0xFF);
comp[3] = (byte)(((pixel & 0x7E0) >> 5) / 0x3F * 0xFF);
comp[4] = (byte)((pixel & 0x1F) / 0x1F * 0xFF);
break;
}
return comp;
}
//Method from https://github.com/aboood40091/BNTX-Editor/blob/master/formConv.py
public static byte[] Decode(byte[] data, int width, int height, TEX_FORMAT format)
{
uint bpp = STGenericTexture.GetBytesPerPixel(format);
int size = width * height * 4;
bpp = (uint)(data.Length / (width * height));
byte[] output = new byte[size];
int inPos = 0;
int outPos = 0;
byte[] compSel = new byte[4] {0,1,2,3 };
if (format == TEX_FORMAT.L8 || format == TEX_FORMAT.LA8)
compSel = new byte[4] { 2, 2, 2, 3 };
for (int Y = 0; Y < height; Y++)
{
for (int X = 0; X < width; X++)
{
inPos = (Y * width + X) * (int)bpp;
outPos = (Y * width + X) * 4;
int pixel = 0;
for (int i = 0; i < bpp; i++)
pixel |= data[inPos + i] << (8 * i);
byte[] comp = GetComponentsFromPixel(format, pixel);
output[outPos + 3] = comp[compSel[3]];
output[outPos + 2] = comp[compSel[2]];
output[outPos + 1] = comp[compSel[1]];
output[outPos + 0] = comp[compSel[0]];
}
}
return output;
}
}
}