#include "Image.h"

Image::Image(unsigned width, unsigned height)
    : mWidth(width),
      mHeight(height)
{

}

Image::~Image()
{

}

void Image::Initialize()
{
    mData = std::vector<unsigned char>(GetBytesPerRow()*mHeight, 0);
}

void Image::SetPixelValue(unsigned idx, unsigned jdx, const Color& color)
{
    if (mData.empty())
    {
        Initialize();
    }

    mData[jdx*GetBytesPerRow() + idx*3] = static_cast<unsigned char>(color.GetR());
    mData[jdx*GetBytesPerRow() + idx*3 + 1] = static_cast<unsigned char>(color.GetG());
    mData[jdx*GetBytesPerRow() + idx*3 + 2] = static_cast<unsigned char>(color.GetB());
}

std::unique_ptr<Image> Image::Create(unsigned width, unsigned height)
{
    return std::make_unique<Image>(width, height);
}

unsigned Image::GetBytesPerRow() const
{
    const auto bitsPerEntry = mBitDepth <= 8 ? 1 : 2;
    return mWidth * mNumChannels *bitsPerEntry;
}

unsigned Image::GetWidth() const
{
    return mWidth;
}

unsigned Image::GetHeight() const
{
    return mHeight;
}

unsigned Image::GetBitDepth() const
{
    return mBitDepth;
}

unsigned char Image::GetByte(unsigned idx, unsigned jdx) const
{
    return mData[jdx*GetBytesPerRow() + idx];
}

unsigned Image::GetNumChannels() const
{
    return mNumChannels;
}

void Image::SetData(const std::vector<unsigned char>& data)
{
    mData = data;
}

void Image::SetWidth(unsigned width)
{
    mWidth = width;
}

void Image::SetHeight(unsigned height)
{
    mHeight = height;
}

void Image::SetBitDepth(unsigned bitDepth)
{
    mBitDepth = bitDepth;
}

void Image::SetNumChannels(unsigned numChannels)
{
    mNumChannels = numChannels;
}
