Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ find_package(sol2 CONFIG REQUIRED)
find_package(Threads REQUIRED)
find_package(zstd REQUIRED)
find_package(ZLIB REQUIRED)
find_package(WebP)

add_library(cmp_core STATIC
dep/compressonator/cmp_core/source/cmp_core.cpp
Expand Down Expand Up @@ -238,6 +239,7 @@ target_link_libraries(SimpleGraphic
re2::re2
sol2
Threads::Threads
WebP::webpdecoder
ZLIB::ZLIB
zstd::libzstd_shared
)
Expand Down
43 changes: 43 additions & 0 deletions engine/core/core_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "webp/decode.h"

#include <algorithm>
#include <filesystem>
Expand Down Expand Up @@ -107,6 +108,8 @@ image_c* image_c::LoaderForFile(IConsole* conHnd, std::filesystem::path const& f
}
if (fileName.extension() == ".dds")
return new dds_c(conHnd);
if (fileName.extension() == ".webp")
return new webp_c(conHnd);

// Attempt to detect image file type from first 4 bytes of file
byte dat[4];
Expand All @@ -126,6 +129,9 @@ image_c* image_c::LoaderForFile(IConsole* conHnd, std::filesystem::path const& f
} else if (*(dword*)dat == 0x20534444) {
// D D S 0x20
return new dds_c(conHnd);
} else if (*(dword*)dat == 0x46464952) {
// R I F F
return new webp_c(conHnd);
} else if ((dat[1] == 0 && (dat[2] == 2 || dat[2] == 3 || dat[2] == 10 || dat[2] == 11)) || (dat[1] == 1 && (dat[2] == 1 || dat[2] == 9))) {
// Detect all valid image types (whether supported or not)
return new targa_c(conHnd);
Expand Down Expand Up @@ -482,3 +488,40 @@ bool dds_c::Save(std::filesystem::path const& fileName)
// Nope.
return true;
}

// =========
// WEBP Image
// =========

bool webp_c::Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback)
{
// Open file
fileInputStream_c in;
if (in.FileOpen(fileName, true))
return true;

std::vector<byte> fileData(in.GetLen());
if (in.Read(fileData.data(), fileData.size()))
return true;

int width;
int height;

bool valid = WebPGetInfo(fileData.data(), fileData.size(), &width, &height);
if (!valid)
return true;

if (sizeCallback)
(*sizeCallback)(width, height);
auto data = WebPDecodeRGBA(fileData.data(), fileData.size(), &width, &height);
bool success = CopyRaw(IMGTYPE_RGBA, width, height, data);
WebPFree(data);

return !success;
}

bool webp_c::Save(std::filesystem::path const& fileName)
{
// Nope.
return true;
}
8 changes: 8 additions & 0 deletions engine/core/core_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ class dds_c : public image_c {
bool Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback = {}) override;
bool Save(std::filesystem::path const& fileName) override;
};

// WEBP Image
class webp_c : public image_c {
public:
webp_c(IConsole* conHnd) : image_c(conHnd) {}
bool Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback = {}) override;
bool Save(std::filesystem::path const& fileName) override;
};
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fmt",
"glfw3",
"gli",
"libwebp",
"luajit",
"ms-gsl",
"pkgconf",
Expand Down
Loading