<?
/*
    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/>.

*/
include ("connection.php");

ini_set("memory_limit","512M");


//////////////////////////////////////////////////////////////////////////////////////////////////////////
function select_recursif($c,$anat)
{
global $table;

$requete="SELECT \"ANAT_ID\",\"NAME\",\"TYPE\" from \"ANAT_ENTITY\" where \"CHILD_OF\"='$anat'";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0) return;
while ($row=pg_fetch_array($stmt))
{
select_recursif($c,$row["ANAT_ID"]);
if ($row["TYPE"]=="cell") $table[count($table)]=$row["ANAT_ID"];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////


/*
function getPath($c,$anat)
{
global $table;

$path=(array) null;


$requete="SELECT \"TYPE\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='$anat'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if ($row["TYPE"]=="cell") 
	{
	//print "ci";
	//$path[0]=$anat;

	$requete0="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$anat."'";
	$stmt0=pg_query($c,$requete0);
	$row0=pg_fetch_array($stmt0);
	
	$path[0]=$row0["PATH"];

	
	return $path;
	}



select_recursif($c,$anat);


for ($i=0;$i<count($table);$i++)
	{
	$requete="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$table[$i]."'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$path[count($path)]=$row["PATH"];

	}

return $path;
}

function display($c,$anat)
{
$path=getPath($c,$anat);


header("Content-type:image/jpg");

for ($GLOB=0;$GLOB<count($path);$GLOB++)
{
$im[$GLOB]=imagecreatefromjpeg($path[$GLOB]);
for ($i=0;$i<imagesx($im[$GLOB]);$i++)
	{
	for ($j=0;$j<imagesy($im[$GLOB]);$j++)
		{
	$rgb = imagecolorat($im[$GLOB], $i, $j);
	$r = ($rgb >> 16) & 0xFF;
	$g= ($rgb>>8) & 0xFF;
	$b= $rgb & 0xFF;
		
	if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($im[0],$i,$j,imagecolorallocate($im[0],255,0,0));
		}
	}

}

imagejpeg($im[0]);

}

*/


$anat=$_GET["anat"]; /////// Recup de l'identifiant de l'anat
if (strlen($anat)==0) 
	{
	print "<center><br><br>Please enter an anatomical identity !</center>";
	include ("frames_footer.php");
	}
//$anat="2464823";
//$anat="2464846";
//$anat="2464847";
//$anat="2464822";

//display($c,$anat); ///// On affiche l'image !



/////////////////////////////////
////NEW
////////////////////////////////






$path=(array) null;
$pathanim=(array) null;
$pathvege=(array) null;

$requete="SELECT \"TYPE\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='$anat'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if ($row["TYPE"]=="cell") 
	{
	//// On demande une feuille
	$requete0="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$anat."'";
	$stmt0=pg_query($c,$requete0);
	if (pg_num_rows($stmt0)==1)
		{
		$row0=pg_fetch_array($stmt0);
		$path[0]=$row0["PATH"];
		}
	else
		{
		$I=0;
		while ($row0=pg_fetch_array($stmt0))
		{
		if ($I==0) $pathanim[0]=$row0["PATH"];
		if ($I==1) $pathvege[0]=$row0["PATH"];
		$I++;
		}
		}

// 	print_r($path);print "<br>";print_r($pathanim);print "<br>";print_r($pathvege);

	}
else
	{
	//// On demande autre chose qu'une feuille

	$table=(array) null;
	select_recursif($c,$anat);

	for ($i=0;$i<count($table);$i++)
	{
	$requete0="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$table[$i]."'";
	$stmt0=pg_query($c,$requete0);
	$I=0;
	while ($row0=pg_fetch_array($stmt0))
		{
// 		print $row0["PATH"]."<br>";
		if (strstr($row0["PATH"],"anim")) $pathanim[count($pathanim)]=$row0["PATH"];
		else if (strstr($row0["PATH"],"vege")) $pathvege[count($pathvege)]=$row0["PATH"];

		else if (strstr($row0["PATH"],"left")) $pathanim[count($pathanim)]=$row0["PATH"];
		else $pathanim[count($pathanim)]=$row0["PATH"];
		//if ($I%2==0) $pathanim[count($pathanim)]=$row0["PATH"];
		//if ($I%2==1) $pathvege[count($pathvege)]=$row0["PATH"];

		$I++;


		}

	}
//    	print_r($path); print "<br>";
//   	print "ANIMAL=";print_r($pathanim);print "<br>";
//   	print "VEGETATIF=";print_r($pathvege);print "<br>";return;
	}



if (count($path)!=0)
	{
	//// On a une vue qui n'est pas animal et vegetative a la fois

	header("Content-type:image/jpg");

	for ($GLOB=0;$GLOB<count($path);$GLOB++)
	{
	$im[$GLOB]=imagecreatefromjpeg($path[$GLOB]);
		for ($i=0;$i<imagesx($im[$GLOB]);$i++)
		{
		for ($j=0;$j<imagesy($im[$GLOB]);$j++)
			{
			$rgb = imagecolorat($im[$GLOB], $i, $j);
			$r = ($rgb >> 16) & 0xFF;
			$g= ($rgb>>8) & 0xFF;
			$b= $rgb & 0xFF;
		
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($im[0],$i,$j,imagecolorallocate($im[0],255,0,0));
		}
	}

	}

	imagejpeg($im[0]);

	}
else
	{
	//// On a une vue animal est vegetative

	header("Content-type:image/jpg");

if (count($pathanim)!=0)
{
	for ($GLOB=0;$GLOB<count($pathanim);$GLOB++)
	{

	$im[$GLOB]=imagecreatefromjpeg($pathanim[$GLOB]);
		for ($i=0;$i<imagesx($im[$GLOB]);$i++)
		{
		for ($j=0;$j<imagesy($im[$GLOB]);$j++)
			{
			$rgb = imagecolorat($im[$GLOB], $i, $j);
			$r = ($rgb >> 16) & 0xFF;
			$g= ($rgb>>8) & 0xFF;
			$b= $rgb & 0xFF;
		
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($im[0],$i,$j,imagecolorallocate($im[0],255,0,0));
		}
	}

	}
}

if (count($pathvege))
{
	for ($GLOB=0;$GLOB<count($pathvege);$GLOB++)
	{
	$im2[$GLOB]=imagecreatefromjpeg($pathvege[$GLOB]);
		for ($i=0;$i<imagesx($im2[$GLOB]);$i++)
		{
		for ($j=0;$j<imagesy($im2[$GLOB]);$j++)
			{
			$rgb = imagecolorat($im2[$GLOB], $i, $j);
			$r = ($rgb >> 16) & 0xFF;
			$g= ($rgb>>8) & 0xFF;
			$b= $rgb & 0xFF;
		
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($im2[0],$i,$j,imagecolorallocate($im2[0],255,0,0));
		}
	}

	}
}

	//imagejpeg($im[0]);
if ((count($pathanim)!=0) && (count($pathvege)!=0))
	{
	/*$dest=imagecreatetruecolor(imagesx($im2[0])*2,imagesy($im2[0]));
	imagecopy($dest,$im[0],0,0,0,0,imagesx($im[0]),imagesy($im[0]));
	imagecopy($dest,$im2[0],imagesx($im[0]),0,0,0,imagesx($im[0]),imagesy($im[0]));
	imagejpeg($dest);*/

if (imagesx($im2[0])==imagesx($im[0])) 
		{
		$dest=imagecreatetruecolor(imagesx($im2[0])*2,imagesy($im2[0]));
		imagecopy($dest,$im[0],0,0,0,0,imagesx($im[0]),imagesy($im[0]));
		imagecopy($dest,$im2[0],imagesx($im[0]),0,0,0,imagesx($im[0]),imagesy($im[0]));
		imagejpeg($dest);
		}
else
		{
		$dest=imagecreatetruecolor(imagesx($im[0])+imagesx($im2[0]),imagesy($im2[0]));
		imagecopy($dest,$im[0],0,0,0,0,imagesx($im[0]),imagesy($im[0]));
		//imagecopy($dest,$im2[0],imagesx($im[0]),0,0,0,imagesx($im[0]),imagesy($im[0]));
		imagecopy($dest,$im2[0],imagesx($im[0]),0,0,0,imagesx($im2[0]),imagesy($im2[0]));
		imagejpeg($dest);
		}



	}

if ((count($pathanim)!=0) && (count($pathvege)==0))
	{
	imagejpeg($im[0]);
	}

if ((count($pathanim)==0) && (count($pathvege)!=0))
	{
	imagejpeg($im2[0]);
	}




	}






















?>