Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Image::setImageFromMemory and Image::setImageFromRawData #162

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string>

#include "captioned_image.hpp"
#include "random_rgba_image.hpp"
#include "components_tab.hpp"
#include "main_activity.hpp"
#include "recycling_list_tab.hpp"
Expand Down Expand Up @@ -60,6 +61,7 @@ int main(int argc, char* argv[])
brls::Application::registerXMLView("CaptionedImage", CaptionedImage::create);
brls::Application::registerXMLView("RecyclingListTab", RecyclingListTab::create);
brls::Application::registerXMLView("ComponentsTab", ComponentsTab::create);
brls::Application::registerXMLView("RandomRGBAImage", RandomRGBAImage::create);

// Add custom values to the theme
brls::getLightTheme().addColor("captioned_image/caption", nvgRGB(2, 176, 183));
Expand Down
99 changes: 99 additions & 0 deletions demo/random_rgba_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2021 Jonathan Verbeek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "random_rgba_image.hpp"

RandomRGBAImage::RandomRGBAImage()
{
// Load the XML file and inflate ourself with its content
this->inflateFromXMLRes("xml/views/random_rgba_image.xml");

// Forward Image XML attributes
this->forwardXMLAttribute("scalingType", this->image);
this->forwardXMLAttribute("image", this->image);
this->forwardXMLAttribute("focusUp", this->image);
this->forwardXMLAttribute("focusRight", this->image);
this->forwardXMLAttribute("focusDown", this->image);
this->forwardXMLAttribute("focusLeft", this->image);
this->forwardXMLAttribute("imageWidth", this->image, "width");
this->forwardXMLAttribute("imageHeight", this->image, "height");

// Register a click action for this button
BRLS_REGISTER_CLICK_BY_ID("image", this->onImageClicked);

// Generate a new image
this->generateRandomImage();
}

bool RandomRGBAImage::onImageClicked(brls::View* view)
{
// Generate a new image
this->generateRandomImage();

return true;
}

unsigned char interp(unsigned char src, unsigned char dst, float alpha)
{
return src * (1.0 - alpha) + dst * alpha;
}

void RandomRGBAImage::generateRandomImage()
{
// Size of the image to generate
int height = 256;
int width = 256;

// Allocate the RGBA image buffer
size_t bufferSize = height * width * 4; // 4 bytes per pixel (RGBA8888)
unsigned char* imageData = (unsigned char*)calloc(1, bufferSize);

// Randomly generate two colors for a gradient
unsigned char color1[3] = {
static_cast<unsigned char>(std::rand() % 255),
static_cast<unsigned char>(std::rand() % 255),
static_cast<unsigned char>(std::rand() % 255)
};
unsigned char color2[3] = {
static_cast<unsigned char>(std::rand() % 255),
static_cast<unsigned char>(std::rand() % 255),
static_cast<unsigned char>(std::rand() % 255)
};

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Set the pixel data
memset(&imageData[(y * width + x) * 4 + 3], (unsigned char)255, sizeof(unsigned char)); // Alpha
memset(&imageData[(y * width + x) * 4 + 2], (unsigned char)interp(color1[2], color2[2], (float)x/(float)width), sizeof(unsigned char)); // Blue
memset(&imageData[(y * width + x) * 4 + 1], (unsigned char)interp(color1[1], color2[1], (float)x/(float)width), sizeof(unsigned char)); // Green
memset(&imageData[(y * width + x) * 4 + 0], (unsigned char)interp(color1[0], color2[0], (float)x/(float)width), sizeof(unsigned char)); // Red
}
}

// Display the image
this->image->setImageFromRGBA(imageData, width, height);

// Free the image buffer
free(imageData);
}

brls::View* RandomRGBAImage::create()
{
// Called by the XML engine to create a new DynamicImage
return new RandomRGBAImage();
}
34 changes: 34 additions & 0 deletions demo/random_rgba_image.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2021 Jonathan Verbeek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <borealis.hpp>

class RandomRGBAImage : public brls::Box
{
public:
RandomRGBAImage();

static brls::View* create();

private:
BRLS_BIND(brls::Image, image, "image");

bool onImageClicked(brls::View* View);

void generateRandomImage();
};
17 changes: 17 additions & 0 deletions library/include/borealis/views/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ class Image : public View
*/
void setImageFromFile(std::string path);

/**
* Sets the image from a memory buffer. The data read has to be
* the same as if the image would've been read from a file.
*
* See NanoVG documentation and the Image class documentation for the
* list of supported image formats.
*/
void setImageFromMemory(const unsigned char* data, int numData);

/**
* Sets the image from a memory buffer. The data should be following the
* R8G8B8A8 pixel format.
*
* See the example implementation dynamic_image in the demo.
*/
void setImageFromRGBA(const unsigned char* data, int width, int height);

/**
* Sets the scaling type for this image.
*
Expand Down
47 changes: 47 additions & 0 deletions library/lib/views/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,53 @@ void Image::setImageFromFile(std::string path)
this->invalidate();
}

void Image::setImageFromMemory(const unsigned char* data, int numData)
{
NVGcontext* vg = Application::getNVGContext();

// Free the old texture if necessary
if (this->texture != 0)
nvgDeleteImage(vg, this->texture);

// Load the new texture
int flags = this->getImageFlags();
this->texture = nvgCreateImageMem(vg, flags, (unsigned char*)data, numData);

if (this->texture == 0)
fatal("Cannot load image from memory");

int width, height;
nvgImageSize(vg, this->texture, &width, &height);
this->originalImageWidth = (float)width;
this->originalImageHeight = (float)height;

this->invalidate();
}

void Image::setImageFromRGBA(const unsigned char* data, int width, int height)
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
{
NVGcontext* vg = Application::getNVGContext();

// Free the old texture if necessary
if (this->texture != 0)
nvgDeleteImage(vg, this->texture);

// Load the new texture
int flags = this->getImageFlags();
this->texture = nvgCreateImageRGBA(vg, width, height, flags, data);

if (this->texture == 0)
{
brls::Logger::error("Cannot create image from RGBA data (" + std::to_string(width) + "x" + std::to_string(height) + ")");
return;
}

this->originalImageWidth = (float)width;
this->originalImageHeight = (float)height;

this->invalidate();
}

void Image::setScalingType(ImageScalingType scalingType)
{
this->scalingType = scalingType;
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ demo_files = files(
'demo/main_activity.cpp',

'demo/captioned_image.cpp',
'demo/random_rgba_image.cpp',

'demo/recycling_list_tab.cpp',
'demo/components_tab.cpp',
Expand Down
4 changes: 3 additions & 1 deletion resources/i18n/en-US/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"images_original": "Original",
"images_upscaled": "Upscaled",
"images_stretched": "Stretched",
"images_cropped": "Cropped"
"images_cropped": "Cropped",

"random_rgba_image": "Randomly generated RGBA (click to refresh)"
},

"about": {
Expand Down
19 changes: 18 additions & 1 deletion resources/xml/tabs/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@
height="auto"
axis="row"
alignItems="center"
justifyContent="spaceEvenly">
justifyContent="spaceEvenly"
marginBottom="30px" >

<CaptionedImage
width="auto"
Expand All @@ -209,6 +210,22 @@

</brls:Box>

<brls:Box
width="auto"
height="auto"
axis="row"
alignItems="center"
justifyContent="spaceEvenly" >

<RandomRGBAImage
width="auto"
height="auto"
imageWidth="400px"
imageHeight="75px"
scalingType="stretch" />

</brls:Box>

</brls:Box>

</brls:ScrollingFrame>
Expand Down
20 changes: 20 additions & 0 deletions resources/xml/views/random_rgba_image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<brls:Box
width="auto"
height="auto"
axis="column"
alignItems="center">

<brls:Image
id="image"
width="auto"
height="auto"
focusable="true"
marginBottom="20px" />

<brls:Label
width="auto"
height="auto"
horizontalAlign="center"
text="@i18n/demo/components/random_rgba_image" />

</brls:Box>