<?php
/*
    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/jpg");
    $value=$_GET["embryo"];
$ps=$_GET["ps"];

if (isset($_GET["blastofate"]))
	{
	$blastofate=$_GET["blastofate"];
	$blastofateT=explode(":",$blastofate);
	}

else $blastofateT=null;

if (isset($_GET["fateP"]))
	{
	$fateP=$_GET["fateP"];
	$fateP=explode(":",$fateP);
	}



$ps=str_replace(".nwk",".ps",$ps);

function LoadJpeg($imgname)
{
   $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
   if (!$im) { /* See if it failed */
       $im  = imagecreatetruecolor(150, 30); /* Create a black image */
       $bgc = imagecolorallocate($im, 255, 255, 255);
       $tc  = imagecolorallocate($im, 0, 0, 0);
       imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
       /* Output an errmsg */
       imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
   }
   return $im;
}

$im=LoadJpeg("treeJPG/$value");


imagefilledrectangle ( $im, 10, 5, 600, 43, imagecolorallocate($im, 255, 255, 255) );

imagestring($im, 1, 5, 5, "Click on blastomeres to display its short anatomical tree.", imagecolorallocate($im, 0, 0, 0));
imagestring($im, 1, 5, 15, "Put your mouse over blastomeres to display its fates.", imagecolorallocate($im, 0, 0, 0));


//Traitement sur l'image



$handle = @fopen($ps, "r");
$contenu="";
if ($handle) {
   while (!feof($handle)) {
       $contenu.= fgets($handle, 4096);
   }
}
fclose($handle);
$contenu=str_replace("\n","",$contenu);

$tab=split(" ",$contenu);
$blasto=(array) null;
for ($i=0;$i<count($tab);$i++)
	if (ereg ("([aAbB][0-9]+\.[0-9]+\*{0,1})", $tab[$i], $regs)) $blasto[count($blasto)]=$regs[1];

$result = array_reverse($blasto);


$col=108;
$cell=count($result);
$pas=(1059-108)/($cell-1)+0.2; //Ajout 0.2 pour recaler ...

$coord=(array) null;
for ($i=0;$i<count($result);$i++)
{

$bobo=$col+20;
/*print '<AREA HREF="short_hierarchy.php?region='.$ids[$i].'&dev='.$dev.'" COORDS="705,'.$col.',750,'.$bobo.'" 
onmouseover="return escape(\'<u>Fate</u>: <br><b>'.$fate.'</b>\');">';*/
	
imagefilledrectangle ( $im, 705, $col, 750, $bobo, imagecolorallocate($im, 255, 255, 255) );
if ($blastofateT!=null)
{
if (in_array($result[$i],$blastofateT))
	{
	imagefilledrectangle ( $im, 705, $col, 750, $bobo, imagecolorallocate($im, 255, 255, 0) );

	}

}
	imagestring($im, 5, 705, $col, $result[$i], imagecolorallocate($im, 0, 0, 0));

$col+=$pas;
}

//FIN


if ($fateP!=null)
	{
	if (count($fateP)==1) imagestring($im, 5, 60, 50, "Fate:", imagecolorallocate($im, 255, 0, 0));
	else imagestring($im, 5, 60, 50, "Fates:", imagecolorallocate($im, 255, 0, 0));
	//$tempo=implode(",",$fateP);
	//$tempo=substr(0,strlen($tempo)-1,$tempo);
	//print "<strong> Choosen fate: $tempo </strong><br>";
	$INDIX=70;
	for ($i=0;$i<count($fateP);$i++)
		{
		if (strlen($fateP[$i])!=0)
			{
			imagestring($im, 10, 60, $INDIX, ". ".$fateP[$i], imagecolorallocate($im, 0, 0, 255));
			$INDIX+=15;
			}
		else {
			imagestring($im, 5, 120, 50, "No fate selected", imagecolorallocate($im, 255, 0, 0));
			$INDIX+=15;
			}
		}
	}



//imagestring($im, 10, 500, 1150, "Aniseed", imagecolorallocate($im, 0, 0, 255));


imagejpeg($im);

?>
