#include <FreeImage.h>
#include <log.h>
#include "raw.h"

namespace render{

rawImage::rawImage()
{
    _data = 0;
    pixeles = 0;
}

rawImage::rawImage(const std::string &path):rawImage()
{
    load(path);
}

rawImage::~rawImage()
{
    free();
}

bool rawImage::load(const std::string &path, bool flip)
{
    free();
    FREE_IMAGE_FORMAT format = FreeImage_GetFileType(path.c_str(), 0);
	FIBITMAP* image = FreeImage_Load(format, path.c_str());
	if(!image)
        return 0;
	image = FreeImage_ConvertTo32Bits(image);
	size.x = FreeImage_GetWidth(image);
	size.y = FreeImage_GetHeight(image);
	if(flip)
        FreeImage_FlipVertical(image);
	pixeles = FreeImage_GetBits(image);
	_data = image;
	return 1;
}

void rawImage::free()
{
    if(_data)
        FreeImage_Unload((FIBITMAP*)_data);
    _data = 0;
    pixeles = 0;
}

}
