/* ============================================================
 * File  : thptra.cpp
 * Author: Stephane Gibault
 * Date  : Sun Feb 4 2007
 * Description :
 *
 * 
 *  (C) 2007 by Stephane Gibault
 *
 * 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 2, 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.
 *
 * ============================================================ */
#include "thptra.h"
 
thptra::thptra()
{
	this->setTerminationEnabled(true);
	srcImage.load(":/Interface/Interface/reload.png");
	srcImage2.load(":/Interface/Interface/reload.png");
	srcImage3.load(":/Interface/Interface/reload.png");
	dstImage.load(":/Interface/Interface/reload.png");

	srcImage = srcImage.convertToFormat ( QImage::Format_ARGB32, Qt::AutoColor );
	srcImage2 = srcImage2.convertToFormat ( QImage::Format_ARGB32, Qt::AutoColor );
	srcImage3 = srcImage3.convertToFormat ( QImage::Format_ARGB32, Qt::AutoColor );
	dstImage = dstImage.convertToFormat ( QImage::Format_ARGB32, Qt::AutoColor );
}

void thptra::run()
{
	if ( type_trans == 0 ) // *** Transitions par luma ***
	{
		if ( srcImage3.width() <= 0 || srcImage3.height() <= 0 )
		{
			emit myfin();
			return;
		}

		lumavalue = (float)((float)255/(float)100)*(float)sequence;
		if ( lumavalue >= 0 )
		{
			mycolor.setRed(lumavalue);
			mycolor.setGreen(lumavalue);
			mycolor.setBlue(lumavalue);
		}
		else
		{
			mycolor.setRed(0);
			mycolor.setGreen(0);
			mycolor.setBlue(0);
		}

		if ( invert == "0" )
		{
			if ( sequence <= 95 )
			{
				mycolor2.setRed((float)((float)255/(float)100)*(float)(sequence+5));
				mycolor2.setGreen((float)((float)255/(float)100)*(float)(sequence+5));
				mycolor2.setBlue((float)((float)255/(float)100)*(float)(sequence+5));
			}
			else
			{
				mycolor2.setRed(255);
				mycolor2.setGreen(255);
				mycolor2.setBlue(255);
			}
		}
		else
		{
			if ( sequence >= 5 )
			{
				mycolor2.setRed((float)((float)255/(float)100)*(float)(sequence-5));
				mycolor2.setGreen((float)((float)255/(float)100)*(float)(sequence-5));
				mycolor2.setBlue((float)((float)255/(float)100)*(float)(sequence-5));
			}
			else
			{
				mycolor2.setRed(0);
				mycolor2.setGreen(0);
				mycolor2.setBlue(0);
			}
		}


		for (y = 0; y < srcImage.height(); y++)
		{
			for (x = 0; x < srcImage.width(); x++) 
			{
				srcPixel = srcImage.pixel(x, y);
				srcPixel2 = srcImage2.pixel(x, y);
				srcPixel3 = srcImage3.pixel(x, y);
				tmpcolor1 = srcPixel;
				tmpcolor2 = srcPixel2;
				tmpcolor3 = srcPixel3;
				if ( invert == "0" )
				{
					if ( tmpcolor3.value() > mycolor.value() )
					{
						if ( tmpcolor3.value() <= mycolor2.value() )
						{
							valuefade = 100 - (float)(((float)tmpcolor3.value()/(float)2.5)-((float)mycolor.value()/(float)2.5))*(float)20;
							if (valuefade > 100 || valuefade < 0)
							{
								dstImage.setPixel(x, y, srcPixel);
							}
							else
							{
								tmpcolor3.setRed(tmpcolor1.red() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.red() * (float)((float)valuefade/(float)100));
								tmpcolor3.setGreen(tmpcolor1.green() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.green() * (float)((float)valuefade/(float)100));
								tmpcolor3.setBlue(tmpcolor1.blue() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.blue() * (float)((float)valuefade/(float)100));
								dstPixel = tmpcolor3.rgb();
								dstImage.setPixel(x, y, dstPixel);
							}
						}
						else
						{
							dstImage.setPixel(x, y, srcPixel);
						}
					}
					else
					{
						dstImage.setPixel(x, y, srcPixel2);
					}
				}
				else
				{
					if ( tmpcolor3.value() > mycolor.value() )
					{
						if ( tmpcolor3.value() >= mycolor2.value() )
						{
							valuefade = (float)(((float)tmpcolor3.value()/(float)2.5)-((float)mycolor.value()/(float)2.5))*(float)20;
							if (valuefade > 100 || valuefade < 0)
							{
								dstImage.setPixel(x, y, srcPixel2);
							}
							else
							{
								tmpcolor3.setRed(tmpcolor1.red() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.red() * (float)((float)valuefade/(float)100));
								tmpcolor3.setGreen(tmpcolor1.green() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.green() * (float)((float)valuefade/(float)100));
								tmpcolor3.setBlue(tmpcolor1.blue() * (1-(float)((float)valuefade/(float)100)) + tmpcolor2.blue() * (float)((float)valuefade/(float)100));
								QRgb dstPixel(tmpcolor3.rgb());
								dstImage.setPixel(x, y, dstPixel);
							}
						}
						else
						{
							dstImage.setPixel(x, y, srcPixel2);
						}
					}
					else
					{
						dstImage.setPixel(x, y, srcPixel);
					}
				}
				/*if ( invert == "0" )
				{
					if ( tmpcolor3.value() > mycolor.value() )
					{
						dstImage.setPixel(x, y, srcPixel);
					}
					else
					{
						dstImage.setPixel(x, y, srcPixel2);
					}
				}
				else
				{
					if ( tmpcolor3.value() > mycolor.value() )
					{
						dstImage.setPixel(x, y, srcPixel2);
					}
					else
					{
						dstImage.setPixel(x, y, srcPixel);
					}
				}*/
			}
		}
	}
	else
	{
		// *** Fondu enchaine ***
		for (y = 0; y < srcImage.height(); y++)
		{
			for (x = 0; x < srcImage.width(); x++)
			{
				srcPixel = srcImage.pixel(x, y);
				srcPixel2 = srcImage2.pixel(x, y);
				tmpcolor1 = srcPixel;
				tmpcolor2 = srcPixel2;
				tmpcolor3.setRed(tmpcolor1.red() * (1-(float)((float)sequence/(float)100)) + tmpcolor2.red() * (float)((float)sequence/(float)100));
				tmpcolor3.setGreen(tmpcolor1.green() * (1-(float)((float)sequence/(float)100)) + tmpcolor2.green() * (float)((float)sequence/(float)100));
				tmpcolor3.setBlue(tmpcolor1.blue() * (1-(float)((float)sequence/(float)100)) + tmpcolor2.blue() * (float)((float)sequence/(float)100));
				dstPixel = tmpcolor3.rgb();
				dstImage.setPixel(x, y, dstPixel);
			}				
		}
	}
	emit myfin();
}

