/*
 * |P|I|X|O| Image cataloging using html and thumb nails
 * Released under GPL http://www.gnu.org/copyleft/gpl.html
 * 
 * kishan@hackorama.com www.hackorama.com     2001 MAY
 *
 * Class "Image"
 * Generates the thumb nails and images  
 *
 */

#include "image.h"


extern int boxFit( int *nw, int *nh, int aw,int ah ,int bw, int bh );

Image::Image()
{
}

Image::Image(char** list , int count )
{
	img_list = list;
	img_count = count;
	th = 80;
        tw = 0;
        mw = 500;
        mh = 0;
	init();
}

Image::Image(char** list, int count, int _tw, int _th, int _mw, int _mh )
{
	img_list = list;
	img_count = count;
        tw = _tw;
	th = _th;
        mw = _mw;
        mh = _mh;
	init();
}

Image::~Image()
{

}

void 
Image::init()
{
	strcpy (t_tag, "pixo-t-");
	strcpy (m_tag, "pixo-m-");
	strcpy( base , "." );
#ifndef _WINDOWS

        strcpy( separator, "/" );
#else

        strcpy( separator, "\\" );
#endif

	init_il();
}


void 
Image::init_il()
{
	if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
	ilGetInteger(ILU_VERSION_NUM) < ILU_VERSION) {
		printf("OpenIL version is different...exiting!\n");
	}
	ilStartup();
	ilGenImages(1, &id);
	ilBindImage(id);

}

void
Image::process()
{
        for( int i = 0 ; i < img_count ; i++) {
                processOne( img_list[i]  );
        }
}



void 
Image::processOne( char *img_file )
{
	char img[PATH_MAX + 1];
	int w=0, h=0, d=0 ;
	static int justcount = 0;
	justcount++;
	char cleaned[PATH_MAX+1];
	resolvePath( cleaned , img_file );
	int nw = 0;
	int nh = 0;
	
	ilLoadImage(cleaned);
	iluImageParameter( ILU_PLACEMENT, ILU_UPPER_RIGHT );
	iluImageParameter( ILU_FILTER, ILU_LINEAR );

	w = ilGetInteger(IL_IMAGE_WIDTH);
	h = ilGetInteger(IL_IMAGE_HEIGHT);
	d = ilGetInteger(IL_IMAGE_DEPTH);
#ifndef SILENT

	cout << "	processing (" << cleaned << ") \t"  ;
	cout << justcount <<"/" << img_count << endl ;
#endif /* SILENT */

        iluFlipImage(); //correction for improper jpeg image origin

	
	sprintf( img, "%s%s", m_tag, img_file );	
	resolvePath( cleaned , img );
	boxFit( &nw, &nh, w, h , mw, mh );
	iluScale( nw, nh , d );
#ifdef DEBUG

	cout << endl << ">> " << cleaned << " " << getWidth(w, h , 1) << " " << getHeight(w, h, 1) << endl ;
#endif /* DEBUG */

        //iluRotate( 180  ); // noe corrected with iluFlipImage() above 

	ilEnable(IL_FILE_OVERWRITE); 
	ilSaveImage(cleaned);

	sprintf( img, "%s%s", t_tag, img_file );	
	resolvePath( cleaned , img );
	boxFit( &nw, &nh, w, h , tw, th );
	iluScale( nw, nh , d );
#ifdef DEBUG

	cout << endl << ">> " << cleaned << " " << getWidth(w, h , 2) << " " << getHeight(w, h, 2) << endl ;
#endif /* DEBUG */

	ilEnable(IL_FILE_OVERWRITE); 
	ilSaveImage(cleaned);

}

void 
Image::cleanUp()
{
	ILuint        Error;
	ilDeleteImages(1, &id);

        while ((Error = ilGetError())) {
	 	printf("Error: %s\n", iluErrorString(Error));
	}

}

void
Image::setBase( char *_base )
{
        strcpy( base , _base );
}

void
Image::setSize(int _tw, int _th , int _mw, int _mh  )
{
        tw = _tw;
	th = _th;
        mw = _mw;
        mh = _mh;
}

void
Image::resolvePath( char* path , char* name)
{
        if( strcmp( base, "." ) == 0  ){
                sprintf( path, "%s" , name );
        }else{
           	char tmp[2];
           	tmp[0] =  base[strlen(base)-1];
	        tmp[1] =  '\0';
            	if( strcmp( tmp, separator) == 0  )
	              	sprintf( path, "%s%s" , base, name );
	       else
	                sprintf( path, "%s%s%s" , base, separator, name );
	}
}


syntax highlighted by Code2HTML, v. 0.9