From 949aaecaa6754ffa7322c32da69a01ff3048d96d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 22 Jan 2021 22:53:31 +0900 Subject: [PATCH] Some refactoring --- src/blue_noise.cpp | 109 +++------------------------------------------ src/blue_noise.hpp | 77 +++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 109 deletions(-) diff --git a/src/blue_noise.cpp b/src/blue_noise.cpp index 8c8dbcb..4fe6056 100644 --- a/src/blue_noise.cpp +++ b/src/blue_noise.cpp @@ -3,10 +3,6 @@ #include #include #include -#include -#include -#include -#include #ifndef NDEBUG # include @@ -169,6 +165,8 @@ std::vector dither::blue_noise(std::size_t width, std::size_t height, std: std::size_t iterations = 0; //#endif + std::size_t filter_size = (width + height) / 2; + while(true) { //#ifndef NDEBUG // if(++iterations % 10 == 0) { @@ -176,57 +174,8 @@ std::vector dither::blue_noise(std::size_t width, std::size_t height, std: // } //#endif // get filter values - if(threads == 1) { - for(std::size_t y = 0; y < height; ++y) { - for(std::size_t x = 0; x < width; ++x) { - filter_out[internal::twoToOne(x, y, width)] = - internal::filter(pbp, x, y, width, height); - } - } - } else { - if(threads == 0) { - threads = 10; - } - std::size_t active_count = 0; - std::mutex cv_mutex; - std::condition_variable cv; - for(std::size_t i = 0; i < count; ++i) { - { - std::unique_lock lock(cv_mutex); - active_count += 1; - } - std::thread t([] (std::size_t *ac, std::mutex *cvm, - std::condition_variable *cv, std::size_t i, - const std::vector *pbp, std::size_t width, - std::size_t height, std::vector *fout) { - std::size_t x, y; - std::tie(x, y) = internal::oneToTwo(i, width); - (*fout)[i] = internal::filter(*pbp, x, y, width, height); - std::unique_lock lock(*cvm); - *ac -= 1; - cv->notify_all(); - }, - &active_count, &cv_mutex, &cv, i, &pbp, width, height, &filter_out); - t.detach(); - - std::unique_lock lock(cv_mutex); - while(active_count >= threads) { -#ifndef NDEBUG -// std::cout << "0, active_count = " << active_count -// << ", pre wait_for" << std::endl; -#endif - cv.wait_for(lock, std::chrono::seconds(1)); -#ifndef NDEBUG -// std::cout << "0, active_count = " << active_count -// << ", post wait_for" << std::endl; -#endif - } - } - std::unique_lock lock(cv_mutex); - while(active_count > 0) { - cv.wait_for(lock, std::chrono::seconds(1)); - } - } + internal::compute_filter(pbp, width, height, count, filter_size, + filter_out, threads); #ifndef NDEBUG // for(std::size_t i = 0; i < count; ++i) { @@ -268,54 +217,8 @@ std::vector dither::blue_noise(std::size_t width, std::size_t height, std: pbp[max_one] = false; // get filter values again - if(threads == 1) { - for(std::size_t y = 0; y < height; ++y) { - for(std::size_t x = 0; x < width; ++x) { - filter_out[internal::twoToOne(x, y, width)] = - internal::filter(pbp, x, y, width, height); - } - } - } else { - std::size_t active_count = 0; - std::mutex cv_mutex; - std::condition_variable cv; - for(std::size_t i = 0; i < count; ++i) { - { - std::unique_lock lock(cv_mutex); - active_count += 1; - } - std::thread t([] (std::size_t *ac, std::mutex *cvm, - std::condition_variable *cv, std::size_t i, - const std::vector *pbp, std::size_t width, - std::size_t height, std::vector *fout) { - std::size_t x, y; - std::tie(x, y) = internal::oneToTwo(i, width); - (*fout)[i] = internal::filter(*pbp, x, y, width, height); - std::unique_lock lock(*cvm); - *ac -= 1; - cv->notify_all(); - }, - &active_count, &cv_mutex, &cv, i, &pbp, width, height, &filter_out); - t.detach(); - - std::unique_lock lock(cv_mutex); - while(active_count >= threads) { -#ifndef NDEBUG -// std::cout << "1, active_count = " << active_count -// << ", pre wait_for" << std::endl; -#endif - cv.wait_for(lock, std::chrono::seconds(1)); -#ifndef NDEBUG -// std::cout << "1, active_count = " << active_count -// << ", post wait_for" << std::endl; -#endif - } - } - std::unique_lock lock(cv_mutex); - while(active_count > 0) { - cv.wait_for(lock, std::chrono::seconds(1)); - } - } + internal::compute_filter(pbp, width, height, count, filter_size, + filter_out, threads); // get second buffer's min std::size_t second_min; diff --git a/src/blue_noise.hpp b/src/blue_noise.hpp index acaba0b..dadacf3 100644 --- a/src/blue_noise.hpp +++ b/src/blue_noise.hpp @@ -6,6 +6,10 @@ #include #include #include +#include +#include +#include +#include namespace dither { @@ -29,17 +33,17 @@ namespace internal { inline double filter( const std::vector& pbp, std::size_t x, std::size_t y, - std::size_t width, std::size_t height) { + std::size_t width, std::size_t height, std::size_t filter_size) { double sum = 0.0; // Should be range -M/2 to M/2, but size_t cannot be negative, so range // is 0 to M. // p' = (M + x - (p - M/2)) % M = (3M/2 + x - p) % M // q' = (N + y - (q - M/2)) % N = (N + M/2 + y - q) % N - for(std::size_t q = 0; q < width; ++q) { - std::size_t q_prime = (height + width / 2 + y - q) % height; - for(std::size_t p = 0; p < width; ++p) { - std::size_t p_prime = (3 * width / 2 + x - p) % width; + for(std::size_t q = 0; q < filter_size; ++q) { + std::size_t q_prime = (height + filter_size / 2 + y - q) % height; + for(std::size_t p = 0; p < filter_size; ++p) { + std::size_t p_prime = (width + filter_size / 2 + x - p) % width; bool pbp_value = pbp[twoToOne(p_prime, q_prime, width)]; if(pbp_value) { sum += gaussian((double)p - width/2.0, (double)q - width/2.0); @@ -50,13 +54,74 @@ namespace internal { return sum; } + inline void compute_filter( + const std::vector &pbp, std::size_t width, std::size_t height, + std::size_t count, std::size_t filter_size, std::vector &filter_out, + std::size_t threads = 1) { + if(threads == 1) { + for(std::size_t y = 0; y < height; ++y) { + for(std::size_t x = 0; x < width; ++x) { + filter_out[internal::twoToOne(x, y, width)] = + internal::filter(pbp, x, y, width, height, filter_size); + } + } + } else { + if(threads == 0) { + threads = 10; + } + std::size_t active_count = 0; + std::mutex cv_mutex; + std::condition_variable cv; + for(std::size_t i = 0; i < count; ++i) { + { + std::unique_lock lock(cv_mutex); + active_count += 1; + } + std::thread t([] (std::size_t *ac, std::mutex *cvm, + std::condition_variable *cv, std::size_t i, + const std::vector *pbp, std::size_t width, + std::size_t height, std::size_t filter_size, + std::vector *fout) { + std::size_t x, y; + std::tie(x, y) = internal::oneToTwo(i, width); + (*fout)[i] = internal::filter( + *pbp, x, y, width, height, filter_size); + std::unique_lock lock(*cvm); + *ac -= 1; + cv->notify_all(); + }, + &active_count, &cv_mutex, &cv, i, &pbp, width, height, + filter_size, &filter_out); + t.detach(); + + std::unique_lock lock(cv_mutex); + while(active_count >= threads) { +#ifndef NDEBUG +// std::cout << "0, active_count = " << active_count +// << ", pre wait_for" << std::endl; +#endif + cv.wait_for(lock, std::chrono::seconds(1)); +#ifndef NDEBUG +// std::cout << "0, active_count = " << active_count +// << ", post wait_for" << std::endl; +#endif + } + } + std::unique_lock lock(cv_mutex); + while(active_count > 0) { + cv.wait_for(lock, std::chrono::seconds(1)); + } + } + + } + inline std::tuple filter_minmax(const std::vector& filter) { double min = std::numeric_limits::infinity(); double max = 0.0; std::size_t min_index = 0; std::size_t max_index = 0; - for(std::size_t i = 0; i < filter.size(); ++i) { + for(std::vector::size_type i = 0; i < filter.size(); ++i) { if(filter[i] < min) { min_index = i; min = filter[i];