summaryrefslogtreecommitdiff
path: root/rotord/src/Pixels.cpp
blob: 78f4bbb56cdb4ff6ca09bf00b634097cfb28d634 (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
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
87
88
89
90
#include "Pixels.h"
Pixels::Pixels(){
	pixels=nullptr;
	pixelsOwner=false;
}
Pixels::~Pixels(){
	clear();
}
void Pixels::allocate(int w, int h, int _channels){
	if (w < 0 || h < 0) {
		return;
	}

	//we check if we are already allocated at the right size
	if(bAllocated && w == width && h == height && channels ==_channels){
		return; //we don't need to allocate
	}

	//we do need to allocate, clear the data
	clear();

	channels = _channels;
	width= w;
	height = h;

	pixels = new uint8_t[w * h * channels];
	bAllocated = true;
	pixelsOwner = true;
}
void Pixels::clear(){
	if(pixels){
		if(pixelsOwner) delete[] pixels;
		pixels = nullptr;
	}

	width			= 0;
	height			= 0;
	channels		= 0;
	bAllocated		= false;
}
bool Pixels::isAllocated() const{
	return bAllocated;
}

void Pixels::setFromExternalPixels(uint8_t * newPixels,int w, int h, int _channels){
	clear();
	channels = _channels;
	width= w;
	height = h;

	pixels = newPixels;
	pixelsOwner = false;
	bAllocated = true;
}

uint8_t * Pixels::getPixels(){
	return &pixels[0];
}

int Pixels::getWidth() const{
	return width;
}

int Pixels::getHeight() const{
	return height;
}

int Pixels::getBytesPerPixel() const{
	return channels;
}

int Pixels::getNumChannels() const{
	return channels;
}

void Pixels::swap(Pixels & pix){
	std::swap(pixels,pix.pixels);
	std::swap(width, pix.width);
	std::swap(height,pix.height);
	std::swap(channels,pix.channels);
	std::swap(pixelsOwner, pix.pixelsOwner);
	std::swap(bAllocated, pix.bAllocated);
}

void Pixels::set(uint8_t val){
	int size = width * height * channels;
	for(int i = 0; i < size; i++){
		pixels[i] = val;
	}
}