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

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