|
| 1 | +# Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +# copy of this software and associated documentation files (the "Software"), |
| 5 | +# to deal in the Software without restriction, including without limitation |
| 6 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +# Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +# DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +from PIL import Image |
| 23 | +import os |
| 24 | +import math |
| 25 | +from pathlib import Path |
| 26 | +import argparse |
| 27 | + |
| 28 | +parser = argparse.ArgumentParser( |
| 29 | + description="Script to calculate mipmaps using the maximum value instead of the average value.\nOutput will be `<inputTexture>_maxmip.dds`" |
| 30 | +) |
| 31 | +parser.add_argument('inputTexture', help="A PNG or DDS file to be processed. DDS files will be re-compressed, so it's better to use the original source png.") |
| 32 | +parser.add_argument( |
| 33 | + '--nvttPath', |
| 34 | + help="If Nvtt_export is installed to a non-default location, the path to it can be passed in here.", |
| 35 | + default='"C:/Program Files/NVIDIA Corporation/NVIDIA Texture Tools/nvtt_export.exe"' |
| 36 | +) |
| 37 | +parser.add_argument( |
| 38 | + '-o', '--outputTexture', |
| 39 | + help="Output dds path. Defaults to inputTexture + `_mipmap.dds`", |
| 40 | + default="" |
| 41 | +) |
| 42 | +args = parser.parse_args() |
| 43 | +NVTTPath = args.nvttPath |
| 44 | +inputPath = Path(args.inputTexture) |
| 45 | +outputDds = args.outputTexture |
| 46 | + |
| 47 | + |
| 48 | +# adapted from https://stackoverflow.com/questions/14549696/mipmap-of-image-in-numpy |
| 49 | +def generate_max_mip_level(image): |
| 50 | + rows, cols, channel = image.shape |
| 51 | + if rows == 1: |
| 52 | + image = image.reshape(rows, cols // 2, 2, channel) |
| 53 | + image = image.max(axis=2) |
| 54 | + elif cols == 1: |
| 55 | + image = image.reshape(rows // 2, 2, cols, channel) |
| 56 | + image = image.max(axis=1) |
| 57 | + else: |
| 58 | + image = image.reshape(rows // 2, 2, cols // 2, 2, channel) |
| 59 | + image = image.max(axis=3).max(axis=1) |
| 60 | + return image.astype('uint8') |
| 61 | + |
| 62 | + |
| 63 | +def generate_max_mip(image): |
| 64 | + img = image.copy() |
| 65 | + rows, cols, channel = image.shape |
| 66 | + result = np.zeros((rows, cols * 2 - 1, channel), dtype='uint8') |
| 67 | + result[:, :cols, :] = img |
| 68 | + col = cols |
| 69 | + while rows > 1 or cols > 1: |
| 70 | + img = generate_max_mip_level(img) |
| 71 | + rows = img.shape[0] |
| 72 | + cols = img.shape[1] |
| 73 | + result[0:rows, col:col + cols, :] = img |
| 74 | + col += cols |
| 75 | + return result |
| 76 | + |
| 77 | + |
| 78 | +inputAsPng = inputPath |
| 79 | +if inputPath.suffix == ".dds": |
| 80 | + inputAsPng = Path(inputPath).with_suffix(".png") |
| 81 | + |
| 82 | + ddsToPngCommand = f'{NVTTPath} --no-mips -o {inputAsPng} {inputPath}' |
| 83 | + print("running: " + ddsToPngCommand) |
| 84 | + os.system(ddsToPngCommand) |
| 85 | + |
| 86 | +outputPng = inputPath.with_stem(inputPath.stem + "_maxmip").with_suffix(".png") |
| 87 | + |
| 88 | +if outputDds is None or outputDds == "": |
| 89 | + outputDds = inputPath.with_stem(inputPath.stem + "_maxmip").with_suffix(".dds") |
| 90 | + |
| 91 | +img = np.asarray(Image.open(inputAsPng)) |
| 92 | +print(f"processing texture with dimensions {img.shape}") |
| 93 | +Image.fromarray(generate_max_mip(img)).save(outputPng) |
| 94 | +rows, cols, channel = img.shape |
| 95 | +levels = int(max(math.log2(rows), math.log2(cols)))+1 |
| 96 | + |
| 97 | +PngToDdsCommand = f'{NVTTPath} -f bc4 --no-mip-gamma-correct --mip-extract-from-atlas --no-mips --atlas-mips {levels} -o {outputDds} {outputPng}' |
| 98 | +print("running: " + PngToDdsCommand) |
| 99 | +os.system(PngToDdsCommand) |
| 100 | +print(f"generated file {outputDds}") |
0 commit comments