GuiImageData: Add option to load directly from a file (#19)

This commit is contained in:
Maschell 2022-02-05 14:47:17 +01:00 committed by GitHub
parent 4a0e27624b
commit 9c1b9ba442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -31,6 +31,9 @@ public:
//!\param imgSize The image size
GuiImageData(const uint8_t *img, int32_t imgSize, GX2TexClampMode textureClamp = GX2_TEX_CLAMP_MODE_CLAMP, GX2SurfaceFormat textureFormat = GX2_SURFACE_FORMAT_UNORM_R8_G8_B8_A8);
//!\param path Image path
GuiImageData(const char *path, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat);
//!Destructor
virtual ~GuiImageData();
@ -39,6 +42,10 @@ public:
//!\param imgSize The image size
void loadImage(const uint8_t *img, int32_t imgSize, GX2TexClampMode textureClamp = GX2_TEX_CLAMP_MODE_CLAMP, GX2SurfaceFormat textureFormat = GX2_SURFACE_FORMAT_UNORM_R8_G8_B8_A8);
//!Load image from file
//!\param path Image path
void loadImageFromFile(const char *path, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat);
//! getter functions
virtual const GX2Texture *getTexture() const {
return texture;

View File

@ -14,10 +14,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <gui/GuiImageData.h>
#include <gui/memory.h>
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/**
@ -38,6 +40,16 @@ GuiImageData::GuiImageData(const uint8_t *img, int32_t imgSize, GX2TexClampMode
loadImage(img, imgSize, textureClamp, textureFormat);
}
/**
* Constructor for the GuiImageData class.
*/
GuiImageData::GuiImageData(const char *path, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat) {
texture = NULL;
sampler = NULL;
loadImageFromFile(path, textureClamp, textureFormat);
}
/**
* Destructor for the GuiImageData class.
*/
@ -70,6 +82,24 @@ void GuiImageData::releaseData(void) {
}
}
void GuiImageData::loadImageFromFile(const char *path, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat) {
FILE *file = fopen(path, "rb");
if (file) {
off_t i = ftello(file);
if (fseek(file, 0, SEEK_END) == 0) {
off_t fileSize = ftello(file);
if (fileSize > 8) {
fseeko(file, i, SEEK_SET);
uint8_t buffer[fileSize];
if (fread(buffer, 1, fileSize, file) == fileSize) {
loadImage(buffer, fileSize, textureClamp, textureFormat);
}
}
}
fclose(file);
}
}
void GuiImageData::loadImage(const uint8_t *img, int32_t imgSize, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat) {
if (!img || (imgSize < 8)) {
return;