home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Image / GIS / Renderer.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  7.5 KB  |  270 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: Image :: GIS :: Renderer Base Class                            |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 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: Renderer.php,v 1.14 2004/07/25 13:56:29 sebastian Exp $
  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 © 2002-2004 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_GIS
  30.  * @abstract
  31.  */
  32. class Image_GIS_Renderer {
  33.     /**
  34.     * Set to TRUE to enable debugging.
  35.     *
  36.     * @var boolean $debug
  37.     */
  38.     var $debug;
  39.  
  40.     /**
  41.     * @var array $min
  42.     */
  43.     var $min = false;
  44.  
  45.     /**
  46.     * @var array $max
  47.     */
  48.     var $max = false;
  49.  
  50.     /**
  51.     * Width of the image.
  52.     *
  53.     * @var integer $width
  54.     */
  55.     var $width;
  56.  
  57.     /**
  58.     * Height of the image.
  59.     *
  60.     * @var integer $height
  61.     */
  62.     var $height;
  63.  
  64.     /**
  65.     * Constructor.
  66.     *
  67.     * @param  mixed   $width
  68.     * @param  integer $height
  69.     * @param  boolean $debug
  70.     * @access public
  71.     */
  72.     function Image_GIS_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.     function &factory($renderer, $width, $height, $debug) {
  100.         if (@include_once('Image/GIS/Renderer/' . $renderer . '.php')) {
  101.             $class  = 'Image_GIS_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.     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.     * @since  Image_GIS 1.0.1
  158.     */
  159.     function getRange() {
  160.         return array(
  161.           $this->min['x'],
  162.           $this->max['x'],
  163.           $this->min['y'],
  164.           $this->max['y']
  165.         );
  166.     }
  167.  
  168.     /**
  169.     * Converts a polar coordinate to an image coordinate.
  170.     *
  171.     * @param  float  $polarCoordinate
  172.     * @param  string $direction
  173.     * @access public
  174.     */
  175.     function polar2image($polarCoordinate, $direction) {
  176.         switch ($direction) {
  177.             case 'x': {
  178.                 return ($polarCoordinate - $this->min[$direction]) *
  179.                        ($this->width / ($this->max[$direction] - $this->min[$direction]));
  180.             }
  181.             break;
  182.  
  183.             case 'y': {
  184.                 return ($polarCoordinate - $this->max[$direction]) *
  185.                        ($this->height / ($this->min[$direction] - $this->max[$direction]));
  186.             }
  187.             break;
  188.         }
  189.     }
  190.  
  191.     /**
  192.     * Renders the image.
  193.     *
  194.     * @param  array $lineSets
  195.     * @access public
  196.     */
  197.     function render($lineSets) {
  198.         if ($this->min == false || $this->max == false) {
  199.             foreach ($lineSets as $lineSet) {
  200.                 if ($this->min == false) {
  201.                     $this->min['x'] = $lineSet->min['x'];
  202.                     $this->min['y'] = $lineSet->min['y'];
  203.                     $this->max['x'] = $lineSet->max['x'];
  204.                     $this->max['y'] = $lineSet->max['y'];
  205.                 } else {
  206.                     $this->min['x'] = min($this->min['x'], $lineSet->min['x']);
  207.                     $this->min['y'] = min($this->min['y'], $lineSet->min['y']);
  208.                     $this->max['x'] = max($this->max['x'], $lineSet->max['x']);
  209.                     $this->max['y'] = max($this->max['y'], $lineSet->max['y']);
  210.                 }
  211.             }
  212.         }
  213.  
  214.         foreach ($lineSets as $lineSet) {
  215.             foreach ($lineSet->lines as $line) {
  216.                 $this->drawClippedLine($line[0], $line[1], $line[2], $line[3], $lineSet->color);
  217.             }
  218.         }
  219.     }
  220.  
  221.     /**
  222.     * Sets the range of the data to be rendered.
  223.     *
  224.     * @param  float $x1
  225.     * @param  float $x2
  226.     * @param  float $y1
  227.     * @param  float $y2
  228.     * @access public
  229.     */
  230.     function setRange($x1, $x2, $y1, $y2) {
  231.         $this->min = array('x' => $x1, 'y' => $y1);
  232.         $this->max = array('x' => $x2, 'y' => $y2);
  233.     }
  234.  
  235.     /**
  236.     * Draws a line from ($x1, $y1) to ($x2, $y2)
  237.     * using the color rgb($r, $g, $b).
  238.     *
  239.     * @param  float   $x1
  240.     * @param  float   $y1
  241.     * @param  float   $x2
  242.     * @param  float   $y2
  243.     * @param  float   $r
  244.     * @param  float   $g
  245.     * @param  float   $b
  246.     * @access public
  247.     * @abstract
  248.     */
  249.     function drawLine($x1, $y1, $x2, $y2, $r, $g, $b) { /* abstract */ }
  250.  
  251.     /**
  252.     * Saves the rendered image to a given file.
  253.     *
  254.     * @param  string  $filename
  255.     * @return boolean
  256.     * @access public
  257.     * @abstract
  258.     */
  259.     function saveImage($filename) { /* abstract */ }
  260.  
  261.     /**
  262.     * Shows the rendered image.
  263.     *
  264.     * @access public
  265.     * @abstract
  266.     */
  267.     function showImage() { /* abstract */ }
  268. }
  269. ?>
  270.