Image.h 8.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef INCLUDE_IMAGE_H
#define INCLUDE_IMAGE_H



#include "util/log.h"
#include "util/images.h"

namespace glare::core
{

	template<typename From, typename To> constexpr float conversionFactor()
	{
		float to_float = 0;

		if constexpr(std::is_same_v<From, float>) 
			to_float = 1.f;
		else 
			to_float = 1 / float(std::numeric_limits<From>::max());

		if constexpr(std::is_same_v<To, float>) 
			return to_float;
		else 
			return to_float * float(std::numeric_limits<To>::max());
	}

27
	struct Extents
28
	{
29
30
31
32
33
34
35
36
37
38
39
40
41
		Extents() : Extents(-1, -1, -1) { dimensions = -1; }

		Extents(int width)
			: Extents(width, 1)
		{
			dimensions = 1;
		}

		Extents(int width, int height)
			: Extents(width, height, 1)
		{
			dimensions = 2;
		}
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
		Extents(int width, int height, int depth)
			: width(width), height(height), depth(depth)
		{
			dimensions = 3;
		}

		int linearize(Extents position)
		{
			return width*height*position.depth + height*position.height + position.width;
		}

		int size() const
		{
			return dimensions;
		}

		union
		{
			struct {
				int width;
				int height;
				int depth;
			};
			int data[3];
		};

	private:
		int dimensions;
	};

	template<typename TBase> struct Image
	{
75
		// You can either have a multisampled image without data.
76
		explicit Image(Extents extents, int comp, int samples);
77
78

		// Or a single-sample image with data.
79
		explicit Image(Extents extents, int comp, std::vector<TBase> data = std::vector<TBase>());
80
81
82
83

		// Or load an image from HDD as single-sample image
		explicit Image(const std::experimental::filesystem::path& path);

Johannes Braun's avatar
Johannes Braun committed
84
85
86
		// Or convert another image
		template<typename Src> Image(const Image<Src>& other);

87
88
89
90
		void save(const fs::path& path);
		void flip();

		// return r,rg,rgb or rgba
91
		std::tuple<TBase&, TBase&, TBase&, TBase&> operator[](Extents pixel);
92
93
94
95
96
97
98
99

		int width() const;
		int height() const;
		int depth() const;
		int components() const;
		int samples() const;
		const std::vector<TBase>& data() const;
		int dimensions() const;
Johannes Braun's avatar
Johannes Braun committed
100
		const Extents& extents() const;
101
102

	private:
103
		Extents m_extents;
104
105
106
107
108
109
110
		int m_components = 0;
		int m_samples = 0;
		TBase m_ignore = 0;
		std::vector<TBase> m_data;
	};

	template <typename TBase>
111
	Image<TBase>::Image(Extents extents, int comp, int samples): m_extents(extents), m_components(comp), m_samples(samples)
112
113
114
115
	{
	}

	template <typename TBase>
116
	Image<TBase>::Image(Extents extents, int comp, std::vector<TBase> data): m_extents(extents), m_components(comp), m_data(std::move(data))
117
118
119
120
121
122
	{
	}

	template <typename TBase>
	Image<TBase>::Image(const std::experimental::filesystem::path& path)
	{
123
		m_extents = Extents(1, 1);
124
125
126

		if constexpr (std::is_same_v<TBase, uint8_t>)
		{
127
			uint8_t* raw_data = stb::stbi_load(path.string().c_str(), &m_extents.width, &m_extents.height, &m_components, 0);
128
129
130
			if (!raw_data)
			{
				Log_Error << "Loading image failed! \"" << path << "\" not found.";
131
				m_extents = Extents(0, 0);
132
133
134
				m_components = 0;
				return;
			}
135
136
			m_data = std::vector<uint8_t>(raw_data, raw_data + m_extents.width * m_extents.height * m_components);
			stb::stbi_image_free(raw_data);
137
138
139
		}
		else
		{
140
			float* raw_data = stb::stbi_loadf(path.string().c_str(), &m_extents.width, &m_extents.height, &m_components, 0);
141
142
143
			if (!raw_data)
			{
				Log_Error << "Loading image failed! \"" << path << "\" not found.";
144
				m_extents = Extents(0, 0);
145
146
147
				m_components = 0;
				return;
			}
148
			m_data = std::vector<TBase>(m_extents.width * m_extents.height * m_components);
149

150
			for (int i = 0; i < m_extents.width * m_extents.height * m_components; ++i)
151
152
153
			{
				m_data[i] = static_cast<TBase>(conversionFactor<float, TBase>() * raw_data[i]);
			}
154
			stb::stbi_image_free(raw_data);
155
156
157
158
		}
		flip();
	}

Johannes Braun's avatar
Johannes Braun committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
	template <typename TBase>
	template<typename Src> Image<TBase>::Image(const Image<Src>& other)
		: m_extents(other.extents()), m_components(other.components()), m_samples(other.samples())
	{
		m_data.resize(other.data().size());

		for (int index = 0; index < other.data().size(); ++index)
		{
			if constexpr(std::is_same_v<TBase, Src>)
			{
				m_data[index] = other.data()[index];
			}
			else if constexpr(std::is_same_v<TBase, float>)
			{
				m_data[index] = static_cast<TBase>(glm::clamp(static_cast<float>(other.data()[index]), 0.f, 1.f) / static_cast<float>(std::numeric_limits<Src>::max()));
			}
			else if constexpr(std::is_same_v<Src, float>)
			{
				m_data[index] = static_cast<TBase>(glm::clamp(static_cast<float>(other.data()[index]), 0.f, 1.f) * static_cast<float>(std::numeric_limits<TBase>::max()));
			}
			else
			{
				m_data[index] = static_cast<TBase>(glm::clamp(conversionFactor<Src, TBase>() * other.data()[index], static_cast<float>(std::numeric_limits<Src>::lowest()), static_cast<float>(std::numeric_limits<Src>::max())));
			}
		}
	}

	template <typename TBase>
	const Extents& Image<TBase>::extents() const
	{
		return m_extents;
	}

192
	template <typename TBase>
193
	std::tuple<TBase&, TBase&, TBase&, TBase&> Image<TBase>::operator[](Extents pixel)
194
195
196
	{
		// Don't allow texel fetches on MS images.
		assert(m_samples == 0);
197
		assert(pixel.size() == m_extents.size());
198
199
		if (m_data.empty()) throw std::out_of_range("This texture has no data. Cannot fetch a pixel for an empty texture.");

200
		glm::ivec3 position(pixel.width, pixel.height, pixel.depth);
201
202

		// Debug bounds check.
203
204
205
		assert(position.x >= 0 && position.x < m_extents.width);
		assert(position.y >= 0 && position.y < m_extents.height);
		assert(position.z >= 0 && position.z < m_extents.depth);
206
207
208
209
210

		color<TBase> result(0);
		for (int i = 0; i < m_components; ++i)
		{
			// 4D access :P
211
			result[i] = m_data[m_extents.linearize(pixel)*m_components + i];
212
213
214
215
216
217
		}

		m_ignore = 0;
		auto get = [this](const glm::ivec3 position, int offset) -> TBase&
			
		{
218
			return offset >= m_components ? m_ignore : m_data[m_extents.linearize(pixel)*m_components + offset];
219
220
221
222
223
224
225
226
227
228
229
230
231
232
		};

		return std::tie(get(position, 0), get(position, 1), get(position, 2), get(position, 3));
	}

	template <typename TBase>
	void Image<TBase>::save(const fs::path& path)
	{
		flip();
		if constexpr(std::is_same_v<TBase, float>)
		{
			//if is float and extension is png, convert!
			if (path.extension() != ".hdr")
			{
233
				std::vector<uint8_t> converted(m_extents.width * m_extents.height * m_components);
234
235
236
237
238
				for (int i = 0; i < converted.size(); ++i)
				{
					converted[i] = static_cast<uint8_t>(conversionFactor<float, uint8_t>() * m_data[i]);
				}

239
				images::save(path, m_extents.width, m_extents.height, m_components, converted.data());
240
241
242
243
			}
			else
			{
				//Otherwise just save the floats.
244
				images::save(path, m_extents.width, m_extents.height, m_components, m_data.data());
245
246
247
248
			}
		}
		else if constexpr(std::is_same_v<TBase, uint8_t>)
		{
249
			images::save(path, m_extents.width, m_extents.height, m_components, m_data.data());
250
251
252
253
254
		}
		else
		{
			if (path.extension() == ".hdr")
			{
255
				std::vector<float> converted(m_extents.width * m_extents.height * m_components);
256
257
258
259
260
				for (int i = 0; i < converted.size(); ++i)
				{
					converted[i] = static_cast<float>(conversionFactor<TBase, float>() * m_data[i]);
				}

261
				images::save(path, m_extents.width, m_extents.height, m_components, converted.data());
262
263
264
			}
			else
			{
265
				std::vector<uint8_t> converted(m_extents.width * m_extents.height * m_components);
266
267
268
269
270
				for (int i = 0; i < converted.size(); ++i)
				{
					converted[i] = static_cast<uint8_t>(conversionFactor<TBase, uint8_t>() * m_data[i]);
				}

271
				images::save(path, m_extents.width, m_extents.height, m_components, converted.data());
272
273
274
275
276
277
278
279
			}
		}
		flip();
	}

	template <typename TBase>
	int Image<TBase>::width() const
	{
280
		return m_extents.width;
281
282
283
284
285
	}

	template <typename TBase>
	int Image<TBase>::height() const
	{
286
		return m_extents.height;
287
288
289
290
291
	}

	template <typename TBase>
	int Image<TBase>::depth() const
	{
292
		return m_extents.depth;
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	}

	template <typename TBase>
	int Image<TBase>::components() const
	{
		return m_components;
	}

	template <typename TBase>
	int Image<TBase>::samples() const
	{
		return m_samples;
	}

	template <typename TBase>
	const std::vector<TBase>& Image<TBase>::data() const
	{
		return m_data;
	}

	template <typename TBase>
	void Image<TBase>::flip()
	{
316
		images::flip(static_cast<unsigned>(m_extents.width), static_cast<unsigned>(m_extents.height), static_cast<int>(m_components), m_data.data());
317
318
319
320
321
	}

	template <typename TBase>
	int Image<TBase>::dimensions() const
	{
322
		return m_extents.size();
323
324
325
326
	}
}

#endif //!INCLUDE_IMAGE_H