From 96d7338fd4dca180615e5f6876d2c690ad9831aa Mon Sep 17 00:00:00 2001 From: koerismo Date: Mon, 26 May 2025 03:07:50 +0200 Subject: [PATCH 1/3] vtfpp: implement NICE/Lanczos-3 filtering --- include/vtfpp/ImageConversion.h | 2 ++ lang/c/include/vtfppc/ImageConversion.h | 1 + lang/python/src/vtfpp.h | 1 + src/vtfpp/ImageConversion.cpp | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/include/vtfpp/ImageConversion.h b/include/vtfpp/ImageConversion.h index 01c4444e1..2a221b8d9 100644 --- a/include/vtfpp/ImageConversion.h +++ b/include/vtfpp/ImageConversion.h @@ -375,6 +375,8 @@ enum class ResizeFilter { // User-defined KAISER = 100, + /// Valve NICE filtering, equivalent to Lanczos-3 + NICE = 101, }; enum class ResizeMethod { diff --git a/lang/c/include/vtfppc/ImageConversion.h b/lang/c/include/vtfppc/ImageConversion.h index dd13dc271..c1bc146b2 100644 --- a/lang/c/include/vtfppc/ImageConversion.h +++ b/lang/c/include/vtfppc/ImageConversion.h @@ -38,6 +38,7 @@ typedef enum { VTFPP_IMAGE_CONVERSION_RESIZE_FILTER_MITCHELL, VTFPP_IMAGE_CONVERSION_RESIZE_FILTER_POINT_SAMPLE, VTFPP_IMAGE_CONVERSION_RESIZE_FILTER_KAISER = 100, + VTFPP_IMAGE_CONVERSION_RESIZE_FILTER_NICE, } vtfpp_image_conversion_resize_filter_e; typedef enum { diff --git a/lang/python/src/vtfpp.h b/lang/python/src/vtfpp.h index 33355d672..0ffc1a479 100644 --- a/lang/python/src/vtfpp.h +++ b/lang/python/src/vtfpp.h @@ -171,6 +171,7 @@ void register_python(py::module_& m) { .value("MITCHELL", ResizeFilter::MITCHELL) .value("POINT_SAMPLE", ResizeFilter::POINT_SAMPLE) .value("KAISER", ResizeFilter::KAISER) + .value("NICE", ResizeFilter::NICE) .export_values(); py::enum_(ImageConversion, "ResizeMethod") diff --git a/src/vtfpp/ImageConversion.cpp b/src/vtfpp/ImageConversion.cpp index e803f0e0f..137f25d2e 100644 --- a/src/vtfpp/ImageConversion.cpp +++ b/src/vtfpp/ImageConversion.cpp @@ -1942,6 +1942,24 @@ std::vector ImageConversion::resizeImageData(std::span float { + if (x == 0.f) return 1.f; + const float a = x * math::pi_f32; + return sinf(a) / a; + }; + static constexpr auto NICE_FILTER = [](float x, float, void*) -> float { + if (x >= 3.f || x <= -3.f) return 0.f; + return SINC(x) * SINC(x / 3.f); + }; + // Normally you would max(1, invScale), but stb is weird and immediately un-scales the result when downsampling. + // See stb_image_resize2.h L2977 (stbir__get_filter_pixel_width) + static constexpr auto NICE_SUPPORT = [](float invScale, void*) -> float { + return invScale * 3.f; + }; + stbir_set_filter_callbacks(&resize, NICE_FILTER, NICE_SUPPORT, NICE_FILTER, NICE_SUPPORT); + break; + } } stbir_resize_extended(&resize); }; From 809557de745bf02b6c80a38078a2fc2eea89596e Mon Sep 17 00:00:00 2001 From: craftablescience Date: Sun, 25 May 2025 17:18:50 -0700 Subject: [PATCH 2/3] docs: give credit for NICE/Lanczos-3 resize filter --- README.md | 1 + docs/index.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 6283435b7..ea556917f 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,7 @@ found on PyPI in the [sourcepp](https://pypi.org/project/sourcepp) package. - `vpkpp`'s ORE parser is based on [reverse-engineering work](https://github.com/erysdren/narbacular-drop-tools) by [@erysdren](https://github.com/erysdren). - `vpkpp`'s VPP parser was contributed by [@erysdren](https://github.com/erysdren). - `vpkpp`'s WAD3 parser/writer was contributed by [@ozxybox](https://github.com/ozxybox). +- `vtfpp`'s NICE/Lanczos-3 resize filter support was contributed by [@koerismo](https://github.com/koerismo). - `vtfpp`'s SHT parser/writer was contributed by [@Trico Everfire](https://github.com/Trico-Everfire). - `vtfpp`'s VTF write support is loosely based on work by [@Trico Everfire](https://github.com/Trico-Everfire). - `vtfpp`'s HDRI to cubemap conversion code is modified from the [HdriToCubemap](https://github.com/ivarout/HdriToCubemap) library by [@ivarout](https://github.com/ivarout). diff --git a/docs/index.md b/docs/index.md index 5db32b7f0..39f512ada 100644 --- a/docs/index.md +++ b/docs/index.md @@ -351,6 +351,7 @@ found on PyPI in the [sourcepp](https://pypi.org/project/sourcepp) package. - `vpkpp`'s ORE parser is based on [reverse-engineering work](https://github.com/erysdren/narbacular-drop-tools) by [@erysdren](https://github.com/erysdren). - `vpkpp`'s VPP parser was contributed by [@erysdren](https://github.com/erysdren). - `vpkpp`'s WAD3 parser/writer was contributed by [@ozxybox](https://github.com/ozxybox). +- `vtfpp`'s NICE/Lanczos-3 resize filter support was contributed by [@koerismo](https://github.com/koerismo). - `vtfpp`'s SHT parser/writer was contributed by [@Trico Everfire](https://github.com/Trico-Everfire). - `vtfpp`'s VTF write support is loosely based on work by [@Trico Everfire](https://github.com/Trico-Everfire). - `vtfpp`'s HDRI to cubemap conversion code is modified from the [HdriToCubemap](https://github.com/ivarout/HdriToCubemap) library by [@ivarout](https://github.com/ivarout). From 0bd83f69b002a5f807521987c1682ef6beaa9be5 Mon Sep 17 00:00:00 2001 From: craftablescience Date: Sun, 25 May 2025 17:20:38 -0700 Subject: [PATCH 3/3] ci: no need to install ninja now --- .github/workflows/build.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a773d3a4d..444340409 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,14 +28,6 @@ jobs: arch: x64 spectre: true - - name: Install Dependencies [macOS] - if: matrix.os == 'macos-latest' - run: brew install ninja - - - name: Install Dependencies [Linux] - if: matrix.os == 'ubuntu-latest' - run: sudo apt update && sudo apt install -y ninja-build - - name: Configure CMake run: cmake -G "Ninja" -B build -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DSOURCEPP_BUILD_TESTS=ON