<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
<title>make volume and star rendering</title>

<script type="text/javascript"><!--

var src ='';

function change_loc() {
location.href = src;
}
// --></script>

<meta http-equiv=content-type content="text/html; charset=x-euc-jp">
</head>
<body BACKGROUND="./img/yellow.jpg" onload=change_loc();>


<H1>start to make volume data</H1><br>
<?php

// onload=change_loc();

//
//	2007.10.04
//	Made by tadashi tsuchimoto @ MITT
//	make_jpeg_pack.php
//

// parameter

@$patid = $_GET['patid'];
@$patname_mod = $_GET['patname_mod'];
@$size = $_GET['size'];
@$browser = $_GET['browser'];
@$scwidth = $_GET['scwidth'];
@$scheight = $_GET['scheight'];
@$userid = $_GET['userid'];
@$permission = $_GET['permission'];
@$study_uid = $_GET['study_uid'];
@$series_uid = $_GET['series_uid'];
@$date = $_GET['date'];







  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);



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




$rtn_image->execute();


$loc_mmtfp = "../mmtfp/";
$make_volume_jar = "MakeVolume.jar";
$AC_OETags = "AC_OETags.js";
$historyhtm = "history.htm";
$historyjs = "history.js";
$historyswf = "history.swf";
$playerProductInstallswf = "playerProductInstall.swf";
$ViewVolumeswf = "ViewVolume.swf";
$indexhtml = "index.html";


$make_volume_jar_path = $loc_mmtfp . $make_volume_jar;
$AC_OETags_path = $loc_mmtfp . $AC_OETags;
$historyhtm_path = $loc_mmtfp . $historyhtm;
$historyjs_path = $loc_mmtfp . $historyjs;
$historyswf_path = $loc_mmtfp . $historyswf;
$playerProductInstallswf_path = $loc_mmtfp . $playerProductInstallswf;
$ViewVolumeswf_path = $loc_mmtfp . $ViewVolumeswf;
$indexhtml_path = $loc_mmtfp . $indexhtml;



@$target_dir=touch_dir($study_uid,$series_uid,"vol","mmtfp");
$target_path=($target_dir . "/" );   

$make_volume_jar_cp_path = $target_path . $make_volume_jar;
$AC_OETags_cp_path = $target_path . $AC_OETags;
$historyhtm_cp_path = $target_path . $historyhtm;
$historyjs_cp_path = $target_path . $historyjs;
$historyswf_cp_path = $target_path . $historyswf;
$playerProductInstallswf_cp_path = $target_path . $playerProductInstallswf;
$ViewVolumeswf_cp_path = $target_path . $ViewVolumeswf;
$indexhtml_cp_path = $target_path . $indexhtml;

//print("$make_volume_jar_path $make_volume_jar_cp_path <br>");
//print("$indexhtml_cp_path <br>");

@copy ($make_volume_jar_path,$make_volume_jar_cp_path);
@chmod ($make_volume_jar_cp_path, 0555);

@copy ($make_volume_jar_path,$make_volume_jar_cp_path);
@copy ($AC_OETags_path,$AC_OETags_cp_path);
@copy ($historyhtm_path,$historyhtm_cp_path);
@copy ($historyjs_path,$historyjs_cp_path);
@copy ($historyswf_path,$historyswf_cp_path);
@copy ($playerProductInstallswf_path,$playerProductInstallswf_cp_path);
@copy ($ViewVolumeswf_path,$ViewVolumeswf_cp_path);
@copy ($indexhtml_path,$indexhtml_cp_path);

chdir($target_path);
//system("pwd");



$file_arg = "";

$pixel_spacing_x = "";
$pixel_spacing_y = "";
$pixel_spacing_thick = "";

$first_load_tag = "first";
$power = 1;
$negate = 0;
$rotate = 0;
$wl = 0;
$ww = 4096;

$z_position = "";
$z_position_sample1 = "";
$z_position_sample2 = "";

$i = 0;
  while ($image_row = $rtn_image->fetch(PDO::FETCH_ASSOC)) {

	$path = trim($image_row['path']) ;
	$path = $file_root_path . $path;
	//print("$path<br>");

$file_name=basename($path);
$target_file_path =  $file_name . ".dcm";
$file_arg = $file_arg . $file_name . ".dcm.jpg" . "\n";
	if($pixel_spacing_x == ""){
	$pixel_value = get_dicom_value("0028,0030",$path);
	$values = explode("\\", $pixel_value);
	$pixel_spacing_x = 1 / trim($values[0]);
	$pixel_spacing_y = 1 / trim($values[1]);
	$pixel_spacing_thick =   trim(get_dicom_value("0018,0050",$path));
	}
	
	if($i == "0"){
	$table_position = trim(get_dicom_value("0020,0032",$path));
	print("$table_position <br>");
	$table_array = explode("\\",$table_position);
	$z_position_sample1 = $table_array[2];
	}
	
	if($i == "1"){
	$table_position = trim(get_dicom_value("0020,0032",$path));
	print("$table_position <br>");
	$table_array = explode("\\",$table_position);
	$z_position_sample2 = $table_array[2];
	$z_position_abs = abs(abs($z_position_sample1) - abs($z_position_sample2));
	$z_position =  1 / $z_position_abs;
	}


	//Symbolic link original file to target files. 
	if(is_link($target_file_path)){
		if($path != linkinfo($target_file_path)){
		//system("rm -f $target_file_path");
		unlink($target_file_path);
		symlink($path,$target_file_path);
		}
	}
	else{
		symlink($path,$target_file_path);
	}
	
	$cache_check =   $file_name . ".dcm.jpg";
//	print("$cache_check <br>");
	if(!file_exists($cache_check)){
	print("now converting images (number $i) <br>");
	$jpg_file_name=dcmtojpg2_set($target_file_path,$ww,$wl);
	}else{
//	print("I hava cache $cache_check. <br>");
	}
	$i++;

}

touch("list");
$fp=fopen("list","w+");
fwrite($fp,"$file_arg");
fclose($fp);

print("resolution of X from DICOM file:$pixel_spacing_x pixels/mm orig_val:$values[0] mm/pixel<br>
resolution of Y from DICOM file:$pixel_spacing_y pixels/mm orig_val:$values[1] mm/pixel<br>
resolution of Z from DICOM file:$z_position pixels/mm orig_val:$z_position_abs interslice distance(mm)<br>
pixel spacing with thick:$pixel_spacing_thick <br>
");



if(!file_exists("out.bd")){
system("java -Xmx800m -jar MakeVolume.jar list -o out.bd -r $pixel_spacing_x $pixel_spacing_y $z_position 2>&1");
}


//debug
//print("java -Xmx800m -jar MakeVolume.jar list -o out.bd -r $pixel_spacing_x $pixel_spacing_y $z_position ");



?>

<script type="text/javascript"><!--
src = '<?php echo $indexhtml_cp_path?>';
// --></script>

</body>
</html>
