From 9c1b9ba442633ed0b362c18d9a8f69185ed2f00b Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 5 Feb 2022 14:47:17 +0100 Subject: [PATCH] GuiImageData: Add option to load directly from a file (#19) --- include/gui/GuiImageData.h | 7 +++++++ source/gui/GuiImageData.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/gui/GuiImageData.h b/include/gui/GuiImageData.h index 0bc1624..416a26f 100644 --- a/include/gui/GuiImageData.h +++ b/include/gui/GuiImageData.h @@ -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; diff --git a/source/gui/GuiImageData.cpp b/source/gui/GuiImageData.cpp index 08d0756..4d149a9 100644 --- a/source/gui/GuiImageData.cpp +++ b/source/gui/GuiImageData.cpp @@ -14,10 +14,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . ****************************************************************************/ + #include #include #include #include +#include #include /** @@ -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;