<?
/*
    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");


//////////////////////////////////////////////////////////////////////////////////////////////////////////
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

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




























?>