|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OC\Blurhash\PhpBlurhash; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +class Blurhash { |
| 8 | + |
| 9 | + public static function encode(array $image, int $components_x = 4, int $components_y = 4, bool $linear = false): string { |
| 10 | + if (($components_x < 1 || $components_x > 9) || ($components_y < 1 || $components_y > 9)) { |
| 11 | + throw new InvalidArgumentException("x and y component counts must be between 1 and 9 inclusive."); |
| 12 | + } |
| 13 | + $height = count($image); |
| 14 | + $width = count($image[0]); |
| 15 | + |
| 16 | + $image_linear = $image; |
| 17 | + if (!$linear) { |
| 18 | + $image_linear = []; |
| 19 | + for ($y = 0; $y < $height; $y++) { |
| 20 | + $line = []; |
| 21 | + for ($x = 0; $x < $width; $x++) { |
| 22 | + $pixel = $image[$y][$x]; |
| 23 | + $line[] = [ |
| 24 | + Color::toLinear($pixel[0]), |
| 25 | + Color::toLinear($pixel[1]), |
| 26 | + Color::toLinear($pixel[2]) |
| 27 | + ]; |
| 28 | + } |
| 29 | + $image_linear[] = $line; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + $components = []; |
| 34 | + $scale = 1 / ($width * $height); |
| 35 | + for ($y = 0; $y < $components_y; $y++) { |
| 36 | + for ($x = 0; $x < $components_x; $x++) { |
| 37 | + $normalisation = $x == 0 && $y == 0 ? 1 : 2; |
| 38 | + $r = $g = $b = 0; |
| 39 | + for ($i = 0; $i < $width; $i++) { |
| 40 | + for ($j = 0; $j < $height; $j++) { |
| 41 | + $color = $image_linear[$j][$i]; |
| 42 | + $basis = $normalisation |
| 43 | + * cos(M_PI * $i * $x / $width) |
| 44 | + * cos(M_PI * $j * $y / $height); |
| 45 | + |
| 46 | + $r += $basis * $color[0]; |
| 47 | + $g += $basis * $color[1]; |
| 48 | + $b += $basis * $color[2]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + $components[] = [ |
| 53 | + $r * $scale, |
| 54 | + $g * $scale, |
| 55 | + $b * $scale |
| 56 | + ]; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + $dc_value = DC::encode(array_shift($components) ?: []); |
| 61 | + |
| 62 | + $max_ac_component = 0; |
| 63 | + foreach ($components as $component) { |
| 64 | + $component[] = $max_ac_component; |
| 65 | + $max_ac_component = max ($component); |
| 66 | + } |
| 67 | + |
| 68 | + $quant_max_ac_component = (int) max(0, min(82, floor($max_ac_component * 166 - 0.5))); |
| 69 | + $ac_component_norm_factor = ($quant_max_ac_component + 1) / 166; |
| 70 | + |
| 71 | + $ac_values = []; |
| 72 | + foreach ($components as $component) { |
| 73 | + $ac_values[] = AC::encode($component, $ac_component_norm_factor); |
| 74 | + } |
| 75 | + |
| 76 | + $blurhash = Base83::encode($components_x - 1 + ($components_y - 1) * 9, 1); |
| 77 | + $blurhash .= Base83::encode($quant_max_ac_component, 1); |
| 78 | + $blurhash .= Base83::encode($dc_value, 4); |
| 79 | + foreach ($ac_values as $ac_value) { |
| 80 | + $blurhash .= Base83::encode((int) $ac_value, 2); |
| 81 | + } |
| 82 | + |
| 83 | + return $blurhash; |
| 84 | + } |
| 85 | + |
| 86 | + public static function decode (string $blurhash, int $width, int $height, float $punch = 1.0, bool $linear = false): array { |
| 87 | + if (empty($blurhash) || strlen($blurhash) < 6) { |
| 88 | + throw new InvalidArgumentException("Blurhash string must be at least 6 characters"); |
| 89 | + } |
| 90 | + |
| 91 | + $size_info = Base83::decode($blurhash[0]); |
| 92 | + $size_y = intdiv($size_info, 9) + 1; |
| 93 | + $size_x = ($size_info % 9) + 1; |
| 94 | + |
| 95 | + $length = strlen($blurhash); |
| 96 | + $expected_length = (int) (4 + (2 * $size_y * $size_x)); |
| 97 | + if ($length !== $expected_length) { |
| 98 | + throw new InvalidArgumentException("Blurhash length mismatch: length is {$length} but it should be {$expected_length}"); |
| 99 | + } |
| 100 | + |
| 101 | + $colors = [DC::decode(Base83::decode(substr($blurhash, 2, 4)))]; |
| 102 | + |
| 103 | + $quant_max_ac_component = Base83::decode($blurhash[1]); |
| 104 | + $max_value = ($quant_max_ac_component + 1) / 166; |
| 105 | + for ($i = 1; $i < $size_x * $size_y; $i++) { |
| 106 | + $value = Base83::decode(substr($blurhash, 4 + $i * 2, 2)); |
| 107 | + $colors[$i] = AC::decode($value, $max_value * $punch); |
| 108 | + } |
| 109 | + |
| 110 | + $pixels = []; |
| 111 | + for ($y = 0; $y < $height; $y++) { |
| 112 | + $row = []; |
| 113 | + for ($x = 0; $x < $width; $x++) { |
| 114 | + $r = $g = $b = 0; |
| 115 | + for ($j = 0; $j < $size_y; $j++) { |
| 116 | + for ($i = 0; $i < $size_x; $i++) { |
| 117 | + $color = $colors[$i + $j * $size_x]; |
| 118 | + $basis = |
| 119 | + cos((M_PI * $x * $i) / $width) * |
| 120 | + cos((M_PI * $y * $j) / $height); |
| 121 | + |
| 122 | + $r += $color[0] * $basis; |
| 123 | + $g += $color[1] * $basis; |
| 124 | + $b += $color[2] * $basis; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $row[] = $linear ? [$r, $g, $b] : [ |
| 129 | + Color::toSRGB($r), |
| 130 | + Color::toSRGB($g), |
| 131 | + Color::toSRGB($b) |
| 132 | + ]; |
| 133 | + } |
| 134 | + $pixels[] = $row; |
| 135 | + } |
| 136 | + |
| 137 | + return $pixels; |
| 138 | + } |
| 139 | +} |
0 commit comments