//	ImageLoader.cpp - Class responsible for loading images to use in a Drawer.
//  ----------------------------------------------------------------------------
//	This file is part of 'The Future Will Only Be Dystopian if Capitalism
//	Remains Undefeated', a game.
//	Copyright (C) 2009  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "ImageLoader.h"

#include "SDL_image.h"
#include <iostream>

using namespace std;

//------------------------------------------------------------------------------
ImageLoader::ImageLoader()
{
	
}

//------------------------------------------------------------------------------
ImageLoader::~ImageLoader()
{
	
}

//------------------------------------------------------------------------------
ImageData ImageLoader::loadImage(const char *data, uint32_t size)
{
	ImageData retval;
	SDL_Surface *tempIm = IMG_Load_RW(SDL_RWFromConstMem(data, size), 1);

	retval.data = 0;
	retval.width = 0;
	retval.height = 0;

	if(tempIm)
	{
		SDL_LockSurface(tempIm);
		{
			if(tempIm->format->BytesPerPixel == 4)
			{
				int i;
				uint8_t *data = (uint8_t *)(tempIm->pixels);
				uint8_t *tempData = new uint8_t[tempIm->w * tempIm->h * 4];

				for(i=0;i<(tempIm->w * tempIm->h * 4);++i)
					tempData[i] = data[i];

				retval.data = tempData;
				retval.width = tempIm->w;
				retval.height = tempIm->h;
			}
			//Assume 24-bit RGB.
			else if(tempIm->format->BytesPerPixel == 3)
			{
				int i;
				uint8_t *data = (uint8_t *)(tempIm->pixels);
				uint8_t *tempData = new uint8_t[tempIm->w * tempIm->h * 4];

				cout << "ImageLoader: Image 24bit.  Assuming 24bit RGB format." << endl;

				for(i=0;i<(tempIm->w * tempIm->h);++i)
				{
					tempData[(i*4)] = data[(i*3)];
					tempData[(i*4)+1] = data[(i*3)+1];
					tempData[(i*4)+2] = data[(i*3)+2];
					tempData[(i*4)+3] = 255;
				}

				retval.data = tempData;
				retval.width = tempIm->w;
				retval.height = tempIm->h;
			}
			else
				cout << "ImageLoader: Image not 32bits." << endl;
		}
		SDL_UnlockSurface(tempIm);

		SDL_FreeSurface(tempIm);
	}

	return retval;
}

//------------------------------------------------------------------------------
ImageData ImageLoader::loadImage(const std::string& path)
{
	ImageData retval;
	SDL_Surface *tempIm = IMG_Load(path.c_str());

	retval.data = 0;
	retval.width = 0;
	retval.height = 0;

	if(tempIm)
	{
		SDL_LockSurface(tempIm);
		{
			if(tempIm->format->BytesPerPixel == 4)
			{
				int i;
				uint8_t *data = (uint8_t *)(tempIm->pixels);
				uint8_t *tempData = new uint8_t[tempIm->w * tempIm->h * 4];

				for(i=0;i<(tempIm->w * tempIm->h * 4);++i)
					tempData[i] = data[i];

				retval.data = tempData;
				retval.width = tempIm->w;
				retval.height = tempIm->h;
			}
			else if(tempIm->format->BytesPerPixel == 3)
			{
				int i, j;
				uint8_t *data = (uint8_t *)(tempIm->pixels);
				uint8_t *tempData = new uint8_t[tempIm->w * tempIm->h * 4];

				j = 0;
				for(i=0;i<(tempIm->w * tempIm->h);++i)
				{
					tempData[j] = data[i*3];
					++j;
					tempData[j] = data[(i*3)+1];
					++j;
					tempData[j] = data[(i*3)+2];
					++j;
					tempData[j] = 255;
					++j;
				}

				retval.data = tempData;
				retval.width = tempIm->w;
				retval.height = tempIm->h;
			}
			else
				cout << "ImageLoader: Image not 32bits." << endl;
		}
		SDL_UnlockSurface(tempIm);

		SDL_FreeSurface(tempIm);
	}

	return retval;
}
