Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_imageloader_stb.inl
Go to the documentation of this file.
1#pragma once
2#include "../foray_logger.hpp"
3#include "../osi/foray_env.hpp"
5#include <tinygltf/stb_image.h>
6
7namespace foray::util {
8
9 namespace impl {
11 {
12 bool Is16bit = false;
13 bool IsHdr = false;
14 };
15
16 } // namespace impl
17
18 template <VkFormat FORMAT>
20 {
21 using namespace impl;
22
23 int width = 0;
24 int height = 0;
25 int component_count = 0;
26 bool gotInfo = !!stbi_info(mInfo.Utf8Path.GetPath().c_str(), &width, &height, &component_count);
27
28 if(!gotInfo)
29 {
30 return false;
31 }
32
33 // Genuinely illegal: reinterprete customloaderinfo pointer (guaranteed at minimum 32bit) to store the loader cache struct (16 bit).
34 // The deleter is never set, so all other code will ignore what is stored here
35 StbLoaderCache& cache = *reinterpret_cast<StbLoaderCache*>(&mCustomLoaderInfo);
36 new(&cache) StbLoaderCache();
37
38 cache.Is16bit = !!stbi_is_16_bit(mInfo.Utf8Path.GetPath().c_str());
39 cache.IsHdr = !!stbi_is_hdr(mInfo.Utf8Path.GetPath().c_str());
40
41 switch(component_count)
42 {
43 case 1:
44 mInfo.Channels = {EImageChannel::Y};
45 break;
46 case 2:
47 mInfo.Channels = {EImageChannel::Y, EImageChannel::A};
48 break;
49 case 3:
51 break;
52 case 4:
54 break;
55 }
56 mInfo.Extent.width = static_cast<uint32_t>(width);
57 mInfo.Extent.height = static_cast<uint32_t>(height);
58
59 if constexpr(FORMAT_TRAITS::COMPONENT_TRAITS::IS_FLOAT && FORMAT_TRAITS::COMPONENT_TRAITS::SIZE != 4)
60 {
61 logger()->warn("ImageLoad: Stb image loader does not support half or double precision floating point values!");
62 return false;
63 }
64
65
66 mInfo.Valid = width > 0 && height > 0 && !!mInfo.Channels.size();
67 return mInfo.Valid;
68 }
69
70 namespace impl {
71 template <typename FORMAT_TRAITS>
72 uint8_t* lReadStbUint8(const char* name, int desired_channels)
73 {
74 int width = 0;
75 int height = 0;
76 int channels_in_file = 0;
77 return reinterpret_cast<uint8_t*>(stbi_load(name, &width, &height, &channels_in_file, desired_channels));
78 }
79 template <typename FORMAT_TRAITS>
80 uint8_t* lReadStbUint16(const char* name, int desired_channels)
81 {
82 int width = 0;
83 int height = 0;
84 int channels_in_file = 0;
85 return reinterpret_cast<uint8_t*>(stbi_load_16(name, &width, &height, &channels_in_file, desired_channels));
86 }
87 template <typename FORMAT_TRAITS>
88 uint8_t* lReadStbFp32(const char* name, int desired_channels)
89 {
90 int width = 0;
91 int height = 0;
92 int channels_in_file = 0;
93 return reinterpret_cast<uint8_t*>(stbi_loadf(name, &width, &height, &channels_in_file, desired_channels));
94 }
95 } // namespace impl
96
97 template <VkFormat FORMAT>
99 {
100 using namespace impl;
101
102 uint8_t* stbdata = nullptr;
103 std::string namestr(mInfo.Utf8Path.GetPath());
104 const char* name = namestr.c_str();
105
106 int desired_channels = static_cast<int>(FORMAT_TRAITS::COMPONENT_COUNT);
107
108 if constexpr(FORMAT_TRAITS::COMPONENT_TRAITS::IS_FLOAT)
109 {
110 stbdata = lReadStbFp32<FORMAT_TRAITS>(name, desired_channels);
111 }
112 if constexpr(FORMAT_TRAITS::COMPONENT_TRAITS::SIZE == 1)
113 {
114 stbdata = lReadStbUint8<FORMAT_TRAITS>(name, desired_channels);
115 }
116 if constexpr(FORMAT_TRAITS::COMPONENT_TRAITS::SIZE == 2)
117 {
118 stbdata = lReadStbUint16<FORMAT_TRAITS>(name, desired_channels);
119 }
120
121 if(!stbdata)
122 {
123 return false;
124 }
125
126 mRawData.resize(FORMAT_TRAITS::BYTESTRIDE * mInfo.Extent.width * mInfo.Extent.height);
127
128 memcpy(mRawData.data(), stbdata, mRawData.size());
129 stbi_image_free(stbdata);
130 return true;
131 }
132
133} // namespace foray::util
bool PopulateImageInfo_Stb()
Definition foray_imageloader_stb.inl:19
bool Load_Stb()
Definition foray_imageloader_stb.inl:98
uint8_t * lReadStbFp32(const char *name, int desired_channels)
Definition foray_imageloader_stb.inl:88
uint8_t * lReadStbUint8(const char *name, int desired_channels)
Definition foray_imageloader_stb.inl:72
uint8_t * lReadStbUint16(const char *name, int desired_channels)
Definition foray_imageloader_stb.inl:80
Definition foray_dualbuffer.hpp:5
spdlog::logger * logger()
Gives access to a global available logger object. The logger writes to console & to a file next to th...
Definition foray_imageloader_stb.inl:11
bool IsHdr
Definition foray_imageloader_stb.inl:13
bool Is16bit
Definition foray_imageloader_stb.inl:12