
<?php
//File modules

//
//	2002.1.1
//	DICOM to WWW Gateway :PHP script
//	Made by Norya Yokohama % Medical Division @ WERC
//  2004.9.19
//  Updated by tadashi tsuchimoto @ MITT belong to yokohamazyuku
//	file_mod.inc	
//



@$convert = exec("which convert");
@$cjpeg = exec("which cjpeg");
@$djpeg = exec("which djpeg");
//convert-y1 is engine for smoothing
@$convert_y1 = exec("which convert-y1");
//convert-y2 is engine for getting HU value
//@$convert_y2 = exec("which convert-y2");
//$dctopgm8 = system("which dctopgm8");
$dctopgm8 = "/usr/local/bin/dctopgm8";
$dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
$dckey = "/usr/local/bin/dckey";
//$get_dicom_value ="/usr/local/bin/get_dicom_value";
$dcmdump = "/usr/local/dicom/bin/dcmdump";
$awk = exec("which awk");
$identify = exec("which identify");
$medcon ="/usr/local/bin/medcon";


//get_dicom_value 
function get_dicom_value($target_tag,$target_file){
	global $dcmdump,$awk;
	$target_tag_value = exec("$dcmdump --search \"$target_tag\" $target_file | $awk '{print $3;}'");
	$target_tag_value = trim($target_tag_value);
	$target_tag_value = str_replace("[","",$target_tag_value);
	$target_tag_value = str_replace("]","",$target_tag_value);
	return $target_tag_value;

}

function get_dicom_value_parenthetic($target_tag,$target_file){
	global $dcmdump,$awk;
	$target_tag_value = exec("$dcmdump --search \"$target_tag\" $target_file");
	$target_tag_value = trim($target_tag_value);
	$start = strpos($target_tag_value,"[");
	$end = strpos($target_tag_value,"]");
	$value = substr($target_tag_value, $start, $end);
	return $value;
}

//touch dir.
function touch_dir($study_uid,$series_uid,$wwwl){
	
	$tmp_dir=("./tmp" ."/" .  $study_uid);
	mkdir($tmp_dir,0777);
	$tmp_dir=($tmp_dir . "/" . $series_uid);
	mkdir($tmp_dir,0777);
	$tmp_dir=($tmp_dir . "/" . $wwwl);
	mkdir($tmp_dir,0777);
	return $tmp_dir;
}



//calc age
function calc_age($birth)
{
    $ty = date("Y");
    $tm = date("m");
    $td = date("d");
    list($by, $bm, $bd) = explode('-', $birth);
    $age = $ty - $by;
    if($tm * 100 + $td < $bm * 100 + $bd) $age--;
    return $age;
}

//convert dicom to jpeg

function dcmtojpg($from_file){
	$dir=dirname(escapeshellcmd($from_file));
	$jpg_file=($from_file . ".jpg");//print $jpg_file;
	$bmp_file=($from_file . ".bmp");//print $bmp_file;
	system("/usr/local/bin/dicom2 --warn=no --to=$dir -w $from_file");
    system("$convert $bmp_file  $jpg_file");
	system("/bin/rm $bmp_file ");
        return $jpg_file;
}

//convert dicom to jpeg about multiframe dicom file




//convert dicom to jpeg with ww wl



function dcmtojpg_set($from_file,$ww,$wl){
	$dir=dirname(escapeshellcmd($from_file));
	$ww = escapeshellcmd($ww);
	$wl = escapeshellcmd($wl);
	$jpg_file=($from_file . ".jpg");//print $jpg_file;
	$bmp_file=($from_file . ".bmp");//print $bmp_file;
	exec("/usr/local/bin/dicom2 --warn=no --to=$dir --win=$wl:$ww -w $from_file");
	system("/usr/X11R6/bin/convert $bmp_file  $jpg_file");
	return $jpg_file;

}

//convert dicom to png

function dcmtopng($from_file){
	$dir=dirname(capeshellcmd($from_file));
        $png_file=($from_file . ".png");//print $png_file;
        system("/usr/local/bin/dicom2 --warn=no --to=$dir -p $from_file ");
        return $png_file;

}

//convert dicom to png with ww wl

function dcmtopng_set($from_file,$ww,$wl){
	$dir=dirname(escapeshellcmd($from_file));
	$ww = escapeshellcmd($ww);
        $wl = escapeshellcmd($wl);
        $png_file=($from_file . ".png");//print $png_file;
        exec("/usr/local/bin/dicom2 --warn=no --to=$dir --win=$wl:$ww -p $from_file");
	return $png_file;

}


function dcmtojpg2($from_file){
	global $convert,$dcm2pnm;
        $dir=dirname(escapeshellcmd($from_file));
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
//        $bmp_file=($from_file . ".bmp");//print $bmp_file;
		system("$dcm2pnm +oj +Wm $from_file $jpg_file");
//        system("$dcm2pnm +ob +Wm $from_file $bmp_file");
//        system("$convert $bmp_file  $jpg_file");
//	system("/bin/rm $bmp_file ");
        return $jpg_file;

}

function dcmtojpg2_set($from_file,$ww,$wl){
	global $convert,$dcm2pnm;
        $dir=dirname(escapeshellcmd($from_file));
	$ww = escapeshellcmd($ww);
        $wl = escapeshellcmd($wl);
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
//        $bmp_file=($from_file . ".bmp");//print $bmp_file;
		system("$dcm2pnm +oj +Ww $wl $ww $from_file $jpg_file");
//        system("$dcm2pnm +ob +Ww $wl $ww $from_file $bmp_file");
//        system("$convert $bmp_file  $jpg_file");
//        system("/bin/rm $bmp_file ");
        return $jpg_file;

}

//convert dicom to jpg without ww wl using dcm2pnm with magnification filter
function dcmtojpg2_filter($from_file,$power,$negate,$rotate){
	global $convert,$dcm2pnm,$cjpeg,$djpeg;
		
		if($rotate != "0"){
		$rotate_tag = "+" . $rotate;
		}else{
		$rotate_tag = "";
		}
		if($negate !=0){
		$negate_tag = "neg";
		}else{
		$negate_tag = "";
		}
		
		$add_tag = "_" . $power . "_" . $negate_tag  . "_" . $rotate_tag;
		
        $dir=dirname(escapeshellcmd($from_file));
		
		
		$jpg_file=($target_dicom . "-" . $add_tag . ".jpg");
        
		if($negate){
		system("$dcm2pnm +oj $rotate_tag +Wm +P $from_file $jpg_file &");
		}else{
		system("$dcm2pnm +oj $rotate_tag +Wm $from_file $jpg_file &");
		}
		
		if($power != 1){
		system("$convert_y1  -s $power  -n -n   $jpg_file  $jpg_file &");
		}
		
		return $jpg_file;

}

function dcmtojpg2_set_filter($from_file,$power,$negate,$rotate,$ww,$wl){
	global $convert,$dcm2pnm,$cjpeg,$djpeg,$convert_y1;
		
		if($rotate != "0"){
		$rotate_tag = "+" . $rotate;
		}else{
		$rotate_tag = "";
		}
		if($negate !=0){
		$negate_tag = "neg";
		}else{
		$negate_tag = "";
		}
		
		$add_tag = $ww . "_" . $wl .  "_" . $power . "_" . $negate_tag  . "_" . $rotate_tag;
		
		
		
		
		
        $dir=dirname(escapeshellcmd($from_file));
		
		$jpg_file=($from_file . "-" . $add_tag . ".jpg");
		
		
        if($negate){
		system("$dcm2pnm +oj $rotate_tag +Ww $wl $ww  +P $from_file $jpg_file &");
		}else{
		system("$dcm2pnm +oj $rotate_tag +Ww $wl $ww $from_file $jpg_file &");
		}
		if($power != 1){
		system("$convert_y1  -s $power  -n -n  $jpg_file  $jpg_file &");
		}
		return $jpg_file;

}




//convert dicom to jpg without ww wl using dctopgm8

function dcmtojpg3($from_file){
	global $convert,$dctopgm8;
        $dir=dirname(escapeshellcmd($from_file));
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
        $pgm_file=($from_file . ".pgm");//print $pgm_file;
        system("$dctopgm8 -signed $from_file $pgm_file");
        system("$convert $pgm_file  $jpg_file");
        system("/bin/rm $pgm_file ");
        return $jpg_file;

}


//convert dicom to jpg for NM using dctopgm8

function dcmtojpg3_nm($from_file,$ww,$wl){
	global $convert,$dctopgm8;
        $dir=dirname(escapeshellcmd($from_file));
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
        $pgm_file=($from_file . ".pgm");//print $pgm_file;
        system("$dctopgm8 -signed -windowwidth $ww -windowlevel $wl $from_file $pgm_file");
        system("$convert -negate $pgm_file  $jpg_file");
        system("/bin/rm $pgm_file ");
        return $jpg_file;

}


function dcmtojpg3_noset_nm($from_file){
	global $convert,$dctopgm8;
        $dir=dirname(escapeshellcmd($from_file));
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
        $pgm_file=($from_file . ".pgm");//print $pgm_file;
        system("$dctopgm8 -signed $from_file $pgm_file");
        system("$convert -negate $pgm_file  $jpg_file");
        system("/bin/rm $pgm_file ");
        return $jpg_file;

}

//convert dicom to jpg with ww wl using dctopgm8

function dcmtojpg3_set($from_file,$ww,$wl){
	global $convert,$dctopgm8;
        $dir=dirname(escapeshellcmd($from_file));
	$ww = escapeshellcmd($ww);
        $wl = escapeshellcmd($wl);
        $jpg_file=($from_file . ".jpg");//print $jpg_file;
        $pgm_file=($from_file . ".pgm");//print $pgm_file;
        system("$dctopgm8 -signed -windowwidth $ww -windowlevel $wl  $from_file  $pgm_file");
        system("$convert $pgm_file  $jpg_file");
        system("/bin/rm $pgm_file ");
        return $jpg_file;

}


function reg_dicom_file($calling,$called,$node,$port,$userfile){
        exec("/usr/local/dicom/bin/send_image -a $calling -c $called $node $port $userfile");
        return ;
}

function read_dicom_tag($file,$tag){
	global $dckey;
        $tag_info = exec("$dckey -k $tag $file");
	echo $tag_info;
        return $tag_info;
}



function dcmtojpg_sum1($from_file){
global $convert,$dcm2pnm;
$dir=dirname(escapeshellcmd($from_file));
$jpg_file=($from_file . "-sum.jpg");//print $jpg_file;
system("$dcm2pnm +oj +Wm $from_file $jpg_file");
return $jpg_file;
}

function dcmtojpg_sum1_w($from_file,$wl,$ww){
global $convert,$dcm2pnm;
$dir=dirname(escapeshellcmd($from_file));
$jpg_file=($from_file . "-sum_w.jpg");//print $jpg_file;
system("$dcm2pnm +oj +Ww $wl $ww $from_file $jpg_file");
return $jpg_file;
}
?>

