aboutsummaryrefslogtreecommitdiff
path: root/src/image.h
blob: be26437e0a2a54f8ad2f37583a68a8a7fd3e2123 (plain) (blame)
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
39
40
41
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
75
76
#pragma once

#include <cstdint>
#include <cstring>
#include <memory>
#include <string>

template <typename T>
struct Pixel
{
	T r, g, b, a;
};


class Image
{
protected:
	std::unique_ptr<uint8_t[]> imageData;
	// uint8_t* imageData;
	uint8_t colorValues;
	uint8_t bpp;
public:
	Image() = default;

	// template<std::derived_from<Image> T>
	Image(const Image& other);

	virtual int readFromFile(std::string filename) = 0;
	virtual int writeToFile(std::string filename) = 0;
	
	template <typename T = uint8_t>
	Pixel<T> getPixel(unsigned int x, unsigned int y);

	uint32_t width = 0;
	uint32_t height = 0;
	uint8_t bitDepth;
	uint8_t colorType;
	uint8_t compressionMethod;
	uint8_t filterMethod;
	uint8_t interlaceMethod;
};


template <typename T>
Pixel<T> Image::getPixel(unsigned int x, unsigned int y)
{
	Pixel<T> pixel;

	if(sizeof(T)*8 == bitDepth)
	{
		unsigned long rIndex = y * width * colorValues + x * colorValues;
		pixel.r = ((T*)imageData.get())[rIndex];
		pixel.g = ((T*)imageData.get())[rIndex + 1];
		pixel.b = ((T*)imageData.get())[rIndex + 2];

		if(colorValues == 4)
			pixel.a = ((T*)imageData.get())[rIndex + 3];
		else
			pixel.a = 0;
	}
	else
	{
		unsigned long startIndex = y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8;
		
		uint32_t pixelData[4];
		int tsize = sizeof(T);
		int bytesToTake = std::min(tsize, bitDepth/8);
		for(int i = 0; i < colorValues; i++)
		{
			for(int j = 0; j < bytesToTake; j++)
				pixelData[i] += imageData[startIndex++] << (bitDepth - j*8 - 8);
			startIndex += bitDepth/8 - bytesToTake;
		}
	}
	return pixel;
}