Support saving images via stb_image_write
This commit is contained in:
parent
6ff3351ab8
commit
1c54dd85a5
4 changed files with 1812 additions and 0 deletions
1724
libs/gfx/include/psemek/gfx/detail/stb_image_write.h
Normal file
1724
libs/gfx/include/psemek/gfx/detail/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -36,6 +36,20 @@ namespace psemek::gfx
|
||||||
extern template pixmap_rgb read_image<color_rgb>(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);
|
extern template pixmap_rgba read_image<color_rgba>(io::istream && is);
|
||||||
|
|
||||||
|
void write_image_png(pixmap_monochrome const & p, io::ostream && os);
|
||||||
|
void write_image_png(pixmap_rgb const & p, io::ostream && os);
|
||||||
|
void write_image_png(pixmap_rgba const & p, io::ostream && os);
|
||||||
|
|
||||||
|
void write_image_tga(pixmap_monochrome const & p, io::ostream && os);
|
||||||
|
void write_image_tga(pixmap_rgb const & p, io::ostream && os);
|
||||||
|
void write_image_tga(pixmap_rgba const & p, io::ostream && os);
|
||||||
|
|
||||||
|
void write_image_bmp(pixmap_rgb const & p, io::ostream && os);
|
||||||
|
|
||||||
|
void write_image_jpg(pixmap_rgb const & p, io::ostream && os, int quality = 100);
|
||||||
|
|
||||||
|
void write_image_hdr(basic_pixmap<color_3f> const & p, io::ostream && os);
|
||||||
|
|
||||||
template <typename Pixel, std::size_t N>
|
template <typename Pixel, std::size_t N>
|
||||||
auto to_srgb(util::array<Pixel, N> pm, float g = 1.f / 2.2f)
|
auto to_srgb(util::array<Pixel, N> pm, float g = 1.f / 2.2f)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
9
libs/gfx/source/detail/stb_image_write.cpp
Normal file
9
libs/gfx/source/detail/stb_image_write.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
|
||||||
|
#include <psemek/util/assert.hpp>
|
||||||
|
#define STBIW_ASSERT(x) assert(x)
|
||||||
|
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||||
|
#include <psemek/gfx/detail/stb_image_write.h>
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <psemek/gfx/pixmap.hpp>
|
#include <psemek/gfx/pixmap.hpp>
|
||||||
#include <psemek/gfx/detail/stb_image.h>
|
#include <psemek/gfx/detail/stb_image.h>
|
||||||
|
#include <psemek/gfx/detail/stb_image_write.h>
|
||||||
#include <psemek/util/exception.hpp>
|
#include <psemek/util/exception.hpp>
|
||||||
|
|
||||||
namespace psemek::gfx
|
namespace psemek::gfx
|
||||||
|
|
@ -51,4 +52,68 @@ namespace psemek::gfx
|
||||||
template pixmap_rgb read_image<color_rgb>(io::istream && is);
|
template pixmap_rgb read_image<color_rgb>(io::istream && is);
|
||||||
template pixmap_rgba read_image<color_rgba>(io::istream && is);
|
template pixmap_rgba read_image<color_rgba>(io::istream && is);
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
void write_func(void * context, void * data, int size)
|
||||||
|
{
|
||||||
|
reinterpret_cast<io::ostream *>(context)->write(reinterpret_cast<char *>(data), size);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_png(pixmap_monochrome const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_png_to_func(write_func, &os, p.width(), p.height(), 1, p.data(), p.width() * 1) == 0)
|
||||||
|
throw util::exception("Failed to write monochrome png");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_png(pixmap_rgb const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_png_to_func(write_func, &os, p.width(), p.height(), 3, p.data(), p.width() * 3) == 0)
|
||||||
|
throw util::exception("Failed to write rgb png");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_png(pixmap_rgba const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_png_to_func(write_func, &os, p.width(), p.height(), 4, p.data(), p.width() * 4) == 0)
|
||||||
|
throw util::exception("Failed to write rgba png");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_tga(pixmap_monochrome const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_tga_to_func(write_func, &os, p.width(), p.height(), 1, p.data()) == 0)
|
||||||
|
throw util::exception("Failed to write monochrome tga");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_tga(pixmap_rgb const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_tga_to_func(write_func, &os, p.width(), p.height(), 3, p.data()) == 0)
|
||||||
|
throw util::exception("Failed to write rgb tga");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_tga(pixmap_rgba const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_tga_to_func(write_func, &os, p.width(), p.height(), 4, p.data()) == 0)
|
||||||
|
throw util::exception("Failed to write rgba tga");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_bmp(pixmap_rgb const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_bmp_to_func(write_func, &os, p.width(), p.height(), 3, p.data()) == 0)
|
||||||
|
throw util::exception("Failed to write rgb bmp");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_jpg(pixmap_rgb const & p, io::ostream && os, int quality)
|
||||||
|
{
|
||||||
|
if (stbi_write_jpg_to_func(write_func, &os, p.width(), p.height(), 3, p.data(), quality) == 0)
|
||||||
|
throw util::exception("Failed to write rgb jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_image_hdr(basic_pixmap<color_3f> const & p, io::ostream && os)
|
||||||
|
{
|
||||||
|
if (stbi_write_hdr_to_func(write_func, &os, p.width(), p.height(), 1, reinterpret_cast<float const *>(p.data())) == 0)
|
||||||
|
throw util::exception("Failed to write rgb hdr");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue