<?
include ("connection.php");

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

$requete="SELECT \"ANAT_ID\",\"NAME\",\"TYPE\" from \"ANAT_ENTITY\" where \"CHILD_OF\"='$anat'";
// print $requete;
$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,$nei)
{
$GLOBvege=0;$GLOBanim=0;


$animCOLOR=(array) null;
$vegeCOLOR=(array) null;

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")) 
	{
// 	print "VEGE=".$tanat[$i]." ".$nei."<br>";
	if ($tanat[$i]==$nei) $vegeCOLOR[$GLOBvege]="GREEN"; else $vegeCOLOR[$GLOBvege]="RED";
	$vege[$GLOBvege++]="./".$row["PATH"]; 
	}
else 
	{
// 	print "ANIM=".$tanat[$i]." ".$nei."<br>";
	if ($tanat[$i]==$nei) $animCOLOR[$GLOBanim]="GREEN"; else $animCOLOR[$GLOBanim]="RED";
	$anim[$GLOBanim++]="./".$row["PATH"];
	}
}
}

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

for ($GLOB=0;$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 ($vegeCOLOR[$GLOB]=="RED") 
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imvege,$i,$j,imagecolorallocate($imvege,255,0,0));
			

			if ($vegeCOLOR[$GLOB]=="GREEN")
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imvege,$i,$j,imagecolorallocate($imvege,0,255,0));



			}
		}
}

return $imvege;
}


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


$animCOLOR=(array) null;
$vegeCOLOR=(array) null;


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")) 
	{
	if ($tanat[$i]==$nei) $vegeCOLOR[$GLOBvege]="GREEN"; else $vegeCOLOR[$GLOBvege]="RED";
	$vege[$GLOBvege++]="./".$row["PATH"]; 

	}
else 
	{
	if ($tanat[$i]==$nei) $animCOLOR[$GLOBanim]="GREEN"; else $animCOLOR[$GLOBanim]="RED";
	$anim[$GLOBanim++]="./".$row["PATH"];
	}
}
}


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

for ($GLOB=0;$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 ($animCOLOR[$GLOB]=="RED") 
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imanim,$i,$j,imagecolorallocate($imanim,255,0,0));

			if ($animCOLOR[$GLOB]=="GREEN")
			if ((($r>220)&&($g<50))&&($b<50)) imagesetpixel($imanim,$i,$j,imagecolorallocate($imanim,0,255,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);

}
*/


$region=$_GET["region"];
$nei=$_GET["nei"];


$table=(array) null;
$table[count($table)]=$region;
$table[count($table)]=$nei;

// select_recursif($c,$region); // CAR POUR LES NEIGHBOURS ON NE S'OCCUPE PAS 
// select_recursif($c,$nei);    // DES CELL PAIR, ON N'A PAS L'INFORMATION


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
















?>