From ca7c09c109f81a80397da6d0bd2ae0ecb6e1b3d8 Mon Sep 17 00:00:00 2001 From: Stefano Azzolini Date: Thu, 16 Oct 2014 13:23:36 +0200 Subject: [PATCH] Cleaning up structure --- composer.json | 13 ++- src/Pixeler/Canvas.php | 51 +++++++++ Pixeler.php => src/Pixeler/Image.php | 165 +-------------------------- src/Pixeler/Matrix.php | 120 +++++++++++++++++++ src/Pixeler/Pixeler.php | 34 ++++++ 5 files changed, 214 insertions(+), 169 deletions(-) create mode 100644 src/Pixeler/Canvas.php rename Pixeler.php => src/Pixeler/Image.php (61%) create mode 100644 src/Pixeler/Matrix.php create mode 100644 src/Pixeler/Pixeler.php diff --git a/composer.json b/composer.json index 2233394..82aad99 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { "name": "lastguest/pixeler", "type": "library", - "description": "UTF-8 Dot matrix renderer", - "keywords": ["pixel","graphics","utf8","render","cli"], - "homepage": "http://dreamnoctis.com/", + "description": "CLI Image renderer", + "keywords": ["pixel","graphics","utf8","render","cli","image"], + "homepage": "https://github.com/lastguest/pixeler", "version": "1.0.0", "license": "MIT", "authors": [ @@ -15,9 +15,12 @@ "require": { "php": ">=5.4" }, - "minimum-stability": "dev", + "config": { + "preferred-install": "dist" + }, + "minimum-stability": "stable", "autoload": { - "psr-4": { "Pixeler\\" : "" } + "psr-4": { "Pixeler\\" : "src/Pixeler/" } }, "bin": ["bin/pixeler"] } diff --git a/src/Pixeler/Canvas.php b/src/Pixeler/Canvas.php new file mode 100644 index 0000000..fa5fb77 --- /dev/null +++ b/src/Pixeler/Canvas.php @@ -0,0 +1,51 @@ +screen = new Matrix($this->width=$w,$this->height=$h); + $this->charHeight = ceil($h/4); + } + + public function clear($clear=true){ + static $ESC; + $ESC or $ESC = chr(27); + $this->screen->clear(); + $h = $this->charHeight +1; + echo $ESC,'[',$h,'A'; + } + + public function setPixel($x,$y,$c=1){ + $this->screen->setPixel($x,$y,$c); + } + + public function width(){ + return $this->width; + } + + public function height(){ + return $this->height; + } + + public function __toString(){ + return $this->screen->render(); + } + +} \ No newline at end of file diff --git a/Pixeler.php b/src/Pixeler/Image.php similarity index 61% rename from Pixeler.php rename to src/Pixeler/Image.php index 60ba55e..1bb309b 100644 --- a/Pixeler.php +++ b/src/Pixeler/Image.php @@ -13,169 +13,6 @@ namespace Pixeler; -class Pixeler { - - public static function image($image_url, $resize_factor = 1.0, $invert = false, $weight = 0.5){ - return new Image($image_url, $resize_factor, $invert, $weight); - } - - public static function dots($width, $height){ - return new Matrix($width, $height); - } - - public static function hide_cursor(){ - echo chr(27).'[?25l'; - } - - public static function show_cursor(){ - echo chr(27).'[?25h'; - } - -} - -class Canvas { - protected $screen; - protected $width; - protected $height; - protected $charHeight; - - public function __construct($w,$h){ - $this->screen = new Matrix($this->width=$w,$this->height=$h); - $this->charHeight = ceil($h/4); - } - - public function clear($clear=true){ - static $ESC; - $ESC or $ESC = chr(27); - $this->screen->clear(); - $h = $this->charHeight +1; - echo $ESC,'[',$h,'A'; - } - - public function setPixel($x,$y,$c=1){ - $this->screen->setPixel($x,$y,$c); - } - - public function width(){ - return $this->width; - } - - public function height(){ - return $this->height; - } - - public function __toString(){ - return $this->screen->render(); - } - -} - -class Matrix { - protected $matrix = null, - $colors = null, - $width = 0, - $height = 0, - $size = 0, - $csize = 0; - - public function __construct($width,$height){ - $this->width = 2 * ($w2=ceil($width/2)); - $this->height = 4 * ($h2=ceil($height/4)); - $this->size = $this->width * $this->height; - $this->csize = $w2 * $h2; - $this->matrix = new \SplFixedArray($this->size); - $this->colors = new \SplFixedArray($this->csize); - } - - public function clearColors() { - $this->colors = new \SplFixedArray($this->csize); - } - - public function clear() { - $this->matrix = new \SplFixedArray($this->size); - $this->colors = new \SplFixedArray($this->csize); - } - - public function setPixel($x, $y, $value = true,$color = null){ - $y = (int)$y; $x = (int)$x; - if ( $x < $this->width && $y < $this->height) { - $this->matrix[$x + $y * $this->width] = !! $value; - $this->colors[$x>>1 + ($y>>2) * $this->width] = $color; - } - } - - public function getPixel($x, $y){ - $y = (int)$y; $x = (int)$x; - if ( $x < $this->width && $y < $this->height) { - return [ $this->matrix[$x + $y * $this->width], $this->colors[$x + $y * $this->width] ]; - } else { - return false; - } - } - - public function render(){ - $i = 0; - $w = $this->width; - $w2 = $this->width >> 1; - $h = $this->height; - $m = $this->matrix; - $c = $this->colors; - $ESC = chr(27); - ob_start(); - for ($y = 0, $cy = 0; $y < $h; $y += 4, $cy++){ - $cx = 0; $cy0 = $cy * $w2; - $y0 = $y * $w; $y1 = ($y + 1) * $w; $y2 = ($y + 2) * $w; $y3 = ($y + 3) * $w; - for ($x = 0; $x < $w; $x += 2, $cx++){ - $cell = 0; - $x1 = $x + 1; - - foreach([ - 0x01 => $x1 + $y3, - 0x02 => $x + $y3, - 0x04 => $x1 + $y2, - 0x08 => $x + $y2, - 0x10 => $x1 + $y1, - 0x20 => $x + $y1, - 0x40 => $x1 + $y0, - 0x80 => $x + $y0, - ] as $bit => $ofs) { - if (!empty($m[$ofs])) $cell |= $bit; - } - - $dots_r = 0x2800; - - if ($cell & 0x80) $dots_r |= 0x01; - if ($cell & 0x40) $dots_r |= 0x08; - if ($cell & 0x20) $dots_r |= 0x02; - if ($cell & 0x10) $dots_r |= 0x10; - if ($cell & 0x08) $dots_r |= 0x04; - if ($cell & 0x04) $dots_r |= 0x20; - if ($cell & 0x02) $dots_r |= 0x40; - if ($cell & 0x01) $dots_r |= 0x80; - - $dots_r_64 = $dots_r % 64; - $dots_r_4096 = $dots_r % 4096; - - // Print UTF-8 character and color - echo - $ESC.'[' . ($c[$cy0+$cx]?'38;5;'.$c[$cy0+$cx]:39).'m' - . chr(224 + (($dots_r - $dots_r_4096) >> 12 )) - . chr(128 + (($dots_r_4096 - $dots_r_64) >> 6 )) - . chr(128 + $dots_r_64); - } - echo $ESC."[0\n"; - } - $buffer = ob_get_contents(); - ob_end_clean(); - return $buffer; - } - - public function __toString(){ - return $this->render(); - } - -} - class Image extends Matrix { public function __construct($img, $resize=1.0, $invert=false, $weight = 0.5){ @@ -335,4 +172,4 @@ public function __construct($img, $resize=1.0, $invert=false, $weight = 0.5){ } } -} +} \ No newline at end of file diff --git a/src/Pixeler/Matrix.php b/src/Pixeler/Matrix.php new file mode 100644 index 0000000..1a45b2f --- /dev/null +++ b/src/Pixeler/Matrix.php @@ -0,0 +1,120 @@ +width = 2 * ($w2=ceil($width/2)); + $this->height = 4 * ($h2=ceil($height/4)); + $this->size = $this->width * $this->height; + $this->csize = $w2 * $h2; + $this->matrix = new \SplFixedArray($this->size); + $this->colors = new \SplFixedArray($this->csize); + } + + public function clearColors() { + $this->colors = new \SplFixedArray($this->csize); + } + + public function clear() { + $this->matrix = new \SplFixedArray($this->size); + $this->colors = new \SplFixedArray($this->csize); + } + + public function setPixel($x, $y, $value = true,$color = null){ + $y = (int)$y; $x = (int)$x; + if ( $x < $this->width && $y < $this->height) { + $this->matrix[$x + $y * $this->width] = !! $value; + $this->colors[$x>>1 + ($y>>2) * $this->width] = $color; + } + } + + public function getPixel($x, $y){ + $y = (int)$y; $x = (int)$x; + if ( $x < $this->width && $y < $this->height) { + return [ $this->matrix[$x + $y * $this->width], $this->colors[$x + $y * $this->width] ]; + } else { + return false; + } + } + + public function render(){ + $i = 0; + $w = $this->width; + $w2 = $this->width >> 1; + $h = $this->height; + $m = $this->matrix; + $c = $this->colors; + $ESC = chr(27); + ob_start(); + for ($y = 0, $cy = 0; $y < $h; $y += 4, $cy++){ + $cx = 0; $cy0 = $cy * $w2; + $y0 = $y * $w; $y1 = ($y + 1) * $w; $y2 = ($y + 2) * $w; $y3 = ($y + 3) * $w; + for ($x = 0; $x < $w; $x += 2, $cx++){ + $cell = 0; + $x1 = $x + 1; + + foreach([ + 0x01 => $x1 + $y3, + 0x02 => $x + $y3, + 0x04 => $x1 + $y2, + 0x08 => $x + $y2, + 0x10 => $x1 + $y1, + 0x20 => $x + $y1, + 0x40 => $x1 + $y0, + 0x80 => $x + $y0, + ] as $bit => $ofs) { + if (!empty($m[$ofs])) $cell |= $bit; + } + + $dots_r = 0x2800; + + if ($cell & 0x80) $dots_r |= 0x01; + if ($cell & 0x40) $dots_r |= 0x08; + if ($cell & 0x20) $dots_r |= 0x02; + if ($cell & 0x10) $dots_r |= 0x10; + if ($cell & 0x08) $dots_r |= 0x04; + if ($cell & 0x04) $dots_r |= 0x20; + if ($cell & 0x02) $dots_r |= 0x40; + if ($cell & 0x01) $dots_r |= 0x80; + + $dots_r_64 = $dots_r % 64; + $dots_r_4096 = $dots_r % 4096; + + // Print UTF-8 character and color + echo + $ESC.'[' . ($c[$cy0+$cx]?'38;5;'.$c[$cy0+$cx]:39).'m' + . chr(224 + (($dots_r - $dots_r_4096) >> 12 )) + . chr(128 + (($dots_r_4096 - $dots_r_64) >> 6 )) + . chr(128 + $dots_r_64); + } + echo $ESC."[0\n"; + } + $buffer = ob_get_contents(); + ob_end_clean(); + return $buffer; + } + + public function __toString(){ + return $this->render(); + } + +} \ No newline at end of file diff --git a/src/Pixeler/Pixeler.php b/src/Pixeler/Pixeler.php new file mode 100644 index 0000000..6b6b151 --- /dev/null +++ b/src/Pixeler/Pixeler.php @@ -0,0 +1,34 @@ +