From 6053f78ec2eec829b4d9c9330cf3b5d591a7bfa2 Mon Sep 17 00:00:00 2001 From: Grayson Date: Fri, 2 Apr 2021 13:08:41 -0400 Subject: [PATCH] add Image::setImageFromMem Allows images to be set from raw data --- library/include/borealis/views/image.hpp | 5 +++++ library/lib/views/image.cpp | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/library/include/borealis/views/image.hpp b/library/include/borealis/views/image.hpp index 49ea36d22..71d6defb7 100644 --- a/library/include/borealis/views/image.hpp +++ b/library/include/borealis/views/image.hpp @@ -76,6 +76,11 @@ class Image : public View */ void setImageFromFile(std::string path); + /** + * Sets the image from raw data + */ + void setImageFromMem(unsigned char* data, int size); + /** * Sets the scaling type for this image. * diff --git a/library/lib/views/image.cpp b/library/lib/views/image.cpp index df4ddd99b..5633af7cf 100644 --- a/library/lib/views/image.cpp +++ b/library/lib/views/image.cpp @@ -260,6 +260,28 @@ void Image::setImageFromFile(std::string path) this->invalidate(); } +void Image::setImageFromMem(unsigned char* data, int size) +{ + NVGcontext* vg = Application::getNVGContext(); + + // Free the old texture if necessary + if (this->texture != 0) + nvgDeleteImage(vg, this->texture); + + // Load the new texture + this->texture = nvgCreateImageMem(vg, 0, data, size); + + if (this->texture == 0) + fatal("Cannot load image from mem!"); + + int width, height; + nvgImageSize(vg, this->texture, &width, &height); + this->originalImageWidth = (float)width; + this->originalImageHeight = (float)height; + + this->invalidate(); +} + void Image::setScalingType(ImageScalingType scalingType) { this->scalingType = scalingType;