forked from drewcassidy/KSP-Toolkit
67 lines
2.1 KiB
Python
Executable File
67 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Checks any number of dds files for common issues"""
|
|
|
|
import sys
|
|
import os
|
|
import os.path
|
|
import enum
|
|
import argparse
|
|
import dds
|
|
|
|
class Check(enum.Enum):
|
|
"""Kinds of check to perform"""
|
|
TRANPARENCY = 1
|
|
FORMAT = 2
|
|
|
|
parser = argparse.ArgumentParser(description= __doc__)
|
|
parser.add_argument('files', type=str, nargs='*', help = "input dds files")
|
|
|
|
parser.add_argument('--transparency', '-t',
|
|
action='append_const', dest='checks', const=Check.TRANPARENCY,
|
|
help = "Check textures for unnecessary transparency map")
|
|
parser.add_argument('--format', '-f',
|
|
action='append_const', dest='checks', const=Check.FORMAT,
|
|
help = "Check texture formats")
|
|
parser.add_argument('--all', '-a',
|
|
action='store_const', dest='checks', const=[Check.TRANPARENCY, Check.FORMAT],
|
|
help = "Perform all checks")
|
|
|
|
parser.add_argument('--formats', nargs='+', default = ['DXT1', 'DXT5'],
|
|
help="Valid texture formats to check against (default: %(default)s)")
|
|
|
|
parser.add_argument('--convertcmd', type=str, metavar='CMD', default=dds.convertcmd,
|
|
help="name of imagemagick's convert tool (default: %(default)s)")
|
|
|
|
args = parser.parse_args()
|
|
|
|
dds.convertcmd = args.convertcmd
|
|
|
|
def printerror(path, shortpath, message):
|
|
"""either prints a message or just the path depending on if stdout is a tty"""
|
|
if sys.stdout.isatty():
|
|
print(f'[{shortpath}]: {message}')
|
|
else:
|
|
sys.stdout.write(path+'\n')
|
|
|
|
def checkfile(shortpath, checks, formats):
|
|
"""check a single dds file for common issues"""
|
|
path = os.path.abspath(shortpath)
|
|
|
|
with open(path, 'rb') as file:
|
|
ddsfile = dds.DDSFile()
|
|
ddsfile.read_header(file)
|
|
four_cc = ddsfile.four_cc
|
|
if four_cc == '':
|
|
four_cc = "uncompressed"
|
|
|
|
alpha = dds.alpha(path)
|
|
|
|
if Check.TRANPARENCY in checks and four_cc == 'DXT5' and alpha > 254:
|
|
printerror(path, shortpath, 'Image is DXT5 but has no alpha channel')
|
|
|
|
if Check.FORMAT in checks and four_cc not in formats:
|
|
printerror(path, shortpath, f'Incompatible format: {four_cc}')
|
|
|
|
for shortpath in args.files:
|
|
checkfile(shortpath, args.checks, args.formats)
|