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
74 changes: 74 additions & 0 deletions demo/dynamic_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2019 WerWolv
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
Copyright 2019 p-sam
Copyright 2020-2021 natinusala

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 "dynamic_image.hpp"

DynamicImage::DynamicImage()
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
{
// Load the XML file and inflate ourself with its content
this->inflateFromXMLRes("xml/views/dynamic_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);
}

bool DynamicImage::onImageClicked(brls::View* view)
{
// Retrieve the image size
int height = this->image->getHeight();
int width = this->image->getWidth();

// Generate the pixel data for a new image
size_t bufferSize = height * width * 4; // 4 bytes per pixel (RGBA)
unsigned char* imageData = (unsigned char*)calloc(1, bufferSize);

for (int y = 0; y < height; y++)
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
{
for (int x = 0; x < width; x++)
{
memset(&imageData[(y * width + x) * 4 + 3], (unsigned char)255, sizeof(unsigned char)); // Alpha
memset(&imageData[(y * width + x) * 4 + 2], (unsigned char)((y+x)*255/(height+width)), sizeof(unsigned char)); // Blue
memset(&imageData[(y * width + x) * 4 + 1], (unsigned char)(x*255/width), sizeof(unsigned char)); // Green
memset(&imageData[(y * width + x) * 4 + 0], (unsigned char)(y*255/height), sizeof(unsigned char)); // Red
}
}

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

// Free the image buffer
free(imageData);

return true;
}

brls::View* DynamicImage::create()
{
// Called by the XML engine to create a new DynamicImage
return new DynamicImage();
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
}
34 changes: 34 additions & 0 deletions demo/dynamic_image.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2019 WerWolv
Copyright 2019 p-sam
Copyright 2020-2021 natinusala

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 DynamicImage : public brls::Box
{
public:
DynamicImage();

static brls::View* create();

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

bool onImageClicked(brls::View* View);
};
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 "dynamic_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("DynamicImage", DynamicImage::create);

// Add custom values to the theme
brls::getLightTheme().addColor("captioned_image/caption", nvgRGB(2, 176, 183));
Expand Down
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(unsigned char* data, int numData);

/**
* Sets the image from a memory buffer. The data should be a
* raw four-channel (RGBA) pixel array of the size width*height*4.
*
* See the example implementation dynamic_image in the demo.
*/
void setImageFromRawData(const unsigned char* data, int width, int height);
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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(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, 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::setImageFromRawData(const unsigned char* data, int width, int height)
{
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 load image from memory (" + std::to_string(width) + "x" + std::to_string(height) + ")");
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved
return;
}

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

this->invalidate();
}

void Image::setScalingType(ImageScalingType scalingType)
{
this->scalingType = scalingType;
Expand Down
15 changes: 15 additions & 0 deletions resources/xml/tabs/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@

</brls:Box>

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

<DynamicImage
width="auto"
height="auto"
imageWidth="256px"
imageHeight="256px" />

</brls:Box>

</brls:Box>

</brls:ScrollingFrame>
Expand Down
20 changes: 20 additions & 0 deletions resources/xml/views/dynamic_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="Click the image!" />
iUltimateLP marked this conversation as resolved.
Show resolved Hide resolved

</brls:Box>