<?php
$img = imagecreatefrompng("dogee.png");
function getScaled($img, $scale) {
$width = imagesx($img);
$height = imagesy($img);
$newwidth = $width*$scale;
$newheight = $height*$scale;
$destination = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($destination, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagedestroy($img);
$img = $destination;
return $img;
}
function encode($img) {
$width = imagesx($img);
$height = imagesy($img);
$out = array();
$stream = 0;
$streamIndex = 0;
$outer = array();
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($img, $x, $y);
$colorIndex = imagecolorat($img, $x, $y);
$colorInfo = imagecolorsforindex($img, $colorIndex);
$r = $colorInfo["red"];
$g = $colorInfo["green"];
$b = $colorInfo["blue"];
$val = ($r+$g+$b) / 3;
$val = $val < 127 ? 1 : 0;
$stream |= $val;
if($streamIndex == 31) {
$out[] = $stream;
$streamIndex = 0;
$stream = 0;
} else {
$streamIndex++;
$stream <<= 1;
}
$outer[($y*$width)+$x] = $val;
}
}
if($streamIndex != 0){
$out[] = $stream << (31-$streamIndex);
}
$nw = array();
$nw[] = $width;
$nw[] = $height;
// Encode stream
$uncodedSequence = array();
$currentEqualityLength = 0;
$currentEqualityValue = array(); // Or something that cannot be number
foreach($out as $o) {
if($currentEqualityLength == 0) { // No prior sequence
if($currentEqualityValue == $o) { // Begin of the sequence
$currentEqualityLength++;
} else {
$uncodedSequence[] = $o;
}
$currentEqualityValue = $o;
} else { // Prior sequence, could either terminate or continue in it
if($currentEqualityValue == $o) { // Sequence continuation
$currentEqualityLength++;
} else { // End of sequence, write it to output
// Start with meta data
$nw[] = (count($uncodedSequence) << 16) | $currentEqualityLength; // Number of uncoded data
// Uncoded sequence
foreach($uncodedSequence as $uc) {
$nw[] = $uc;
}
// Coded sequence
$nw[] = $currentEqualityValue;
// Reset settings
$currentEqualityValue = $o;
$uncodedSequence = array();
$currentEqualityLength = 0;
$uncodedSequence[] = $o;
}
}
}
// End of stream, push what is left
$nw[] = (count($uncodedSequence) << 16) | $currentEqualityLength; // Number of collowing coded data
foreach($uncodedSequence as $uc) {
$nw[] = $uc;
}
if($currentEqualityLength > 0) {
$nw[] = $currentEqualityValue;
}
$out = $nw;
$sb = "const unsigned int dogee[] = {\r\n";
$cnt = count($out)-1;
$lastBreakLen = 0;
foreach($out as $i=>$o) {
$lb = $i % 10 == 9;
$sb.="0x".dechex($o);
if($cnt != $i) {
$sb.=", ";
}
$len = strlen($sb);
if($len - $lastBreakLen > 150) {
$sb.="\r\n";
$lastBreakLen = $len;
}
}
$sb .= "\r\n};\r\n";
return array($sb, $out, $outer);
}
function decode($img, $out, $outer) {
$width = $out[0];
$height = $out[1];
$white = imagecolorallocate($img, 255,255,255);
$black = imagecolorallocate($img, 0,0,0);
$undef= imagecolorallocate($img, 255,127,127);
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
imagesetpixel($img, $x, $y, $undef);
}
}
$buffer = $out;
$currentValIndex = 2;
$currentValOffset = 31;
$uncodedLeft = 0;
$codedLeft = 0;
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
if($uncodedLeft == 0 && $codedLeft == 0) {
$uncodedLeft = $out[$currentValIndex] >> 16;
$codedLeft = $out[$currentValIndex] & 0xffff;
$currentValIndex += 1;
}
$val = $out[$currentValIndex];
$val = $val >> $currentValOffset;
$val = $val & 1;
if($currentValOffset == 0) {
if($uncodedLeft > 0) {
$currentValIndex++;
$uncodedLeft--;
} else if($codedLeft > 0) {
$codedLeft--;
if($codedLeft == 0) {
$currentValIndex++;
}
}
$currentValOffset = 31;
} else {
$currentValOffset--;
}
imagesetpixel($img, $x, $y, $val == 1 ? $black : $white);
}
}
return $img;
}
function show($img) {
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}
if(isset($_GET["scale"])) {
$img = getScaled($img, $_GET["scale"]);
}
list($sb, $out, $outer) = encode($img);
if(isset($_GET["output"])) {
$output = $_GET["output"];
if($output == "code") {
die($sb);
} else if($output == "image") {
show($img);
} else if($output == "test") {
$img = decode($img, $out, $outer);
show($img);
} else {
die("Invalid output. Valid is \"code\", \"test\" and \"image\"");
}
}
die("<a href=?output=image>Continue here</a>");
?>
Comments