Files

118 lines
4.1 KiB
C++

#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;
}