Advertisement
Tarferi

Dogee image coder

Dec 11th, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2. $img = imagecreatefrompng("dogee.png");
  3.  
  4.  
  5.  
  6. function getScaled($img, $scale) {
  7.     $width = imagesx($img);
  8.     $height = imagesy($img);
  9.  
  10.     $newwidth = $width*$scale;
  11.     $newheight = $height*$scale;
  12.  
  13.     $destination = imagecreatetruecolor($newwidth, $newheight);
  14.     imagecopyresampled($destination, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  15.     imagedestroy($img);
  16.     $img = $destination;
  17.     return $img;
  18. }
  19.  
  20. function encode($img) {
  21.     $width = imagesx($img);
  22.     $height = imagesy($img);
  23.     $out = array();
  24.     $stream = 0;
  25.     $streamIndex = 0;
  26.     $outer = array();
  27.     for($y = 0; $y < $height; $y++) {
  28.         for($x = 0; $x < $width; $x++) {
  29.             $rgb = imagecolorat($img, $x, $y);
  30.             $colorIndex = imagecolorat($img, $x, $y);
  31.             $colorInfo = imagecolorsforindex($img, $colorIndex);
  32.             $r = $colorInfo["red"];
  33.             $g = $colorInfo["green"];
  34.             $b = $colorInfo["blue"];
  35.             $val = ($r+$g+$b) / 3;
  36.             $val = $val < 127 ? 1 : 0;
  37.             $stream |= $val;
  38.             if($streamIndex == 31) {
  39.                 $out[] = $stream;
  40.                 $streamIndex = 0;
  41.                 $stream = 0;
  42.             } else {
  43.                 $streamIndex++;
  44.                 $stream <<= 1;
  45.             }
  46.             $outer[($y*$width)+$x] = $val;
  47.         }
  48.     }
  49.     if($streamIndex != 0){
  50.         $out[] = $stream << (31-$streamIndex);
  51.     }
  52.     $nw  = array();
  53.     $nw[] = $width;
  54.     $nw[] = $height;
  55.     // Encode stream
  56.     $uncodedSequence = array();
  57.     $currentEqualityLength = 0;
  58.     $currentEqualityValue = array(); // Or something that cannot be number
  59.     foreach($out as $o) {
  60.         if($currentEqualityLength == 0) { // No prior sequence
  61.             if($currentEqualityValue == $o) { // Begin of the sequence
  62.                 $currentEqualityLength++;
  63.             } else {
  64.                 $uncodedSequence[] = $o;
  65.             }
  66.             $currentEqualityValue = $o;
  67.         } else { // Prior sequence, could either terminate or continue in it
  68.             if($currentEqualityValue == $o) { // Sequence continuation
  69.                 $currentEqualityLength++;
  70.             } else { // End of sequence, write it to output
  71.                 // Start with meta data
  72.                 $nw[] = (count($uncodedSequence) << 16) | $currentEqualityLength; // Number of uncoded data
  73.                 // Uncoded sequence
  74.                 foreach($uncodedSequence as $uc) {
  75.                     $nw[] = $uc;
  76.                 }
  77.                 // Coded sequence
  78.                 $nw[] = $currentEqualityValue;
  79.                
  80.                 // Reset settings
  81.                 $currentEqualityValue = $o;
  82.                 $uncodedSequence = array();
  83.                 $currentEqualityLength = 0;
  84.                 $uncodedSequence[] = $o;
  85.             }
  86.         }
  87.     }
  88.  
  89.     // End of stream, push what is left
  90.     $nw[] = (count($uncodedSequence) << 16) | $currentEqualityLength; // Number of collowing coded data
  91.     foreach($uncodedSequence as $uc) {
  92.         $nw[] = $uc;
  93.     }
  94.     if($currentEqualityLength > 0) {
  95.         $nw[] = $currentEqualityValue;
  96.     }
  97.    
  98.     $out = $nw;
  99.    
  100.     $sb = "const unsigned int dogee[] = {\r\n";
  101.     $cnt = count($out)-1;
  102.     $lastBreakLen = 0;
  103.     foreach($out as $i=>$o) {
  104.         $lb = $i % 10 == 9;
  105.        
  106.         $sb.="0x".dechex($o);
  107.         if($cnt != $i) {
  108.             $sb.=", ";
  109.         }
  110.         $len = strlen($sb);
  111.         if($len - $lastBreakLen > 150) {
  112.             $sb.="\r\n";
  113.             $lastBreakLen = $len;
  114.         }
  115.     }
  116.     $sb .= "\r\n};\r\n";
  117.     return array($sb, $out, $outer);
  118. }
  119.  
  120. function decode($img, $out, $outer) {
  121.     $width = $out[0];
  122.     $height = $out[1];
  123.     $white = imagecolorallocate($img, 255,255,255);
  124.     $black = imagecolorallocate($img, 0,0,0);
  125.     $undef= imagecolorallocate($img, 255,127,127);
  126.     for($y = 0; $y < $height; $y++) {
  127.         for($x = 0; $x < $width; $x++) {
  128.             imagesetpixel($img, $x, $y, $undef);
  129.         }
  130.     }
  131.    
  132.     $buffer = $out;
  133.     $currentValIndex = 2;
  134.     $currentValOffset = 31;
  135.    
  136.     $uncodedLeft = 0;
  137.     $codedLeft = 0;
  138.    
  139.     for($y = 0; $y < $height; $y++) {
  140.         for($x = 0; $x < $width; $x++) {
  141.             if($uncodedLeft == 0 && $codedLeft == 0) {
  142.                 $uncodedLeft = $out[$currentValIndex] >> 16;
  143.                 $codedLeft = $out[$currentValIndex] & 0xffff;
  144.                 $currentValIndex += 1;
  145.             }
  146.             $val = $out[$currentValIndex];
  147.             $val = $val >> $currentValOffset;
  148.             $val = $val & 1;
  149.             if($currentValOffset == 0) {
  150.                 if($uncodedLeft > 0) {
  151.                     $currentValIndex++;
  152.                     $uncodedLeft--;
  153.                 } else if($codedLeft > 0) {
  154.                     $codedLeft--;
  155.                     if($codedLeft == 0) {
  156.                         $currentValIndex++;
  157.                     }
  158.                 }
  159.                 $currentValOffset = 31;
  160.             } else {
  161.                 $currentValOffset--;
  162.             }
  163.             imagesetpixel($img, $x, $y, $val == 1 ? $black : $white);
  164.         }
  165.     }
  166.     return $img;
  167. }
  168.  
  169. function show($img) {
  170.     header('Content-Type: image/png');
  171.     imagepng($img);
  172.     imagedestroy($img);
  173. }
  174. if(isset($_GET["scale"])) {
  175.     $img = getScaled($img, $_GET["scale"]);
  176. }
  177. list($sb, $out, $outer) = encode($img);
  178.  
  179. if(isset($_GET["output"])) {
  180.     $output = $_GET["output"];
  181.     if($output == "code") {
  182.         die($sb);
  183.     } else if($output == "image") {
  184.         show($img);
  185.     } else if($output == "test") {
  186.         $img = decode($img, $out, $outer);
  187.         show($img);
  188.     } else {
  189.         die("Invalid output. Valid is \"code\", \"test\" and \"image\"");
  190.     }
  191. }
  192.  
  193. die("<a href=?output=image>Continue here</a>");
  194.  
  195.  
  196. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement