Advertisement
Guest User

Untitled

a guest
Dec 21st, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.60 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Benchmark for http://www.tophatstuff.co.uk/?p=119
  5.    
  6.     Using a Paeth pre-compression filter as described by
  7.     http://www.gnupdf.org/PNG_and_TIFF_Predictors_Filter#Filter_type_4:_Paeth
  8.     http://www.w3.org/TR/PNG-Filters.html
  9.  
  10.  
  11.   Test with:
  12.  
  13.   ./locality.php
  14.   ./locality.php?XFIRST
  15.    
  16. */
  17.  
  18.  
  19.  
  20.     define("WIDTH", 512);
  21.     define("ITERATIONS", 3);
  22.  
  23.  
  24.  
  25.     function PaethPredictor($a, $b, $c)
  26.     {
  27.  
  28.         // These apply to bytes.
  29.  
  30.         // a = left, b = above, c = upper left
  31.         $p = $a + $b - $c;        // initial estimate
  32.         $pa = abs($p - $a);      // distances to a, b, c
  33.         $pb = abs($p - $b);
  34.         $pc = abs($p - $c);
  35.        
  36.         // return nearest of a,b,c,
  37.         // breaking ties in order a,b,c.
  38.         if (($pa <= $pb) && ($pa <= $pc)) { return $a; }
  39.         else if ($pb <= $pc) { return $b; }
  40.         else { return $c; }
  41.     }
  42.      
  43.  
  44.     function TestOutput($data, $w, $h)
  45.     {
  46.  
  47.         if (WIDTH < 8)
  48.         {
  49.             die("WIDTH must be at least 8");
  50.         }
  51.  
  52.         printf("TestOutput:\n");
  53.  
  54.         for ($y = 0; $y < 8; $y++)
  55.         {
  56.             for ($x = 0; $x < 8; $x++)
  57.             {
  58.                 $i = ($y * $w) + $x;
  59.                
  60.                 echo str_pad($data[$i], 3, " ", STR_PAD_LEFT);
  61.             }
  62.             echo("<br />\n");
  63.         }
  64.        
  65.         echo("<br /><br />\n\n");
  66.     }
  67.  
  68.  
  69.  
  70.     function main()
  71.     {
  72.        
  73.        
  74.         /* --- SETUP --- */
  75.        
  76.         $w = $h = WIDTH;
  77.         $size  = $w * $h;
  78.        
  79.         $orig = array();
  80.         $orig = array_pad($orig, $size, 0);
  81.        
  82.         $filter = array();
  83.         $filter = array_pad($filter, $size, 0);
  84.        
  85.         if (count($orig) != $size) { die("err orig is ".count($orig).
  86.             " (want $size)"); }
  87.         if (count($filter) != $size) { die("err filter"); }
  88.        
  89.        
  90.         /* fill with numbers typical of an image */
  91.         for ($i = 0; $i < $size; $i++)
  92.         {
  93.             $seq_length = rand() % 80;
  94.             $seq_randomness = 1 + (rand() % 10);
  95.             $seq_base = rand() % 10;
  96.            
  97.             for ($j = $i; ($j < $size) && ($j < ($i + $seq_length)); $j++)
  98.             {
  99.                 $orig[$j] = $seq_base + (rand() % $seq_randomness);
  100.             }
  101.             $i = $j;
  102.         }
  103.        
  104.         echo "Original:<br />\n";
  105.         TestOutput($orig, $w, $h);
  106.        
  107.        
  108.         /* --- MAIN --- */
  109.         $start = MicroTime(TRUE);
  110.        
  111.         /* --- Precompression filter (this is what we are timing) --- */
  112.        
  113.         for ($i = 0; $i < ITERATIONS; $i++)
  114.         {
  115.        
  116.             /* fill x=0 and y=0 rows */
  117.             for ($y = 0; $y < $h; $y++)
  118.             {
  119.                 $index = $y * $w;
  120.                 $filter[$index] = $orig[$index];
  121.             }
  122.             for ($x = 0; $x < $w; $x++)
  123.             {
  124.                 $filter[$x] = $orig[$x];
  125.             }
  126.        
  127.             /* start from 1,1 */
  128.             if (isset($_GET['XFIRST']))
  129.             {
  130.                 for ($x = 1; $x < $w; $x++)
  131.                 {
  132.                     for ($y = 1; $y < $h; $y++)
  133.                     {
  134.                         // a = left, b = above, c = upper left
  135.                         $index = ($y * $w) + $x;
  136.                         $a = $index - 1;
  137.                         $b = (($y-1) * $w) + $x;
  138.                         $c = $b - 1;
  139.                        
  140.                         $filter[$index] = $orig[$index] -
  141.                             PaethPredictor($orig[$a], $filter[$b], $filter[$c]);
  142.                     }
  143.                 }
  144.                
  145.             }
  146.             else
  147.             {
  148.                 for ($y = 1; $y < $h; $y++)
  149.                 {
  150.                     for ($x = 1; $x < $w; $x++)
  151.                     {
  152.                         // a = left, b = above, c = upper left
  153.                         $index = ($y * $w) + $x;
  154.                         $a = $index - 1;
  155.                         $b = (($y-1) * $w) + $x;
  156.                         $c = $b - 1;
  157.                        
  158.                         $filter[$index] = $orig[$index] -
  159.                             PaethPredictor($orig[$a], $filter[$b], $filter[$c]);
  160.                     }
  161.                 }
  162.             }
  163.            
  164.         }
  165.        
  166.         $end = MicroTime(TRUE);
  167.      
  168.         echo "Filtered:<br />\n";
  169.         TestOutput($filter, $w, $h);
  170.        
  171.  
  172.         /* --- Verify by undoing the filter --- */
  173.          
  174.         for ($i = 0; $i < ITERATIONS; $i++)
  175.         {
  176.        
  177.             /* start from 1,1 */
  178.             for ($y = 1; $y < $h; $y++)
  179.             {
  180.                 for ($x = 1; $x < $w; $x++)
  181.                 {
  182.                     // a = left, b = above, c = upper left
  183.                     $index = ($y * $w) + $x;
  184.                     $a = $index - 1;
  185.                     $b = (($y-1) * $w) + $x;
  186.                     $c = $b - 1;
  187.                    
  188.                     $orig[$index] = $filter[$index] +
  189.                         PaethPredictor($orig[$a], $filter[$b], $filter[$c]);
  190.                 }
  191.             }
  192.            
  193.         }
  194.      
  195.         echo "Unfilted: This should be back to the original!<br />\n";
  196.         TestOutput($orig, $w, $h);
  197.        
  198.        
  199.          
  200.             echo("w=".$w.", wxh=".($w*$h).", ".($end-$start)." secs<br />\n");
  201.            
  202.             if (isset($_GET['XFIRST']))
  203.             {
  204.             echo "This should be the slow version! XFIRST is defined\n";
  205.             }
  206.  
  207.         return 0;
  208.     }
  209.  
  210.     main();
  211.    
  212.  
  213. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement