<?
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    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 3 of the License, 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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
header("Content-Type: image/jpeg");

$tab=$_GET["region"];
$type=$_GET["type"];
$stade=$_GET["stade"];

$region=explode(":",$tab);





function pathtoIMG_GIF($filename)
{
return ImagecreatefromGif($filename);
}

function pathtoIMG_JPG($filename)
{
return ImagecreatefromJpeg($filename);
}



//Colorize given image
function colorizeIMG($img0,$img1,$img2)
{
for ($i=0;$i<imagesx($img1);$i++)
	{
	for ($j=0;$j<imagesy($img1);$j++)
		{
		$rgb0 = ImageColorAt($img0, $i, $j);
		$rgb1 = ImageColorAt($img1, $i, $j);
		$rgb2 = ImageColorAt($img2, $i, $j);


		$r1= ($rgb1>>16) & 0xFF;
		$g1= ($rgb1>>8) & 0xFF;
		$b1= $rgb1 & 0xFF;
		
		if ((($r1>220)&&($g1<50))&&($b1<50)) 
			{
			$newr1=255;
			$newg1=0;
			$newb1=0;
			$newrgb1=Imagecolorallocate($img0,$newr1,$newg1,$newb1);

			imagesetpixel($img0,$i,$j,$newrgb1);
			}

		$r2= ($rgb2>>16) & 0xFF;
		$g2= ($rgb2>>8) & 0xFF;
		$b2= $rgb2 & 0xFF;

		if ((($r2>220)&&($g2<50))&&($b2<50)) 
			{
			$newr2=255;
			$newg2=0;
			$newb2=0;
			$newrgb2=Imagecolorallocate($img0,$newr2,$newg2,$newb2);

			imagesetpixel($img0,$i,$j,$newrgb2);
			}
		}
	}
return $img0;
}

/*
//Colorize given relative path to images files
function colorize($cellBACK,$cellA,$cellB)
{

$img0 = ImageCreateFromGif($cellBACK);
$img1 = ImageCreateFromJpeg($cellA);
$img2 = ImageCreateFromJpeg($cellB);

for ($i=0;$i<imagesx($img1);$i++)
	{
	for ($j=0;$j<imagesy($img1);$j++)
		{
		$rgb0 = ImageColorAt($img0, $i, $j);
		$rgb1 = ImageColorAt($img1, $i, $j);
		$rgb2 = ImageColorAt($img2, $i, $j);


		$r1= ($rgb1>>16) & 0xFF;
		$g1= ($rgb1>>8) & 0xFF;
		$b1= $rgb1 & 0xFF;
		
		if ((($r1>220)&&($g1<50))&&($b1<50)) 
			{
			$newr1=255;
			$newg1=0;
			$newb1=0;
			$newrgb1=Imagecolorallocate($img0,$newr1,$newg1,$newb1);

			imagesetpixel($img0,$i,$j,$newrgb1);
			}

		$r2= ($rgb2>>16) & 0xFF;
		$g2= ($rgb2>>8) & 0xFF;
		$b2= $rgb2 & 0xFF;

		if ((($r2>220)&&($g2<50))&&($b2<50)) 
			{
			$newr2=255;
			$newg2=0;
			$newb2=0;
			$newrgb2=Imagecolorallocate($img0,$newr2,$newg2,$newb2);

			imagesetpixel($img0,$i,$j,$newrgb2);
			}
		}
	}
return $img0;
}
*/



//$img0=colorize("../late32c/late32anim.gif","../late32c/lateb6.5anim.jpg","../late32c/lateb6.5rightanim.jpg");
//$img1=colorize("../late32c/late32vege.gif","../late32c/lateA6.1vege.jpg","../late32c/lateA6.1rightvege.jpg");

/*
$imgBACK=pathtoIMG_GIF("../late32c/late32vege.gif");
$img1=pathtoIMG_JPG("../late32c/lateA6.1vege.jpg");
$img2=pathtoIMG_JPG("../late32c/lateA6.1rightvege.jpg");


$imgRES=colorizeIMG($imgBACK,$img1,$img2);

$img3=pathtoIMG_JPG("../late32c/lateA6.4vege.jpg");
$img4=pathtoIMG_JPG("../late32c/lateA6.4rightvege.jpg");

$imgRES2=colorizeIMG($imgBACK,$imgRES,$img3);
$imgRES3=colorizeIMG($imgBACK,$imgRES2,$img4);
*/

if ($type=="anim") $imgBACKpath="embryos/".$stade."c/".$stade."anim.gif";
if ($type=="vege") $imgBACKpath="embryos/".$stade."c/".$stade."vege.gif";

$imgBACK=pathtoIMG_GIF($imgBACKpath);
$img1=pathtoIMG_JPG($region[0]);
$img2=pathtoIMG_JPG($region[1]);

$imgRES=colorizeIMG($imgBACK,$img1,$img2);



//imageJpeg($img0);
imageJpeg($imgRES);


?>