Skip to content

Commit

Permalink
resize: add spline64 filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sekrit-twc committed Mar 2, 2020
1 parent 1ea31d1 commit 6ffe4b1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/testapp/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ const zimg::static_string_map<DitherType, 4> g_dither_table{
{ "error_diffusion", DitherType::ERROR_DIFFUSION },
};

const zimg::static_string_map<std::unique_ptr<zimg::resize::Filter>(*)(double, double), 7> g_resize_table{
const zimg::static_string_map<std::unique_ptr<zimg::resize::Filter>(*)(double, double), 8> g_resize_table{
{ "point", make_filter<zimg::resize::PointFilter> },
{ "bilinear", make_filter<zimg::resize::BilinearFilter> },
{ "bicubic", make_bicubic_filter },
{ "spline16", make_filter<zimg::resize::Spline16Filter> },
{ "spline36", make_filter<zimg::resize::Spline36Filter> },
{ "spline64", make_filter<zimg::resize::Spline64Filter> },
{ "lanczos", make_lanczos_filter },
{ "unresize", make_null_filter },
};
2 changes: 1 addition & 1 deletion src/testapp/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ extern const zimg::static_string_map<zimg::colorspace::MatrixCoefficients, 12> g
extern const zimg::static_string_map<zimg::colorspace::TransferCharacteristics, 12> g_transfer_table;
extern const zimg::static_string_map<zimg::colorspace::ColorPrimaries, 12> g_primaries_table;
extern const zimg::static_string_map<zimg::depth::DitherType, 4> g_dither_table;
extern const zimg::static_string_map<std::unique_ptr<zimg::resize::Filter>(*)(double, double), 7> g_resize_table;
extern const zimg::static_string_map<std::unique_ptr<zimg::resize::Filter>(*)(double, double), 8> g_resize_table;

#endif // TABLE_H_
2 changes: 2 additions & 0 deletions src/zimg/api/zimg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ std::unique_ptr<zimg::resize::Filter> translate_resize_filter(zimg_resample_filt
return ztd::make_unique<zimg::resize::Spline16Filter>();
case ZIMG_RESIZE_SPLINE36:
return ztd::make_unique<zimg::resize::Spline36Filter>();
case ZIMG_RESIZE_SPLINE64:
return ztd::make_unique<zimg::resize::Spline64Filter>();
case ZIMG_RESIZE_LANCZOS:
param_a = std::isnan(param_a) ? zimg::resize::LanczosFilter::DEFAULT_TAPS : std::max(param_a, 1.0);
return ztd::make_unique<zimg::resize::LanczosFilter>(static_cast<unsigned>(param_a));
Expand Down
1 change: 1 addition & 0 deletions src/zimg/api/zimg.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ typedef enum zimg_resample_filter_e {
ZIMG_RESIZE_BICUBIC = 2, /**< Bicubic convolution (separable) filter. */
ZIMG_RESIZE_SPLINE16 = 3, /**< "Spline16" filter from AviSynth. */
ZIMG_RESIZE_SPLINE36 = 4, /**< "Spline36" filter from AviSynth. */
ZIMG_RESIZE_SPLINE64 = 6, /**< "Spline64" filter from AviSynth. */
ZIMG_RESIZE_LANCZOS = 5 /**< Lanczos resampling filter with variable number of taps. */
} zimg_resample_filter_e;

Expand Down
23 changes: 23 additions & 0 deletions src/zimg/resize/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ double Spline36Filter::operator()(double x) const
}


unsigned Spline64Filter::support() const { return 4; }

double Spline64Filter::operator()(double x) const
{
x = std::abs(x);

if (x < 1.0) {
return poly3(x, 1.0, -3.0 / 2911.0, -6387.0 / 2911.0, 49.0 / 41.0);
} else if (x < 2.0) {
x -= 1.0;
return poly3(x, 0.0, -2328.0 / 2911.0, 4032.0 / 2911.0, -24.0 / 41.0);
} else if (x < 3.0) {
x -= 2.0;
return poly3(x, 0.0, 582.0 / 2911.0, -1008.0 / 2911.0, 6.0 / 41.0);
} else if (x < 4.0) {
x -= 3.0;
return poly3(x, 0.0, -97.0 / 2911.0, 168.0 / 2911.0, -1.0 / 41.0);
} else {
return 0.0;
}
}


LanczosFilter::LanczosFilter(unsigned taps) : taps{ taps }
{
if (taps <= 0)
Expand Down
10 changes: 10 additions & 0 deletions src/zimg/resize/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ class Spline36Filter : public Filter {
double operator()(double x) const override;
};

/**
* Spline64 filter from Avisynth.
*/
class Spline64Filter : public Filter {
public:
unsigned support() const override;

double operator()(double x) const override;
};

/**
* Lanczos filter.
*/
Expand Down
15 changes: 15 additions & 0 deletions test/resize/filter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ TEST(FilterTest, test_spline36)
EXPECT_NEAR(0.0197368, f(-2.5), 1e-6);
}

TEST(FilterTest, test_spline64)
{
zimg::resize::Spline64Filter f;
EXPECT_EQ(4U, f.support());
check_interpolating(f);
EXPECT_NEAR(0.600352, f(0.5), 1e-6);
EXPECT_NEAR(0.600352, f(-0.5), 1e-6);
EXPECT_NEAR(-0.126760, f(1.5), 1e-6);
EXPECT_NEAR(-0.126760, f(-1.5), 1e-6);
EXPECT_NEAR(0.0316901, f(2.5), 1e-7);
EXPECT_NEAR(0.0316901, f(-2.5), 1e-7);
EXPECT_NEAR(-0.00528169, f(3.5), 1e-8);
EXPECT_NEAR(-0.00528169, f(-3.5), 1e-8);
}

TEST(FilterTest, test_lanczos)
{
for (unsigned i = 1; i < 4; ++i) {
Expand Down

0 comments on commit 6ffe4b1

Please sign in to comment.