11#include < algorithm>
2- #include < emmintrin.h>
32#include < string>
43
54#include " vinverse.h"
@@ -150,6 +149,8 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
150149
151150 if constexpr (eclip)
152151 {
152+ constexpr auto peak = std::numeric_limits<T>::max ();
153+
153154 for (int y = 0 ; y < height; ++y)
154155 {
155156 for (int x = 0 ; x < width; ++x)
@@ -173,8 +174,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
173174 const int minm = srcp[x] - amnt_;
174175 const int maxf = srcp[x] + amnt_;
175176
176- df = std::max (df, minm);
177- dstp[x] = std::min (df, maxf);
177+ dstp[x] = std::clamp (std::clamp (df, minm, maxf), 0 , static_cast <int >(peak));
178178 }
179179 }
180180 else
@@ -190,8 +190,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
190190 const int minm = srcp[x] - amnt_;
191191 const int maxf = srcp[x] + amnt_;
192192
193- df = std::max (df, minm);
194- dstp[x] = std::min (df, maxf);
193+ dstp[x] = std::clamp (std::clamp (df, minm, maxf), 0 , static_cast <int >(peak));
195194 }
196195 }
197196
@@ -227,8 +226,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
227226 const int minm = srcp[x] - amnt_;
228227 const int maxf = srcp[x] + amnt_;
229228
230- df = std::max (df, minm);
231- dstp[x] = std::min (df, maxf);
229+ dstp[x] = std::clamp (df, minm, maxf);
232230 }
233231 }
234232 else
@@ -245,8 +243,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
245243 const int minm = srcp[x] - amnt_;
246244 const int maxf = srcp[x] + amnt_;
247245
248- df = std::max (df, minm);
249- dstp[x] = std::min (df, maxf);
246+ dstp[x] = std::clamp (df, minm, maxf);
250247 }
251248 }
252249
@@ -260,7 +257,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
260257
261258template <typename T, VinverseMode mode, bool eclip, bool thresh>
262259Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, int uv, float scl, int opt, PClip clip2, int thr, IScriptEnvironment* env)
263- : GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), opt_(opt), clip2_(clip2), thr_(thr), blur3_buffer(nullptr ), blur6_buffer(nullptr )
260+ : GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), clip2_(clip2), thr_(thr), blur3_buffer(nullptr ), blur6_buffer(nullptr )
264261{
265262 if (!vi.IsPlanar ())
266263 env->ThrowError (" Vinverse: only planar input is supported!" );
@@ -274,18 +271,18 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
274271 env->ThrowError (" Vinverse: amnt must be greater than 0 and less than or equal to %s!" , std::to_string (peak).c_str ());
275272 if (uv < 1 || uv > 3 )
276273 env->ThrowError (" Vinverse: uv must be set to 1, 2, or 3!" );
277- if (opt_ < -1 || opt_ > 3 )
274+ if (opt < -1 || opt > 3 )
278275 env->ThrowError (" Vinverse: opt must be between -1..3." );
279276
280- avx512 = !!(env->GetCPUFlags () & CPUF_AVX512F);
281- avx2 = !!(env->GetCPUFlags () & CPUF_AVX2);
282- sse2 = !!(env->GetCPUFlags () & CPUF_SSE2);
277+ const bool avx512 = !!(env->GetCPUFlags () & CPUF_AVX512F);
278+ const bool avx2 = !!(env->GetCPUFlags () & CPUF_AVX2);
279+ const bool sse2 = !!(env->GetCPUFlags () & CPUF_SSE2);
283280
284- if (!avx512 && opt_ == 3 )
281+ if (!avx512 && opt == 3 )
285282 env->ThrowError (" Vinverse: opt=3 requires AVX512F." );
286- if (!avx2 && opt_ == 2 )
283+ if (!avx2 && opt == 2 )
287284 env->ThrowError (" Vinverse: opt=2 requires AVX2." );
288- if (!sse2 && opt_ == 1 )
285+ if (!sse2 && opt == 1 )
289286 env->ThrowError (" Vinverse: opt=1 requires SSE2." );
290287
291288 if constexpr (eclip)
@@ -303,7 +300,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
303300 if (thr_ < 0 || thr_ > peak)
304301 env->ThrowError (" Vinverse: thr must be between 0..%s!" , std::to_string (peak).c_str ());
305302
306- if ((avx512 && opt_ < 0 ) || opt_ == 3 )
303+ if ((avx512 && opt < 0 ) || opt == 3 )
307304 {
308305 pb_pitch = (vi.width + 63 ) & ~63 ;
309306
@@ -350,7 +347,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
350347
351348 fin_plane = &Vinverse::finalize_plane_avx512;
352349 }
353- else if ((avx2 && opt_ < 0 ) || opt_ == 2 )
350+ else if ((avx2 && opt < 0 ) || opt == 2 )
354351 {
355352 pb_pitch = (vi.width + 31 ) & ~31 ;
356353
@@ -397,7 +394,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
397394
398395 fin_plane = &Vinverse::finalize_plane_avx2;
399396 }
400- else if ((sse2 && opt_ < 0 ) || opt_ == 1 )
397+ else if ((sse2 && opt < 0 ) || opt == 1 )
401398 {
402399 pb_pitch = (vi.width + 15 ) & ~15 ;
403400
0 commit comments