Desafío Carácter Marciano
#3 Solución de Arias
Ver código
|
Ver comentarios (4)
|
Descargar código
Fecha: 28 septiembre 2006
Tamaño: 22622 caracteres
Comentarios: 4
Solución online:
Lo siento, pero no está disponible
Valoración









10.00
(1 votos)
No ha realizado ningún comentario
Valora esta solución
<?php
class Image {
/* private */ var $image;
/* public */ function Image($width, $height, $truecolor=true) {
$this->image = $truecolor ? imagecreatetruecolor($width, $height) : imagecreate($width, $height);
}
/* public static */ function fromAsciiFile($file, $palette) {
$parser = new AsciiImageParser($file, $palette);
return $parser->parse() ? $parser->getImage() : null;
}
/* public */ function showAsPng() {
imagepng($this->image);
}
/* public */ function drawRectangle($x1, $y1, $x2, $y2, $red, $green, $blue) {
imagerectangle($this->image, $x1, $y1, $x2, $y2, $this->getColorIndex($red, $green, $blue));
}
/* public */ function drawText($text, $x, $y, $red, $green, $blue, $font=2) {
imagestring($this->image, $font, $x, $y, $text, $this->getColorIndex($red, $green, $blue));
}
/* private */ function getColorIndex($red, $green, $blue) {
$color = imagecolorexact($this->image, $red, $green, $blue);
return $color === -1 ? imagecolorallocate($this->image, $red, $green, $blue) : $color;
}
}
class AsciiImageParser {
/* private */ var $file;
/* private */ var $image = null;
/* private */ var $palette;
/* private */ var $densities = array();
/* public */ function AsciiImageParser($file, $palette) {
$this->file = $file;
$this->palette = $palette;
}
/* public */ function parse() {
if (!is_file($this->file) || !is_readable($this->file)) return false;
$file = file($this->file);
$width = strlen($file[0]);
$height = count($file);
$this->image = new Image($width, $height << 1);
for ($row=0, $i=0; $i<$height; ++$i, $row+=2) {
for ($j=0; $j<$width; ++$j) {
$c = $j;
while ($c+1<$width && $file[$i][$j] === $file[$i][$c]) ++$c;
list($r, $g, $b) = $this->palette->getColor($file[$i][$j]);
$this->image->drawRectangle($j, $row, $j=$c, $row+1, $r, $g, $b);
}
}
return true;
}
/* public */ function getImage() {
return $this->image;
}
}
class AsciiImagePalette {
/* protected */ var $colors = array();
/* private */ var $white = array(255, 255, 255);
/* private */ function AsciiImagePalette() { }
/* public static */ function fromSortedString($str) {
$p = new AsciiImagePalette();
$len = strlen($str) - 1;
for ($i=0; $i<=$len; ++$i) $p->colors[$str{$i}] = array_fill(0, 3, 255 * (1 - ($i / $len)));
return $p;
}
/* public */ function getColor($char) {
return isset($this->colors[$char]) ? $this->colors[$char] : $this->white;
}
}
function getmicrotime() {
list($micro, $time) = explode(' ', microtime());
return $time + $micro;
}
header("Content-Type: image/png");
$start = getmicrotime();
$palette = AsciiImagePalette::fromSortedString(' .+%$2Y0DAUQ#HNM');
$image = Image::fromAsciiFile('marcianos.txt', $palette);
$end = getmicrotime();
$image->drawText(number_format($end-$start, 3).'s', 5, 5, 0, 0, 0);
$image->showAsPng();
?>