#include "thtra.h"
 
thtra::thtra()
{
	srcImage.load(":/new/prefix2/Interface/Theme/nullpixmap.png");
	srcImage2.load(":/new/prefix2/Interface/Theme/nullpixmap.png");
	srcImage3.load(":/new/prefix2/Interface/Theme/nullpixmap.png");
	dstImage.load(":/new/prefix2/Interface/Theme/nullpixmap.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 thtra::scaleimg(int x, int y)
{
	srcImage = srcImage.scaled( x, y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
	srcImage2 = srcImage2.scaled( x, y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
	srcImage3 = srcImage3.scaled( x, y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
	dstImage = dstImage.scaled( x, y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
}

void thtra::run()
{
	if ( numtrans == 1 )
	{
		// *** Fondu enchaine ***
		for (int y = 0; y < srcImage.height(); y=y+1)
		{
			for (int 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);
			}				
		}
	}
	else
	{
		// *** Transitions par luma ***
		mycolor.setRed((float)((float)255/(float)100)*(float)sequence);
		mycolor.setGreen((float)((float)255/(float)100)*(float)sequence);
		mycolor.setBlue((float)((float)255/(float)100)*(float)sequence);

		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 (int y = 0; y < srcImage.height(); y++)
		{
			for (int 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);
					}
				}
			}
		}
	}
	//exec();
	emit myfin();
}

