blurhash-rb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +12 -0
- data/lib/blurhash/base83.rb +42 -0
- data/lib/blurhash/encode.rb +128 -0
- data/lib/blurhash/version.rb +5 -0
- data/lib/blurhash.rb +16 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e6bf34cd0850084ed01d434630f305a64a975c18925bfdfcdd9166b80e395a0
|
4
|
+
data.tar.gz: 52e1b5032437812fe02502131ac047967ed81dd1dfb2731f460436ab87934826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 29677b0b8d35ce21b3aa2fd50291dcd63c0a6bba8bd0ffd74756f4a70fe180a5aa4a2e8c8e196258672a378782a53ffb8c810173f5ddd80d36aa7b30bfba8786
|
7
|
+
data.tar.gz: dfc376653a4deb4d980fdd0a1dff0da97577809413eddf321387d2b9e1f45002e98e7201fc6e84a1be68e3d523226805211cc1459454a88a5b450589764fffe4
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Nolan J Tait
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Blurhash
|
2
|
+
|
3
|
+
This is a pure ruby implementation of the [blurhash](https://blurha.sh/)
|
4
|
+
algorithm.
|
5
|
+
|
6
|
+
There are other Ruby based libraries for this. But all of them use compiled
|
7
|
+
dependencies which tend to cause issues over time.
|
8
|
+
|
9
|
+
Currently it only handles encoding.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Install the gem and add to the application's Gemfile by executing:
|
14
|
+
|
15
|
+
```bash bundle add blurhash-rb ```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by
|
18
|
+
executing:
|
19
|
+
|
20
|
+
```bash gem install blurhash-rb ```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
To use it you just need to feed in the `pixels`, `width` and `height` of the
|
25
|
+
image.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require "blurhash-rb"
|
29
|
+
|
30
|
+
def load_image_as_rgba(path)
|
31
|
+
image = ChunkyPNG::Image.from_file(path)
|
32
|
+
width = image.width
|
33
|
+
height = image.height
|
34
|
+
pixels = []
|
35
|
+
|
36
|
+
height.times do |y|
|
37
|
+
width.times do |x|
|
38
|
+
pixel = image[x, y]
|
39
|
+
r = ChunkyPNG::Color.r(pixel)
|
40
|
+
g = ChunkyPNG::Color.g(pixel)
|
41
|
+
b = ChunkyPNG::Color.b(pixel)
|
42
|
+
a = ChunkyPNG::Color.a(pixel)
|
43
|
+
pixels.push(r, g, b, a)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
[pixels, width, height]
|
48
|
+
end
|
49
|
+
|
50
|
+
pixels, width, height = load_image_as_rgba("spec/fixtures/test.png")
|
51
|
+
|
52
|
+
hash = described_class.call(
|
53
|
+
pixels:,
|
54
|
+
width:,
|
55
|
+
height:,
|
56
|
+
component_x: 4,
|
57
|
+
component_y: 3
|
58
|
+
)
|
59
|
+
|
60
|
+
puts hash #=> "LEHV6nae2yk8pyo0adR*.7kCMdnj"
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
66
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
67
|
+
prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
70
|
+
release a new version, update the version number in `version.rb`, and then run
|
71
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
72
|
+
git commits and the created tag, and push the `.gem` file to
|
73
|
+
[rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at
|
78
|
+
https://github.com/nolantait/blurhash-rb.
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT
|
83
|
+
License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Blurhash
|
4
|
+
module Base83
|
5
|
+
DIGIT_CHARACTERS =
|
6
|
+
# rubocop:disable Layout/MultilineArrayLineBreaks
|
7
|
+
%w(
|
8
|
+
0 1 2 3 4 5 6 7 8 9
|
9
|
+
A B C D E F G H I J
|
10
|
+
K L M N O P Q R S T
|
11
|
+
U V W X Y Z a b c d
|
12
|
+
e f g h i j k l m n
|
13
|
+
o p q r s t u v w x
|
14
|
+
y z # $ % * + , - .
|
15
|
+
: ; = ? @ [ ] ^ _ {
|
16
|
+
| } ~
|
17
|
+
).freeze
|
18
|
+
# rubocop:enable Layout/MultilineArrayLineBreaks
|
19
|
+
|
20
|
+
def self.encode(number, length)
|
21
|
+
result = ""
|
22
|
+
|
23
|
+
(1..length).each do |integer|
|
24
|
+
digit = (number.floor / (83**(length - integer))) % 83
|
25
|
+
result += DIGIT_CHARACTERS[digit.floor]
|
26
|
+
end
|
27
|
+
|
28
|
+
result
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.decode(string)
|
32
|
+
value = 0
|
33
|
+
|
34
|
+
string.each_char.with_index do |char, _index|
|
35
|
+
digit = DIGIT_CHARACTERS.index(char)
|
36
|
+
value = (value * 83) + digit
|
37
|
+
end
|
38
|
+
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Blurhash
|
4
|
+
class Encode
|
5
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
6
|
+
|
7
|
+
def self.call(...) = new.call(...)
|
8
|
+
|
9
|
+
def call(pixels:, width:, height:, component_x:, component_y:)
|
10
|
+
if component_x < 1 ||
|
11
|
+
component_x > 9 ||
|
12
|
+
component_y < 1 ||
|
13
|
+
component_y > 9
|
14
|
+
raise ValidationError,
|
15
|
+
"BlurHash must have between 1 and 9 components"
|
16
|
+
end
|
17
|
+
unless pixels.length == width * height * 3
|
18
|
+
raise ValidationError,
|
19
|
+
"Width and height must match the pixels array"
|
20
|
+
end
|
21
|
+
|
22
|
+
factors = []
|
23
|
+
|
24
|
+
component_y.times do |y|
|
25
|
+
component_x.times do |x|
|
26
|
+
normalisation = x.zero? && y.zero? ? 1 : 2
|
27
|
+
factor = multiply_basis_function(pixels, width, height) do |i, j|
|
28
|
+
normalisation *
|
29
|
+
Math.cos(Math::PI * x * i / width) *
|
30
|
+
Math.cos(Math::PI * y * j / height)
|
31
|
+
end
|
32
|
+
factors << factor
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
dc = factors[0]
|
37
|
+
ac = factors[1..]
|
38
|
+
|
39
|
+
hash = +""
|
40
|
+
|
41
|
+
size_flag = (component_x - 1) + ((component_y - 1) * 9)
|
42
|
+
hash << Base83.encode(size_flag, 1)
|
43
|
+
|
44
|
+
if ac.any?
|
45
|
+
actual_max = ac.map(&:max).max
|
46
|
+
quant_max = ((actual_max * 166) - 0.5).floor.clamp(0, 82)
|
47
|
+
max_val = (quant_max + 1) / 166.0
|
48
|
+
hash << Base83.encode(quant_max, 1)
|
49
|
+
else
|
50
|
+
max_val = 1.0
|
51
|
+
hash << Base83.encode(0, 1)
|
52
|
+
end
|
53
|
+
|
54
|
+
hash << Base83.encode(encode_dc(dc), 4)
|
55
|
+
|
56
|
+
ac.each do |factor|
|
57
|
+
hash << Base83.encode(encode_ac(factor, max_val), 2)
|
58
|
+
end
|
59
|
+
|
60
|
+
hash
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def multiply_basis_function(pixels, width, height, &)
|
66
|
+
r = g = b = 0.0
|
67
|
+
bytes_per_pixel = 3
|
68
|
+
bytes_per_row = width * bytes_per_pixel
|
69
|
+
|
70
|
+
width.times do |x|
|
71
|
+
base_x = bytes_per_pixel * x
|
72
|
+
height.times do |y|
|
73
|
+
index = base_x + (y * bytes_per_row)
|
74
|
+
basis = yield(x, y)
|
75
|
+
|
76
|
+
r += basis * srgb_to_linear(pixels[index])
|
77
|
+
g += basis * srgb_to_linear(pixels[index + 1])
|
78
|
+
b += basis * srgb_to_linear(pixels[index + 2])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
scale = 1.0 / (width * height)
|
83
|
+
[r * scale, g * scale, b * scale]
|
84
|
+
end
|
85
|
+
|
86
|
+
def encode_dc(value)
|
87
|
+
r = linear_to_srgb(value[0])
|
88
|
+
g = linear_to_srgb(value[1])
|
89
|
+
b = linear_to_srgb(value[2])
|
90
|
+
(r << 16) + (g << 8) + b
|
91
|
+
end
|
92
|
+
|
93
|
+
def encode_ac(value, maximum_value)
|
94
|
+
quant_r = ((sign_pow(value[0] / maximum_value, 0.5) * 9) + 9.5)
|
95
|
+
.floor
|
96
|
+
.clamp(0, 18)
|
97
|
+
quant_g = ((sign_pow(value[1] / maximum_value, 0.5) * 9) + 9.5)
|
98
|
+
.floor
|
99
|
+
.clamp(0, 18)
|
100
|
+
quant_b = ((sign_pow(value[2] / maximum_value, 0.5) * 9) + 9.5)
|
101
|
+
.floor
|
102
|
+
.clamp(0, 18)
|
103
|
+
|
104
|
+
(quant_r * 19 * 19) + (quant_g * 19) + quant_b
|
105
|
+
end
|
106
|
+
|
107
|
+
def srgb_to_linear(value)
|
108
|
+
v = value / 255.0
|
109
|
+
v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055)**2.4
|
110
|
+
end
|
111
|
+
|
112
|
+
def linear_to_srgb(value)
|
113
|
+
value = value.clamp(0, 1)
|
114
|
+
|
115
|
+
if value <= 0.0031308
|
116
|
+
((value * 12.92 * 255) + 0.5).truncate(0)
|
117
|
+
else
|
118
|
+
((((1.055 * (value**(1.0 / 2.4))) - 0.055) * 255) + 0.5).truncate(0)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def sign_pow(val, exp)
|
123
|
+
(val.abs**exp) * (val.negative? ? -1 : 1)
|
124
|
+
end
|
125
|
+
|
126
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
127
|
+
end
|
128
|
+
end
|
data/lib/blurhash.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "blurhash/version"
|
4
|
+
require_relative "blurhash/base83"
|
5
|
+
require_relative "blurhash/encode"
|
6
|
+
|
7
|
+
module Blurhash
|
8
|
+
class Error < StandardError; end
|
9
|
+
# Your code goes here...
|
10
|
+
|
11
|
+
ValidationError = Class.new(Error)
|
12
|
+
|
13
|
+
def self.encode(pixels:, width:, height:, component_x: 4, component_y: 3)
|
14
|
+
Encode.call(pixels:, width:, height:, component_x:, component_y:)
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blurhash-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nolan J Tait
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: |
|
13
|
+
A pure Ruby implementation of the BlurHash algorithm, which encodes images
|
14
|
+
into a short string representation that can be used to display a blurred
|
15
|
+
version of the image while it is loading.
|
16
|
+
email:
|
17
|
+
- nolanjtait@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/blurhash.rb
|
29
|
+
- lib/blurhash/base83.rb
|
30
|
+
- lib/blurhash/encode.rb
|
31
|
+
- lib/blurhash/version.rb
|
32
|
+
homepage: https://github.com/nolantait/blurhash-rb
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/nolantait/blurhash-rb
|
37
|
+
source_code_uri: https://github.com/nolantait/blurhash-rb
|
38
|
+
changelog_uri: https://github.com/nolantait/blurhash-rb
|
39
|
+
rubygems_mfa_required: 'true'
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.6.7
|
55
|
+
specification_version: 4
|
56
|
+
summary: A pure Ruby implementation of the BlurHash algorithm.
|
57
|
+
test_files: []
|