Advertisement
Guest User

Heroes 3 h3l def frames bmp => png converter

a guest
May 12th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3. error_reporting(-1);
  4.  
  5. function d (...$args) {
  6.   var_dump(...$args);
  7. }
  8.  
  9.  
  10. function dd (...$args) {
  11.   d(...$args);
  12.  
  13.   exit;
  14. }
  15.  
  16. function changeExt (string $filePath, string $newExt): string {
  17.   return preg_replace('~\.[^.]++\z~', $newExt, $filePath);
  18. }
  19.  
  20. class BaseObject {
  21.   public function __construct (array $fields = []) {
  22.     foreach ($fields as $key => $value) {
  23.       $this->{$key} = $value;
  24.     }
  25.   }
  26. }
  27.  
  28. class H3lDefFrame extends BaseObject {
  29.   public string $fileName;
  30.   //public string $pngFilePath;
  31.   public int    $groupInd;
  32.   public int    $frameInd;
  33.  
  34.   public function getPngFilePath (): string {
  35.     return dirname($this->filePath) . "/{$this->groupInd}_{$this->frameInd}.png";
  36.   }
  37. }
  38.  
  39. class BmpToPngConverter {
  40.   protected string $bmpFilePath;
  41.   protected string $pngFilePath;
  42.  
  43.   public function __construct (string $bmpFilePath, string $pngFilePath) {
  44.     $this->bmpFilePath = $bmpFilePath;
  45.     $this->pngFilePath = $pngFilePath;
  46.   }
  47.  
  48.   public function convert () {
  49.     $image  = $this->loadImage($this->bmpFilePath);
  50.     $width  = imagesx($image);
  51.     $height = imagesy($image);
  52.  
  53.     $replacements = [
  54.       0x0000ffff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 0),
  55.       0x00ff00ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 128 / 2),
  56.       0x00ff96ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 96 / 2),
  57.       0x00ff64ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 64 / 2),
  58.       0x00ff32ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 32 / 2),
  59.     ];
  60.  
  61.     for ($j = 0; $j < $height; $j++) {
  62.       for ($i = 0; $i < $width; $i++) {
  63.         $color    = imagecolorat($image, $i, $j);
  64.         $newColor = $replacements[$color] ?? null;
  65.         isset($newColor) and imagesetpixel($image, $i, $j, $newColor);
  66.       }
  67.     }
  68.  
  69.     imagesavealpha($image, true);
  70.     imagepng($image, $this->pngFilePath, 9);
  71.     imagedestroy($image);
  72.   } // .function convert
  73.  
  74.   protected function loadImage (string $imageFilePath) {
  75.     $origImage = imagecreatefromstring(file_get_contents($imageFilePath));
  76.     $result    = imagecreatetruecolor(imagesx($origImage), imagesy($origImage));
  77.     imagecopy($result, $origImage, 0, 0, 0, 0, imagesx($origImage), imagesy($origImage));
  78.     imagealphablending($result, false);
  79.     imagedestroy($origImage);
  80.  
  81.     return $result;
  82.   }
  83. } // .class BmpToPngConverter
  84.  
  85. class H3lDefToPngConverter {
  86.   protected string $inputDir;
  87.   protected string $outputDir;
  88.   protected string $configFilePath;
  89.   protected array $defGRoups;
  90.  
  91.   public function __construct (array $opts = []) {
  92.     $this->inputDir       = (string) $opts['inputDir'];
  93.     $this->outputDir      = (string) ($opts['outputDir'] ?? $this->inputDir);
  94.     $this->configFilePath = (string) $opts['configFilePath'];
  95.   }
  96.  
  97.   public function convert () {
  98.     foreach ($this->loadDefGroupsFromConfigFile() as $groupInd => $group) {
  99.       foreach ($group as $frameInd => $fileName) {
  100.         $frameConverter = new BmpToPngConverter("{$this->inputDir}/{$fileName}", "{$this->outputDir}/{$groupInd}_{$frameInd}.png");
  101.         $frameConverter->convert();
  102.       }
  103.     }
  104.   }
  105.  
  106.   public function loadDefGroupsFromConfigFile (): array {
  107.     $lines = array_map(fn ($x) => trim($x), explode("\n", trim(file_get_contents($this->configFilePath))));
  108.  
  109.     $groups = [];
  110.     $group  = [];
  111.  
  112.     foreach (array_slice($lines, 15) as $line) {
  113.       if (preg_match('~\A(?<fileName>[^ ]++) ++(?<groupInd>\d++)~', $line, $m)) {
  114.         $group[]                = $m['fileName'];
  115.         $groups[$m['groupInd']] = $group;
  116.         $group                  = [];
  117.       } else {
  118.         $group[] = $line;
  119.       }
  120.     }
  121.  
  122.     return $groups;
  123.   }
  124. } // .class H3lDefToPngConverter
  125.  
  126. $converter = new H3lDefToPngConverter([
  127.   'inputDir'       => __DIR__,
  128.   'outputDir'      => __DIR__,
  129.   'configFilePath' => __DIR__ . '/cefres_.h3l',
  130. ]);
  131.  
  132. $converter->convert();
  133.  
  134. // $converter = new BmpToPngConverter('D:\Heroes 3\Mods\Mixed Neutrals\Data\Defs\cefres.def\cefres29.bmp', 'D:\Heroes 3\Mods\Mixed Neutrals\Data\Defs\cefres.def\cefres29.png');
  135. // $converter->convert();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement