#include "Pixels.h" 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; } }