Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.33 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4. ini_set("memory_limit","500M");
  5.  
  6. //path to .mul files
  7. $path = "/home/escribano/UOClassic/";
  8.  
  9. $hue = (isset($_REQUEST['hue']) ? $_REQUEST['hue'] : 0);
  10. $id = $_REQUEST['id'];
  11.  
  12. $hue = hex($hue);
  13. $id = hex($id);
  14. $id += 0x4000;
  15.  
  16. $hues = FALSE;
  17. $tiledata = FALSE;
  18. $gumpindex = FALSE;
  19. $gumpfile = FALSE;
  20. $partialhue = false;
  21.  
  22. //open files for reading
  23. //**********************
  24.  
  25. if ($hue < 1 || $hue > 65535) {
  26. //If invalid or missing hue, unset hue and don't read hues.mul and tiledata.mul
  27.     $hue = 0;
  28. } else {
  29. //If valid hue, read hues.mul and tiledata.mul
  30.     $hues = fopen("{$path}hues.mul", "rb");
  31.     if ($hues == FALSE) {
  32.         $hue = 0;
  33.     }
  34.  
  35.     $tiledata = fopen("{$path}tiledata.mul", "rb");
  36.  
  37.     if ($tiledata == FALSE) {
  38.         $hue = 0;
  39.     } else {
  40.         $index = $id-0x4000;
  41.         $group = intval($index / 32);
  42.         $groupidx = $index % 32;
  43.  
  44.         // fseek($tiledata, 512 * 836 + 1149356888 * $group + 4 + $groupidx * 37, SEEK_SET);
  45.         fseek($tiledata, 493568 + 1316 * $group + 4 + $groupidx * 41, SEEK_SET);
  46.         $tileflag = read_byte($tiledata, 4);
  47.  
  48.         if ($tileflag & 0x00040000) {
  49.             // var_dump($tileflag);
  50.             // $partialhue = 1;
  51.         }
  52.  
  53.         fclose($tiledata);
  54.     }
  55. }
  56.  
  57. //Read artidx.mul
  58. $gumpindex = fopen("{$path}artidx.mul", "rb");
  59. if ($gumpindex == FALSE) {
  60.     unavailable_pic();
  61.     exit;
  62. }
  63. else {
  64.     fseek($gumpindex, $id * 12, SEEK_SET);
  65.     $lookup = read_byte($gumpindex, 4);
  66.     $size = read_byte($gumpindex, 4);
  67.     fclose($gumpindex);
  68. }
  69.  
  70. //Read art.mul
  71. $gumpfile = fopen("{$path}art.mul", "rb");
  72. if ($gumpfile == FALSE) {
  73.     unavailable_pic();
  74.     exit;
  75. }
  76. else {
  77.     fseek($gumpfile, $lookup, SEEK_SET);
  78.     $flag = read_byte($gumpfile, 4);
  79.     $width = read_byte($gumpfile, 2);
  80.     $height = read_byte($gumpfile, 2);
  81.  
  82.  
  83. //create base image
  84. //**********************
  85.  
  86.     $im = imagecreate($width, $height);
  87.     $bg = imageColorAllocate($im, 256, 0, 0);
  88.     imagecolortransparent($im, $bg);
  89.  
  90. //Read pixels
  91. //**********************
  92.  
  93.     for($i=0; $i < $height; $i++) {
  94.         $offset[$i] = read_byte($gumpfile, 2);
  95.     }
  96.  
  97.     $datastart = ftell($gumpfile);
  98.  
  99.     $x = 0;
  100.     $y = 0;
  101.  
  102. //Display without hues
  103. //**********************
  104.  
  105.     if ($hue <= 0) {
  106.         while ( $y < $height ) {
  107.             $xoffset = read_byte($gumpfile, 2);
  108.             $xRun = read_byte($gumpfile, 2);
  109.             if ( ($xRun + $xoffset) > 2048 ) {
  110.                 break;
  111.             }
  112.             else {
  113.                 if ( ( $xRun + $xoffset ) != 0 ) {
  114.                     $x += $xoffset;
  115.                     for($Run = 0; $Run < $xRun; $Run++) {
  116.                         $color[$Run] = read_byte($gumpfile, 2);
  117.                         $r = (($color[$Run] >> 10)*8);
  118.                         $g = ((($color[$Run] >> 5) & 0x1F)*8);
  119.                         $b = (($color[$Run] & 0x1F)*8);
  120.                         if (imagecolorexact($im, $r, $g, $b) == -1) {
  121.                             $col = imageColorAllocate($im, $r, $g, $b);
  122.                             imagesetpixel($im, $x, $y, $col);
  123.                         }
  124.                         else {
  125.                             $found = imagecolorexact($im, $r, $g, $b);
  126.                             imagesetpixel($im, $x, $y, $found);
  127.                         }
  128.  
  129.                         $x++;
  130.                     }
  131.                 }
  132.                 else {
  133.                     $x = 0;
  134.                     $y++;
  135.                     @fseek($gumpfile, $offset[$y] * 2 + $datastart, SEEK_SET);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.  
  141. //Display with hues
  142. //**********************
  143.  
  144.     else {
  145.         $hue = $hue - 1;
  146.         $orighue = $hue;
  147.  
  148.         if ($hue > 0x8000)
  149.             $hue = $hue - 0x8000;
  150.         if ($hue > 3001)
  151.             $hue = 1;
  152.  
  153.         $colors = intval($hue / 8) * 4;
  154.         $colors = 4 + $hue * 88 + $colors;
  155.         fseek($hues, $colors, SEEK_SET);
  156.         for ($i = 0; $i < 32; $i++) {
  157.             $color32[$i] = read_byte($hues, 2);
  158.             $color32[$i] |= 0x8000;
  159.         }
  160.  
  161.         while ( $y < $height ) {
  162.             $xoffset = read_byte($gumpfile, 2);
  163.             $xRun = read_byte($gumpfile, 2);
  164.             if ( ($xRun + $xoffset) > 2048 ) {
  165.                 break;
  166.             } else {
  167.                 if ( ( $xRun + $xoffset ) != 0 ) {
  168.                     $x += $xoffset;
  169.                     for($Run = 0; $Run < $xRun; $Run++) {
  170.                         $color[$Run] = read_byte($gumpfile, 2);
  171.                         // echo $color[$Run] . " ($partialhue)<br>";
  172.  
  173.                         $r = ($color[$Run] >> 10);
  174.                         $g = (($color[$Run] >> 5) & 0x1F);
  175.                         $b = ($color[$Run] & 0x1F);
  176.  
  177.  
  178.                         if (($partialhue == 1) && ($r == $g && $r == $b)) {
  179.                             $newr = (($color32[$r] >> 10))*8;
  180.                             $newg = (($color32[$r] >> 5) & 0x1F)*8;
  181.                             $newb = ($color32[$r] & 0x1F)*8;
  182.                         }
  183.                         else if ($partialhue == 1) {
  184.                             $newr = $r * 8;
  185.                             $newg = $g * 8;
  186.                             $newb = $b * 8;
  187.                         }
  188.                         else {
  189.                             $newr = (($color32[$r] >> 10))*8;
  190.                             $newg = (($color32[$r] >> 5) & 0x1F)*8;
  191.                             $newb = ($color32[$r] & 0x1F)*8;
  192.                         }
  193.  
  194.                         if (imagecolorexact($im, $newr, $newg, $newb) == -1) {
  195.                             $col = imageColorAllocate($im, $newr, $newg, $newb);
  196.                             imagesetpixel($im, $x, $y, $col);
  197.                         }
  198.                         else {
  199.                             $found = imagecolorexact($im, $newr, $newg, $newb);
  200.                             imagesetpixel($im, $x, $y, $found);
  201.                         }
  202.                         $x++;
  203.                     }
  204.                 } else {
  205.                     $x = 0;
  206.                     $y++;
  207.                     @fseek($gumpfile, $offset[$y] * 2 + $datastart, SEEK_SET);
  208.                 }
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. // echo "FIM";
  215. // exit();
  216.  
  217. header ("Content-type: image/png");
  218. imagepng($im);
  219. imagedestroy($im);
  220.  
  221.  
  222. function read_byte($file, $length) {
  223.     if (($val = fread($file, $length)) == FALSE)
  224.         return -1;
  225.  
  226.     switch($length) {
  227.         case 4: $val = unpack('l', $val);
  228.             break;
  229.         case 2: $val = unpack('s', $val);
  230.             break;
  231.         case 1: $val = unpack('c', $val);
  232.             break;
  233.         default: $val = unpack('l*', $val);
  234.             return $val;
  235.     }
  236.     return ($val[1]);
  237. }
  238.  
  239. function  unavailable_pic() {
  240.     $im = imagecreatetruecolor(44, 47);
  241.     $black = imageColorAllocate($im, 0, 0, 0);
  242.     $red = imageColorAllocate($im, 255, 0, 0);
  243.     imagefill($im, $black);
  244.     imagettftext($im, 8, 315, 0, 7, $red, 'arial.ttf', 'Unavailable');
  245.     imagepng($im);
  246.     imagedestroy($im);
  247. }
  248.  
  249. function  hex($number) {
  250.     if (strlen($number) > 1) {
  251.         if (substr($number, 0, 1) == 0) {
  252.             $number = str_pad(substr($number, 1), strlen($number)+2, "0x", STR_PAD_LEFT);
  253.         }
  254.     }
  255.     return ($number);
  256. }
  257.  
  258. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement