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

Source for file ASCII.php

Documentation is available at ASCII.php

  1. <?php
  2.  
  3. define('IMAGE_3D_DRIVER_ASCII_GRAY',    0.01);
  4.  
  5.     
  6.     protected $_size;
  7.     protected $_filetype;
  8.     protected $_points;
  9.     protected $_heigth;
  10.     protected $_image;
  11.  
  12.     protected $_charArray = array(
  13.         0   => ' ',
  14.         1   => '`',
  15.         2   => '\'',
  16.         3   => '^',
  17.         4   => '-',
  18.         5   => '`',
  19.         6   => '/',
  20.         7   => '/',
  21.         8   => '-',
  22.         9   => '\\',
  23.         10  => '\'',
  24.         11  => '\\',
  25.         12  => '~',
  26.         13  => '+',
  27.         14  => '+',
  28.         15  => '*',
  29.         16  => '.',
  30.         17  => '|',
  31.         18  => '/',
  32.         19  => '/',
  33.         20  => '|',
  34.         21  => '|',
  35.         22  => '/',
  36.         23  => '/',
  37.         24  => '/',
  38.         25  => ')',
  39.         26  => '/',
  40.         27  => 'Y',
  41.         28  => 'r',
  42.         29  => '}',
  43.         30  => '/',
  44.         31  => 'P',
  45.         32  => '.',
  46.         33  => '\\',
  47.         34  => '|',
  48.         35  => '^',
  49.         36  => '\\',
  50.         37  => '\\',
  51.         38  => '(',
  52.         39  => '(',
  53.         40  => ':',
  54.         41  => '\\',
  55.         42  => '|',
  56.         43  => 'I',
  57.         44  => ';',
  58.         45  => '\\',
  59.         46  => '{',
  60.         47  => '9',
  61.         48  => '_',
  62.         49  => '_',
  63.         50  => '_',
  64.         51  => 'C',
  65.         52  => '<',
  66.         53  => 'L',
  67.         54  => 'l',
  68.         55  => 'C',
  69.         56  => '>',
  70.         57  => 'J',
  71.         58  => 'J',
  72.         59  => 'J',
  73.         60  => 'o',
  74.         61  => 'b',
  75.         62  => 'd',
  76.         63  => '#',
  77.     );
  78.  
  79.     public function __construct({
  80.         parent::__construct();
  81.         $this->_filetype = 'txt';
  82.         
  83.         $this->reset();
  84.     }
  85.  
  86.     public function reset({
  87.         $this->_points = array();
  88.         $this->_heigth = array();
  89.         
  90.         $this->_image = array();
  91.     }
  92.     
  93.     public function createImage($x$y{
  94.         $this->_size = array($x$y);
  95.     }
  96.     
  97.     protected function _getColor(Image_3D_Color $color$alpha = 1.{
  98.         $values $color->getValues();
  99.         return array($values[0]$values[1]$values[2](1 - $values[3]$alpha);
  100.     }
  101.     
  102.     protected function _mixColor($old$new{
  103.         $faktor (1 - $new[3]$old[3]// slight speed improvement
  104.         return array(
  105.             $old[0$faktor $new[0$new[3],
  106.             $old[1$faktor $new[1$new[3],
  107.             $old[2$faktor $new[2$new[3],
  108.             $old[3$old[3$new[3]
  109.         );
  110.         
  111.     }
  112.     
  113.     public function setBackground(Image_3D_Color $color{
  114.         $bg $this->_getColor($color);
  115.  
  116.         for ($x = 0; $x $this->_size[0]; ++$x{
  117.             for ($y = 0; $y $this->_size[1]; ++$y{
  118.                 $this->_image[$x][$y$bg;
  119.             }
  120.         }
  121.     }
  122.     
  123.     protected function _drawLine(Image_3D_Point $p1Image_3D_Point $p2{
  124.         list($x1$y1$p1->getScreenCoordinates();
  125.         list($x2$y2$p2->getScreenCoordinates();
  126.         
  127.         $steps ceil(max(abs($x1 $x2)abs($y1 $y2)));
  128.         
  129.         $xdiff ($x2 $x1$steps;
  130.         $ydiff ($y2 $y1$steps;
  131.         
  132.         $points = array();
  133.         for ($i = 0; $i $steps; ++$i$points[(int) round($x1 $i $xdiff)][(int) round($y1 $i $ydiff)= true;
  134.         return $points;
  135.     }
  136.     
  137.     protected function _getPolygonOutlines($pointArray{
  138.         $map = array();
  139.         
  140.         $last end($pointArray);
  141.         foreach ($pointArray as $point{
  142.             $line $this->_drawLine($last$point);
  143.             $last $point;
  144.             // Merge line to map
  145.             foreach ($line as $x => $row)
  146.                 foreach ($row as $y => $height)
  147.                     $map[(int) $x][(int) $y$height;
  148.         }
  149.         
  150.         return $map;
  151.     }
  152.     
  153.     public function drawPolygon(Image_3D_Polygon $polygon{
  154.         $points $this->_getPolygonOutlines($polygon->getPoints());
  155.  
  156.         foreach ($points as $x => $row{
  157.             if (count($row< 2continue;
  158.             
  159.             $start min(array_keys($row));
  160.             $end max(array_keys($row));
  161.             
  162.             // Starting point
  163.             $this->_heigth[$x][$start$this->_getColor($polygon->getColor());
  164.             
  165.             // the way between
  166.             for ($y $start + 1; $y $end; ++$y{
  167.                 $this->_heigth[$x][$y$this->_getColor($polygon->getColor());
  168.             }
  169.  
  170.             // Ending point
  171.             $this->_points[$x][$end$this->_getColor($polygon->getColor());
  172.         }
  173.     }
  174.     
  175.     public function drawGradientPolygon(Image_3D_Polygon $polygon{
  176.         $this->drawPolygon($polygon);
  177.     }
  178.     
  179.     public function setFiletye($type{
  180.         $type strtolower($type);
  181.         if (in_array($typearray('png''jpeg'))) {
  182.             $this->_filetype = $type;
  183.             return true;
  184.         else {
  185.             return false;
  186.         }
  187.     }
  188.     
  189.     public function _getAnsiColorCode($color$last ''{
  190.         $code "\033[0;" (30 + bindec((int) round($color[2]. (int) round($color[1]. (int) round($color[0]))) 'm';
  191.         if ($last !== $codereturn $code;
  192.         return '';
  193.     }
  194.     
  195.     public function save($file{
  196.         
  197.         $asciiWidth = (int) ceil($this->_size[0/ 2);
  198.         $asciiHeight = (int) ceil($this->_size[1/ 6)
  199.         
  200.         $output "\033[2J";
  201.         $lastColor '';
  202.         
  203.         for ($y = 0; $y $asciiHeight; ++$y{
  204.             for ($x = 0; $x $asciiWidth; ++$x{
  205.                 // Get pixelarray
  206.                 $char = 0;
  207.                 $charColor = array(000);
  208.                 for ($xi = 0; $xi < 2; ++$xi{
  209.                     for ($yi = 0; $yi < 3; ++$yi{
  210.                         $xPos $x * 2 + $xi;
  211.                         $yPos $y * 6 + $yi;
  212.                         $color $this->_mixColor($this->_image[$xPos][$yPos]$this->_heigth[$xPos][$yPos]);
  213.                         if ((($color[0$color[1$color[2]/ 3IMAGE_3D_DRIVER_ASCII_GRAY$char |= pow(2$yi ($xi * 3));
  214.                         $charColor[0+= $color[0];
  215.                         $charColor[1+= $color[1];
  216.                         $charColor[2+= $color[2];
  217.                     }
  218.                 }
  219.                 $lastColor $this->_getAnsiColorCode(array($charColor[0/ 6$charColor[1/ 6$charColor[2/ 6)$lastColor);
  220.                 $output .= $lastColor $this->_charArray[$char];
  221.             }
  222.             $lastColor '';
  223.             $output .= "\n";
  224.         }
  225.         $fp fopen($file'w');
  226.         fwrite($fp$output);
  227.         fclose($fp);
  228.     }
  229.  
  230.     public function getSupportedShading({
  231.         return array(    Image_3D_Renderer::SHADE_NO
  232.                         Image_3D_Renderer::SHADE_FLAT
  233. //                        Image_3D_Renderer::SHADE_GAUROUD,
  234.                         );
  235.     }
  236. }
  237.  
  238. ?>

Documentation generated on Mon, 11 Mar 2019 14:04:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.