Skip to content

Commit

Permalink
blurhash listener
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Oct 22, 2023
1 parent 15cdbe8 commit dc9eccb
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 0 deletions.
92 changes: 92 additions & 0 deletions lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace OC\Blurhash\Listener;

use GdImage;
use OC\Blurhash\PhpBlurhash\Blurhash;
use OC\Files\Node\File;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\FilesMetadata\Event\MetadataBackgroundEvent;
use OCP\FilesMetadata\Event\MetadataLiveEvent;

class GenerateBlurhashMetadata implements IEventListener {

Check failure

Code scanning / Psalm

MissingTemplateParam Error

OC\Blurhash\Listener\GenerateBlurhashMetadata has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1

Check failure on line 16 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:16:43: MissingTemplateParam: OC\Blurhash\Listener\GenerateBlurhashMetadata has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)
self::RESIZED_BOXSIZE = 800;

Check failure

Code scanning / Psalm

ParseError Error

Syntax error, unexpected T_STRING on line 17

Check failure on line 17 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:17:2: ParseError: Syntax error, unexpected T_STRING on line 17 (see https://psalm.dev/173)

private const COMPONENTS_X = 4;
private const COMPONENTS_Y = 3;

public function __construct() {
}

public function handle(Event $event): void {
if (!($event instanceof MetadataLiveEvent)
&& !($event instanceof MetadataBackgroundEvent)) {
return;
}

$file = $event->getNode();
if (!($file instanceof File) || !str_starts_with($file->getMimetype(), 'image/')) {
return;
}

if ($event instanceof MetadataLiveEvent) {
$event->requestBackgroundJob();

return;
}

$image = $this->resizedImageFromFile($file);

$metadata = $event->getMetadata();
$metadata->set('blurhash', $this->generateBlurHash($image));
}

private function resizedImageFromFile(File $file): GdImage {

Check failure

Code scanning / Psalm

InvalidReturnType Error

The declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile is incorrect, got 'bool|resource'

Check failure on line 48 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnType

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:48:53: InvalidReturnType: The declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile is incorrect, got 'false|resource' (see https://psalm.dev/011)
$image = imagecreatefromstring($file->getContent());
// in need of composer package gumlet/php-image-resize
// try {
// $newImage = ImageResize::createFromString($content);
// $newImage->quality_jpg = 80;
// $newImage->quality_png = 7;
//
// $newImage->resizeToBestFit(self::RESIZED_BOXSIZE, self::RESIZED_BOXSIZE);
// $image = $newImage;
// } catch (ImageResizeException $e) {
// }

return $image;

Check failure

Code scanning / Psalm

InvalidReturnStatement Error

The inferred type 'bool|resource' does not match the declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile

Check failure

Code scanning / Psalm

FalsableReturnStatement Error

The declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile does not allow false, but the function returns 'false|resource'

Check failure on line 61 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnStatement

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:61:10: InvalidReturnStatement: The inferred type 'false|resource' does not match the declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile (see https://psalm.dev/128)

Check failure on line 61 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

FalsableReturnStatement

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:61:10: FalsableReturnStatement: The declared return type 'GdImage' for OC\Blurhash\Listener\GenerateBlurhashMetadata::resizedImageFromFile does not allow false, but the function returns 'false|resource' (see https://psalm.dev/137)
}

public function generateBlurHash(GdImage $image): string {
$width = imagesx($image);

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of imagesx expects resource, but GdImage provided

Check failure on line 65 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:65:20: InvalidArgument: Argument 1 of imagesx expects resource, but GdImage provided (see https://psalm.dev/004)
$height = imagesy($image);

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of imagesy expects resource, but GdImage provided

Check failure on line 66 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:66:21: InvalidArgument: Argument 1 of imagesy expects resource, but GdImage provided (see https://psalm.dev/004)

$pixels = [];
for ($y = 0; $y < $height; ++$y) {
$row = [];
for ($x = 0; $x < $width; ++$x) {
$index = imagecolorat($image, $x, $y);

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of imagecolorat expects resource, but GdImage provided

Check failure on line 72 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:72:27: InvalidArgument: Argument 1 of imagecolorat expects resource, but GdImage provided (see https://psalm.dev/004)
$colors = imagecolorsforindex($image, $index);

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of imagecolorsforindex expects resource, but GdImage provided

Check failure on line 73 in lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php:73:35: InvalidArgument: Argument 1 of imagecolorsforindex expects resource, but GdImage provided (see https://psalm.dev/004)
$row[] = [$colors['red'], $colors['green'], $colors['blue']];
}

$pixels[] = $row;
}

return Blurhash::encode($pixels, self::COMPONENTS_X, self::COMPONENTS_Y);
}

/**
* @param IEventDispatcher $eventDispatcher
*
* @return void
*/
public static function loadListeners(IEventDispatcher $eventDispatcher): void {
$eventDispatcher->addServiceListener(MetadataLiveEvent::class, self::class);
$eventDispatcher->addServiceListener(MetadataBackgroundEvent::class, self::class);
}
}
34 changes: 34 additions & 0 deletions lib/private/Blurhash/PhpBlurhash/AC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OC\Blurhash\PhpBlurhash;

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 lib/private/Blurhash/PhpBlurhash/Base83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OC\Blurhash\PhpBlurhash;

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;

Check failure

Code scanning / Psalm

InvalidScalarArgument Error

Argument 2 of intdiv expects int, but float|int provided

Check failure on line 26 in lib/private/Blurhash/PhpBlurhash/Base83.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidScalarArgument

lib/private/Blurhash/PhpBlurhash/Base83.php:26:39: InvalidScalarArgument: Argument 2 of intdiv expects int, but float|int provided (see https://psalm.dev/012)
$result .= self::ALPHABET[$digit];

Check failure

Code scanning / Psalm

InvalidArrayOffset Error

Cannot access value on variable OC\Blurhash\PhpBlurhash\Base83::ALPHABET using a int<-82, 82> offset, expecting int<0, 82>
}
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;
}
}
139 changes: 139 additions & 0 deletions lib/private/Blurhash/PhpBlurhash/Blurhash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace OC\Blurhash\PhpBlurhash;

use InvalidArgumentException;

class Blurhash {

public static function encode(array $image, int $components_x = 4, int $components_y = 4, bool $linear = false): string {
if (($components_x < 1 || $components_x > 9) || ($components_y < 1 || $components_y > 9)) {
throw new InvalidArgumentException("x and y component counts must be between 1 and 9 inclusive.");
}
$height = count($image);
$width = count($image[0]);

$image_linear = $image;
if (!$linear) {
$image_linear = [];
for ($y = 0; $y < $height; $y++) {
$line = [];
for ($x = 0; $x < $width; $x++) {
$pixel = $image[$y][$x];
$line[] = [
Color::toLinear($pixel[0]),
Color::toLinear($pixel[1]),
Color::toLinear($pixel[2])
];
}
$image_linear[] = $line;
}
}

$components = [];
$scale = 1 / ($width * $height);
for ($y = 0; $y < $components_y; $y++) {
for ($x = 0; $x < $components_x; $x++) {
$normalisation = $x == 0 && $y == 0 ? 1 : 2;
$r = $g = $b = 0;
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
$color = $image_linear[$j][$i];
$basis = $normalisation
* cos(M_PI * $i * $x / $width)
* cos(M_PI * $j * $y / $height);

$r += $basis * $color[0];
$g += $basis * $color[1];
$b += $basis * $color[2];
}
}

$components[] = [
$r * $scale,
$g * $scale,
$b * $scale
];
}
}

$dc_value = DC::encode(array_shift($components) ?: []);

$max_ac_component = 0;
foreach ($components as $component) {
$component[] = $max_ac_component;
$max_ac_component = max ($component);
}

$quant_max_ac_component = (int) max(0, min(82, floor($max_ac_component * 166 - 0.5)));
$ac_component_norm_factor = ($quant_max_ac_component + 1) / 166;

$ac_values = [];
foreach ($components as $component) {
$ac_values[] = AC::encode($component, $ac_component_norm_factor);
}

$blurhash = Base83::encode($components_x - 1 + ($components_y - 1) * 9, 1);
$blurhash .= Base83::encode($quant_max_ac_component, 1);
$blurhash .= Base83::encode($dc_value, 4);
foreach ($ac_values as $ac_value) {
$blurhash .= Base83::encode((int) $ac_value, 2);
}

return $blurhash;
}

public static function decode (string $blurhash, int $width, int $height, float $punch = 1.0, bool $linear = false): array {
if (empty($blurhash) || strlen($blurhash) < 6) {
throw new InvalidArgumentException("Blurhash string must be at least 6 characters");
}

$size_info = Base83::decode($blurhash[0]);
$size_y = intdiv($size_info, 9) + 1;
$size_x = ($size_info % 9) + 1;

$length = strlen($blurhash);
$expected_length = (int) (4 + (2 * $size_y * $size_x));
if ($length !== $expected_length) {
throw new InvalidArgumentException("Blurhash length mismatch: length is {$length} but it should be {$expected_length}");
}

$colors = [DC::decode(Base83::decode(substr($blurhash, 2, 4)))];

$quant_max_ac_component = Base83::decode($blurhash[1]);
$max_value = ($quant_max_ac_component + 1) / 166;
for ($i = 1; $i < $size_x * $size_y; $i++) {
$value = Base83::decode(substr($blurhash, 4 + $i * 2, 2));
$colors[$i] = AC::decode($value, $max_value * $punch);
}

$pixels = [];
for ($y = 0; $y < $height; $y++) {
$row = [];
for ($x = 0; $x < $width; $x++) {
$r = $g = $b = 0;
for ($j = 0; $j < $size_y; $j++) {
for ($i = 0; $i < $size_x; $i++) {
$color = $colors[$i + $j * $size_x];
$basis =
cos((M_PI * $x * $i) / $width) *
cos((M_PI * $y * $j) / $height);

$r += $color[0] * $basis;
$g += $color[1] * $basis;
$b += $color[2] * $basis;
}
}

$row[] = $linear ? [$r, $g, $b] : [
Color::toSRGB($r),
Color::toSRGB($g),
Color::toSRGB($b)
];
}
$pixels[] = $row;
}

return $pixels;
}
}
20 changes: 20 additions & 0 deletions lib/private/Blurhash/PhpBlurhash/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace OC\Blurhash\PhpBlurhash;

final class Color {
public static function toLinear(int $value): float {
$value = $value / 255;
return ($value <= 0.04045)
? $value / 12.92
: pow(($value + 0.055) / 1.055, 2.4);
}

public static function tosRGB(float $value): int {
$normalized = max(0, min(1, $value));
$result = ($normalized <= 0.0031308)
? (int) round($normalized * 12.92 * 255 + 0.5)
: (int) round((1.055 * pow($normalized, 1 / 2.4) - 0.055) * 255 + 0.5);
return max(0, min($result, 255));
}
}
24 changes: 24 additions & 0 deletions lib/private/Blurhash/PhpBlurhash/DC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace OC\Blurhash\PhpBlurhash;

final class DC {

public static function encode(array $value): int {
$rounded_r = Color::tosRGB($value[0]);
$rounded_g = Color::tosRGB($value[1]);
$rounded_b = Color::tosRGB($value[2]);
return ($rounded_r << 16) + ($rounded_g << 8) + $rounded_b;
}

public static function decode(int $value): array {
$r = $value >> 16;
$g = ($value >> 8) & 255;
$b = $value & 255;
return [
Color::toLinear($r),
Color::toLinear($g),
Color::toLinear($b)
];
}
}
2 changes: 2 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use OC\Authentication\LoginCredentials\Store;
use OC\Authentication\Token\IProvider;
use OC\Avatar\AvatarManager;
use OC\Blurhash\Listener\GenerateBlurhashMetadata;
use OC\Collaboration\Collaborators\GroupPlugin;
use OC\Collaboration\Collaborators\MailPlugin;
use OC\Collaboration\Collaborators\RemoteGroupPlugin;
Expand Down Expand Up @@ -1475,6 +1476,7 @@ private function connectDispatcher(): void {
$eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class);

FilesMetadataManager::loadListeners($eventDispatcher);
GenerateBlurhashMetadata::loadListeners($eventDispatcher);
}

/**
Expand Down

0 comments on commit dc9eccb

Please sign in to comment.