Skip to content

Commit fa329e0

Browse files
committed
Added proper Image class to load images from disk and encoded memory
1 parent 09109f6 commit fa329e0

3 files changed

Lines changed: 108 additions & 68 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @project: Overload
3+
* @author: Overload Tech.
4+
* @licence: MIT
5+
*/
6+
7+
#include <filesystem>
8+
9+
namespace OvRendering::Data
10+
{
11+
/**
12+
* Simple wrapper for stb_image. Handles SDR and HDR image loading,
13+
* and enforces RAII for the loaded data.
14+
*/
15+
struct Image
16+
{
17+
int width = 0;
18+
int height = 0;
19+
int bpp = 0;
20+
bool isHDR = false;
21+
void* data = nullptr;
22+
23+
/**
24+
* Creates an image from a file on disk
25+
* @param p_filepath
26+
*/
27+
Image(const std::filesystem::path& p_filepath);
28+
29+
/**
30+
* Creates an image from encoded image data in memory
31+
*/
32+
Image(const uint8_t* p_data, const size_t p_size);
33+
34+
/**
35+
* Destructor of the image
36+
*/
37+
virtual ~Image();
38+
39+
/**
40+
* Returns true if the image is valid
41+
*/
42+
bool IsValid() const;
43+
44+
/**
45+
* Alias for IsValid()
46+
*/
47+
operator bool() const;
48+
};
49+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @project: Overload
3+
* @author: Overload Tech.
4+
* @licence: MIT
5+
*/
6+
7+
#include <OvRendering/Data/Image.h>
8+
9+
#define STB_IMAGE_IMPLEMENTATION
10+
11+
#include <stb_image/stb_image.h>
12+
13+
OvRendering::Data::Image::Image(const std::filesystem::path& p_filepath)
14+
{
15+
stbi_set_flip_vertically_on_load(true);
16+
17+
isHDR = stbi_is_hdr(p_filepath.c_str());
18+
19+
data = isHDR ?
20+
static_cast<void*>(stbi_loadf(p_filepath.c_str(), &width, &height, &bpp, 4)) :
21+
static_cast<void*>(stbi_load(p_filepath.c_str(), &width, &height, &bpp, 4));
22+
}
23+
24+
OvRendering::Data::Image::Image(const uint8_t* p_data, const size_t p_size)
25+
{
26+
if (!p_data || p_size == 0 || p_size > static_cast<size_t>(std::numeric_limits<int>::max()))
27+
{
28+
return;
29+
}
30+
31+
const int encodedSize = static_cast<int>(p_size);
32+
stbi_set_flip_vertically_on_load(true);
33+
isHDR = stbi_is_hdr_from_memory(p_data, encodedSize) != 0;
34+
35+
data = isHDR ?
36+
static_cast<void*>(stbi_loadf_from_memory(p_data, encodedSize, &width, &height, &bpp, 4)) :
37+
static_cast<void*>(stbi_load_from_memory(p_data, encodedSize, &width, &height, &bpp, 4));
38+
}
39+
40+
OvRendering::Data::Image::~Image()
41+
{
42+
stbi_image_free(data);
43+
}
44+
45+
bool OvRendering::Data::Image::IsValid() const
46+
{
47+
return data;
48+
}
49+
50+
OvRendering::Data::Image::operator bool() const
51+
{
52+
return IsValid();
53+
}
54+

Sources/OvRendering/src/OvRendering/Resources/Loaders/TextureLoader.cpp

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,16 @@
44
* @licence: MIT
55
*/
66

7-
#define STB_IMAGE_IMPLEMENTATION
8-
97
#include <array>
10-
#include <limits>
118
#include <memory>
12-
#include <stb_image/stb_image.h>
139

1410
#include <OvDebug/Logger.h>
11+
#include <OvRendering/Data/Image.h>
1512
#include <OvRendering/Resources/Loaders/TextureLoader.h>
1613
#include <OvTools/Utils/PathParser.h>
1714

1815
namespace
1916
{
20-
/**
21-
* Simple wrapper for stb_image. Handles SDR and HDR image loading,
22-
* and enforces RAII for the loaded data.
23-
*/
24-
struct Image
25-
{
26-
int width = 0;
27-
int height = 0;
28-
int bpp = 0;
29-
bool isHDR = false;
30-
void* data = nullptr;
31-
32-
Image(const std::string& p_filepath)
33-
{
34-
stbi_set_flip_vertically_on_load(true);
35-
36-
isHDR = stbi_is_hdr(p_filepath.c_str());
37-
38-
data = isHDR ?
39-
static_cast<void*>(stbi_loadf(p_filepath.c_str(), &width, &height, &bpp, 4)) :
40-
static_cast<void*>(stbi_load(p_filepath.c_str(), &width, &height, &bpp, 4));
41-
}
42-
43-
virtual ~Image() { stbi_image_free(data); }
44-
bool IsValid() const { return data; }
45-
operator bool() const { return IsValid(); }
46-
};
47-
48-
/**
49-
* Wrapper for stb_image when loading encoded image bytes from memory.
50-
*/
51-
struct EncodedImage
52-
{
53-
int width = 0;
54-
int height = 0;
55-
int bpp = 0;
56-
bool isHDR = false;
57-
void* data = nullptr;
58-
59-
EncodedImage(const uint8_t* p_data, const size_t p_size)
60-
{
61-
if (!p_data || p_size == 0 || p_size > static_cast<size_t>(std::numeric_limits<int>::max()))
62-
{
63-
return;
64-
}
65-
66-
const int encodedSize = static_cast<int>(p_size);
67-
stbi_set_flip_vertically_on_load(true);
68-
isHDR = stbi_is_hdr_from_memory(p_data, encodedSize) != 0;
69-
70-
data = isHDR ?
71-
static_cast<void*>(stbi_loadf_from_memory(p_data, encodedSize, &width, &height, &bpp, 4)) :
72-
static_cast<void*>(stbi_load_from_memory(p_data, encodedSize, &width, &height, &bpp, 4));
73-
}
74-
75-
virtual ~EncodedImage() { stbi_image_free(data); }
76-
bool IsValid() const { return data; }
77-
operator bool() const { return IsValid(); }
78-
};
79-
8017
void PrepareTexture(
8118
OvRendering::HAL::Texture& p_texture,
8219
const void* p_data,
@@ -121,7 +58,7 @@ OvRendering::Resources::Texture* OvRendering::Resources::Loaders::TextureLoader:
12158
bool p_generateMipmap
12259
)
12360
{
124-
if (Image image{ p_filepath })
61+
if (Data::Image image{ p_filepath })
12562
{
12663
auto texture = std::make_unique<HAL::Texture>(
12764
Settings::ETextureType::TEXTURE_2D,
@@ -205,7 +142,7 @@ OvRendering::Resources::Texture* OvRendering::Resources::Loaders::TextureLoader:
205142
bool p_generateMipmap
206143
)
207144
{
208-
if (EncodedImage image{ p_data, p_size })
145+
if (Data::Image image{ p_data, p_size })
209146
{
210147
auto texture = std::make_unique<HAL::Texture>(Settings::ETextureType::TEXTURE_2D, "FromEncodedMemory");
211148

@@ -238,7 +175,7 @@ void OvRendering::Resources::Loaders::TextureLoader::Reload(
238175
bool p_generateMipmap
239176
)
240177
{
241-
if (Image image{ p_filePath })
178+
if (Data::Image image{ p_filePath })
242179
{
243180
auto texture = std::make_unique<HAL::Texture>(
244181
Settings::ETextureType::TEXTURE_2D,
@@ -303,7 +240,7 @@ void OvRendering::Resources::Loaders::TextureLoader::ReloadFromEncodedMemory(
303240
bool p_generateMipmap
304241
)
305242
{
306-
if (EncodedImage image{ p_data, p_size })
243+
if (Data::Image image{ p_data, p_size })
307244
{
308245
auto texture = std::make_unique<HAL::Texture>(Settings::ETextureType::TEXTURE_2D, "FromEncodedMemory");
309246

0 commit comments

Comments
 (0)