<?php

//parameter

$studyUID = $_GET['studyUID'];
$seriesUID = $_GET['seriesUID'];
@$remove_recycle = $_GET['remove_recycle'];



$studyUID = trim($studyUID);
$seriesUID = trim($seriesUID);


$study_uid = $studyUID;
$series_uid = $seriesUID;

//tmpディレクトリ作成・取得


$seriesUID = trim($seriesUID);
//$seriesUID = str_replace(" ","",$seriesUID);
$target_file_for_mpeg = $seriesUID . ".wmv";
?>
<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
<title>make dicom packed file</title>
<META HTTP-EQUIV="refresh" content="1;  
 URL=./tmp/<?php echo $study_uid ?>/<?php echo $series_uid ?>//movie/<?php echo $target_file_for_mpeg ?>">
<meta http-equiv=content-type content="text/html; charset=x-euc-jp">
</head>
<body BACKGROUND="./img/yellow.jpg">

<?php




//
//  2007.10.15
//  made by tadashi tsuchimoto @ MITT 
//

  include "init.inc";
  include "param_setting.ini";
  include "sql_generator.ini";
  include "file_mod.inc";
  $include_path=".";
  require("$include_path/dbconnect.ini");


//DB query

$db_handle = db_contact_start($db_type,$target_host,$target_db);

//patient query

$sql = sql_generator("show_image");
$rtn_image = $db_handle->prepare($sql);
$rtn_image->bindParam(1, $seriesUID);
$rtn_image->bindParam(2, $studyUID);



$rtn_image->execute();





@$target_dir=touch_dir($studyUID,$seriesUID,"movie");


$target_path=($target_dir . "/" );

//table position array

$table_set_x = array();
$table_set_y = array();
$table_set_z = array();

$mencoder_cmd = "/usr/local/bin/mencoder ";


$cache_check = $target_path . $target_file_for_mpeg;


if($remove_recycle == 1){
system("rm -rdf $cache_check");
print("delete cache file and remake movie <br>");
}

if (!file_exists($cache_check)) {


	while ($image_row = $rtn_image->fetch(PDO::FETCH_ASSOC)) {
	$path = $image_row['path'] ;
	$sernum = $image_row['sernum'] ;
	$file_name=basename($path);
    $target_file_path_without_dcm=$target_path . $file_name ;
	
	//add dcm4chee root path
	$path = $file_root_path . $path;
	   
    $target_file_path=$target_path . $file_name . ".dcm";

	//Symbolic link original file to target files. 
	if(is_link($target_file_path)){
		if($path != readlink($target_file_path)){
		system("rm -f $target_file_path");
		symlink($path,$target_file_path);
		}
	}
	else{
		symlink($path,$target_file_path);
	}

	
	
	
	$multiframe_tag = get_dicom_value("0028,0008",$target_file_path);
	
	$frame_rate = trim(get_dicom_value("0008,2144",$target_file_path));
	
	if(!$frame_rate or $frame_rate == ""){
	$frame_rate = 15;
	}
	
	if($multiframe_tag and $multiframe_tag != "" ){
	$multiframe_flag = 1;

	}
	

	$dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
	system("$dcm2pnm +Fa +oj  $target_file_path $target_file_path");
	

}



chdir($target_path);

exec("pwd ",$pwd_cmd);
exec("ls -1v ",$ls);

while (list ($key, $val) = each ($pwd_cmd)) {
	$pwd = $val;
}

$target = 0;
while (list ($key, $val) = each ($ls)) {
//    print("$val \n");
        if(fnmatch("*.jpg",$val)){
        if(strlen($target) == 1){
        $target_name = "000" . $target;
        }elseif(strlen($target) == 2){
        $target_name = "00" . $target;
        }elseif(strlen($target) == 3){
        $target_name = "0" . $target;
        }elseif(strlen($target) == 4){
        $target_name =  $target;
        }
		
        $target_name = $target_name . ".jpg";
		rename($val,$target_name);
		//print("$val $target_name<br>");
$target += 1;
        }
}




$mencoder_cmd = $mencoder_cmd . " \"mf://" .  $pwd . "/*.jpg\" -mf fps=$frame_rate -oac lavc -ovc lavc -of lavf -vf harddup -lavcopts vcodec=wmv2:vbitrate=2500:autoaspect:acodec=wmav2:abitrate=128 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -ofps 30000/1001 -o " . $target_file_for_mpeg;




exec("$mencoder_cmd");
exec("rm -rdf *.jpg");

$target_file_mpeg = $target_path . $target_file_for_mpeg;

print("
<CENTER><image src=./img/pgctn.gif><a href=$target_file_mpeg>Click Here to Download converted mpeg file</a>
");


}else{

//print("we have cache");

$target_file_mpeg = $target_path . $target_file_for_mpeg;

print("
<CENTER><image src=./img/pgctn.gif><a href=$target_file_mpeg>Click Here to Download converted mpeg file</a>
");

}

print("<br><br><br><br>");

print("<form action=\"make_mpg.php\">");
print("<input type=hidden name=studyUID value=$studyUID>");
print("<input type=hidden name=seriesUID value=$seriesUID>");
print("<input type=hidden name=remove_recycle value=1>");
print("<input type=\"submit\" value=\"remove cache file (and recreate movie)\">");
print("</form>");
print("<br><br>");

?>

<br><br>


<h3><B><a href=javascript:window.close()>close</a></B></H3>



</CENTER>





</body>
</html>
