#include "pixelarray.h"

Pixel operator+(const Pixel &p1, const Pixel &p2) {
    const uint8_t r = ui8_clamp((int) p1.r + p2.r);
    const uint8_t g = ui8_clamp((int) p1.g + p2.g);
    const uint8_t b = ui8_clamp((int) p1.b + p2.b);
    return {r, g, b};
}

Pixel operator/(const Pixel &p1, const uint8_t &n) {
    const uint8_t r = p1.r / n;
    const uint8_t g = p1.g / n;
    const uint8_t b = p1.b / n;
    return {r, g, b};
}

Pixel operator*(const Pixel &p, const int &n) {
    uint8_t r = ui8_clamp(n * p.r);
    uint8_t g = ui8_clamp(n * p.g);
    uint8_t b = ui8_clamp(n * p.b);
    return {r, g, b};
}

Pixel operator-(const uint8_t &n, const Pixel &p) {
    uint8_t r = ui8_clamp(n - p.r);
    uint8_t g = ui8_clamp(n - p.g);
    uint8_t b = ui8_clamp(n - p.b);
    return {r, g, b};
}

Pixel operator-(const Pixel &p, const uint8_t &n) {
    uint8_t r = ui8_clamp(p.r - n);
    uint8_t g = ui8_clamp(p.g - n);
    uint8_t b = ui8_clamp(p.b - n);
    return {r, g, b};
}

Pixel operator-(const Pixel &p1, const Pixel &p2) {
    auto r = ui8_clamp((int) p1.r - p2.r);
    auto g = ui8_clamp((int) p1.g - p2.g);
    auto b = ui8_clamp((int) p1.b - p2.b);
    return {r, g, b};
}

PixelArray::PixelArray(uint32_t width, uint32_t height) {
    this->_width = width;
    this->_height = height;
    this->array = new Pixel *[height];
    for (int i = 0; i < height; ++i) {
        this->array[i] = new Pixel[width];
    }
}

Pixel &PixelArray::operator()(const uint32_t &i, const uint32_t &j) {
    return this->array[i][j];
}

PixelArray::~PixelArray() {
    for (int i = 0; i < this->_height; ++i) {
        delete[] this->array[i];
    }
    delete[] this->array;
}

PixelArray::PixelArray(const PixelArray &pA) {
    this->_width = pA.width();
    this->_height = pA.height();
    this->array = pA.array;
}

Pixel *&PixelArray::operator()(const uint32_t &i) {
    return this->array[i];
}

const uint32_t &PixelArray::width() const { return this->_width; }

const uint32_t &PixelArray::height() const { return this->_height; }