Skip to content

Commit c919288

Browse files
committed
Add support for loading webp images
1 parent 0cc1451 commit c919288

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (APPLE)
88
endif ()
99

1010
if (MSVC)
11-
add_compile_options("/Zi")
11+
add_compile_options("/Zi" "/W4")
1212
add_link_options("/DEBUG:FULL")
1313
endif ()
1414

@@ -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

engine/core/core_image.cpp

Lines changed: 36 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>
@@ -126,6 +127,9 @@ image_c* image_c::LoaderForFile(IConsole* conHnd, std::filesystem::path const& f
126127
} else if (*(dword*)dat == 0x20534444) {
127128
// D D S 0x20
128129
return new dds_c(conHnd);
130+
} else if (*(dword*)dat == 0x46464952) {
131+
// R I F F
132+
return new webp_c(conHnd);
129133
} 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))) {
130134
// Detect all valid image types (whether supported or not)
131135
return new targa_c(conHnd);
@@ -482,3 +486,35 @@ bool dds_c::Save(std::filesystem::path const& fileName)
482486
// Nope.
483487
return true;
484488
}
489+
490+
// =========
491+
// WEBP Image
492+
// =========
493+
494+
bool webp_c::Load(std::filesystem::path const& fileName, std::optional<size_callback_t> sizeCallback)
495+
{
496+
// Open file
497+
fileInputStream_c in;
498+
if (in.FileOpen(fileName, true))
499+
return true;
500+
501+
std::vector<byte> fileData(in.GetLen());
502+
if (in.Read(fileData.data(), fileData.size()))
503+
return true;
504+
505+
int width;
506+
int height;
507+
508+
WebPGetInfo(fileData.data(), fileData.size(), &width, &height);
509+
510+
auto data = WebPDecodeRGBA(fileData.data(), fileData.size(), &width, &height);
511+
bool success = CopyRaw(IMGTYPE_RGBA, width, height, data);
512+
513+
return success;
514+
}
515+
516+
bool webp_c::Save(std::filesystem::path const& fileName)
517+
{
518+
// Nope.
519+
return true;
520+
}

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)