From 0bdaa6a8a7df78be7b17fce7737d85345a6395a7 Mon Sep 17 00:00:00 2001 From: Sergej Geringer Date: Mon, 19 Apr 2021 23:13:20 +0200 Subject: [PATCH] Screenshot Service: write ImageWrapper to Screenshot --- frontend/resources/include/Screenshots.h | 12 ++++++ .../screenshot_service/Screenshot_Service.cpp | 41 ++++++++++++++++++- .../screenshot_service/Screenshot_Service.hpp | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/frontend/resources/include/Screenshots.h b/frontend/resources/include/Screenshots.h index 6f74c59c6e..ddabf91c60 100644 --- a/frontend/resources/include/Screenshots.h +++ b/frontend/resources/include/Screenshots.h @@ -66,6 +66,18 @@ class IImageDataWriter { ~IImageDataWriter() = default; }; +struct ImageWrapper; +class ImageWrapperScreenshotSource : public IScreenshotSource { +public: + ImageWrapperScreenshotSource() = default; + ImageWrapperScreenshotSource(ImageWrapper const& image); + + ScreenshotImageData const& take_screenshot() const override; + +private: + ImageWrapper* m_image = nullptr; +}; + class GLScreenshotSource : public IScreenshotSource { public: enum ReadBuffer { FRONT, BACK, COLOR_ATT0, COLOR_ATT1, COLOR_ATT2, COLOR_ATT3}; diff --git a/frontend/services/screenshot_service/Screenshot_Service.cpp b/frontend/services/screenshot_service/Screenshot_Service.cpp index 1b036de2bd..ae9061230c 100644 --- a/frontend/services/screenshot_service/Screenshot_Service.cpp +++ b/frontend/services/screenshot_service/Screenshot_Service.cpp @@ -11,6 +11,9 @@ #include #include "GUIState.h" +#include "ImageWrapper.h" +#include "ImageWrapper_to_ByteArray.h" + #include "mmcore/MegaMolGraph.h" // to write png files @@ -109,6 +112,35 @@ static bool write_png_to_file(megamol::frontend_resources::ScreenshotImageData c return true; } +megamol::frontend_resources::ImageWrapperScreenshotSource::ImageWrapperScreenshotSource(ImageWrapper const& image) + : m_image{& const_cast(image)} +{} + +megamol::frontend_resources::ScreenshotImageData const& megamol::frontend_resources::ImageWrapperScreenshotSource::take_screenshot() const { + static ScreenshotImageData screenshot_image; + + // keep allocated vector memory around + // note that this initially holds a nullptr texture - bad! + static frontend_resources::byte_texture image_bytes({}); + + // fill bytes with image data + image_bytes = *m_image; + auto& byte_vector = image_bytes.as_byte_vector(); + + screenshot_image.resize(m_image->size.width, m_image->size.height); + + for (size_t i = 0, j = 0; i < byte_vector.size();) { + auto r = [&]() { return byte_vector[i++]; }; + auto g = [&]() { return byte_vector[i++]; }; + auto b = [&]() { return byte_vector[i++]; }; + auto a = [&]() { return (m_image->channels == ImageWrapper::DataChannels::RGBA8) ? byte_vector[i++] : default_alpha_value; }; // alpha either from image or 1.0 + ScreenshotImageData::Pixel pixel = { r(), g(), b(), a() }; + screenshot_image.image[j++] = pixel; + } + + return screenshot_image; +} + void megamol::frontend_resources::GLScreenshotSource::set_read_buffer(ReadBuffer buffer) { m_read_buffer = buffer; GLenum read_buffer; @@ -190,6 +222,12 @@ bool Screenshot_Service::init(const Config& config) { return m_toFileWriter_resource.write_screenshot(m_frontbufferSource_resource, filename); }; + this->m_imagewrapperToPNG_trigger = [&](megamol::frontend_resources::ImageWrapper const& image, std::string const& filename) -> bool + { + log("write screenshot to " + filename); + return m_toFileWriter_resource.write_screenshot(megamol::frontend_resources::ImageWrapperScreenshotSource(image), filename); + }; + log("initialized successfully"); return true; } @@ -205,7 +243,8 @@ std::vector& Screenshot_Service::getProvidedResources() { { {"GLScreenshotSource", m_frontbufferSource_resource}, {"ImageDataToPNGWriter", m_toFileWriter_resource}, - {"GLFrontbufferToPNG_ScreenshotTrigger", m_frontbufferToPNG_trigger} + {"GLFrontbufferToPNG_ScreenshotTrigger", m_frontbufferToPNG_trigger}, + {"ImageWrapperToPNG_ScreenshotTrigger", m_imagewrapperToPNG_trigger} }; return m_providedResourceReferences; diff --git a/frontend/services/screenshot_service/Screenshot_Service.hpp b/frontend/services/screenshot_service/Screenshot_Service.hpp index 382c0bfe39..abc7bb567a 100644 --- a/frontend/services/screenshot_service/Screenshot_Service.hpp +++ b/frontend/services/screenshot_service/Screenshot_Service.hpp @@ -58,6 +58,7 @@ class Screenshot_Service final : public AbstractFrontendService { megamol::frontend_resources::ScreenshotImageDataToPNGWriter m_toFileWriter_resource; std::function m_frontbufferToPNG_trigger; + std::function m_imagewrapperToPNG_trigger; std::vector m_providedResourceReferences; std::vector m_requestedResourcesNames;