Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- error_reporting(-1);
- function d (...$args) {
- var_dump(...$args);
- }
- function dd (...$args) {
- d(...$args);
- exit;
- }
- function changeExt (string $filePath, string $newExt): string {
- return preg_replace('~\.[^.]++\z~', $newExt, $filePath);
- }
- class BaseObject {
- public function __construct (array $fields = []) {
- foreach ($fields as $key => $value) {
- $this->{$key} = $value;
- }
- }
- }
- class H3lDefFrame extends BaseObject {
- public string $fileName;
- //public string $pngFilePath;
- public int $groupInd;
- public int $frameInd;
- public function getPngFilePath (): string {
- return dirname($this->filePath) . "/{$this->groupInd}_{$this->frameInd}.png";
- }
- }
- class BmpToPngConverter {
- protected string $bmpFilePath;
- protected string $pngFilePath;
- public function __construct (string $bmpFilePath, string $pngFilePath) {
- $this->bmpFilePath = $bmpFilePath;
- $this->pngFilePath = $pngFilePath;
- }
- public function convert () {
- $image = $this->loadImage($this->bmpFilePath);
- $width = imagesx($image);
- $height = imagesy($image);
- $replacements = [
- 0x0000ffff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 0),
- 0x00ff00ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 128 / 2),
- 0x00ff96ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 96 / 2),
- 0x00ff64ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 64 / 2),
- 0x00ff32ff => imagecolorallocatealpha($image, 0, 0, 0, 127 - 32 / 2),
- ];
- for ($j = 0; $j < $height; $j++) {
- for ($i = 0; $i < $width; $i++) {
- $color = imagecolorat($image, $i, $j);
- $newColor = $replacements[$color] ?? null;
- isset($newColor) and imagesetpixel($image, $i, $j, $newColor);
- }
- }
- imagesavealpha($image, true);
- imagepng($image, $this->pngFilePath, 9);
- imagedestroy($image);
- } // .function convert
- protected function loadImage (string $imageFilePath) {
- $origImage = imagecreatefromstring(file_get_contents($imageFilePath));
- $result = imagecreatetruecolor(imagesx($origImage), imagesy($origImage));
- imagecopy($result, $origImage, 0, 0, 0, 0, imagesx($origImage), imagesy($origImage));
- imagealphablending($result, false);
- imagedestroy($origImage);
- return $result;
- }
- } // .class BmpToPngConverter
- class H3lDefToPngConverter {
- protected string $inputDir;
- protected string $outputDir;
- protected string $configFilePath;
- protected array $defGRoups;
- public function __construct (array $opts = []) {
- $this->inputDir = (string) $opts['inputDir'];
- $this->outputDir = (string) ($opts['outputDir'] ?? $this->inputDir);
- $this->configFilePath = (string) $opts['configFilePath'];
- }
- public function convert () {
- foreach ($this->loadDefGroupsFromConfigFile() as $groupInd => $group) {
- foreach ($group as $frameInd => $fileName) {
- $frameConverter = new BmpToPngConverter("{$this->inputDir}/{$fileName}", "{$this->outputDir}/{$groupInd}_{$frameInd}.png");
- $frameConverter->convert();
- }
- }
- }
- public function loadDefGroupsFromConfigFile (): array {
- $lines = array_map(fn ($x) => trim($x), explode("\n", trim(file_get_contents($this->configFilePath))));
- $groups = [];
- $group = [];
- foreach (array_slice($lines, 15) as $line) {
- if (preg_match('~\A(?<fileName>[^ ]++) ++(?<groupInd>\d++)~', $line, $m)) {
- $group[] = $m['fileName'];
- $groups[$m['groupInd']] = $group;
- $group = [];
- } else {
- $group[] = $line;
- }
- }
- return $groups;
- }
- } // .class H3lDefToPngConverter
- $converter = new H3lDefToPngConverter([
- 'inputDir' => __DIR__,
- 'outputDir' => __DIR__,
- 'configFilePath' => __DIR__ . '/cefres_.h3l',
- ]);
- $converter->convert();
- // $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');
- // $converter->convert();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement