#include "image.h"
#include "kernel.h"

using namespace VRUT;

#ifndef VRUT_HPC
bool Image::CheckAndFixImageResolution(wxImage *image, TEXTURE_SAMPLER_TYPE type)
{
	bool resized(false);
	GLenum target = (type == TEXCUBE) ? GL_PROXY_TEXTURE_CUBE_MAP : GL_PROXY_TEXTURE_2D;
	for (GLint param = 0; param == 0;)
	{
		if (image->HasAlpha())
		{
			glTexImage2D(target, 0, GL_RGBA8, image->GetWidth(), image->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
			glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &param);
		}
		else
		{
			glTexImage2D(target, 0, GL_RGB8, image->GetWidth(), image->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
			glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &param);
		}
		if (!param)
		{
			if (image->GetWidth() <= 1 || image->GetHeight() <= 1)
			{
				LOGERROR(wxT("<ImageCommon::CheckAndFixImageDimensions> GL_PROXY_TEXTURE reports the texture can not be uploaded into the video memory despite the fact that the texture resolution is very small. Texture will probably not be shown."));
				return resized;
			}
			LOGWARNING(wxT("<ImageCommon::CheckAndFixImageDimensions> GL_PROXY_TEXTURE reports the texture can not be uploaded into the video memory. Halving the resolution and trying again."));
			*image = image->Scale(image->GetWidth() / 2, image->GetHeight() / 2);
			resized = true;
		}
	}
	return resized;
}
#endif