From bbe3917ed18fa164dd31a0f3bfcc9d4362b8f545 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Apr 2022 15:03:16 +0800 Subject: Specialize pixel::gamma2 for small values. --- bitmap.h | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'bitmap.h') diff --git a/bitmap.h b/bitmap.h index 58b9a25..c955803 100644 --- a/bitmap.h +++ b/bitmap.h @@ -72,15 +72,34 @@ struct pixel { return mod; // FIXME } - inline pixel gamma2() const { + pixel gamma2() const { const auto max = max_value(); - const double r_ = sqrt(1.0 * this->r / max); - const double g_ = sqrt(1.0 * this->g / max); - const double b_ = sqrt(1.0 * this->b / max); - return pixel::from_normalized(r_, g_, b_); + if (sizeof(T) <= 2) { + // 26% faster than using double + const auto r_ = sqrtf((float) (this->r) / (float) max); + const auto g_ = sqrtf((float) (this->g) / (float) max); + const auto b_ = sqrtf((float) (this->b) / (float) max); + return pixel::from_normalized(r_, g_, b_); + } else { + const auto r_ = sqrt(1.0 * this->r / max); + const auto g_ = sqrt(1.0 * this->g / max); + const auto b_ = sqrt(1.0 * this->b / max); + return pixel::from_normalized(r_, g_, b_); + } } + }; +//template<> +//pixel pixel::gamma2() const { +// // 26% faster than using double +// const auto max = max_value(); +// const auto r_ = sqrtf((float) (this->r) / (float) (max)); +// const auto g_ = sqrtf((float) (this->g) / (float) (max)); +// const auto b_ = sqrtf((float) (this->b) / (float) (max)); +// return pixel::from_normalized(r_, g_, b_); +//} + template< typename T, typename S, -- cgit v1.2.3