public static byte[] DecompressBC1(Byte[] data, int width, int height, bool IsSRGB) { int W = (width + 3) / 4; int H = (height + 3) / 4; byte[] Output = new byte[W * H * 64]; for (int Y = 0; Y < H; Y++) { for (int X = 0; X < W; X++) { int IOffs = (Y * W + X) * 8; byte[] Tile = BCnDecodeTile(data, IOffs, true); int TOffset = 0; for (int TY = 0; TY < 4; TY++) { for (int TX = 0; TX < 4; TX++) { int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4; Output[OOffset + 0] = Tile[TOffset + 0]; Output[OOffset + 1] = Tile[TOffset + 1]; Output[OOffset + 2] = Tile[TOffset + 2]; Output[OOffset + 3] = Tile[TOffset + 3]; TOffset += 4; } } } } return(ImageUtility.ConvertBgraToRgba(Output)); }
public byte[] DecodeImage(STGenericTexture texture, byte[] data, uint width, uint height, int array, int mip) { var settings = new CTR_3DS.SwizzleSettings() { Orientation = SwizzleMode, }; return(ImageUtility.ConvertBgraToRgba(CTR_3DS.DecodeBlock(data, (int)width, (int)height, Format, settings))); }
public static byte[] DecompressBC3(Byte[] data, int width, int height, bool IsSRGB) { int W = (width + 3) / 4; int H = (height + 3) / 4; byte[] Output = new byte[W * H * 64]; for (int Y = 0; Y < H; Y++) { for (int X = 0; X < W; X++) { int IOffs = (Y * W + X) * 16; byte[] Tile = BCnDecodeTile(data, IOffs + 8, false); byte[] Alpha = new byte[8]; Alpha[0] = data[IOffs + 0]; Alpha[1] = data[IOffs + 1]; CalculateBC3Alpha(Alpha); int AlphaLow = Get32(data, IOffs + 2); int AlphaHigh = Get16(data, IOffs + 6); ulong AlphaCh = (uint)AlphaLow | (ulong)AlphaHigh << 32; int TOffset = 0; for (int TY = 0; TY < 4; TY++) { for (int TX = 0; TX < 4; TX++) { int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4; byte AlphaPx = Alpha[(AlphaCh >> (TY * 12 + TX * 3)) & 7]; Output[OOffset + 0] = Tile[TOffset + 0]; Output[OOffset + 1] = Tile[TOffset + 1]; Output[OOffset + 2] = Tile[TOffset + 2]; Output[OOffset + 3] = AlphaPx; TOffset += 4; } } } } return(ImageUtility.ConvertBgraToRgba(Output)); }
//Method from https://github.com/aboood40091/BNTX-Editor/blob/master/formConv.py public bool Decode(TexFormat format, byte[] input, int width, int height, out byte[] output) { if (!IsSupported(format)) { output = null; return(false); } if (format == TexFormat.BGRA8_UNORM || format == TexFormat.BGRA8_SRGB) { output = ImageUtility.ConvertBgraToRgba(input); return(true); } uint bpp = TextureFormatHelper.GetBytesPerPixel(format); int size = width * height * 4; bpp = (uint)(input.Length / (width * height)); output = new byte[size]; int inPos = 0; int outPos = 0; byte[] comp = new byte[] { 0, 0, 0, 0xFF, 0, 0xFF }; byte[] compSel = new byte[4] { 0, 1, 2, 3 }; if (format == TexFormat.LA8) { compSel = new byte[4] { 0, 0, 0, 1 }; bpp = 2; } else if (format == TexFormat.L8) { compSel = new byte[4] { 0, 0, 0, 5 } } ; 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 |= input[inPos + i] << (8 * i); } comp = GetComponentsFromPixel(format, pixel, comp); 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 != null); }