Image_GIS2
[ class tree: Image_GIS2 ] [ index: Image_GIS2 ] [ all elements ]

Source for file Renderer.php

Documentation is available at Renderer.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: Image :: GIS :: Renderer Base Class                            |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Jan Kneschke <jan@kneschke.de> and             |
  7. // |                         Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  8. // +------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,        |
  10. // | that is available at http://www.php.net/license/3_0.txt.               |
  11. // | If you did not receive a copy of the PHP license and are unable to     |
  12. // | obtain it through the world-wide-web, please send a note to            |
  13. // | license@php.net so we can mail you a copy immediately.                 |
  14. // +------------------------------------------------------------------------+
  15. //
  16. // $Id$
  17. //
  18.  
  19. require_once 'Image/Color.php';
  20.  
  21. /**
  22.  * Renderer Base Class.
  23.  *
  24.  * @author      Jan Kneschke <jan@kneschke.de>
  25.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  26.  * @copyright   Copyright &copy; 2002-2005 Jan Kneschke <jan@kneschke.de> and Sebastian Bergmann <sb@sebastian-bergmann.de>
  27.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  28.  * @category    Image
  29.  * @package     Image_GIS2
  30.  * @abstract
  31.  */
  32. abstract class Image_GIS2_Renderer {
  33.     /**
  34.     * Set to TRUE to enable debugging.
  35.     *
  36.     * @var boolean $debug 
  37.     */
  38.     protected $debug;
  39.  
  40.     /**
  41.     * @var array $min 
  42.     */
  43.     protected $min = false;
  44.  
  45.     /**
  46.     * @var array $max 
  47.     */
  48.     protected $max = false;
  49.  
  50.     /**
  51.     * Width of the image.
  52.     *
  53.     * @var integer $width 
  54.     */
  55.     protected $width;
  56.  
  57.     /**
  58.     * Height of the image.
  59.     *
  60.     * @var integer $height 
  61.     */
  62.     protected $height;
  63.  
  64.     /**
  65.     * Constructor.
  66.     *
  67.     * @param  mixed   $width 
  68.     * @param  integer $height 
  69.     * @param  boolean $debug 
  70.     * @access public
  71.     */
  72.     public function Image_GIS2_Renderer($width$height$debug{
  73.         $this->debug  = $debug;
  74.  
  75.         if ($width < 0 ||
  76.             $width > 2048{
  77.             $width = 640;
  78.         }
  79.  
  80.         if ($height < 0 ||
  81.             $height > 2048{
  82.             $height = 480;
  83.         }
  84.  
  85.         $this->width  = $width;
  86.         $this->height = $height;
  87.    }
  88.  
  89.     /**
  90.     * Factory.
  91.     *
  92.     * @param  string  $renderer 
  93.     * @param  mixed   $width 
  94.     * @param  integer $height 
  95.     * @param  boolean $debug 
  96.     * @return object 
  97.     * @access public
  98.     */
  99.     public static function factory($renderer$width$height$debug{
  100.         if (@include_once('Image/GIS2/Renderer/' $renderer '.php')) {
  101.             $class  'Image_GIS2_Renderer_' $renderer;
  102.             $object = new $class($width$height$debug);
  103.  
  104.             return $object;
  105.         }
  106.     }
  107.  
  108.     /**
  109.     * Draws a clipped line from ($x1, $y1) to ($x2, $y2)
  110.     * using $color.
  111.     *
  112.     * @param  float $x1 
  113.     * @param  float $y1 
  114.     * @param  float $x2 
  115.     * @param  float $y2 
  116.     * @param  mixed $color 
  117.     * @access public
  118.     */
  119.     public function drawClippedLine($x1$y1$x2$y2$color{
  120.         if (($x1 $this->max['x']  ||
  121.              $x1 $this->min['x']  ||
  122.              $y1 $this->max['y']  ||
  123.              $y1 $this->min['y']&&
  124.             ($x2 $this->max['x']  ||
  125.              $x2 $this->min['x']  ||
  126.              $y2 $this->max['y']  ||
  127.              $y2 $this->min['y'])) {
  128.             if ($this->debug{
  129.                 printf('clipped x1: %d %d %d<br />'$x1$this->min['x']$this->max['x']);
  130.                 printf('clipped y1: %d %d %d<br />'$y1$this->min['y']$this->max['y']);
  131.                 printf('clipped x2: %d %d %d<br />'$x2$this->min['x']$this->max['x']);
  132.                 printf('clipped y2: %d %d %d<br />'$y2$this->min['y']$this->max['y']);
  133.             }
  134.         else {
  135.             if (!is_array($color)) {
  136.                 $color = Image_Color::namedColor2RGB($color);
  137.             }
  138.  
  139.             $x1 $this->polar2image($x1'x');
  140.             $y1 $this->polar2image($y1'y');
  141.             $x2 $this->polar2image($x2'x');
  142.             $y2 $this->polar2image($y2'y');
  143.  
  144.             if ($this->debug{
  145.                 printf('Drawing line (%s, %s) -> (%s, %s)<br />'$x1$y1$x2$y2);
  146.             }
  147.  
  148.             $this->drawLine($x1$y1$x2$y2$color[0]$color[1]$color[2]);
  149.         }
  150.     }
  151.  
  152.     /**
  153.     * Returns the range of the data to be rendered.
  154.     *
  155.     * @return array 
  156.     * @access public
  157.     */
  158.     public function getRange({
  159.         return array(
  160.           $this->min['x'],
  161.           $this->max['x'],
  162.           $this->min['y'],
  163.           $this->max['y']
  164.         );
  165.     }
  166.  
  167.     /**
  168.     * Converts a polar coordinate to an image coordinate.
  169.     *
  170.     * @param  float  $polarCoordinate 
  171.     * @param  string $direction 
  172.     * @access public
  173.     */
  174.     public function polar2image($polarCoordinate$direction{
  175.         switch ($direction{
  176.             case 'x'{
  177.                 return ($polarCoordinate $this->min[$direction]*
  178.                        ($this->width / ($this->max[$direction$this->min[$direction]));
  179.             }
  180.             break;
  181.  
  182.             case 'y'{
  183.                 return ($polarCoordinate $this->max[$direction]*
  184.                        ($this->height / ($this->min[$direction$this->max[$direction]));
  185.             }
  186.             break;
  187.         }
  188.     }
  189.  
  190.     /**
  191.     * Renders the image.
  192.     *
  193.     * @param  array $lineSets 
  194.     * @access public
  195.     */
  196.     public function render($lineSets{
  197.         if ($this->min == false || $this->max == false{
  198.             foreach ($lineSets as $lineSet{
  199.                 if ($this->min == false{
  200.                     $this->min['x'$lineSet->min['x'];
  201.                     $this->min['y'$lineSet->min['y'];
  202.                     $this->max['x'$lineSet->max['x'];
  203.                     $this->max['y'$lineSet->max['y'];
  204.                 else {
  205.                     $this->min['x'min($this->min['x']$lineSet->min['x']);
  206.                     $this->min['y'min($this->min['y']$lineSet->min['y']);
  207.                     $this->max['x'max($this->max['x']$lineSet->max['x']);
  208.                     $this->max['y'max($this->max['y']$lineSet->max['y']);
  209.                 }
  210.             }
  211.         }
  212.  
  213.         foreach ($lineSets as $lineSet{
  214.             foreach ($lineSet->lines as $line{
  215.                 $this->drawClippedLine($line[0]$line[1]$line[2]$line[3]$lineSet->color);
  216.             }
  217.         }
  218.     }
  219.  
  220.     /**
  221.     * Sets the range of the data to be rendered.
  222.     *
  223.     * @param  float $x1 
  224.     * @param  float $x2 
  225.     * @param  float $y1 
  226.     * @param  float $y2 
  227.     * @access public
  228.     */
  229.     public function setRange($x1$x2$y1$y2{
  230.         $this->min = array('x' => $x1'y' => $y1);
  231.         $this->max = array('x' => $x2'y' => $y2);
  232.     }
  233.  
  234.     /**
  235.     * Draws a line from ($x1, $y1) to ($x2, $y2)
  236.     * using the color rgb($r, $g, $b).
  237.     *
  238.     * @param  float   $x1 
  239.     * @param  float   $y1 
  240.     * @param  float   $x2 
  241.     * @param  float   $y2 
  242.     * @param  float   $r 
  243.     * @param  float   $g 
  244.     * @param  float   $b 
  245.     * @access public
  246.     * @abstract
  247.     */
  248.     public abstract function drawLine($x1$y1$x2$y2$r$g$b);
  249.  
  250.     /**
  251.     * Saves the rendered image to a given file.
  252.     *
  253.     * @param  string  $filename 
  254.     * @return boolean 
  255.     * @access public
  256.     * @abstract
  257.     */
  258.     public abstract function saveImage($filename);
  259.  
  260.     /**
  261.     * Shows the rendered image.
  262.     *
  263.     * @access public
  264.     * @abstract
  265.     */
  266.     public abstract function showImage();
  267. }
  268. ?>

Documentation generated on Mon, 11 Mar 2019 15:48:42 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.