Skip to content

Commit

Permalink
Merge pull request #1625 from nextcloud/artonge/add/blurhash
Browse files Browse the repository at this point in the history
Add kornrunner/blurhash
  • Loading branch information
artonge authored Jan 4, 2024
2 parents 5b8a5fc + 26ff570 commit a71bd8a
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ symfony/process/Tests
thecodingmachine/safe/rector-migrate-0.7.php

wapmorgan/mp3info/_config.yml

kornrunner/blurhash/test
kornrunner/blurhash/.coveralls.yml
kornrunner/blurhash/infection.json.dist
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"guzzlehttp/guzzle": "^7.5.0",
"icewind/searchdav": "^3.1.0",
"icewind/streams": "^0.7.7",
"kornrunner/blurhash": "^1.2",
"laravel/serializable-closure": "^1.2.2",
"mexitek/phpcolors": "^1.0",
"microsoft/azure-storage-blob": "^1.5.4",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,11 @@
'cweagans\\Composer\\PatchEvent' => $vendorDir . '/cweagans/composer-patches/src/PatchEvent.php',
'cweagans\\Composer\\PatchEvents' => $vendorDir . '/cweagans/composer-patches/src/PatchEvents.php',
'cweagans\\Composer\\Patches' => $vendorDir . '/cweagans/composer-patches/src/Patches.php',
'kornrunner\\Blurhash\\AC' => $vendorDir . '/kornrunner/blurhash/src/AC.php',
'kornrunner\\Blurhash\\Base83' => $vendorDir . '/kornrunner/blurhash/src/Base83.php',
'kornrunner\\Blurhash\\Blurhash' => $vendorDir . '/kornrunner/blurhash/src/Blurhash.php',
'kornrunner\\Blurhash\\Color' => $vendorDir . '/kornrunner/blurhash/src/Color.php',
'kornrunner\\Blurhash\\DC' => $vendorDir . '/kornrunner/blurhash/src/DC.php',
'libphonenumber\\CountryCodeSource' => $vendorDir . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeSource.php',
'libphonenumber\\CountryCodeToRegionCodeMap' => $vendorDir . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeToRegionCodeMap.php',
'libphonenumber\\CountryCodeToRegionCodeMapForTesting' => $vendorDir . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeToRegionCodeMapForTesting.php',
Expand Down
1 change: 1 addition & 0 deletions composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'phpseclib\\' => array($vendorDir . '/phpseclib/phpseclib/phpseclib'),
'ownCloud\\TarStreamer\\' => array($vendorDir . '/deepdiver1975/tarstreamer/src'),
'libphonenumber\\' => array($vendorDir . '/giggsey/libphonenumber-for-php-lite/src'),
'kornrunner\\Blurhash\\' => array($vendorDir . '/kornrunner/blurhash/src'),
'cweagans\\Composer\\' => array($vendorDir . '/cweagans/composer-patches/src'),
'bantu\\IniGetWrapper\\' => array($vendorDir . '/bantu/ini-get-wrapper/src'),
'ZipStreamer\\' => array($vendorDir . '/deepdiver/zipstreamer/src'),
Expand Down
13 changes: 13 additions & 0 deletions composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652
array (
'libphonenumber\\' => 15,
),
'k' =>
array (
'kornrunner\\Blurhash\\' => 20,
),
'c' =>
array (
'cweagans\\Composer\\' => 18,
Expand Down Expand Up @@ -301,6 +305,10 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652
array (
0 => __DIR__ . '/..' . '/giggsey/libphonenumber-for-php-lite/src',
),
'kornrunner\\Blurhash\\' =>
array (
0 => __DIR__ . '/..' . '/kornrunner/blurhash/src',
),
'cweagans\\Composer\\' =>
array (
0 => __DIR__ . '/..' . '/cweagans/composer-patches/src',
Expand Down Expand Up @@ -4089,6 +4097,11 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652
'cweagans\\Composer\\PatchEvent' => __DIR__ . '/..' . '/cweagans/composer-patches/src/PatchEvent.php',
'cweagans\\Composer\\PatchEvents' => __DIR__ . '/..' . '/cweagans/composer-patches/src/PatchEvents.php',
'cweagans\\Composer\\Patches' => __DIR__ . '/..' . '/cweagans/composer-patches/src/Patches.php',
'kornrunner\\Blurhash\\AC' => __DIR__ . '/..' . '/kornrunner/blurhash/src/AC.php',
'kornrunner\\Blurhash\\Base83' => __DIR__ . '/..' . '/kornrunner/blurhash/src/Base83.php',
'kornrunner\\Blurhash\\Blurhash' => __DIR__ . '/..' . '/kornrunner/blurhash/src/Blurhash.php',
'kornrunner\\Blurhash\\Color' => __DIR__ . '/..' . '/kornrunner/blurhash/src/Color.php',
'kornrunner\\Blurhash\\DC' => __DIR__ . '/..' . '/kornrunner/blurhash/src/DC.php',
'libphonenumber\\CountryCodeSource' => __DIR__ . '/..' . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeSource.php',
'libphonenumber\\CountryCodeToRegionCodeMap' => __DIR__ . '/..' . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeToRegionCodeMap.php',
'libphonenumber\\CountryCodeToRegionCodeMapForTesting' => __DIR__ . '/..' . '/giggsey/libphonenumber-for-php-lite/src/CountryCodeToRegionCodeMapForTesting.php',
Expand Down
51 changes: 51 additions & 0 deletions composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,57 @@
},
"install-path": "../justinrainbow/json-schema"
},
{
"name": "kornrunner/blurhash",
"version": "v1.2.2",
"version_normalized": "1.2.2.0",
"source": {
"type": "git",
"url": "https://github.com/kornrunner/php-blurhash.git",
"reference": "bc8a4596cb0a49874f0158696a382ab3933fefe4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kornrunner/php-blurhash/zipball/bc8a4596cb0a49874f0158696a382ab3933fefe4",
"reference": "bc8a4596cb0a49874f0158696a382ab3933fefe4",
"shasum": ""
},
"require": {
"php": "^7.3|^8.0"
},
"require-dev": {
"ext-gd": "*",
"ocramius/package-versions": "^1.4|^2.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^9",
"vimeo/psalm": "^4.3"
},
"time": "2022-07-13T19:38:39+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"kornrunner\\Blurhash\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Boris Momčilović",
"email": "[email protected]"
}
],
"description": "Pure PHP implementation of Blurhash",
"homepage": "https://github.com/kornrunner/php-blurhash",
"support": {
"issues": "https://github.com/kornrunner/php-blurhash/issues",
"source": "https://github.com/kornrunner/php-blurhash.git"
},
"install-path": "../kornrunner/blurhash"
},
{
"name": "laravel/serializable-closure",
"version": "v1.2.2",
Expand Down
13 changes: 11 additions & 2 deletions composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'nextcloud/3rdparty',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '8417c6d97148318316957adc082f7f50e5ea6e0e',
'reference' => '5b8a5fc015968956a000d269561cb5ec9d931870',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand Down Expand Up @@ -235,6 +235,15 @@
'aliases' => array(),
'dev_requirement' => false,
),
'kornrunner/blurhash' => array(
'pretty_version' => 'v1.2.2',
'version' => '1.2.2.0',
'reference' => 'bc8a4596cb0a49874f0158696a382ab3933fefe4',
'type' => 'library',
'install_path' => __DIR__ . '/../kornrunner/blurhash',
'aliases' => array(),
'dev_requirement' => false,
),
'laravel/serializable-closure' => array(
'pretty_version' => 'v1.2.2',
'version' => '1.2.2.0',
Expand Down Expand Up @@ -310,7 +319,7 @@
'nextcloud/3rdparty' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '8417c6d97148318316957adc082f7f50e5ea6e0e',
'reference' => '5b8a5fc015968956a000d269561cb5ec9d931870',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand Down
21 changes: 21 additions & 0 deletions kornrunner/blurhash/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Boris Momčilović

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions kornrunner/blurhash/src/AC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace kornrunner\Blurhash;

final class AC {

public static function encode(array $value, float $max_value): float {
$quant_r = static::quantise($value[0] / $max_value);
$quant_g = static::quantise($value[1] / $max_value);
$quant_b = static::quantise($value[2] / $max_value);
return $quant_r * 19 * 19 + $quant_g * 19 + $quant_b;
}

public static function decode(int $value, float $max_value): array {
$quant_r = intdiv($value, 19 * 19);
$quant_g = intdiv($value, 19) % 19;
$quant_b = $value % 19;

return [
static::signPow(($quant_r - 9) / 9, 2) * $max_value,
static::signPow(($quant_g - 9) / 9, 2) * $max_value,
static::signPow(($quant_b - 9) / 9, 2) * $max_value
];
}

private static function quantise(float $value): float {
return floor(max(0, min(18, floor(static::signPow($value, 0.5) * 9 + 9.5))));
}

private static function signPow(float $base, float $exp): float {
$sign = $base <=> 0;
return $sign * pow(abs($base), $exp);
}
}
39 changes: 39 additions & 0 deletions kornrunner/blurhash/src/Base83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace kornrunner\Blurhash;

use InvalidArgumentException;

class Base83 {
private const ALPHABET = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '#', '$', '%', '*', '+', ',', '-', '.',
':', ';', '=', '?', '@', '[', ']', '^', '_', '{', '|', '}', '~'
];

private const BASE = 83;

public static function encode(int $value, int $length): string {
if (intdiv($value, self::BASE ** $length) != 0) {
throw new InvalidArgumentException('Specified length is too short to encode given value.');
}

$result = '';
for ($i = 1; $i <= $length; $i++) {
$digit = intdiv($value, self::BASE ** ($length - $i)) % self::BASE;
$result .= self::ALPHABET[$digit];
}
return $result;
}

public static function decode(string $hash): int {
$result = 0;
foreach (str_split($hash) as $char) {
$result = $result * self::BASE + (int) array_search($char, self::ALPHABET, true);
}
return $result;
}
}
Loading

0 comments on commit a71bd8a

Please sign in to comment.