Skip to content

Commit

Permalink
[formats] add: Image::ctor to construct from Plt on cpu.
Browse files Browse the repository at this point in the history
  • Loading branch information
jd28 committed Jul 8, 2024
1 parent 3526bd7 commit 63411c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
26 changes: 25 additions & 1 deletion lib/nw/formats/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include "../log.hpp"
#include "../util/platform.hpp"
#include "../util/string.hpp"
#include "Plt.hpp"

#include <nowide/convert.hpp>
#include <stb/image_DXT.h>
#include <stb/stb_image.h>
#include <stb/stb_image_write.h>

#include <filesystem>
#include <memory>

namespace fs = std::filesystem;

Expand All @@ -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<uint8_t*>(malloc(4ull * height_ * width_));
auto ptr = reinterpret_cast<uint32_t*>(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_}
Expand Down
8 changes: 5 additions & 3 deletions lib/nw/formats/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include "../resources/ResourceData.hpp"

#include <memory>
#include <string>
#include <filesystem>

namespace nw {

struct Plt;
struct PltColors;

/**
* @brief Image Resource
*
Expand All @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions tests/legacy_plt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

0 comments on commit 63411c1

Please sign in to comment.