#include "texture/dxt.h" #include #include namespace metin2::texture { namespace { inline uint16_t ReadU16(const uint8_t* p) { return static_cast(p[0]) | (static_cast(p[1]) << 8); } inline uint32_t ReadU32(const uint8_t* p) { return static_cast(p[0]) | (static_cast(p[1]) << 8) | (static_cast(p[2]) << 16) | (static_cast(p[3]) << 24); } inline void DecodeRGB565(uint16_t c, uint8_t& r, uint8_t& g, uint8_t& b) { r = static_cast(((c >> 11) & 0x1F) * 255 / 31); g = static_cast(((c >> 5) & 0x3F) * 255 / 63); b = static_cast((c & 0x1F) * 255 / 31); } void DecodeDXT1Block(const uint8_t* block, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t* dst_rgba) { uint16_t c0 = ReadU16(block); uint16_t c1 = ReadU16(block + 2); uint32_t code = ReadU32(block + 4); uint8_t colors[4][4]; DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]); colors[0][3] = 255; DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]); colors[1][3] = 255; if (c0 > c1) { colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3; colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3; colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3; colors[2][3] = 255; colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3; colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3; colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3; colors[3][3] = 255; } else { colors[2][0] = (colors[0][0] + colors[1][0]) / 2; colors[2][1] = (colors[0][1] + colors[1][1]) / 2; colors[2][2] = (colors[0][2] + colors[1][2]) / 2; colors[2][3] = 255; colors[3][0] = 0; colors[3][1] = 0; colors[3][2] = 0; colors[3][3] = 0; } for (int py = 0; py < 4; ++py) { if (y + py >= height) continue; for (int px = 0; px < 4; ++px) { if (x + px >= width) continue; int bit_idx = (py * 4 + px) * 2; int color_idx = (code >> bit_idx) & 3; size_t dst_offset = ((y + py) * width + (x + px)) * 4; dst_rgba[dst_offset + 0] = colors[color_idx][0]; dst_rgba[dst_offset + 1] = colors[color_idx][1]; dst_rgba[dst_offset + 2] = colors[color_idx][2]; dst_rgba[dst_offset + 3] = colors[color_idx][3]; } } } } // namespace bool DxtDecoder::DecompressDXT1(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) { if (!src || !dst_rgba || width == 0 || height == 0) return false; uint32_t block_x = (width + 3) / 4; uint32_t block_y = (height + 3) / 4; size_t src_offset = 0; for (uint32_t by = 0; by < block_y; ++by) { for (uint32_t bx = 0; bx < block_x; ++bx) { DecodeDXT1Block(src + src_offset, bx * 4, by * 4, width, height, dst_rgba); src_offset += 8; } } return true; } bool DxtDecoder::DecompressDXT3(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) { if (!src || !dst_rgba || width == 0 || height == 0) return false; uint32_t block_x = (width + 3) / 4; uint32_t block_y = (height + 3) / 4; size_t src_offset = 0; for (uint32_t by = 0; by < block_y; ++by) { for (uint32_t bx = 0; bx < block_x; ++bx) { const uint8_t* alpha_block = src + src_offset; const uint8_t* color_block = src + src_offset + 8; uint16_t c0 = ReadU16(color_block); uint16_t c1 = ReadU16(color_block + 2); uint32_t code = ReadU32(color_block + 4); uint8_t colors[4][3]; DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]); DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]); colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3; colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3; colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3; colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3; colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3; colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3; for (int py = 0; py < 4; ++py) { if (by * 4 + py >= height) continue; uint16_t row_alpha = ReadU16(alpha_block + py * 2); for (int px = 0; px < 4; ++px) { if (bx * 4 + px >= width) continue; int color_idx = (code >> ((py * 4 + px) * 2)) & 3; uint8_t a4 = (row_alpha >> (px * 4)) & 0x0F; uint8_t alpha = a4 * 17; size_t dst_offset = ((by * 4 + py) * width + (bx * 4 + px)) * 4; dst_rgba[dst_offset + 0] = colors[color_idx][0]; dst_rgba[dst_offset + 1] = colors[color_idx][1]; dst_rgba[dst_offset + 2] = colors[color_idx][2]; dst_rgba[dst_offset + 3] = alpha; } } src_offset += 16; } } return true; } bool DxtDecoder::DecompressDXT5(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba) { if (!src || !dst_rgba || width == 0 || height == 0) return false; uint32_t block_x = (width + 3) / 4; uint32_t block_y = (height + 3) / 4; size_t src_offset = 0; for (uint32_t by = 0; by < block_y; ++by) { for (uint32_t bx = 0; bx < block_x; ++bx) { uint8_t a0 = src[src_offset + 0]; uint8_t a1 = src[src_offset + 1]; uint64_t a_bits = 0; for (int k = 0; k < 6; ++k) { a_bits |= (static_cast(src[src_offset + 2 + k]) << (k * 8)); } uint8_t alphas[8]; alphas[0] = a0; alphas[1] = a1; if (a0 > a1) { alphas[2] = (6 * a0 + 1 * a1) / 7; alphas[3] = (5 * a0 + 2 * a1) / 7; alphas[4] = (4 * a0 + 3 * a1) / 7; alphas[5] = (3 * a0 + 4 * a1) / 7; alphas[6] = (2 * a0 + 5 * a1) / 7; alphas[7] = (1 * a0 + 6 * a1) / 7; } else { alphas[2] = (4 * a0 + 1 * a1) / 5; alphas[3] = (3 * a0 + 2 * a1) / 5; alphas[4] = (2 * a0 + 3 * a1) / 5; alphas[5] = (1 * a0 + 4 * a1) / 5; alphas[6] = 0; alphas[7] = 255; } const uint8_t* color_block = src + src_offset + 8; uint16_t c0 = ReadU16(color_block); uint16_t c1 = ReadU16(color_block + 2); uint32_t code = ReadU32(color_block + 4); uint8_t colors[4][3]; DecodeRGB565(c0, colors[0][0], colors[0][1], colors[0][2]); DecodeRGB565(c1, colors[1][0], colors[1][1], colors[1][2]); colors[2][0] = (2 * colors[0][0] + colors[1][0]) / 3; colors[2][1] = (2 * colors[0][1] + colors[1][1]) / 3; colors[2][2] = (2 * colors[0][2] + colors[1][2]) / 3; colors[3][0] = (colors[0][0] + 2 * colors[1][0]) / 3; colors[3][1] = (colors[0][1] + 2 * colors[1][1]) / 3; colors[3][2] = (colors[0][2] + 2 * colors[1][2]) / 3; for (int py = 0; py < 4; ++py) { if (by * 4 + py >= height) continue; for (int px = 0; px < 4; ++px) { if (bx * 4 + px >= width) continue; int p_idx = py * 4 + px; int color_idx = (code >> (p_idx * 2)) & 3; int alpha_idx = (a_bits >> (p_idx * 3)) & 7; size_t dst_offset = ((by * 4 + py) * width + (bx * 4 + px)) * 4; dst_rgba[dst_offset + 0] = colors[color_idx][0]; dst_rgba[dst_offset + 1] = colors[color_idx][1]; dst_rgba[dst_offset + 2] = colors[color_idx][2]; dst_rgba[dst_offset + 3] = alphas[alpha_idx]; } } src_offset += 16; } } return true; } bool DxtDecoder::DecodeDDS(const uint8_t* dds_data, size_t size, ImageBuffer& out_image) { if (!dds_data || size < 128) return false; // Magic 'DDS ' if (dds_data[0] != 'D' || dds_data[1] != 'D' || dds_data[2] != 'S' || dds_data[3] != ' ') { return false; } uint32_t height = ReadU32(dds_data + 12); uint32_t width = ReadU32(dds_data + 16); uint32_t fourcc = ReadU32(dds_data + 84); if (width == 0 || height == 0) return false; out_image.width = width; out_image.height = height; out_image.format = EPixelFormat::RGBA8888; out_image.pixels.resize(width * height * 4); const uint8_t* pixel_data = dds_data + 128; if (fourcc == 0x31545844) { // DXT1 return DecompressDXT1(pixel_data, width, height, out_image.pixels.data()); } else if (fourcc == 0x33545844) { // DXT3 return DecompressDXT3(pixel_data, width, height, out_image.pixels.data()); } else if (fourcc == 0x35545844) { // DXT5 return DecompressDXT5(pixel_data, width, height, out_image.pixels.data()); } return false; } } // namespace metin2::texture