<?
$font = 4;
$xfont = 1;
$W = 80;
$H = 25;
$rw = 4;
$rh = 5;
$colors = array(
"bg" => array(255, 255, 255),
"text" => array(0, 0, 0),
"nbg" => array(64, 64, 64),
"ntext" => array(255, 255, 255),
"flows" => array(
array(255, 153, 255), array(255, 153, 204),
array(255, 153, 153), array(255, 204, 153),
array(255, 255, 153), array(204, 255, 153),
array(153, 255, 153), array(153, 255, 204),
array(153, 255, 255), array(153, 204, 255),
array(153, 153, 255), array(204, 153, 255)
)
);
$DIR = array(array(1, 0), array(0, 1), array(-1, 0), array(0, -1));
$color_cur = 1;
$page = $vpage = array();
function error($msg) {
$im = imagecreate(strlen($msg) * imagefontwidth(5), imagefontheight(5));
$w = imagecolorallocate($im, 255, 255, 255);
$b = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $w);
imagestring($im, 5, 0, 0, $msg, $b);
imagepng($im);
imagedestroy($im);
exit;
}
function bef_parse($contents, $bf93 = true) {
global $WW, $HH, $page, $W, $H;
$max_W = 0;
$contents = str_replace("\r", "\n", str_replace("\r\n", "\n", $contents));
foreach(explode("\n", $contents) as $_) {
if($bf93 && strlen($_) > $W) { error(".error: line is too long."); }
if(strlen($_) > $max_W) $max_W = strlen($_);
$page[] = $_;
if($bf93 && count($page) > $H) { error(".error: Too many lines."); }
}
if($bf93) {
list($WW, $HH) = array($W, $H);
$W = $max_W;
$H = count($page);
} else {
$WW = $W = $max_W;
$HH = $H = count($page);
}
}
function rec() {
global $DIR, $color_cur, $page, $vpage, $WW, $HH;
$stack = array(array(0, 0, 0, 0, 0, 0));
while(count($stack)) {
list($x, $y, $dir, $out, $str, $newcolor) = array_pop($stack);
if($newcolor) $color_cur++;
if($out) {
if($vpage[$y][$x][($dir+2)&3]["o"][$str]) continue;
$vpage[$y][$x][($dir+2)&3]["o"][$str] = $color_cur;
$x += $DIR[$dir][0] + $WW; $x %= $WW;
$y += $DIR[$dir][1] + $HH; $y %= $HH;
}
if($vpage[$y][$x][$dir]["i"][$str]) continue;
$vpage[$y][$x][$dir]["i"][$str] = $color_cur;
if(!($c = $page[$y]{$x})) $c = " ";
if($str == 1) {
array_push($stack, array($x, $y, $dir, 1, $c != '"', 0));
} elseif($str == 2) {
array_push($stack, array($x, $y, $dir, 1, 0, 0));
} elseif($c == "#") {
$X = $x + 2 * $DIR[$dir][0] + $WW; $X %= $WW;
$Y = $y + 2 * $DIR[$dir][1] + $HH; $Y %= $HH;
array_push($stack, array($X, $Y, $dir, 0, 0, 0));
} elseif(strpos(">v<^", $c) !== false) {
$dir = strpos(">v<^", $c);
array_push($stack, array($x, $y, $dir, 1, 0, 0));
} elseif($c == "|") {
array_push($stack, array($x, $y, 3, 1, 0, 1));
array_push($stack, array($x, $y, 1, 1, 0, 1));
} elseif($c == "_") {
array_push($stack, array($x, $y, 2, 1, 0, 1));
array_push($stack, array($x, $y, 0, 1, 0, 1));
} elseif($c == "?") {
array_push($stack, array($x, $y, 3, 1, 0, 1));
array_push($stack, array($x, $y, 2, 1, 0, 1));
array_push($stack, array($x, $y, 1, 1, 0, 1));
array_push($stack, array($x, $y, 0, 1, 0, 1));
} elseif($c == '"') {
array_push($stack, array($x, $y, $dir, 1, 1, 0));
} elseif($bf93) {
if($c != "@") {
array_push($stack, array($x, $y, $dir, 1, 0, 0));
}
} else {
if(strpos("ABCDEFGHIJKLMNOPQRSTUVWYXZ()t", $c) !== false) {
$X = $x + $DIR[($dir+2)&3][0] + $WW; $X %= $WW;
$Y = $y + $DIR[($dir+2)&3][1] + $HH; $Y %= $HH;
array_push($stack, array($X, $Y, ($dir+2)&3, 0, 0, 1));
array_push($stack, array($x, $y, $dir, 1, 0, 0));
} elseif($c == ";") {
$X = $x; $Y = $y;
do {
$X += $DIR[$dir][0] + $WW; $X %= $WW;
$Y += $DIR[$dir][1] + $HH; $Y %= $HH;
} while($page[$Y]{$X} != ";");
$X += $DIR[$dir][0] + $WW; $X %= $WW;
$Y += $DIR[$dir][1] + $HH; $Y %= $HH;
array_push($stack, array($X, $Y, $dir, 0, 0, 0));
} elseif($c == "[") {
array_push($stack, array($x, $y, ($dir+3)&3, 1, 0, 0));
} elseif($c == "]") {
array_push($stack, array($x, $y, ($dir+1)&3, 1, 0, 0));
} elseif($c == "w") {
array_push($stack, array($x, $y, ($dir+1)&3, 1, 0, 1));
array_push($stack, array($x, $y, ($dir+3)&3, 1, 0, 1));
array_push($stack, array($x, $y, $dir, 1, 0, 0));
} elseif($c == "'") {
array_push($stack, array($x, $y, $dir, 1, 2, 0));
} elseif($c != "@" && $c != "q") {
array_push($stack, array($x, $y, $dir, 1, 0, 0));
}
}
}
}
function vis_draw($number = false) {
global $colors, $font, $xfont, $im, $w, $h, $rw, $rh, $W, $H;
global $vpage, $page;
$msg = "Generated by .tokinized online Befunge visualizer 0.71";
$w = imagefontwidth($font);
$h = imagefontheight($font);
$xw = $number ? imagefontwidth($xfont) * strlen($H-1) + 4 : 0;
$xh = $number ? imagefontwidth($xfont) * strlen($W-1) + 4 : 0;
$xxw = 0; $xxh = imagefontheight($xfont) + 2;
if($W*$w+$xw+$xxw < imagefontwidth($xfont)*strlen($msg)+4) $xxh = 0;
$im = imagecreate($W*$w+$xw+$xxw, $H*$h+$xh+$xxh);
$color_bg = imagecolorallocate($im, $colors["bg"][0], $colors["bg"][1], $colors["bg"][2]);
$color_text = imagecolorallocate($im, $colors["text"][0], $colors["text"][1], $colors["text"][2]);
$color_nbg = imagecolorallocate($im, $colors["nbg"][0], $colors["nbg"][1], $colors["nbg"][2]);
$color_ntext = imagecolorallocate($im, $colors["ntext"][0], $colors["ntext"][1], $colors["ntext"][2]);
foreach($colors["flows"] as $k => $v)
$color[$k] = imagecolorallocate($im, $v[0], $v[1], $v[2]);
imagefill($im, 0, 0, $color_bg);
if($xxh) {
imagestring($im, $xfont, $W*$w+$xw+$xxw-2-strlen($msg)*imagefontwidth($xfont), 1, $msg, $color_nbg);
}
if($number) {
imagefilledrectangle($im, $xxw, $xxh, $W*$w+$xw+$xxw-1, $xh+$xxh-1, $color_nbg);
imagefilledrectangle($im, $xxw, $xxh, $xw+$xxw-1, $H*$h+$xh+$xxh-1, $color_nbg);
for($i=0;$i<$H;$i++)
imagestring($im, $xfont, 2+$xxw, $xh+$i*$h+$xxh+4, sprintf("%".strlen($H-1)."d",$i), $color_ntext);
for($i=0;$i<$W;$i++)
imagestringup($im, $xfont, $xw+$xxw+$i*$w, $xh+$xxh-3, $i, $color_ntext);
}
for($y=0;$y<=$H;$y++) { for($x=0;$x<=$W;$x++) { for($dir=0;$dir<4;$dir++) {
if($vpage[$y][$x][$dir]["o"][0]) $c = $vpage[$y][$x][$dir]["o"][0];
elseif($vpage[$y][$x][$dir]["o"][1]) $c = $vpage[$y][$x][$dir]["o"][1];
elseif($vpage[$y][$x][$dir]["i"][0]) $c = $vpage[$y][$x][$dir]["i"][0];
elseif($vpage[$y][$x][$dir]["i"][1]) $c = $vpage[$y][$x][$dir]["i"][1];
else $c = false;
if($c !== false) {
imagefilledrectangle($im,
$xxw + $xw + $w * $x + ($dir==0 ? 0 : $w/$rw),
$xxh + $xh + $h * $y + ($dir==1 ? 0 : $h/$rh),
$xxw + $xw + $w * $x + ($rw - ($dir==2 ? 0 : 1)) * $w / $rw,
$xxh + $xh + $h * $y + ($rh - ($dir==3 ? 0 : 1)) * $h / $rh,
$color[$c % count($color)]);
}
} } }
for($y=0;$y<=$H;$y++) { for($x=0;$x<=$W;$x++) {
if($page[$y]{$x} != " ")
imagechar($im, $font,
$xxw + $xw + $x * $w, $xxh + $xh + $y * $h,
$page[$y]{$x}, $color_text);
} }
}
function vis_out() {
global $im;
imagepng($im);
imagedestroy($im);
}
if($_GET["source"] == 42) { echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n\n"; show_source(__FILE__); exit; }
header("Content-type: image/png");
$baseuri = "http://page.tokigun.net/visbef/";
if(!isset($_GET['path']))
if(substr($_SERVER["HTTP_REFERER"], 0, strlen($baseuri)) != $baseuri) error(".error: referer check was failed. (your referer is: ".($_SERVER["HTTP_REFERER"] ? "$_SERVER[HTTP_REFERER])" : "manual adress typing.. :p)"));
if(isset($_GET['path']) && preg_match('/^[0-9a-zA-Z.]+$/', $_GET['path']))
$contents = file_get_contents($_GET['path']);
else
$contents = base64_decode($_GET["contents"]);
if($contents) { bef_parse($contents, $_GET["bf93"]=="yeah!"); rec(); vis_draw($_GET["number"]==42); vis_out($_GET["number"]==42); }
else { error(".error: no code was inputed."); }
?>