Support reading arbitrary image formats via stb_image
This commit is contained in:
parent
a8fc4ea741
commit
dca01b7cc4
5 changed files with 8057 additions and 2 deletions
|
|
@ -1,7 +1,7 @@
|
|||
find_file(KHR_PLATFORM_FILE KHR/khrplatform.h)
|
||||
|
||||
file(GLOB_RECURSE PSEMEK_GFX_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "include/*.hpp")
|
||||
file(GLOB_RECURSE PSEMEK_GFX_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "source/*.cpp")
|
||||
file(GLOB_RECURSE PSEMEK_GFX_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "include/*.hpp" "include/*.h")
|
||||
file(GLOB_RECURSE PSEMEK_GFX_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "source/*.cpp" "source/*.c")
|
||||
|
||||
psemek_add_library(psemek-gfx ${PSEMEK_GFX_HEADERS} ${PSEMEK_GFX_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/api/${PSEMEK_GL_API}/source/gl.cpp")
|
||||
target_include_directories(psemek-gfx PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/api/${PSEMEK_GL_API}/include")
|
||||
|
|
|
|||
7987
libs/gfx/include/psemek/gfx/detail/stb_image.h
Normal file
7987
libs/gfx/include/psemek/gfx/detail/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -29,6 +29,13 @@ namespace psemek::gfx
|
|||
|
||||
void write_png(pixmap_rgba const & p, io::ostream && os);
|
||||
|
||||
template <typename Pixel>
|
||||
basic_pixmap<Pixel> read_image(io::istream && is);
|
||||
|
||||
extern template pixmap_monochrome read_image<std::uint8_t>(io::istream && is);
|
||||
extern template pixmap_rgb read_image<color_rgb>(io::istream && is);
|
||||
extern template pixmap_rgba read_image<color_rgba>(io::istream && is);
|
||||
|
||||
template <typename Pixel, std::size_t N>
|
||||
auto to_srgb(util::array<Pixel, N> pm, float g = 1.f / 2.2f)
|
||||
{
|
||||
|
|
|
|||
7
libs/gfx/source/detail/stb_image.c
Normal file
7
libs/gfx/source/detail/stb_image.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_FAILURE_USERMSG
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
|
||||
#include <psemek/gfx/detail/stb_image.h>
|
||||
#pragma GCC diagnostic pop
|
||||
54
libs/gfx/source/pixmap.cpp
Normal file
54
libs/gfx/source/pixmap.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <psemek/gfx/pixmap.hpp>
|
||||
#include <psemek/gfx/detail/stb_image.h>
|
||||
#include <psemek/util/exception.hpp>
|
||||
|
||||
namespace psemek::gfx
|
||||
{
|
||||
|
||||
template <typename Pixel>
|
||||
basic_pixmap<Pixel> read_image(io::istream && is)
|
||||
{
|
||||
stbi_io_callbacks callbacks;
|
||||
|
||||
callbacks.read = [](void * user, char * data, int size) -> int
|
||||
{
|
||||
return reinterpret_cast<io::istream *>(user)->read(data, size);
|
||||
};
|
||||
|
||||
callbacks.skip = [](void * user, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
throw util::exception("unget is not supported in stb_image skip callback");
|
||||
|
||||
char buffer[1024];
|
||||
while (count > 0)
|
||||
{
|
||||
int read_count = std::min(1024, count);
|
||||
count -= reinterpret_cast<io::istream *>(user)->read(buffer, read_count);
|
||||
}
|
||||
};
|
||||
|
||||
callbacks.eof = [](void * user)
|
||||
{
|
||||
return reinterpret_cast<io::istream *>(user)->finished() ? 1 : 0;
|
||||
};
|
||||
|
||||
int width, height, channels;
|
||||
auto data = stbi_load_from_callbacks(&callbacks, &is, &width, &height, &channels, sizeof(Pixel));
|
||||
|
||||
if (!data)
|
||||
throw util::exception(stbi_failure_reason());
|
||||
|
||||
basic_pixmap<Pixel> result({width, height});
|
||||
std::copy(data, data + width * height * sizeof(Pixel), reinterpret_cast<stbi_uc *>(result.data()));
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template pixmap_monochrome read_image<std::uint8_t>(io::istream && is);
|
||||
template pixmap_rgb read_image<color_rgb>(io::istream && is);
|
||||
template pixmap_rgba read_image<color_rgba>(io::istream && is);
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue