
// il - A image library representation.

// Copyright (C) 2005-2006 Editopia Inc.
// Released under the LGPL.
// For more information, see http://www.openlibraries.org.

// simple example to exercise the load features of il (rgb24 only).

#include <cstdio>
#include <cstdlib>
#include <string>

#ifdef HAVE_CONFIG_H
#include <openlibraries_global_config.hpp>
#endif

#include <openpluginlib/pl/openpluginlib.hpp>
#include <openpluginlib/pl/utf8_utils.hpp>
#include <openpluginlib/pl/GL_utility.hpp>

#include <openimagelib/il/openimagelib_plugin.hpp>

#include <GL/glew.h>

#if defined ( WIN32 ) || defined ( HAVE_GL_GLUT_H )
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif

const int WIN_WIDTH		= 640;
const int WIN_HEIGHT	= 480;

namespace il  = olib::openimagelib::il;
namespace opl = olib::openpluginlib;

namespace
{
	GLuint texid_jpg;

	size_t crop_w = 2;
	size_t crop_h = 2;
	size_t orig_w, orig_h;

	il::image_type_ptr orig;
	
	struct query_traits
	{
		query_traits( const opl::wstring& filename )
			: filename_( filename )
		{ }
		
		opl::wstring libname( ) const
		{ return L"openimagelib"; }
		
		opl::wstring to_match( ) const
		{ return filename_; }

		opl::wstring type( ) const
		{ return L""; }
		
		int merit( ) const
		{ return 0; }
		
		opl::wstring filename_;
	};
}

static GLuint download_texture( const opl::string& path )
{
	typedef opl::discovery<query_traits> oil_discovery;
	
	oil_discovery plugins( query_traits( opl::to_wstring( path ) ) );
	if( plugins.empty( ) ) return 0; 
	
	oil_discovery::const_iterator i = plugins.begin( );
	il::openimagelib_plugin_ptr plug = boost::shared_dynamic_cast<il::openimagelib_plugin>( i->create_plugin( "" ) );
	if( !plug ) return 0;

	il::image_type_ptr image = plug->load( path );
	if( !image ) return 0;

	orig   = image;
	orig_w = image->width( );
	orig_h = image->height( );

	image = il::conform( image, il::flipped | il::flopped );
	image = il::tm_linear( image );

	GLint internal_format;
	GLenum format, type;
	opl::pf_to_gl_format( image->pf( ), internal_format, format, type );

	glActiveTexture( GL_TEXTURE0 );

	GLuint id;	
	glGenTextures( 1, &id );
	glBindTexture( GL_TEXTURE_2D, id );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
	glTexImage2D( GL_TEXTURE_2D, 0, internal_format, image->width( ), image->height( ), 0, format, type, image->data( ) );
	
	glEnable( GL_TEXTURE_2D );

	return id;
}

static void display( void )
{
	glDisable( GL_DEPTH_TEST );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glEnable( GL_DEPTH_TEST );

	orig->crop( 0, 0, static_cast<int>( crop_w ), static_cast<int>( crop_h ), false );
	il::image_type_ptr new_im = il::conform( orig, il::cropped | il::flipped | il::flopped  );

	new_im = il::conform( new_im, il::flipped | il::flopped );
	new_im = il::tm_linear( new_im );

	GLint internal_format;
	GLenum format, type;
	opl::pf_to_gl_format( new_im->pf( ), internal_format, format, type );

	glBindTexture( GL_TEXTURE_2D, texid_jpg );
	glTexImage2D( GL_TEXTURE_2D, 0, internal_format, new_im->width( ), new_im->height( ), 0, format, type, new_im->data( ) );

	glPushMatrix( );

	glBegin( GL_QUADS );
		glMultiTexCoord2f( GL_TEXTURE0, 0.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f, -1.0f );
		glMultiTexCoord2f( GL_TEXTURE0, 1.0f, 0.0f );
		glVertex3f(  1.0f, -1.0f, -1.0f );
		glMultiTexCoord2f( GL_TEXTURE0, 1.0f, 1.0f );
		glVertex3f(  1.0f,  1.0f, -1.0f );
		glMultiTexCoord2f( GL_TEXTURE0, 0.0f, 1.0f );
		glVertex3f( -1.0f,  1.0f, -1.0f );
	glEnd( );
	
	glPopMatrix( );

#ifndef NDEBUG
	GLenum error;
	while( ( error = glGetError( ) ) != GL_NO_ERROR )
		fprintf( stderr, "GLerror:%s\n", gluErrorString( error ) );
#endif

	glutSwapBuffers( );
}

static void idle( void )
{
	glutPostRedisplay( );
}

static void timer( int )
{
	crop_w = 100;
	crop_h = 100;
/*	crop_w *= 2;
	crop_h *= 2;

	if( crop_w > orig_w )
		crop_w = 2;
		
	if( crop_h > orig_h )
		crop_h = 2;
	
	glutTimerFunc( 660, timer, 0 );	*/
}

static void reshape( int w, int h )
{
	glViewport( 0, 0, w, h );
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	gluPerspective( 45.0f, ( float ) w / h, 0.01f, 1000.0f );
	
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	gluLookAt( 0.0f, 0.0f, -3.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
}

static void keyboard( unsigned char key, int /*x*/, int /*y*/ )
{
	switch( key )
	{
		case 27:
			orig = il::image_type_ptr( );
			exit( 0 );
			break;
		
		default:
			break;
	}
}

int main( int argc, char* argv[ ] )
{
	glutInit( &argc, argv );
	glutInitWindowSize( WIN_WIDTH, WIN_HEIGHT );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
	glutCreateWindow( "Image Crop GL Draw" );
	glutDisplayFunc( display );
	glutIdleFunc( idle );
	glutTimerFunc( 66, timer, 0 );
	glutReshapeFunc( reshape );
	glutKeyboardFunc( keyboard );

	glewExperimental = GL_TRUE;
	glewInit( );
	
	opl::init( );
	
	//texid_jpg = download_texture( "./test.jpg" );

	texid_jpg = download_texture( "../../../../media/textures/Bonita.exr" );

	glutMainLoop( );
}


