|
4 | 4 | * @licence: MIT |
5 | 5 | */ |
6 | 6 |
|
7 | | -#define STB_IMAGE_IMPLEMENTATION |
8 | | - |
9 | 7 | #include <array> |
10 | | -#include <limits> |
11 | 8 | #include <memory> |
12 | | -#include <stb_image/stb_image.h> |
13 | 9 |
|
14 | 10 | #include <OvDebug/Logger.h> |
| 11 | +#include <OvRendering/Data/Image.h> |
15 | 12 | #include <OvRendering/Resources/Loaders/TextureLoader.h> |
16 | 13 | #include <OvTools/Utils/PathParser.h> |
17 | 14 |
|
18 | 15 | namespace |
19 | 16 | { |
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 | | - |
80 | 17 | void PrepareTexture( |
81 | 18 | OvRendering::HAL::Texture& p_texture, |
82 | 19 | const void* p_data, |
@@ -121,7 +58,7 @@ OvRendering::Resources::Texture* OvRendering::Resources::Loaders::TextureLoader: |
121 | 58 | bool p_generateMipmap |
122 | 59 | ) |
123 | 60 | { |
124 | | - if (Image image{ p_filepath }) |
| 61 | + if (Data::Image image{ p_filepath }) |
125 | 62 | { |
126 | 63 | auto texture = std::make_unique<HAL::Texture>( |
127 | 64 | Settings::ETextureType::TEXTURE_2D, |
@@ -205,7 +142,7 @@ OvRendering::Resources::Texture* OvRendering::Resources::Loaders::TextureLoader: |
205 | 142 | bool p_generateMipmap |
206 | 143 | ) |
207 | 144 | { |
208 | | - if (EncodedImage image{ p_data, p_size }) |
| 145 | + if (Data::Image image{ p_data, p_size }) |
209 | 146 | { |
210 | 147 | auto texture = std::make_unique<HAL::Texture>(Settings::ETextureType::TEXTURE_2D, "FromEncodedMemory"); |
211 | 148 |
|
@@ -238,7 +175,7 @@ void OvRendering::Resources::Loaders::TextureLoader::Reload( |
238 | 175 | bool p_generateMipmap |
239 | 176 | ) |
240 | 177 | { |
241 | | - if (Image image{ p_filePath }) |
| 178 | + if (Data::Image image{ p_filePath }) |
242 | 179 | { |
243 | 180 | auto texture = std::make_unique<HAL::Texture>( |
244 | 181 | Settings::ETextureType::TEXTURE_2D, |
@@ -303,7 +240,7 @@ void OvRendering::Resources::Loaders::TextureLoader::ReloadFromEncodedMemory( |
303 | 240 | bool p_generateMipmap |
304 | 241 | ) |
305 | 242 | { |
306 | | - if (EncodedImage image{ p_data, p_size }) |
| 243 | + if (Data::Image image{ p_data, p_size }) |
307 | 244 | { |
308 | 245 | auto texture = std::make_unique<HAL::Texture>(Settings::ETextureType::TEXTURE_2D, "FromEncodedMemory"); |
309 | 246 |
|
|
0 commit comments