Skip to content

Commit 4662344

Browse files
committed
feat: Implement NICE/Lanczos-3 filtering
1 parent bc129fb commit 4662344

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/vtfpp/ImageConversion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ enum class ResizeFilter {
375375

376376
// User-defined
377377
KAISER = 100,
378+
LANCZOS3 = 101,
378379
};
379380

380381
enum class ResizeMethod {

src/vtfpp/ImageConversion.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,24 @@ std::vector<std::byte> ImageConversion::resizeImageData(std::span<const std::byt
19421942
stbir_set_filter_callbacks(&resize, KAISER_FILTER, KAISER_SUPPORT, KAISER_FILTER, KAISER_SUPPORT);
19431943
break;
19441944
}
1945+
case ResizeFilter::LANCZOS3: {
1946+
static constexpr auto SINC = [](const float x) -> float {
1947+
if (x == 0.f) return 1.f;
1948+
const float a = x * math::pi_f32;
1949+
return sinf(a) / a;
1950+
};
1951+
// Normally you would max(1, inv_scale), but stb is weird and immediately un-scales the result when downsampling.
1952+
// See stb_image_resize2.h L2977
1953+
static constexpr auto LANCZOS3_SUPPORT = [](float inv_scale, void*) -> float {
1954+
return inv_scale * 3.f;
1955+
};
1956+
static constexpr auto LANCZOS3_FILTER = [](float x, float inv_scale, void*) -> float {
1957+
if (x >= 3.f || x <= -3.f) return 0.f;
1958+
return SINC(x) * SINC(x / 3.f);
1959+
};
1960+
stbir_set_filter_callbacks(&resize, LANCZOS3_FILTER, LANCZOS3_SUPPORT, LANCZOS3_FILTER, LANCZOS3_SUPPORT);
1961+
break;
1962+
}
19451963
}
19461964
stbir_resize_extended(&resize);
19471965
};

0 commit comments

Comments
 (0)