/*
	GameEngine and Editor
	by John Ryland
	Copyright (c) 2023
*/

////////////////////////////////////////////////////////////////////////////////////
//	Image

#include "Image.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
//#include "external/stb_image/stb_image.h"

namespace ApplicationFramework {

Image::Image()
    : m_data(nullptr)
{
}

Image::~Image()
{
    Unload();
}

void Image::Unload()
{
    if (m_deleteData && m_data)
        stbi_image_free(m_data);
    m_deleteData = false;
    m_data = nullptr;
}

void Image::Init(unsigned char *data, int width, int height, int components)
{
    Unload();
    m_data = data;
    m_width = width;
    m_height = height;
    m_components = components;
}

void Image::Load(const char *filename)
{
    Unload();
    m_data = stbi_load(filename, &m_width, &m_height, &m_components, 4); // m_channels);  4 = RGBA, 3 = RGB
    m_deleteData = true;
    //m_data = stbi_load_16(filename, &m_width, &m_height, &m_components, 4);  // uint16_t per channel
    //m_data = stbi_loadf(filename, &m_width, &m_height, &m_components, 4);    // float per channel
    if (!m_data)
        printf("Failed to load image: %s", filename);
}

} // ApplicationFramework namespace
