using System.Buffers.Binary; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace AssetRipper.TextureDecoder.Dxt { public static class DxtDecoder { /// /// Decompress a DXT1 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer of size 4 * width * height. /// Number of bytes read from public static int DecompressDXT1(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * sizeof(uint)]; return DecompressDXT1(input, width, height, output); } /// /// Decompress a DXT1 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer. Must be at least 4 * width * height. /// Number of bytes read from public static int DecompressDXT1(ReadOnlySpan input, int width, int height, Span output) { ThrowHelper.ThrowIfNotEnoughSpace(output, width, height); int offset = 0; int bcw = (width + 3) / 4; int bch = (height + 3) / 4; int clen_last = (width + 3) % 4 + 1; uint[] buffer = new uint[16]; int[] colors = new int[4]; for (int t = 0; t < bch; t++) { for (int s = 0; s < bcw; s++, offset += 8) { int q0 = input[offset + 0] | input[offset + 1] << 8; int q1 = input[offset + 2] | input[offset + 3] << 8; Rgb565(q0, out int r0, out int g0, out int b0); Rgb565(q1, out int r1, out int g1, out int b1); colors[0] = Color(r0, g0, b0, 255); colors[1] = Color(r1, g1, b1, 255); if (q0 > q1) { colors[2] = Color((r0 * 2 + r1) / 3, (g0 * 2 + g1) / 3, (b0 * 2 + b1) / 3, 255); colors[3] = Color((r0 + r1 * 2) / 3, (g0 + g1 * 2) / 3, (b0 + b1 * 2) / 3, 255); } else { colors[2] = Color((r0 + r1) / 2, (g0 + g1) / 2, (b0 + b1) / 2, 255); } uint d = ToUInt32(input, offset + 4); for (int i = 0; i < 16; i++, d >>= 2) { buffer[i] = unchecked((uint)colors[d & 3]); } int clen = (s < bcw - 1 ? 4 : clen_last) * 4; for (int i = 0, y = t * 4; i < 4 && y < height; i++, y++) { ReadOnlySpan bufferSpan = MemoryMarshal.Cast(new ReadOnlySpan(buffer)); BlockCopy(bufferSpan, i * 4 * 4, output, (y * width + s * 4) * 4, clen); } } } return offset; } /// /// Decompress a DXT3 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer of size 4 * width * height. /// Number of bytes read from public static int DecompressDXT3(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * sizeof(uint)]; return DecompressDXT3(input, width, height, output); } /// /// Decompress a DXT3 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer. Must be at least 4 * width * height. /// Number of bytes read from public static int DecompressDXT3(ReadOnlySpan input, int width, int height, Span output) { ThrowHelper.ThrowIfNotEnoughSpace(output, width, height); int offset = 0; int bcw = (width + 3) / 4; int bch = (height + 3) / 4; int clen_last = (width + 3) % 4 + 1; uint[] buffer = new uint[16]; int[] colors = new int[4]; int[] alphas = new int[16]; for (int t = 0; t < bch; t++) { for (int s = 0; s < bcw; s++, offset += 16) { for (int i = 0; i < 4; i++) { int alpha = input[offset + i * 2] | input[offset + i * 2 + 1] << 8; alphas[i * 4 + 0] = (((alpha >> 0) & 0xF) * 0x11) << 24; alphas[i * 4 + 1] = (((alpha >> 4) & 0xF) * 0x11) << 24; alphas[i * 4 + 2] = (((alpha >> 8) & 0xF) * 0x11) << 24; alphas[i * 4 + 3] = (((alpha >> 12) & 0xF) * 0x11) << 24; } int q0 = input[offset + 8] | input[offset + 9] << 8; int q1 = input[offset + 10] | input[offset + 11] << 8; Rgb565(q0, out int r0, out int g0, out int b0); Rgb565(q1, out int r1, out int g1, out int b1); colors[0] = Color(r0, g0, b0, 0); colors[1] = Color(r1, g1, b1, 0); if (q0 > q1) { colors[2] = Color((r0 * 2 + r1) / 3, (g0 * 2 + g1) / 3, (b0 * 2 + b1) / 3, 0); colors[3] = Color((r0 + r1 * 2) / 3, (g0 + g1 * 2) / 3, (b0 + b1 * 2) / 3, 0); } else { colors[2] = Color((r0 + r1) / 2, (g0 + g1) / 2, (b0 + b1) / 2, 0); } uint d = ToUInt32(input, offset + 12); for (int i = 0; i < 16; i++, d >>= 2) { buffer[i] = unchecked((uint)(colors[d & 3] | alphas[i])); } int clen = (s < bcw - 1 ? 4 : clen_last) * 4; for (int i = 0, y = t * 4; i < 4 && y < height; i++, y++) { ReadOnlySpan bufferSpan = MemoryMarshal.Cast(new ReadOnlySpan(buffer)); BlockCopy(bufferSpan, i * 4 * 4, output, (y * width + s * 4) * 4, clen); } } } return offset; } /// /// Decompress a DXT5 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer of size 4 * width * height. /// Number of bytes read from public static int DecompressDXT5(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * sizeof(uint)]; return DecompressDXT5(input, width, height, output); } /// /// Decompress a DXT5 image /// /// Input buffer containing the compressed image. /// Pixel width of the image. /// Pixel height of the image. /// An output buffer. Must be at least 4 * width * height. /// Number of bytes read from public static int DecompressDXT5(ReadOnlySpan input, int width, int height, Span output) { ThrowHelper.ThrowIfNotEnoughSpace(output, width, height); int offset = 0; int bcw = (width + 3) / 4; int bch = (height + 3) / 4; int clen_last = (width + 3) % 4 + 1; uint[] buffer = new uint[16]; int[] colors = new int[4]; int[] alphas = new int[8]; for (int t = 0; t < bch; t++) { for (int s = 0; s < bcw; s++, offset += 16) { alphas[0] = input[offset + 0]; alphas[1] = input[offset + 1]; if (alphas[0] > alphas[1]) { alphas[2] = (alphas[0] * 6 + alphas[1]) / 7; alphas[3] = (alphas[0] * 5 + alphas[1] * 2) / 7; alphas[4] = (alphas[0] * 4 + alphas[1] * 3) / 7; alphas[5] = (alphas[0] * 3 + alphas[1] * 4) / 7; alphas[6] = (alphas[0] * 2 + alphas[1] * 5) / 7; alphas[7] = (alphas[0] + alphas[1] * 6) / 7; } else { alphas[2] = (alphas[0] * 4 + alphas[1]) / 5; alphas[3] = (alphas[0] * 3 + alphas[1] * 2) / 5; alphas[4] = (alphas[0] * 2 + alphas[1] * 3) / 5; alphas[5] = (alphas[0] + alphas[1] * 4) / 5; alphas[7] = 255; } for (int i = 0; i < 8; i++) { alphas[i] <<= 24; } int q0 = input[offset + 8] | input[offset + 9] << 8; int q1 = input[offset + 10] | input[offset + 11] << 8; Rgb565(q0, out int r0, out int g0, out int b0); Rgb565(q1, out int r1, out int g1, out int b1); colors[0] = Color(r0, g0, b0, 0); colors[1] = Color(r1, g1, b1, 0); if (q0 > q1) { colors[2] = Color((r0 * 2 + r1) / 3, (g0 * 2 + g1) / 3, (b0 * 2 + b1) / 3, 0); colors[3] = Color((r0 + r1 * 2) / 3, (g0 + g1 * 2) / 3, (b0 + b1 * 2) / 3, 0); } else { colors[2] = Color((r0 + r1) / 2, (g0 + g1) / 2, (b0 + b1) / 2, 0); } ulong da = ToUInt64(input, offset) >> 16; uint dc = ToUInt32(input, offset + 12); for (int i = 0; i < 16; i++, da >>= 3, dc >>= 2) { buffer[i] = unchecked((uint)(alphas[da & 7] | colors[dc & 3])); } int clen = (s < bcw - 1 ? 4 : clen_last) * 4; for (int i = 0, y = t * 4; i < 4 && y < height; i++, y++) { ReadOnlySpan bufferSpan = MemoryMarshal.Cast(new ReadOnlySpan(buffer)); BlockCopy(bufferSpan, i * 4 * 4, output, (y * width + s * 4) * 4, clen); } } } return offset; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Rgb565(int c, out int r, out int g, out int b) { r = (c & 0xf800) >> 8; g = (c & 0x07e0) >> 3; b = (c & 0x001f) << 3; r |= r >> 5; g |= g >> 6; b |= b >> 5; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int Color(int r, int g, int b, int a) { return r << 16 | g << 8 | b | a << 24; } /// /// Based on /// /// The source buffer. /// The zero-based byte offset into source. /// The destination buffer. /// The zero-based byte offset into destination. /// The number of bytes to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void BlockCopy(ReadOnlySpan source, int sourceOffset, Span destination, int destinationOffset, int count) { source.Slice(sourceOffset, count).CopyTo(destination.Slice(destinationOffset)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ToUInt32(ReadOnlySpan input, int offset) { return BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(offset)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ulong ToUInt64(ReadOnlySpan input, int offset) { return BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(offset)); } } }