Index: /trunk/FACT++/www/viewer/index.php
===================================================================
--- /trunk/FACT++/www/viewer/index.php	(revision 17721)
+++ /trunk/FACT++/www/viewer/index.php	(revision 17722)
@@ -119,12 +119,11 @@
         return;
 
-    $data = fread($handle, $size*$count);
-    $data = unpack($format.$count, $data);
+    $binary = fread($handle, $size*$count);
+    $data   = unpack($format.$count, $binary);
 
     return $count==1 ? $data[1] : $data;
 }
 
-// This is to allow for events of ROI=1024
-ini_set("memory_limit", "384M");
+//ini_set("memory_limit", "64M");
 
 //define('E_FATAL',  E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
@@ -162,14 +161,17 @@
     // Read the data and copy it from an associative array to an Array
     // (this is just nicer and seems more logical)
+    //$data   = array();
+    $binary = array();
+    for ($i=0; $i<$evt['numPix']; $i++)
+        $binary[$i] = fread($file, 2*$evt['numRoi']);
+    /*
     $data = array();
     for ($i=0; $i<$evt['numPix']; $i++)
     {
-        //$data[$i] = get($file, "s", $evt['numRoi']);
         $var = get($file, "s", $evt['numRoi']);
-
         $data[$i] = array();
         for ($j=0; $j<$evt['numRoi']; $j++)
             $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
-    }
+    }*/
 }
 
@@ -229,9 +231,27 @@
 
     // This is much faster than the variables option in the constructor
-    $v8->data  = $data;
-    $v8->event = $evt;
-    $v8->clone = function($data) { return $data; };
+
+    $roi = $evt['numRoi'];
+
+    //$v8->data   = $data;
+    //$v8->test   = array();;
+    //$v8->data   = array();
+    $v8->event  = $evt;
+    $v8->clone  = function($data) { return $data; };
+    $v8->unpack = function($i)
+    {
+        global $binary, $roi;
+        $u = unpack("s".$roi, $binary[$i]);
+        $arr = array();
+        for ($i=0; $i<$roi; $i++)
+            $arr[$i] = $u[$i+1]*0.48828125;
+        return $arr;
+    };
+
+    //  10, 445, 91.75 MiB
+    // 376, 720, 91.75 MiB
 
     $rc['startJs'] = microtime(true);
+    $rc['timeJS']  = array();
 
     // Buffer output from javascript
@@ -240,6 +260,18 @@
     try
     {
+
+        // We unpack the data pixel by pixel and copy the array directly to the
+        // Javasscript array. This significantly decreases memory usage because
+        // we need only one of the super memory hungry php arrays of size ROI
+        // instead of 1440.
+        $JS = "$.data = new Array($.event.numPix); for (var i=0; i<$.event.numPix; i++) $.data[i] = $.unpack(i);";
+        $v8->executeString($JS, 'internal');
+
+        $rc['timeJs'][0] = (microtime(true) - $rc['startJs']);
+
         $JS = "function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
         $rc['waveform'] = $v8->executeString($JS, 'proc');
+
+        $rc['timeJs'][1] = (microtime(true) - $rc['startJs']);
 
         if (isset($_POST['source2']))
@@ -270,8 +302,10 @@
     $now = microtime(true);
 
-    $rc['timeJs']  = ($now - $rc['startJs']);
+    $rc['timeJs'][2]  = ($now - $rc['startJs']);
 }
 
 $rc['timePhp'] = ($now - $rc['startPhp']);
+
+$rc['memory'] = memory_get_peak_usage(true)/1024/1024;
 
 // Output result as JSON object
