aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Image.hpp
blob: c577c243f689fe30ee6bf899b866b4f12c6fe45a (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once

#include "Color.hpp"
#include "RcPtr.hpp"

#include <cstdint>
#include <glm/glm.hpp>
#include <memory>
#include <span>

/// Image is a 2d array of pixels, stored as a continuous array in memory, with the first pixel
/// being the top-left pixel. If a vertically flipped image data is needed, load using stb_image
/// yourself, or flip the data here.
class Image : public RefCounted {
private:
	std::unique_ptr<uint8_t[]> mData;
	glm::ivec2 mSize;
	int mChannels;

public:
	Image();

	bool InitFromImageFile(const char* filePath, int desiredChannels = 0);
	bool InitFromImageData(std::span<uint8_t> data, int desiredChannels = 0);
	bool InitFromPixels(std::span<uint8_t> pixels, glm::ivec2 dimensions, int channels);
	bool InitFromPixels(std::unique_ptr<uint8_t[]> pixels, glm::ivec2 dimensions, int channels);

	/// Get the pixel at the given location.
	RgbaColor GetPixel(int x, int y) const;
	void SetPixel(int x, int y, RgbaColor color);

	uint8_t* GetDataPtr() const;
	size_t GetDataLength() const;
	std::span<uint8_t> GetData() const;
	glm::ivec2 GetSize() const;
	int GetChannels() const;
	bool IsEmpty() const;
};