-
Notifications
You must be signed in to change notification settings - Fork 130
Added Image::setImageFromMemory and Image::setImageFromRawData #162
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
Open
iUltimateLP
wants to merge
10
commits into
natinusala:main
Choose a base branch
from
iUltimateLP:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d62606
Added Image::setImageFromMemory
iUltimateLP 04fe37b
Don't need address in error message
iUltimateLP 9f46f99
Changed nvgCreateImageMem to nvgCreateImageRGBA and implemented a sma…
iUltimateLP c0d748b
Differentiated between setImageFromMemory and setImageFromRawData, as…
iUltimateLP 38fcb80
Renamed DynamicImage to RandomRGBAImage, added random image generatio…
iUltimateLP dd20119
Hardcode the size of the random rgba_image to 256x256
iUltimateLP 5698d47
Fixed "narrow conversion" warning
iUltimateLP f7effeb
Added padding and changed size of random rgba image
iUltimateLP 6740d25
Made Image::setImageFromMemory's data buffer const too
iUltimateLP 52c341f
nanovg expects a non-const data buffer in Image::setImageFromMemory b…
iUltimateLP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.