// ------------------------------------------------------------------------ // Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved. // See LICENSE file in the project root for license information. // ------------------------------------------------------------------------ namespace Pal3.Core.DataReader.Dxt { using System; using Utilities; /// /// Decode DXT3 image data and convert it to RGBA32(RGB888). /// public static class Dxt3Decoder { // 4 chars header label // 30 integers + 4 chars format info // Total of: 4 + 30 x 4 + 4 = 128 private const int DDS_FILE_HEADER_SIZE = 128; public static byte[] ToRgba32(byte[] data, int width, int height) { byte[] buffer = new byte[width * height * 4]; ToRgba32(data, width, height, buffer); return buffer; } public static unsafe void ToRgba32(byte[] data, int width, int height, byte[] buffer, int sourceOffset = DDS_FILE_HEADER_SIZE) { if (width <= 0 || height <= 0 || buffer == null || (long)buffer.Length < (long)width * height * 4) { throw new ArgumentException("buffer is null or too small"); } if (sourceOffset < 0 || data == null || (long)sourceOffset + ((width + 3L) / 4) * ((height + 3L) / 4) * 16 > data.Length) throw new ArgumentException("DXT source data is too small"); fixed (byte* srcStart = &data[sourceOffset], dstStart = buffer) { byte* src = srcStart; byte* dst = dstStart; for (int j = 0; j < (height + 3) / 4; j++) { for (int i = 0; i < (width + 3) / 4; i++) { DecodeDxt3Block(i, j, width, height, src, dst); src += 16; // 8 + 2 + 2 + 4 } } } } private static unsafe void DecodeDxt3Block(int x, int y, int width, int height, byte* src, byte* dst) { ushort color0 = *(ushort*)(src + 8); ushort color1 = *(ushort*)(src + 10); CoreUtility.Rgb565ToRgb888(color0, out byte r0, out byte g0, out byte b0); CoreUtility.Rgb565ToRgb888(color1, out byte r1, out byte g1, out byte b1); uint lookupTable = *(uint*)(src + 12); int alphaIndex = 0; for (int blockY = 0; blockY < 4; blockY++) { for (int blockX = 0; blockX < 4; blockX++) { byte r = 0, g = 0, b = 0, a = 0; uint index = (lookupTable >> 2 * (4 * blockY + blockX)) & 0x03; if (alphaIndex % 2 == 0) { a = (byte) ((*(src + alphaIndex / 2) & 0x0F) | ((*(src + alphaIndex / 2) & 0x0F) << 4)); } else { a = (byte) ((*(src + (alphaIndex-1) / 2) & 0xF0) | ((*(src + (alphaIndex-1) / 2) & 0xF0) >> 4)); } alphaIndex++; switch (index) { case 0: r = r0; g = g0; b = b0; break; case 1: r = r1; g = g1; b = b1; break; case 2: r = (byte) ((2 * r0 + r1) / 3); g = (byte) ((2 * g0 + g1) / 3); b = (byte) ((2 * b0 + b1) / 3); break; case 3: r = (byte) ((r0 + 2 * r1) / 3); g = (byte) ((g0 + 2 * g1) / 3); b = (byte) ((b0 + 2 * b1) / 3); break; } int px = (x << 2) + blockX; int py = (y << 2) + blockY; if (px < width && py < height) { int offset = ((py * width) + px) << 2; *(dst + offset) = r; *(dst + offset + 1) = g; *(dst + offset + 2) = b; *(dst + offset + 3) = a; } } } } } }