<?
include ("connection.php");

//////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {/*if (count($table)==0) $table[count($table)]=$anat;*/return;}
while ($row=pg_fetch_array($stmt))
{
select_recursif($c,$row["ANAT_ID"]);
if ($row["TYPE"]=="cell") $table[count($table)]=$row["ANAT_ID"];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////


function image_vege($c,$tanat)
{
$GLOBvege=0;$GLOBanim=0;

for ($i=0;$i<count($tanat);$i++)
{

$requete="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$tanat[$i]."'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
if (strstr($row["PATH"],"vege")) $vege[$GLOBvege++]="../".$row["PATH"];
else $anim[$GLOBanim++]="../".$row["PATH"];
}
}

$imvege=imagecreatefromjpeg($vege[0]);

for ($GLOB=1;$GLOB<count($vege);$GLOB++)
{
$cur=imagecreatefromjpeg($vege[$GLOB]);

for ($i=1;$i<imagesx($cur);$i++)
		{
		for ($j=0;$j<imagesy($cur);$j++)
			{
			$rgb = imagecolorat($cur, $i, $j);
			$r = ($rgb >> 16) & 0xFF;
			$g= ($rgb>>8) & 0xFF;
			$b= $rgb & 0xFF;
		
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imvege,$i,$j,imagecolorallocate($imvege,255,0,0));
			}
		}
}

return $imvege;
}


function image_anim($c,$tanat)
{
$GLOBvege=0;$GLOBanim=0;

for ($i=0;$i<count($tanat);$i++)
{

$requete="SELECT \"PATH\" from \"MAP\" where \"ANAT_ID\"='".$tanat[$i]."'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
if (strstr($row["PATH"],"vege")) $vege[$GLOBvege++]="../".$row["PATH"];
else $anim[$GLOBanim++]="../".$row["PATH"];
}
}

$imanim=imagecreatefromjpeg($anim[0]);

for ($GLOB=1;$GLOB<count($anim);$GLOB++)
{
$cur=imagecreatefromjpeg($anim[$GLOB]);

for ($i=1;$i<imagesx($cur);$i++)
		{
		for ($j=0;$j<imagesy($cur);$j++)
			{
			$rgb = imagecolorat($cur, $i, $j);
			$r = ($rgb >> 16) & 0xFF;
			$g= ($rgb>>8) & 0xFF;
			$b= $rgb & 0xFF;
		
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imanim,$i,$j,imagecolorallocate($imanim,255,0,0));
			}
		}
}

return $imanim;
}



function affiche($imanim,$imvege)
{
header("Content-type:image/jpeg");

if (($imanim!=null) && ($imvege!=null))
{
$dest=imagecreatetruecolor(imagesx($imvege)*2,imagesy($imvege));
imagecopy($dest,$imanim,0,0,0,0,imagesx($imanim),imagesy($imanim));
imagecopy($dest,$imvege,imagesx($imanim),0,0,0,imagesx($imanim),imagesy($imanim));
imagejpeg($dest);
}
else if ($imanim==null)
	{
	imagejpeg($imvege);
	}
else if ($imvege==null)
	{
	imagejpeg($imanim);
	}


}


$ish="";


$ish=$_GET["ish"];

$table=(array) null;
$requete="SELECT \"REGION_ID\" from \"TARGET\" where \"IN_SITU_ID\"='$ish'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$id=$row["REGION_ID"];

select_recursif($c,$id);

}



affiche(image_anim($c,$table),image_vege($c,$table));

















?>