Esempio n. 1
0
 private function _load()
 {
     $wld = fopen($this->file, "rb");
     if (!$wld) {
         return $this->_error("invalid file");
     }
     if (!file_helpers::freadid($wld)) {
         // WORLD_V1.0
         return $this->_error("no S2-Map");
     }
     $this->header['name'] = file_helpers::freadstring($wld, 20);
     // "foo"
     $this->header['width'] = file_helpers::freadshort($wld);
     // w
     $this->header['height'] = file_helpers::freadshort($wld);
     // h
     $this->header['type'] = file_helpers::freadchar($wld);
     // green-/waste-/winterland
     $this->header['players'] = file_helpers::freadchar($wld);
     // number of players
     $this->header['author'] = file_helpers::freadstring($wld, 20);
     // "bar"
     // jump to first subheader
     fseek($wld, 2342, SEEK_SET);
     if (!file_helpers::freadshid($wld, 0x11)) {
         // 11 27 0 0 0 0
         return $this->_error("invalid S2-Map");
     }
     $this->header['width'] = file_helpers::freadshort($wld);
     // w (again)
     $this->header['height'] = file_helpers::freadshort($wld);
     // h (again)
     $keys = array_keys($this->blocks);
     // read the blocks
     for ($i = 0; $i < 14; $i++) {
         if (!file_helpers::freadshid($wld, 0x10)) {
             // 10 27 0 0 0 0
             return $this->_error("invalid S2-Subheader");
         }
         $width = file_helpers::freadshort($wld);
         // sub w
         $height = file_helpers::freadshort($wld);
         // sub h
         if (file_helpers::freadshort($wld) != 1) {
             // 01 00
             return $this->_error("invalid S2-Sub-Id");
         }
         $length = file_helpers::freadint($wld);
         // length of block (w*h)
         $this->blocks[$keys[$i]] = file_helpers::freadchars($wld, $length);
     }
     fclose($wld);
     return true;
 }
Esempio n. 2
0
 private function _loadLBM($file)
 {
     $lbm = fopen($file, "rb");
     if (!$lbm) {
         return false;
     }
     fseek($lbm, 0, SEEK_END);
     $size = ftell($lbm);
     fseek($lbm, 0, SEEK_SET);
     $form = file_helpers::freadstring($lbm, 4, false);
     if ($form != "FORM") {
         return $this->_error("not a LBM file");
     }
     $length = file_helpers::freadint($lbm);
     $pbm = file_helpers::freadstring($lbm, 4, false);
     if ($pbm != "PBM ") {
         return $this->_error("not a LBM file with PBM structure");
     }
     while (!feof($lbm) && ftell($lbm) < $size) {
         $chunk = file_helpers::freadstring($lbm, 4, false);
         // Länge einlesen
         $length = file_helpers::freadbint($lbm);
         #echo $chunk." = ".$length."<br>\n";
         // Bei ungerader Zahl aufrunden
         if ($length & 1) {
             ++$length;
         }
         switch ($chunk) {
             case "BMHD":
                 // Breite & Höhe einlesen
                 $this->image['header']['width'] = file_helpers::freadbshort($lbm);
                 $this->image['header']['height'] = file_helpers::freadbshort($lbm);
                 // Unbekannte Daten ( 4 Byte ) berspringen
                 fseek($lbm, 4, SEEK_CUR);
                 // Farbtiefe einlesen
                 $this->image['header']['depth'] = file_helpers::freadshort($lbm);
                 // Nur 256 Farben und nicht mehr!
                 if ($this->image['header']['depth'] != 8) {
                     $this->_error("Invalid File: LBM with 8bit colors are supported only");
                     return false;
                 }
                 // Kompressionflag lesen
                 $this->image['header']['compression'] = file_helpers::freadshort($lbm);
                 if ($this->image['header']['compression'] != 1) {
                     $this->_error("Invalid File: LBM has unknown compression flag: " . $this->image['header']['compression']);
                     return false;
                 }
                 $length -= 12;
                 // Rest überspringen
                 fseek($lbm, $length, SEEK_CUR);
                 break;
             case "BODY":
                 switch ($this->image['header']['compression']) {
                     case 0:
                         // uncompressed
                         return false;
                     case 1:
                         $pos = 0;
                         // Solange einlesen, bis Block zuende bzw. Datei zuende ist
                         while ($length >= 0 && !feof($lbm) && ftell($lbm) < $size) {
                             // Typ lesen
                             $ctype = file_helpers::freadchar($lbm);
                             --$length;
                             if ($length == 0) {
                                 continue;
                             }
                             if ($ctype < 128) {
                                 for ($i = 0; $i <= $ctype; ++$i) {
                                     $this->image['body'][$pos] = file_helpers::freadchar($lbm);
                                     --$length;
                                     ++$pos;
                                 }
                             } else {
                                 $count = 0xff - $ctype + 1;
                                 $color = file_helpers::freadchar($lbm);
                                 --$length;
                                 for ($i = 0; $i <= $count; ++$i) {
                                     $this->image['body'][$pos] = $color;
                                     ++$pos;
                                 }
                             }
                         }
                         if ($pos < $this->image['header']['width'] * $this->image['header']['height']) {
                             $this->_error("ooops? {$pos} " . $this->image['header']['width'] * $this->image['header']['height']);
                             return false;
                         }
                         break;
                 }
                 break;
             case "CMAP":
                 if ($length != 256 * 3) {
                     $this->_error("Invalid Chunk: {$chunk} with length {$length} does not have 256 rgb values");
                     return false;
                 }
                 $this->palette->set_256rgb(s2pal::read_256rgb($lbm));
                 break;
             case "DPPS":
                 // known but unused
             // known but unused
             case "CRNG":
             case "TINY":
                 fseek($lbm, $length, SEEK_CUR);
                 break;
             default:
                 $this->_error("Unknown Chunk: {$chunk} with length {$length}");
                 // Rest überspringen
                 fseek($lbm, $length, SEEK_CUR);
                 break;
         }
     }
     fclose($lbm);
     return true;
 }
 static function freadint($file)
 {
     $a = file_helpers::freadshort($file);
     $b = file_helpers::freadshort($file);
     return $b * 256 * 256 + $a;
 }