Skip to content

Commit 1096926

Browse files
authored
Merge pull request #88 from Wires77/webp_support
2 parents 0cc1451 + be98f7e commit 1096926

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ find_package(sol2 CONFIG REQUIRED)
129129
find_package(Threads REQUIRED)
130130
find_package(zstd REQUIRED)
131131
find_package(ZLIB REQUIRED)
132+
find_package(WebP)
132133

133134
add_library(cmp_core STATIC
134135
dep/compressonator/cmp_core/source/cmp_core.cpp
@@ -238,6 +239,7 @@ target_link_libraries(SimpleGraphic
238239
re2::re2
239240
sol2
240241
Threads::Threads
242+
WebP::webpdecoder
241243
ZLIB::ZLIB
242244
zstd::libzstd_shared
243245
)

engine/core/core_image.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#define STB_IMAGE_WRITE_IMPLEMENTATION
1919
#include "stb_image_write.h"
20+
#include "webp/decode.h"
2021

2122
#include <algorithm>
2223
#include <filesystem>
@@ -107,6 +108,8 @@ image_c* image_c::LoaderForFile(IConsole* conHnd, std::filesystem::path const& f
107108
}
108109
if (fileName.extension() == ".dds")
109110
return new dds_c(conHnd);
111+
if (fileName.extension() == ".webp")
112+
return new webp_c(conHnd);
110113

111114
// Attempt to detect image file type from first 4 bytes of file
112115
byte dat[4];
@@ -126,6 +129,9 @@ image_c* image_c::LoaderForFile(IConsole* conHnd, std::filesystem::path const& f
126129
} else if (*(dword*)dat == 0x20534444) {
127130
// D D S 0x20
128131
return new dds_c(conHnd);
132+
} else if (*(dword*)dat == 0x46464952) {
133+
// R I F F
134+
return new webp_c(conHnd);
129135
} 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))) {
130136
// Detect all valid image types (whether supported or not)
131137
return new targa_c(conHnd);
@@ -482,3 +488,40 @@ bool dds_c::Save(std::filesystem::path const& fileName)
482488
// Nope.
483489
return true;
484490
}
491+
492+
// =========
493+
// WEBP Image
494+
// =========
495+
496+
bool webp_c::Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback)
497+
{
498+
// Open file
499+
fileInputStream_c in;
500+
if (in.FileOpen(fileName, true))
501+
return true;
502+
503+
std::vector<byte> fileData(in.GetLen());
504+
if (in.Read(fileData.data(), fileData.size()))
505+
return true;
506+
507+
int width;
508+
int height;
509+
510+
bool valid = WebPGetInfo(fileData.data(), fileData.size(), &width, &height);
511+
if (!valid)
512+
return true;
513+
514+
if (sizeCallback)
515+
(*sizeCallback)(width, height);
516+
auto data = WebPDecodeRGBA(fileData.data(), fileData.size(), &width, &height);
517+
bool success = CopyRaw(IMGTYPE_RGBA, width, height, data);
518+
WebPFree(data);
519+
520+
return !success;
521+
}
522+
523+
bool webp_c::Save(std::filesystem::path const& fileName)
524+
{
525+
// Nope.
526+
return true;
527+
}

engine/core/core_image.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ class dds_c : public image_c {
118118
bool Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback = {}) override;
119119
bool Save(std::filesystem::path const& fileName) override;
120120
};
121+
122+
// WEBP Image
123+
class webp_c : public image_c {
124+
public:
125+
webp_c(IConsole* conHnd) : image_c(conHnd) {}
126+
bool Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback = {}) override;
127+
bool Save(std::filesystem::path const& fileName) override;
128+
};

vcpkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"fmt",
88
"glfw3",
99
"gli",
10+
"libwebp",
1011
"luajit",
1112
"ms-gsl",
1213
"pkgconf",

0 commit comments

Comments
 (0)