aboutsummaryrefslogtreecommitdiff
path: root/src/image.h
blob: 52f42799455001455d71715857f82f7dd0c95ce9 (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
77
78
79
80
81
82
83
84
85
86
#pragma once

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

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

struct ImageData
{



	
	//unsigned long imageDataSize = 0;
};

class Image
{
protected:
	uint8_t* imageData;
	uint8_t colorValues;
	uint8_t bpp;
public:
	Image() = default;
	~Image();

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

	virtual int readFromFile(std::string filename) = 0;
	virtual int writeToFile(std::string filename) = 0;
	
	template <typename 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<std::derived_from<Image> T> Image::Image(const T& other)
{
	this->colorValues = other.colorValues;
	this->bpp = other.bpp;
	
	this->width = other.width;
	this->height = other.height;
	this->bitDepth = other.bitDepth;
	this->colorType = other.colorType;
	this->compressionMethod = other.compressionMethod;
	this->filterMethod = other.filterMethod;
	this->interlaceMethod = other.interlaceMethod;
	
	unsigned long imageDataSize = width * height * bpp;
	imageData = new uint8_t[imageDataSize];
	mempcpy(imageData, other.imageData, imageDataSize);
}


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

	pixel.r = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8];
	pixel.g = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 1];
	pixel.b = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 2];

	if(colorValues == 4)
		pixel.a = imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 3];
	else
		pixel.a = 0;

	return pixel;
}