diff --git a/lib/nw/formats/Image.cpp b/lib/nw/formats/Image.cpp index 3f287740e..57a99aebb 100644 --- a/lib/nw/formats/Image.cpp +++ b/lib/nw/formats/Image.cpp @@ -3,13 +3,14 @@ #include "../log.hpp" #include "../util/platform.hpp" #include "../util/string.hpp" +#include "Plt.hpp" #include #include #include #include -#include +#include namespace fs = std::filesystem; @@ -31,6 +32,29 @@ Image::Image(ResourceData data) is_loaded_ = parse(); } +Image::Image(const Plt& plt, const PltColors& colors) + : bytes_{nullptr} + , is_dds_(false) + , is_bio_dds_(false) + , width_(plt.width()) + , height_(plt.height()) + , channels_(4) +{ + if (!plt.valid()) { + is_loaded_ = false; + return; + } + + bytes_ = reinterpret_cast(malloc(4ull * height_ * width_)); + auto ptr = reinterpret_cast(bytes_); + for (uint32_t x = 0; x < width_; ++x) { + for (uint32_t y = 0; y < height_; ++y) { + ptr[y * width_ + x] = decode_plt_color(plt, colors, x, y); + } + } + is_loaded_ = true; +} + Image::Image(Image&& other) : data_{std::move(other.data_)} , is_loaded_{other.is_loaded_} diff --git a/lib/nw/formats/Image.hpp b/lib/nw/formats/Image.hpp index a6638ff53..418a4fd12 100644 --- a/lib/nw/formats/Image.hpp +++ b/lib/nw/formats/Image.hpp @@ -2,11 +2,13 @@ #include "../resources/ResourceData.hpp" -#include -#include +#include namespace nw { +struct Plt; +struct PltColors; + /** * @brief Image Resource * @@ -16,9 +18,9 @@ namespace nw { * * @note Even though this supports writing images, this is **catagorically** * not a tool for converting/compressing textures. - * @todo plt */ struct Image { + explicit Image(const Plt& plt, const PltColors& colors); explicit Image(const std::filesystem::path& filename); explicit Image(ResourceData data); diff --git a/tests/legacy_plt.cpp b/tests/legacy_plt.cpp index c412b1e48..b1ce05572 100644 --- a/tests/legacy_plt.cpp +++ b/tests/legacy_plt.cpp @@ -22,3 +22,16 @@ TEST(Plt, Load) auto color = nw::decode_plt_color(plt, {0}, 0, 0); EXPECT_EQ(color, 0x8B6651FF); } + +TEST(Plt, ToImage) +{ + nw::Plt plt{"test_data/user/development/pmh0_head001.plt"}; + EXPECT_TRUE(plt.valid()); + nw::PltColors colors; + colors.data[uint8_t(nw::PltLayer::plt_layer_hair)] = 10; + colors.data[uint8_t(nw::PltLayer::plt_layer_skin)] = 56; + + nw::Image img(plt, colors); + EXPECT_TRUE(img.valid()); + EXPECT_TRUE(img.write_to("tmp/pmh0_head001.png")); +}