Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.20)
project(texture LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(texture STATIC
src/dxt.cpp
src/tga.cpp
src/sub_image.cpp
)
target_include_directories(texture PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
add_library(texture::texture ALIAS texture)
option(TEXTURE_BUILD_TESTS "Build texture SDK tests" ON)
if(TEXTURE_BUILD_TESTS)
add_executable(texture_test tests/texture_test.cpp)
target_link_libraries(texture_test PRIVATE texture)
add_test(NAME texture_test COMMAND texture_test)
endif()
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "types.h"
#include <cstdint>
#include <cstddef>
namespace metin2::texture {
class DxtDecoder {
public:
static bool DecompressDXT1(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba);
static bool DecompressDXT3(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba);
static bool DecompressDXT5(const uint8_t* src, uint32_t width, uint32_t height, uint8_t* dst_rgba);
// Parse complete DDS file header (Magic 'DDS ', DDS_HEADER, pixel format) and decode to RGBA8888
static bool DecodeDDS(const uint8_t* dds_data, size_t size, ImageBuffer& out_image);
};
} // namespace metin2::texture
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include "types.h"
#include <string>
namespace metin2::texture {
class SubImage {
public:
SubImage() = default;
~SubImage() = default;
bool LoadFromString(const std::string& content);
bool LoadFromFile(const std::string& file_path);
const std::string& GetImagePath() const { return m_image_path; }
const Rect2D& GetRect() const { return m_rect; }
int32_t GetWidth() const { return m_rect.GetWidth(); }
int32_t GetHeight() const { return m_rect.GetHeight(); }
UVRect ComputeUV(uint32_t atlas_width, uint32_t atlas_height) const;
// Generate 9-Slice mesh for UI window / button rendering
NineSliceMesh BuildNineSliceMesh(float x, float y, float width, float height,
float border_l, float border_t, float border_r, float border_b,
uint32_t atlas_width, uint32_t atlas_height,
uint32_t color = 0xFFFFFFFF) const;
private:
std::string m_image_path;
Rect2D m_rect;
};
} // namespace metin2::texture
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include "types.h"
#include "dxt.h"
#include "tga.h"
#include "sub_image.h"
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "types.h"
#include <cstdint>
#include <cstddef>
namespace metin2::texture {
class TgaDecoder {
public:
// Decodes uncompressed and RLE-compressed 24-bit/32-bit TGA to RGBA8888
static bool DecodeTGA(const uint8_t* tga_data, size_t size, ImageBuffer& out_image);
};
} // namespace metin2::texture
+58
View File
@@ -0,0 +1,58 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
namespace metin2::texture {
enum class EPixelFormat {
Unknown = 0,
RGBA8888,
RGB888,
DXT1,
DXT3,
DXT5,
BGRA8888
};
struct ImageBuffer {
uint32_t width = 0;
uint32_t height = 0;
EPixelFormat format = EPixelFormat::RGBA8888;
std::vector<uint8_t> pixels;
bool IsValid() const { return width > 0 && height > 0 && !pixels.empty(); }
};
struct Rect2D {
int32_t left = 0;
int32_t top = 0;
int32_t right = 0;
int32_t bottom = 0;
int32_t GetWidth() const { return right - left; }
int32_t GetHeight() const { return bottom - top; }
};
struct UVRect {
float u0 = 0.0f;
float v0 = 0.0f;
float u1 = 1.0f;
float v1 = 1.0f;
};
struct NineSliceVertex {
float x = 0.0f;
float y = 0.0f;
float u = 0.0f;
float v = 0.0f;
uint32_t color = 0xFFFFFFFF;
};
struct NineSliceMesh {
std::vector<NineSliceVertex> vertices;
std::vector<uint32_t> indices;
};
} // namespace metin2::texture
+253
View File
@@ -0,0 +1,253 @@
#include "texture/dxt.h"
#include <cstring>
#include <algorithm>
namespace metin2::texture {
namespace {
inline uint16_t ReadU16(const uint8_t* p) {
return static_cast<uint16_t>(p[0]) | (static_cast<uint16_t>(p[1]) << 8);
}
inline uint32_t ReadU32(const uint8_t* p) {
return static_cast<uint32_t>(p[0]) |
(static_cast<uint32_t>(p[1]) << 8) |
(static_cast<uint32_t>(p[2]) << 16) |
(static_cast<uint32_t>(p[3]) << 24);
}
inline void DecodeRGB565(uint16_t c, uint8_t& r, uint8_t& g, uint8_t& b) {
r = static_cast<uint8_t>(((c >> 11) & 0x1F) * 255 / 31);
g = static_cast<uint8_t>(((c >> 5) & 0x3F) * 255 / 63);
b = static_cast<uint8_t>((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<uint64_t>(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
+144
View File
@@ -0,0 +1,144 @@
#include "texture/sub_image.h"
#include <fstream>
#include <sstream>
#include <algorithm>
namespace metin2::texture {
namespace {
inline std::string Trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, (last - first + 1));
}
inline std::string ToLower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return str;
}
} // namespace
bool SubImage::LoadFromFile(const std::string& file_path) {
std::ifstream file(file_path);
if (!file.is_open()) return false;
std::stringstream buffer;
buffer << file.rdbuf();
return LoadFromString(buffer.str());
}
bool SubImage::LoadFromString(const std::string& content) {
std::istringstream stream(content);
std::string line;
m_image_path.clear();
m_rect = Rect2D();
while (std::getline(stream, line)) {
std::string trimmed = Trim(line);
if (trimmed.empty() || trimmed[0] == '#') continue;
std::istringstream ls(trimmed);
std::string key;
ls >> key;
key = ToLower(key);
std::string val;
ls >> std::ws;
std::getline(ls, val);
val = Trim(val);
if (val.size() >= 2 && val.front() == '"' && val.back() == '"') {
val = val.substr(1, val.size() - 2);
}
if (key == "image") {
m_image_path = val;
} else if (key == "left") {
m_rect.left = std::stoi(val);
} else if (key == "top") {
m_rect.top = std::stoi(val);
} else if (key == "right") {
m_rect.right = std::stoi(val);
} else if (key == "bottom") {
m_rect.bottom = std::stoi(val);
}
}
return !m_image_path.empty() && m_rect.GetWidth() > 0 && m_rect.GetHeight() > 0;
}
UVRect SubImage::ComputeUV(uint32_t atlas_width, uint32_t atlas_height) const {
UVRect uv;
if (atlas_width == 0 || atlas_height == 0) return uv;
float inv_w = 1.0f / static_cast<float>(atlas_width);
float inv_h = 1.0f / static_cast<float>(atlas_height);
uv.u0 = static_cast<float>(m_rect.left) * inv_w;
uv.v0 = static_cast<float>(m_rect.top) * inv_h;
uv.u1 = static_cast<float>(m_rect.right) * inv_w;
uv.v1 = static_cast<float>(m_rect.bottom) * inv_h;
return uv;
}
NineSliceMesh SubImage::BuildNineSliceMesh(float x, float y, float width, float height,
float border_l, float border_t, float border_r, float border_b,
uint32_t atlas_width, uint32_t atlas_height,
uint32_t color) const {
NineSliceMesh mesh;
if (atlas_width == 0 || atlas_height == 0) return mesh;
float inv_w = 1.0f / static_cast<float>(atlas_width);
float inv_h = 1.0f / static_cast<float>(atlas_height);
float xs[4] = { x, x + border_l, x + width - border_r, x + width };
float ys[4] = { y, y + border_t, y + height - border_b, y + height };
float us[4] = {
static_cast<float>(m_rect.left) * inv_w,
static_cast<float>(m_rect.left + border_l) * inv_w,
static_cast<float>(m_rect.right - border_r) * inv_w,
static_cast<float>(m_rect.right) * inv_w
};
float vs[4] = {
static_cast<float>(m_rect.top) * inv_h,
static_cast<float>(m_rect.top + border_t) * inv_h,
static_cast<float>(m_rect.bottom - border_b) * inv_h,
static_cast<float>(m_rect.bottom) * inv_h
};
mesh.vertices.reserve(16);
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
mesh.vertices.push_back({xs[c], ys[r], us[c], vs[r], color});
}
}
mesh.indices.reserve(54); // 9 quads * 6 indices
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
uint32_t i0 = r * 4 + c;
uint32_t i1 = r * 4 + (c + 1);
uint32_t i2 = (r + 1) * 4 + c;
uint32_t i3 = (r + 1) * 4 + (c + 1);
mesh.indices.push_back(i0);
mesh.indices.push_back(i2);
mesh.indices.push_back(i1);
mesh.indices.push_back(i1);
mesh.indices.push_back(i2);
mesh.indices.push_back(i3);
}
}
return mesh;
}
} // namespace metin2::texture
+88
View File
@@ -0,0 +1,88 @@
#include "texture/tga.h"
#include <cstring>
#include <algorithm>
namespace metin2::texture {
bool TgaDecoder::DecodeTGA(const uint8_t* data, size_t size, ImageBuffer& out_image) {
if (!data || size < 18) return false;
uint8_t id_length = data[0];
uint8_t image_type = data[2];
uint16_t width = static_cast<uint16_t>(data[12]) | (static_cast<uint16_t>(data[13]) << 8);
uint16_t height = static_cast<uint16_t>(data[14]) | (static_cast<uint16_t>(data[15]) << 8);
uint8_t bpp = data[16];
uint8_t descriptor = data[17];
if (width == 0 || height == 0) return false;
if (bpp != 24 && bpp != 32) return false;
if (image_type != 2 && image_type != 10) return false; // Only uncompressed or RLE truecolor
bool is_rle = (image_type == 10);
bool flip_vertically = !(descriptor & 0x20); // Bit 5: 0 means origin is bottom-left
size_t pixel_bytes = bpp / 8;
size_t offset = 18 + id_length;
out_image.width = width;
out_image.height = height;
out_image.format = EPixelFormat::RGBA8888;
out_image.pixels.resize(width * height * 4);
std::vector<uint8_t> raw_pixels(width * height * pixel_bytes);
if (!is_rle) {
if (size < offset + width * height * pixel_bytes) return false;
std::memcpy(raw_pixels.data(), data + offset, width * height * pixel_bytes);
} else {
size_t current_pixel = 0;
size_t total_pixels = width * height;
while (current_pixel < total_pixels && offset < size) {
uint8_t header = data[offset++];
int count = (header & 0x7F) + 1;
if (header & 0x80) { // RLE packet
if (offset + pixel_bytes > size) return false;
const uint8_t* p = data + offset;
offset += pixel_bytes;
for (int i = 0; i < count && current_pixel < total_pixels; ++i) {
std::memcpy(raw_pixels.data() + current_pixel * pixel_bytes, p, pixel_bytes);
current_pixel++;
}
} else { // Raw packet
for (int i = 0; i < count && current_pixel < total_pixels; ++i) {
if (offset + pixel_bytes > size) return false;
std::memcpy(raw_pixels.data() + current_pixel * pixel_bytes, data + offset, pixel_bytes);
offset += pixel_bytes;
current_pixel++;
}
}
}
}
// Convert BGR/BGRA to RGBA with vertical flip consideration
for (uint32_t y = 0; y < height; ++y) {
uint32_t src_y = flip_vertically ? (height - 1 - y) : y;
for (uint32_t x = 0; x < width; ++x) {
size_t src_idx = (src_y * width + x) * pixel_bytes;
size_t dst_idx = (y * width + x) * 4;
uint8_t b = raw_pixels[src_idx + 0];
uint8_t g = raw_pixels[src_idx + 1];
uint8_t r = raw_pixels[src_idx + 2];
uint8_t a = (bpp == 32) ? raw_pixels[src_idx + 3] : 255;
out_image.pixels[dst_idx + 0] = r;
out_image.pixels[dst_idx + 1] = g;
out_image.pixels[dst_idx + 2] = b;
out_image.pixels[dst_idx + 3] = a;
}
}
return true;
}
} // namespace metin2::texture
+117
View File
@@ -0,0 +1,117 @@
#include "texture/texture.h"
#include <iostream>
#include <cassert>
#include <vector>
#include <cstring>
using namespace metin2::texture;
int main() {
std::cout << "[Test 1] DXT1 Decompression..." << std::endl;
// 4x4 block of DXT1:
// Color 0: Pure Red (0xF800 in RGB565)
// Color 1: Pure Blue (0x001F in RGB565)
// Color codes: 0x00000000 (all 16 pixels use Color 0)
uint8_t dxt1_block[8] = {
0x00, 0xF8, // c0 = 0xF800 (Red)
0x1F, 0x00, // c1 = 0x001F (Blue)
0x00, 0x00, 0x00, 0x00 // all pixel indices = 0 (c0)
};
std::vector<uint8_t> dst_rgba(4 * 4 * 4, 0);
bool ok_dxt1 = DxtDecoder::DecompressDXT1(dxt1_block, 4, 4, dst_rgba.data());
assert(ok_dxt1);
// Verify first pixel is Red
assert(dst_rgba[0] == 255); // R
assert(dst_rgba[1] == 0); // G
assert(dst_rgba[2] == 0); // B
assert(dst_rgba[3] == 255); // A
std::cout << "[Test 2] DXT5 Decompression..." << std::endl;
// 4x4 block of DXT5:
// Alpha 0: 255, Alpha 1: 0, Indices: all 0 (Alpha 0)
// Color 0: Pure Green (0x07E0), Color 1: Black (0x0000), Indices: all 0
uint8_t dxt5_block[16] = {
255, 0, // a0 = 255, a1 = 0
0, 0, 0, 0, 0, 0, // alpha indices = 0
0xE0, 0x07, // c0 = 0x07E0 (Green)
0x00, 0x00, // c1 = 0x0000 (Black)
0x00, 0x00, 0x00, 0x00 // color indices = 0
};
std::vector<uint8_t> dst_dxt5(4 * 4 * 4, 0);
bool ok_dxt5 = DxtDecoder::DecompressDXT5(dxt5_block, 4, 4, dst_dxt5.data());
assert(ok_dxt5);
assert(dst_dxt5[0] == 0); // R
assert(dst_dxt5[1] == 255); // G
assert(dst_dxt5[2] == 0); // B
assert(dst_dxt5[3] == 255); // A
std::cout << "[Test 3] TGA Uncompressed 24-bit Decompression..." << std::endl;
// 2x2 uncompressed 24-bit TGA:
// Header 18 bytes: width=2, height=2, bpp=24, image_type=2, descriptor=0x20 (top-left)
uint8_t tga_header[18] = {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 0, // width = 2
2, 0, // height = 2
24, // bpp = 24
0x20 // top-to-bottom
};
// 4 pixels BGR: all yellow (B=0, G=255, R=255)
uint8_t tga_pixels[12] = {
0, 255, 255,
0, 255, 255,
0, 255, 255,
0, 255, 255
};
std::vector<uint8_t> tga_data(18 + 12);
std::memcpy(tga_data.data(), tga_header, 18);
std::memcpy(tga_data.data() + 18, tga_pixels, 12);
ImageBuffer tga_img;
bool ok_tga = TgaDecoder::DecodeTGA(tga_data.data(), tga_data.size(), tga_img);
assert(ok_tga);
assert(tga_img.width == 2);
assert(tga_img.height == 2);
assert(tga_img.pixels[0] == 255); // R
assert(tga_img.pixels[1] == 255); // G
assert(tga_img.pixels[2] == 0); // B
assert(tga_img.pixels[3] == 255); // A
std::cout << "[Test 4] SubImage (.sub) parsing and Atlas UV computation..." << std::endl;
const std::string sample_sub =
"title subImage\r\n"
"version 0.1\r\n"
"image \"d:/ymir work/ui/public.dds\"\r\n"
"left 64\r\n"
"top 128\r\n"
"right 128\r\n"
"bottom 192\r\n";
SubImage sub;
bool ok_sub = sub.LoadFromString(sample_sub);
assert(ok_sub);
assert(sub.GetImagePath() == "d:/ymir work/ui/public.dds");
assert(sub.GetWidth() == 64);
assert(sub.GetHeight() == 64);
UVRect uv = sub.ComputeUV(256, 256);
assert(std::abs(uv.u0 - 0.25f) < 1e-4f);
assert(std::abs(uv.v0 - 0.50f) < 1e-4f);
assert(std::abs(uv.u1 - 0.50f) < 1e-4f);
assert(std::abs(uv.v1 - 0.75f) < 1e-4f);
std::cout << "[Test 5] 9-Slice Mesh Generation..." << std::endl;
NineSliceMesh mesh = sub.BuildNineSliceMesh(100.0f, 100.0f, 200.0f, 200.0f, 10.0f, 10.0f, 10.0f, 10.0f, 256, 256);
assert(mesh.vertices.size() == 16);
assert(mesh.indices.size() == 54);
// Check top-left corner vertex
assert(mesh.vertices[0].x == 100.0f);
assert(mesh.vertices[0].y == 100.0f);
// Check bottom-right corner vertex
assert(mesh.vertices[15].x == 300.0f);
assert(mesh.vertices[15].y == 300.0f);
std::cout << "All Texture SDK tests passed successfully!" << std::endl;
return 0;
}