mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-21 18:50:47 +03:00
Optimize blurhashes a bit more
This commit is contained in:
parent
e05720b5ca
commit
87ba5796bc
1 changed files with 45 additions and 43 deletions
88
third_party/blurhash/blurhash.cpp
vendored
88
third_party/blurhash/blurhash.cpp
vendored
|
@ -230,25 +230,17 @@ decodeAC(std::string_view value, float maximumValue)
|
||||||
return decodeAC(decode83(value), maximumValue);
|
return decodeAC(decode83(value), maximumValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color
|
std::vector<float>
|
||||||
multiplyBasisFunction(Components components, int width, int height, unsigned char *pixels)
|
bases_for(size_t dimension, size_t components)
|
||||||
{
|
{
|
||||||
Color c{};
|
std::vector<float> bases(dimension * components, 0.f);
|
||||||
float normalisation = (components.x == 0 && components.y == 0) ? 1 : 2;
|
auto scale = pi<float> / float(dimension);
|
||||||
|
for (size_t x = 0; x < dimension; x++) {
|
||||||
for (int y = 0; y < height; y++) {
|
for (size_t nx = 0; nx < size_t(components); nx++) {
|
||||||
for (int x = 0; x < width; x++) {
|
bases[x * components + nx] = std::cos(scale * float(nx * x));
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bases;
|
||||||
float scale = normalisation / (width * height);
|
|
||||||
c *= scale;
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,23 +273,10 @@ decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPi
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
i.image.reserve(height * width * bytesPerPixel);
|
i.image = decltype(i.image)(height * width * bytesPerPixel, 255);
|
||||||
|
|
||||||
std::vector<float> basis_x(width * components.x, 0.f);
|
std::vector<float> basis_x = bases_for(width, components.x);
|
||||||
std::vector<float> basis_y(height * components.y, 0.f);
|
std::vector<float> basis_y = bases_for(height, components.y);
|
||||||
|
|
||||||
for (size_t x = 0; x < width; x++) {
|
|
||||||
for (size_t nx = 0; nx < size_t(components.x); nx++) {
|
|
||||||
basis_x[x * components.x + nx] =
|
|
||||||
std::cos(pi<float> * float(nx * x) / float(width));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (size_t y = 0; y < height; y++) {
|
|
||||||
for (size_t ny = 0; ny < size_t(components.y); ny++) {
|
|
||||||
basis_y[y * components.y + ny] =
|
|
||||||
std::cos(pi<float> * float(ny * y) / float(height));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t y = 0; y < height; y++) {
|
for (size_t y = 0; y < height; y++) {
|
||||||
for (size_t x = 0; x < width; x++) {
|
for (size_t x = 0; x < width; x++) {
|
||||||
|
@ -311,12 +290,12 @@ decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i.image.push_back(static_cast<unsigned char>(linearToSrgb(c.r)));
|
i.image[(y * width + x) * bytesPerPixel + 0] =
|
||||||
i.image.push_back(static_cast<unsigned char>(linearToSrgb(c.g)));
|
static_cast<unsigned char>(linearToSrgb(c.r));
|
||||||
i.image.push_back(static_cast<unsigned char>(linearToSrgb(c.b)));
|
i.image[(y * width + x) * bytesPerPixel + 1] =
|
||||||
|
static_cast<unsigned char>(linearToSrgb(c.g));
|
||||||
for (size_t p = 3; p < bytesPerPixel; p++)
|
i.image[(y * width + x) * bytesPerPixel + 2] =
|
||||||
i.image.push_back(255);
|
static_cast<unsigned char>(linearToSrgb(c.b));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,14 +312,37 @@ encode(unsigned char *image, size_t width, size_t height, int components_x, int
|
||||||
components_y > 9 || !image)
|
components_y > 9 || !image)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
std::vector<Color> factors;
|
std::vector<float> basis_x = bases_for(width, components_x);
|
||||||
factors.reserve(components_x * components_y);
|
std::vector<float> basis_y = bases_for(height, components_y);
|
||||||
for (int y = 0; y < components_y; y++) {
|
|
||||||
for (int x = 0; x < components_x; x++) {
|
std::vector<Color> factors(components_x * components_y, Color{});
|
||||||
factors.push_back(multiplyBasisFunction({x, y}, width, height, image));
|
for (size_t y = 0; y < height; y++) {
|
||||||
|
for (size_t x = 0; x < width; x++) {
|
||||||
|
Color linear{srgbToLinear(image[3 * x + 0 + y * width * 3]),
|
||||||
|
srgbToLinear(image[3 * x + 1 + y * width * 3]),
|
||||||
|
srgbToLinear(image[3 * x + 2 + y * width * 3])};
|
||||||
|
|
||||||
|
// other half of normalization.
|
||||||
|
linear *= 1.f / width;
|
||||||
|
|
||||||
|
for (size_t ny = 0; ny < size_t(components_y); ny++) {
|
||||||
|
for (size_t nx = 0; nx < size_t(components_x); nx++) {
|
||||||
|
float basis = basis_x[x * size_t(components_x) + nx] *
|
||||||
|
basis_y[y * size_t(components_y) + ny];
|
||||||
|
factors[ny * components_x + nx] += linear * basis;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scale by normalization. Half the scaling is done in the previous loop to prevent going
|
||||||
|
// too far outside the float range.
|
||||||
|
for (size_t i = 0; i < factors.size(); i++) {
|
||||||
|
float normalisation = (i == 0) ? 1 : 2;
|
||||||
|
float scale = normalisation / (height);
|
||||||
|
factors[i] *= scale;
|
||||||
|
}
|
||||||
|
|
||||||
assert(factors.size() > 0);
|
assert(factors.size() > 0);
|
||||||
|
|
||||||
auto dc = factors.front();
|
auto dc = factors.front();
|
||||||
|
|
Loading…
Reference in a new issue