summary refs log tree commit diff
path: root/third_party
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-10-20 15:19:04 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2021-10-20 15:19:23 +0200
commit5bce8fd915fef97dca685551be5d6e7b2fc1bd0b (patch)
treef407e54476762d6ee35d0985eeef29af85e68422 /third_party
parentFix calculating hidden space children (diff)
downloadnheko-5bce8fd915fef97dca685551be5d6e7b2fc1bd0b.tar.xz
speed up blurhash decode a bit
Diffstat (limited to 'third_party')
-rw-r--r--third_party/blurhash/blurhash.cpp33
-rw-r--r--third_party/blurhash/blurhash.hpp2
2 files changed, 17 insertions, 18 deletions
diff --git a/third_party/blurhash/blurhash.cpp b/third_party/blurhash/blurhash.cpp
index a4adf89f..bcfcce5c 100644
--- a/third_party/blurhash/blurhash.cpp
+++ b/third_party/blurhash/blurhash.cpp
@@ -6,10 +6,6 @@
 #include <cmath>
 #include <stdexcept>
 
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
-
 #ifdef DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
 #include <doctest.h>
 #endif
@@ -17,6 +13,9 @@
 using namespace std::literals;
 
 namespace {
+template<class T>
+T pi = 3.14159265358979323846;
+
 constexpr std::array<char, 84> int_to_b83{
   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"};
 
@@ -62,13 +61,13 @@ struct Components
 };
 
 int
-packComponents(const Components &c)
+packComponents(const Components &c) noexcept
 {
         return (c.x - 1) + (c.y - 1) * 9;
 }
 
 Components
-unpackComponents(int c)
+unpackComponents(int c) noexcept
 {
         return {c % 9 + 1, c / 9 + 1};
 }
@@ -88,7 +87,7 @@ decode83(std::string_view value)
 }
 
 float
-decodeMaxAC(int quantizedMaxAC)
+decodeMaxAC(int quantizedMaxAC) noexcept
 {
         return (quantizedMaxAC + 1) / 166.;
 }
@@ -101,13 +100,13 @@ decodeMaxAC(std::string_view maxAC)
 }
 
 int
-encodeMaxAC(float maxAC)
+encodeMaxAC(float maxAC) noexcept
 {
-        return std::max(0, std::min(82, int(maxAC * 166 - 0.5)));
+        return std::max(0, std::min(82, int(maxAC * 166 - 0.5f)));
 }
 
 float
-srgbToLinear(int value)
+srgbToLinear(int value) noexcept
 {
         auto srgbToLinearF = [](float x) {
                 if (x <= 0.0f)
@@ -124,7 +123,7 @@ srgbToLinear(int value)
 }
 
 int
-linearToSrgb(float value)
+linearToSrgb(float value) noexcept
 {
         auto linearToSrgbF = [](float x) -> float {
                 if (x <= 0.0f)
@@ -137,7 +136,7 @@ linearToSrgb(float value)
                         return std::pow(x, 1.0f / 2.4f) * 1.055f - 0.055f;
         };
 
-        return int(linearToSrgbF(value) * 255.f + 0.5);
+        return int(linearToSrgbF(value) * 255.f + 0.5f);
 }
 
 struct Color
@@ -235,8 +234,8 @@ multiplyBasisFunction(Components components, int width, int height, unsigned cha
 
         for (int y = 0; y < height; y++) {
                 for (int x = 0; x < width; x++) {
-                        float basis = std::cos(M_PI * components.x * x / float(width)) *
-                                      std::cos(M_PI * components.y * y / float(height));
+                        float basis = std::cos(pi<float> * components.x * x / float(width)) *
+                                      std::cos(pi<float> * components.y * y / float(height));
                         c.r += basis * srgbToLinear(pixels[3 * x + 0 + y * width * 3]);
                         c.g += basis * srgbToLinear(pixels[3 * x + 1 + y * width * 3]);
                         c.b += basis * srgbToLinear(pixels[3 * x + 2 + y * width * 3]);
@@ -251,7 +250,7 @@ multiplyBasisFunction(Components components, int width, int height, unsigned cha
 
 namespace blurhash {
 Image
-decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel)
+decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel) noexcept
 {
         Image i{};
 
@@ -287,8 +286,8 @@ decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPi
                         for (size_t nx = 0; nx < size_t(components.x); nx++) {
                                 for (size_t ny = 0; ny < size_t(components.y); ny++) {
                                         float basis =
-                                          std::cos(M_PI * float(x) * float(nx) / float(width)) *
-                                          std::cos(M_PI * float(y) * float(ny) / float(height));
+                                          std::cos(pi<float> * float(nx * x) / float(width)) *
+                                          std::cos(pi<float> * float(ny * y) / float(height));
                                         c += values[nx + ny * components.x] * basis;
                                 }
                         }
diff --git a/third_party/blurhash/blurhash.hpp b/third_party/blurhash/blurhash.hpp
index e01b9b3f..d4e138ef 100644
--- a/third_party/blurhash/blurhash.hpp
+++ b/third_party/blurhash/blurhash.hpp
@@ -13,7 +13,7 @@ struct Image
 
 // Decode a blurhash to an image with size width*height
 Image
-decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel = 3);
+decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel = 3) noexcept;
 
 // Encode an image of rgb pixels (without padding) with size width*height into a blurhash with x*y
 // components