From 9e1fcb31ef3a1ab26cfae68b11d8c99ca4fc8657 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Thu, 14 Feb 2019 15:30:05 +0100 Subject: [PATCH] guided filter: further small reduction of peak memory usage --- rtengine/guidedfilter.cc | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/rtengine/guidedfilter.cc b/rtengine/guidedfilter.cc index 1aad138e3..78f6306ad 100644 --- a/rtengine/guidedfilter.cc +++ b/rtengine/guidedfilter.cc @@ -32,7 +32,8 @@ #include "boxblur.h" #include "rescale.h" #include "imagefloat.h" - +#define BENCHMARK +#include "StopWatch.h" namespace rtengine { #if 0 @@ -78,6 +79,7 @@ int calculate_subsampling(int w, int h, int r) void guidedFilter(const array2D &guide, const array2D &src, array2D &dst, int r, float epsilon, bool multithread, int subsampling) { + BENCHFUN const int W = src.width(); const int H = src.height(); @@ -135,16 +137,6 @@ void guidedFilter(const array2D &guide, const array2D &src, array2 const array2D &p = src; array2D &q = dst; - AlignedBuffer blur_buf(I.width() * I.height()); - const auto f_mean = - [&](array2D &d, array2D &s, int rad) -> void - { - rad = LIM(rad, 0, (min(s.width(), s.height()) - 1) / 2 - 1); - float **src = s; - float **dst = d; - boxblur(src, dst, blur_buf.data, rad, rad, s.width(), s.height()); - }; - const auto f_subsample = [=](array2D &d, const array2D &s) -> void { @@ -153,8 +145,18 @@ void guidedFilter(const array2D &guide, const array2D &src, array2 const auto f_upsample = f_subsample; - const int w = W / subsampling; - const int h = H / subsampling; + const size_t w = W / subsampling; + const size_t h = H / subsampling; + + AlignedBuffer blur_buf(w * h); + const auto f_mean = + [&](array2D &d, array2D &s, int rad) -> void + { + rad = LIM(rad, 0, (min(s.width(), s.height()) - 1) / 2 - 1); + float **src = s; + float **dst = d; + boxblur(src, dst, blur_buf.data, rad, rad, s.width(), s.height()); + }; array2D I1(w, h); array2D p1(w, h); @@ -203,6 +205,9 @@ void guidedFilter(const array2D &guide, const array2D &src, array2 apply(SUBMUL, b, a, meanI, meanp); DEBUG_DUMP(b); + meanI.free(); // frees w * h * 4 byte + meanp.free(); // frees w * h * 4 byte + array2D &meana = a; f_mean(meana, a, r1); DEBUG_DUMP(meana); @@ -211,7 +216,7 @@ void guidedFilter(const array2D &guide, const array2D &src, array2 f_mean(meanb, b, r1); DEBUG_DUMP(meanb); - blur_buf.resize(0); // frees W * H * 4 byte + blur_buf.resize(0); // frees w * h * 4 byte array2D meanA(W, H); f_upsample(meanA, meana);