Skip to content

Commit

Permalink
Screenshot Service: write ImageWrapper to Screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
geringsj committed Apr 20, 2021
1 parent f72edb7 commit 0bdaa6a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
12 changes: 12 additions & 0 deletions frontend/resources/include/Screenshots.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
41 changes: 40 additions & 1 deletion frontend/services/screenshot_service/Screenshot_Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <glad/glad.h>
#include "GUIState.h"

#include "ImageWrapper.h"
#include "ImageWrapper_to_ByteArray.h"

#include "mmcore/MegaMolGraph.h"

// to write png files
Expand Down Expand Up @@ -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<ImageWrapper&>(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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -205,7 +243,8 @@ std::vector<FrontendResource>& 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Screenshot_Service final : public AbstractFrontendService {
megamol::frontend_resources::ScreenshotImageDataToPNGWriter m_toFileWriter_resource;

std::function<bool(std::string const&)> m_frontbufferToPNG_trigger;
std::function<bool(megamol::frontend_resources::ImageWrapper const&, std::string const&)> m_imagewrapperToPNG_trigger;

std::vector<FrontendResource> m_providedResourceReferences;
std::vector<std::string> m_requestedResourcesNames;
Expand Down

0 comments on commit 0bdaa6a

Please sign in to comment.