#include "image.h"

Image::Image(bool colors, uint32_t width, uint32_t height,
             std::vector<uint8_t> d) {
  this->rgb = colors;
  this->width = width;
  this->height = height;
  this->data = d;
}
Image::Image(bool colors, uint32_t width, uint32_t height) {
  this->rgb = colors;
  this->width = width;
  this->height = height;
}

Image::Image() {
  this->width = 1;
  this->height = 1;
  this->rgb = false;
}

void Image::setWidth(uint32_t w) { this->width = w; }
void Image::setHeight(uint32_t h) { this->height = h; }
void Image::setColors(bool c) { this->rgb = c; }

void Image::setParams(uint32_t w, uint32_t h, bool c) {
  this->width = w;
  this->height = h;
  this->rgb = c;
}

void Image::appendData(uint8_t *d, uint32_t length) {
  for (uint32_t i = 0; i < length; i++) {
    this->data.push_back(d[i]);
  }
}

void Image::appendData(const QByteArray &data) {
  for (int i = 0; i < data.length(); i++) {
    this->data.push_back(static_cast<uint8_t>(data[i]));
  }
}

void Image::appendData(const QByteArray &data, uint32_t length) {
  for (uint32_t i = 0; i < length; i++) {
    this->data.push_back(static_cast<uint8_t>(data[i]));
  }
}

void Image::clearData() { this->data.clear(); }

void Image::image2rgb() {
  std::vector<uint8_t> tmp = this->data;
  this->clearData();
  for (uint32_t i = 0; i < tmp.size(); i++) {
    if ((i / this->width) % 2 == 0) {
      if ((i % 2) == 0) {
        this->data.push_back(0);
        this->data.push_back(tmp.at(i));
        this->data.push_back(0);
      } else {
        this->data.push_back(tmp.at(i));
        this->data.push_back(0);
        this->data.push_back(0);
      }
    } else {
      if ((i % 2) == 0) {
        this->data.push_back(0);
        this->data.push_back(0);
        this->data.push_back(tmp.at(i));
      } else {
        this->data.push_back(0);
        this->data.push_back(tmp.at(i));
        this->data.push_back(0);
      }
    }
  }
}
