diff --git a/.gitignore b/.gitignore index 63f7e81..def7938 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,6 @@ sysinfo.txt # Crashlytics generated file crashlytics-build.properties +# Python files +**/__pycache__ diff --git a/Scripts/README.md b/Scripts/README.md index cd814d4..8f019b2 100644 --- a/Scripts/README.md +++ b/Scripts/README.md @@ -1,19 +1,23 @@ # Scripts -Bash scripts for interacting with files +Python scripts for interacting with files ## Installation -All files require bash to be installed (default location of `/bin/bash`). +All files require python3 to be installed. -`convert2dds` and `dds2png` require [ImageMagick](https://imagemagick.org/index.php) and [Nvidia Texture Tools](https://github.com/HeliumProject/NvidiaTextureTools) to be installed and available in your `$PATH`. +`ddscompress`, `ddsdecompress` and `ddsck` require [ImageMagick](https://imagemagick.org/index.php) and [Nvidia Texture Tools](https://github.com/HeliumProject/NvidiaTextureTools) to be installed and available in your `$PATH`. ## Usage -### convert2dds +### ddscompress.py -converts any number of files to `.dds` format, automatically choosing dxt1 or dxt5 depending on if the source image has anything in its alpha channel. Designed to be used with a glob, e.g: `convert2dds *.png` +Converts any number of files to `.dds` format, automatically choosing dxt1 or dxt5 depending on if the source image has anything in its alpha channel. Designed to be used with a glob, e.g: `ddscompress.py *.png` -### dds2png +### ddsdecompress.py -converts any number of files to `.png` from `.dds`. Designed to be used with a glob, e.g: `dds2png *.dds` \ No newline at end of file +Converts any number of files to `.png` from `.dds`. Designed to be used with a glob, e.g: `ddsdecompress.py *.dds` + +### ddsck.py + +Checks any number of dds files for common issues, including formats not supported by KSP, and DXT5 textures that don't use the alpha channel. Designed to be used with a glob, e.g: `ddsck.py *.dds` \ No newline at end of file diff --git a/Scripts/convert2dds b/Scripts/convert2dds deleted file mode 100755 index 0dcffed..0000000 --- a/Scripts/convert2dds +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -mkdir -p /tmp/dds > /dev/null -tool="nvcompress" - -for file in "$@" -do - echo "$file" - dirname=$(dirname "$file") - basename=$(basename "$file") - extname="${file#${file%.*}}" - filename=$(basename "$basename" "$extname") - - convert -flip "$file" "/tmp/dds/$basename" - alpha=$(convert "$file" -resize 1x1 -format "%[fx:int(255*a+.5)]" info:-) - if [ "$tool" == "crunch" ] - then - if [ $alpha -lt 255 ] - then - format="-dxt5" - else - format="-dxt1" - fi - - crunch -quiet -file "/tmp/dds/$basename" -fileformat dds $format && rm $file - else - if [ $alpha -lt 255 ] - then - format="-bc3" - else - format="-bc1" - fi - - nvcompress $format "/tmp/dds/$basename" "$dirname/$filename.dds" && rm $file - fi -done \ No newline at end of file diff --git a/Scripts/dds.py b/Scripts/dds.py new file mode 100644 index 0000000..4d09135 --- /dev/null +++ b/Scripts/dds.py @@ -0,0 +1,30 @@ +import subprocess +import re + +def alpha(file): + result = subprocess.run(["convert", file, "-resize", "1x1", "-format", "%[fx:int(255*a+.5)]", "info:-"], capture_output=True) + result.check_returncode() + return int(result.stdout) + +def flip(file, output): + result = subprocess.run(['convert', '-flip', file, output], capture_output=True) + result.check_returncode() + +def nvcompress(format, file, output): + result = subprocess.run(['nvcompress', format, file, output], capture_output=True) + result.check_returncode() + print(result) + +def nvdecompress(file, output): + result = subprocess.run(['nvdecompress', '-format', 'png', file, output], capture_output=True) + result.check_returncode() + +def nvinfo(file): + result = subprocess.run(['nvddsinfo', file], capture_output=True) + result.check_returncode() + + info = { + "format": re.search(r"FourCC: '(.{4})'", str(result.stdout)).group(1) + } + + return info \ No newline at end of file diff --git a/Scripts/dds2png b/Scripts/dds2png deleted file mode 100755 index b8eeb55..0000000 --- a/Scripts/dds2png +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -mkdir -p /tmp/dds > /dev/null - -for file in "$@" -do - echo "$file" - dirname=$(dirname "$file") - basename=$(basename "$file") - extname="${file#${file%.*}}" - filename=$(basename "$basename" "$extname") - - convert -flip "$file" "$dirname/$filename.png" && rm $file -done \ No newline at end of file diff --git a/Scripts/ddsck.py b/Scripts/ddsck.py new file mode 100755 index 0000000..6ad7891 --- /dev/null +++ b/Scripts/ddsck.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import dds + +for argv in sys.argv[1:]: + file = os.path.abspath(argv) + output = os.path.splitext(file)[0] + ".png" + + info = dds.nvinfo(file) + format = info["format"] + alpha = dds.alpha(file) + + if format == "DXT1": + pass + elif format == "DXT5": + if alpha > 254: + print(f'[{argv}]: Image is DXT5 but has no alpha channel') + else: + print(f'[{argv}]: incompatible format') \ No newline at end of file diff --git a/Scripts/ddscompress.py b/Scripts/ddscompress.py new file mode 100755 index 0000000..f549f26 --- /dev/null +++ b/Scripts/ddscompress.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import tempfile +import dds + +with tempfile.TemporaryDirectory(prefix="dds") as tempDir: + for argv in sys.argv[1:]: + print(f'[{argv}]: converting to dds') + file = os.path.abspath(argv) + tmpOutput = os.path.join(tempDir, os.path.basename(file)) + output = os.path.splitext(file)[0] + ".dds" + + alpha = dds.alpha(file) + dds.flip(file, tmpOutput) + + print(alpha) + if alpha < 255: + format = "-bc3" + else: + format = "-bc1" + print(format) + dds.nvcompress(format, tmpOutput, output) + os.remove(file) \ No newline at end of file diff --git a/Scripts/ddsdecompress.py b/Scripts/ddsdecompress.py new file mode 100755 index 0000000..ff78da5 --- /dev/null +++ b/Scripts/ddsdecompress.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import dds + +for argv in sys.argv[1:]: + print(f'[{argv}]: converting from dds') + file = os.path.abspath(argv) + output = os.path.splitext(file)[0] + ".png" + + dds.nvdecompress(file, output) + os.remove(file) \ No newline at end of file