00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 function out1x1gif() {
00046 header("Content-Type: image/gif");
00047 header("Content-Length: 35\n");
00048 echo "GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00".
00049 "\xff\xff\xff\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x4c".
00050 "\x01\x00\x3b";
00051 }
00052 function myval ($s) {
00053 $s=trim($s,"\" ");
00054 if (strtoupper(substr($s,0,2))=="0X") return intval(hexdec($s));
00055 else return intval($s);
00056 }
00057
00063 function decodeSet ($encoded_set) {
00064 global $set,$allNative;
00065 $set=array();
00066 $frame=0;
00067 if (!$encoded_set) return;
00068 $done=explode("/",rtrim($encoded_set,'/'));
00069 foreach ($done as $term) {
00070 $term=explode(":",$term);
00071 if (count($term)==1) {
00072 $frame=$term[0];
00073 } else {
00074 if (($term[1]!="undefined") && ($term[1]!="NaN") && ($term[1]!=666)) {
00075
00076 $set[$term[0]]= array('value'=>$term[1], 'delay'=>$frame, 'native'=>abstractToNative($term[0]));
00077 foreach ($set[$term[0]]['native'] as $nat=>$natVal) $allNative[$nat]=0;
00078 }
00079 }
00080 }
00081 }
00082
00087 function fixRGBScales() {
00088 global $set,$allNative;
00089 $scales=((array_key_exists('rscale',$set))?1:0) |
00090 ((array_key_exists('bscale',$set))?2:0) |
00091 ((array_key_exists('gscale',$set))?4:0);
00092 if ($scales==0) return;
00093 else if ($scales==7) {
00094 $set['bscale']['delay']= $set['rscale']['delay'];
00095 $set['gscale']['delay']= $set['rscale']['delay'];
00096 } else {
00097 if ($scales & 1) unset ($set['rscale']);
00098 if ($scales & 2) unset ($set['bscale']);
00099 if ($scales & 4) unset ($set['gscale']);
00100 }
00101 }
00102
00103 function fixGammaBlack() {
00104 global $set;
00105 $gampxl=((array_key_exists('gam',$set))?1:0) |
00106 ((array_key_exists('pxl',$set))?2:0);
00107 if ($gampxl==0) return;
00108 else if ($gampxl==3) {
00109 $set['pxl']['delay']= $set['gam']['delay'];
00110 elphel_gamma_add ((int)$set['gam'], (int) $set['pxl']);
00111 } else {
00112 if ($gampxl & 1) unset ($set['gam']);
00113 if ($gampxl & 2) unset ($set['pxl']);
00114 }
00115 }
00116
00117
00119 function prepSetNative() {
00120 global $set,$allNative,$setNative;
00121 $setNative=array();
00122 foreach ($set as $absPar=>$value) {
00123 $delay= $value['delay'];
00124 $natArr=$value['native'];
00125 if (!array_key_exists($delay,$setNative)) $setNative[$delay]=array();
00126 switch ($absPar) {
00128 case "rscale":
00129 $rs= (float) $set['rscale']['value'];
00130 $gs= 1.0;
00131 $bs= (float) $set['bscale']['value'];
00132 $gbs=(float) $set['gscale']['value'];
00134 $minScale=min($rs,$bs,$gs,$gbs);
00135 if ($minScale>0) {
00136 if ($minScale<1.0) {
00137 $k=1.0/$minScale;
00138 if ($rs > $minScale) $rs *=$k; else $rs= 1.0;
00139 if ($bs > $minScale) $bs *=$k; else $bs= 1.0;
00140 if ($gs > $minScale) $gs *=$k;
00141 if ($gbs > $minScale) $gbs*=$k; else $gbs=1.0;
00142 }
00143 $setNative[$delay]["GTAB_R__1600"] =(int) round($natArr["GTAB_R__1600"] * $rs);
00144 $setNative[$delay]["GTAB_G__1600"] =(int) round($natArr["GTAB_G__1600"] * $gs);
00145 $setNative[$delay]["GTAB_B__1600"] =(int) round($set['bscale']['native']["GTAB_B__1600"] * $bs);
00146 $setNative[$delay]["GTAB_GB__1600"]=(int) round($set['gscale']['native']["GTAB_GB__1600"] * $gbs);
00147 }
00148 break;
00149 case "bscale":
00150 case "gscale":
00151 break;
00152 case "gam":
00153 $k= (float) $value['value'];
00154 $setNative[$delay]["GTAB_R__0816"] =(int) round($natArr["GTAB_R__0816"] * $k);
00155 $setNative[$delay]["GTAB_G__0816"] =(int) round($natArr["GTAB_G__0816"] * $k);
00156 $setNative[$delay]["GTAB_B__0816"] =(int) round($natArr["GTAB_B__0816"] * $k);
00157 $setNative[$delay]["GTAB_GB__0816"]=(int) round($natArr["GTAB_GB__0816"] * $k);
00158 break;
00159 case "pxl":
00160 $k= (float) $value['value'];
00161 $setNative[$delay]["GTAB_R__0824"] =(int) round($natArr["GTAB_R__0824"] * $k);
00162 $setNative[$delay]["GTAB_G__0824"] =(int) round($natArr["GTAB_G__0824"] * $k);
00163 $setNative[$delay]["GTAB_B__0824"] =(int) round($natArr["GTAB_B__0824"] * $k);
00164 $setNative[$delay]["GTAB_GB__0824"]=(int) round($natArr["GTAB_GB__0824"] * $k);
00165 break;
00166 default:
00168 $scale=reset ($natArr);
00169 if (is_array($scale)) {
00170 $val=array_search ($value['value'],$scale);
00171
00172 if ($val !== false) $setNative[$delay][key($natArr)]= (int) $val;
00173 } else $setNative[$delay][key($natArr)]= (int) round(((float) $value['value']) * $scale);
00174 break;
00175 }
00176 }
00177
00178 foreach ($set as $absPar=>$value) {
00179 $delay= $value['delay'];
00180 $natArr=$value['native'];
00181 foreach ($natArr as $natName => $natValue) {
00182 $set[$absPar]['native'][$natName]=$setNative[$delay][$natName];
00183 }
00184 }
00185 }
00186 function applySetNative($ahead) {
00187 global $set,$setNative,$pgmFrameNumber;
00188 $pgmFrameNumber=elphel_get_frame()+$ahead;
00190 foreach ($setNative as $since=>$pgmpars) {
00191 elphel_set_P_arr ($pgmpars, $pgmFrameNumber+$since);
00192 }
00193 }
00194
00201 function addGammas() {
00202 global $setNative,$gammas;
00203 $gammas=array();
00204 foreach ($setNative as $pars) {
00205 if (array_key_exists('GTAB_R', $pars)) $gammas[$pars['GTAB_R' ]>>16]=1;
00206 if (array_key_exists('GTAB_G', $pars)) $gammas[$pars['GTAB_G' ]>>16]=1;
00207 if (array_key_exists('GTAB_GB',$pars)) $gammas[$pars['GTAB_GB']>>16]=1;
00208 if (array_key_exists('GTAB_B', $pars)) $gammas[$pars['GTAB_B' ]>>16]=1;
00209
00210 if (array_key_exists('GTAB_R__0816', $pars)) $gammas[$pars['GTAB_R__0816'] | ($pars['GTAB_R__0824'] << 8)]=1;
00211 if (array_key_exists('GTAB_G__0816', $pars)) $gammas[$pars['GTAB_G__0816'] | ($pars['GTAB_G__0824'] << 8)]=1;
00212 if (array_key_exists('GTAB_GB__0816', $pars)) $gammas[$pars['GTAB_GB__0816'] | ($pars['GTAB_GB__0824'] << 8)]=1;
00213 if (array_key_exists('GTAB_B__0816', $pars)) $gammas[$pars['GTAB_B__0816'] | ($pars['GTAB_B__0824'] << 8)]=1;
00214 }
00215
00216 foreach ($gammas as $gamma_black=>$whatever) {
00217 $black=($gamma_black>>8) & 0xff;
00218 $gamma=($gamma_black & 0xff)*0.01;
00219 elphel_gamma_add ($gamma, $black);
00220 }
00221 }
00222
00223 function abstractToNative($absPar) {
00224 switch ($absPar) {
00225 case "e": return array ("EXPOS"=>1000);
00226 case "ve": return array ("VEXPOS"=>1);
00227 case "fps": return array ("FP1000S"=>1000);
00228 case "fpslm": return array ("FP1000SLIM"=>1000);
00229 case "fpsflags": return array ("FPSFLAGS"=>1);
00230 case "color": return array ("COLOR"=>1);
00231 case "fliph": return array ("FLIPH"=>1);
00232 case "flipv": return array ("FLIPV"=>1);
00233 case "ww": return array ("WOI_WIDTH"=>1);
00234 case "wh": return array ("WOI_HEIGHT"=>1);
00235 case "wl": return array ("WOI_LEFT"=>1);
00236 case "wt": return array ("WOI_TOP"=>1);
00237 case "aw": return array ("ACTUAL_WIDTH"=>1);
00238 case "ah": return array ("ACTUAL_HEIGHT"=>1);
00239 case "dh": return array ("DCM_HOR"=>1);
00240 case "dv": return array ("DCM_VERT"=>1);
00241 case "bh": return array ("BIN_HOR"=>1);
00242 case "bv": return array ("BIN_VERT"=>1);
00243 case "iq": return array ("QUALITY"=>1);
00244 case "gr": return array ("GAINR"=>256);
00245 case "gg": return array ("GAING"=>256);
00246 case "ggb": return array ("GAINGB"=>256);
00247 case "gb": return array ("GAINB"=>256);
00248 case "rscale":return array ("GTAB_R__1600"=>1024, "GTAB_G__1600"=>1024);
00249 case "bscale":return array ("GTAB_B__1600"=>1024, "GTAB_G__1600"=>1024);
00250 case "gscale":return array ("GTAB_GB__1600"=>1024,"GTAB_G__1600"=>1024);
00251 case "bit": return array ("BIT"=>1);
00252 case "gam": return array ("GTAB_R__0816"=>100,"GTAB_G__0816"=>100,"GTAB_GB__0816"=>100,"GTAB_B__0816"=>100);
00253 case "pxl": return array ("GTAB_R__0824"=>1, "GTAB_G__0824"=> 1,"GTAB_GB__0824"=> 1,"GTAB_B__0824"=> 1);
00254 case "csb": return array ("COLOR_SATURATION_BLUE"=>100);
00255 case "csr": return array ("COLOR_SATURATION_RED"=>100);
00256 case "comp_run": return array ("COMPRESSOR_RUN"=>array("stop","single","run"));
00257 case "sens_run": return array ("SENSOR_RUN"=>array("stop","single","run"));
00258 case "sensor": return array ("SENSOR"=>array(4 => "ZR32112", 8 => "ZR32212",32 => "KAC1310",36 => "KAC5000",48 => "MI1300",49 => "MT9M001",50 => "MT9D001",51 => "MT9T001",52 => "MT9P001",64 => "IBIS51300"));
00259 case "decXmask": return array ("SENSOR"=>array(4 => 139, 8 => 139, 32 => 32907, 36 => 15, 48 => 139, 49 => 139, 50 => 139, 51 => 255, 52 => 255, 64 => 0));
00260 case "decYmask": return array ("SENSOR"=>array(4 => 139, 8 => 139, 32 => 32907, 36 => 13, 48 => 139, 49 => 139, 50 => 139, 51 => 255, 52 => 255, 64 => 0));
00261 case "binXmask": return array ("SENSOR"=>array(4 => 0, 8 => 0, 32 => 0, 36 => 3, 48 => 0, 49 => 0, 50 => 0, 51 => 255, 52 => 255, 64 => 0));
00262 case "binYmask": return array ("SENSOR"=>array(4 => 0, 8 => 0, 32 => 0, 36 => 3, 48 => 0, 49 => 0, 50 => 0, 51 => 255, 52 => 255, 64 => 0));
00263 case "hrw": return array ("HISTWND_RWIDTH"=>0x10000);
00264 case "hrh": return array ("HISTWND_RHEIGHT"=>0x10000);
00265 case "hrl": return array ("HISTWND_RLEFT"=>0x10000);
00266 case "hrt": return array ("HISTWND_RTOP"=>0x10000);
00267 default: return elphel_parse_P_name($absPar)? array ($absPar=>1):array();
00268 }
00269 }
00270
00271
00272 function abstractValue($absPar) {
00273 global $get,$allNative;
00274 switch ($absPar) {
00275 case "rscale": return $allNative["GTAB_R__1600"]/$allNative["GTAB_G__1600"];
00276 case "bscale": return $allNative["GTAB_B__1600"]/$allNative["GTAB_G__1600"];
00277 case "gscale": return $allNative["GTAB_GB__1600"]/$allNative["GTAB_G__1600"];
00278 case "gam":
00279 return $allNative["GTAB_G__0816"]/$get[$absPar]["GTAB_G__0816"];
00280 case "pxl":
00281 return $allNative["GTAB_G__0824"]/$get[$absPar]["GTAB_G__0824"];
00282 default:
00283 $value=reset ($get[$absPar]);
00284 if (is_array($value)) return $value[$allNative[key($get[$absPar])]];
00285 else return $allNative[key($get[$absPar])]/$value;
00286 }
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00303 function decodeGet ($encoded_get) {
00304 global $get,$allNative;
00305 if (!$encoded_get) return array();
00306 $encoded_get=explode("/",trim($encoded_get,'/'));
00307 foreach ($encoded_get as $name) {
00308 $get[$name]=abstractToNative($name);
00309 foreach ($get[$name] as $key=>$value) $allNative[$key]=0;
00310 }
00311 $allNative['FRAME'] = 0;
00312 }
00314 $MAX_EXECUTION_TIME=20;
00315 $deadline=time()+$MAX_EXECUTION_TIME;
00317 $exif_get=false;
00318 $circbuf_get=false;
00319 $debug_arr=array();
00320 $toRead=array();
00321 $hist_in_thresh=0.0;
00322 $hist_out_thresh=0.0;
00323 $get=array();
00324 $set=array();
00325 $setNative=array();
00326
00327 $allNative=array();
00328 $ahead=3;
00329 foreach($_GET as $key=>$value) {
00330 switch($key) {
00332 case "_time":
00333 $toRead["req_ts"]=$value;
00334 if (elphel_get_fpga_time() > 100000000) break;
00335
00336 case "_stime":
00337 $toRead["req_ts"]=$value;
00338 $a=((float) $value)/1000;
00339 elphel_set_fpga_time ($a);
00340 exec("date -s ".date("mdHiY.s",(int)$a),$out,$ret);
00341 break;
00342 case "imgsrv":
00343 $toRead["imgsrv"]='http:
00344 break;
00345 case "exif":
00346 $exif_get=$value+0;
00347 break;
00348 case "description":
00349 if ( $value!==null) elphel_set_exif_field(0x10e, $value.chr(0));
00350 break;
00351 case "circbuf":
00352 $circbuf_get=true;
00353 break;
00354 case "hist_in_thresh":
00355 $hist_in_thresh=$value+0.0;
00356 break;
00357 case "hist_out_thresh":
00358 $hist_out_thresh=$value+0.0;
00359 break;
00360 case "ahead":
00361 $ahead= myval($value);
00362 break;
00363 case "set":
00364 decodeSet ($value);
00365 break;
00366 case "get":
00367 decodeGet ($value);
00368 break;
00369 case "timeout":
00370 $deadline=time()+$value;
00371 break;
00372
00373 }
00374 }
00375
00376 if (array_key_exists("dbgwait",$_GET)) {
00378 while (elphel_get_P_value(ELPHEL_DEBUG+(1<<21)+(28<<16))) {
00379 elphel_skip_frames(1);
00380 if (time()>$deadline) {
00381 error_log("Aborted due to custom timeout");
00382 exit(1);
00383 }
00384 }
00385 }
00387
00388 $allNative=elphel_get_P_arr($allNative);
00389 $parVal=array();
00390
00391 foreach ($get as $key=>$value) $parVal[$key]= abstractValue($key);
00392 fixRGBScales();
00393 fixGammaBlack();
00394 prepSetNative();
00395 addGammas();
00396 applySetNative($ahead);
00397
00399
00401 if ($_GET["out"]=="gif") {
00402 out1x1gif();
00403 exit (0);
00404 }
00405 if ($_GET["debug"]=="1") {
00406 echo "<pre>\n";
00407 echo "\nparVal\n"; print_r($parVal);
00408 echo "\nallNative\n"; print_r($allNative);
00409 echo "\nget\n"; print_r($get);
00410 echo "\nsetNative\n"; print_r($setNative);
00411 echo "\nset\n"; print_r($set);
00412 echo "\ngammas\n"; print_r($gammas);
00413 echo "</pre>\n";
00414 exit (0);
00415 }
00416 $xml = new SimpleXMLElement("<?xml version='1.0'?><pars/>");
00417 if (count($set)>0) {
00418 $xml->addChild ('set');
00419 foreach ($set as $parName=>$parPars) {
00420 $xParName=$xml->set->addChild ($parName);
00421 $xParName->addChild ('frame',$parPars['delay']+$pgmFrameNumber);
00422 $xNative=$xParName->addChild ('native');
00423 foreach ($parPars['native'] as $natName=>$natValue) {
00424 $xNative->addChild ($natName,$natValue);
00425 }
00426 }
00427 }
00428
00429 if (count($parVal)>0) {
00430 $xml->addChild ('frame',$allNative['FRAME']);
00431 $xml->addChild ('get');
00432 foreach ($parVal as $parName=>$parValue) {
00433 $xml->get->addChild ($parName,$parValue);
00434 }
00435 }
00436
00437 if (count($allNative)>0) {
00438 $xml->addChild ('nativeGot');
00439 foreach ($allNative as $natName=>$natValue) {
00440 $xml->nativeGot->addChild ($natName,$natValue);
00441 }
00442 }
00443
00444
00445
00446
00447 if ($_GET["STATE"]!==NULL) $toRead["STATE"]=elphel_get_state();
00448 if ($_GET["imgsrv"]!==NULL) $toRead["imgsrv"]='http:
00449 foreach ($debug_arr as $key=>$value) {
00450 $xml->addChild ($key,$value);
00451 }
00452 $xml->addChild ("ELPHEL_DEBUG__0128",elphel_get_P_value(ELPHEL_DEBUG+(1<<21)+(28<<16)));
00453 foreach ($toRead as $key=>$value) {
00454 $xml->addChild ($key,$value);
00455 }
00456 if ($exif_get!==false) {
00457 $exif_got=elphel_get_exif_elphel($exif_get);
00458 if ($exif_got) {
00459 $xml->addChild ('Exif');
00460 $xml->Exif->addChild ("Exif_page",$exif_get);
00461 foreach ($exif_got as $key=>$value) {
00462 $xml->Exif->addChild ($key,$value);
00463 }
00464 }
00465 }
00468 if ($hist_in_thresh) {
00469 $xml->addChild ('hist_in');
00470 $xml->hist_in->addChild ('hist_in_thresh',$hist_in_thresh);
00471 $xml->hist_in->addChild ('hist_in_r', elphel_histogram(0,elphel_gamma(0,$hist_in_thresh)));
00472 $xml->hist_in->addChild ('hist_in_g', elphel_histogram(1,elphel_gamma(1,$hist_in_thresh)));
00473 $xml->hist_in->addChild ('hist_in_g2',elphel_histogram(2,elphel_gamma(2,$hist_in_thresh)));
00474 $xml->hist_in->addChild ('hist_in_b', elphel_histogram(3,elphel_gamma(3,$hist_in_thresh)));
00475 }
00477 if ($hist_out_thresh) {
00478 $xml->addChild ('hist_out');
00479 $xml->hist_out->addChild ('hist_out_thresh',$hist_out_thresh);
00480 $xml->hist_out->addChild ('hist_out_r', elphel_histogram(0,$hist_out_thresh));
00481 $xml->hist_out->addChild ('hist_out_g', elphel_histogram(1,$hist_out_thresh));
00482 $xml->hist_out->addChild ('hist_out_g2',elphel_histogram(2,$hist_out_thresh));
00483 $xml->hist_out->addChild ('hist_out_b', elphel_histogram(3,$hist_out_thresh));
00484 }
00486 if ($circbuf_get) {
00487 $xml->addChild ('circbuf');
00488 $circbuf=elphel_get_circbuf_pointers();
00489 if (is_array ($circbuf)) {
00490 $circbuf_count=count($circbuf);
00491 $xml->circbuf->addChild ('circbuf_count',$circbuf_count);
00492 for ($i=0;$i<$circbuf_count;$i++) {
00493 $xml->circbuf->addChild ('frame'.$i);
00494 $xml->circbuf->{'frame'.$i}->addChild ('frame' ,$circbuf[$i]['frame']);
00495 $xml->circbuf->{'frame'.$i}->addChild ('circbuf_pointer',$circbuf[$i]['circbuf_pointer']);
00496 $xml->circbuf->{'frame'.$i}->addChild ('exif_pointer' ,$circbuf[$i]['exif_pointer']);
00497 }
00498 }
00499 }
00500 $rslt=$xml->asXML();
00501 header("Content-Type: text/xml");
00502 header("Content-Length: ".strlen($rslt)."\n");
00503 header("Pragma: no-cache\n");
00504 printf($rslt);
00505
00506 ?>