-
Notifications
You must be signed in to change notification settings - Fork 30
Add a pure Ruby implementation #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tenderlove
wants to merge
1
commit into
Gargron:master
Choose a base branch
from
tenderlove:pure-ruby
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| module Blurhash | ||
| # :stopdoc: | ||
| module Ruby | ||
| class ThreeDArray | ||
| def initialize(y, x, z) | ||
| @y = y | ||
| @x = x | ||
| @z = z | ||
| @list = Array.new(y * x * z) | ||
| end | ||
|
|
||
| def set(y, x, z, val) | ||
| i = z + (x * @z) + (y * @z * @x) | ||
| @list[i] = val | ||
| end | ||
|
|
||
| def get(y, x, z) | ||
| i = z + (x * @z) + (y * @z * @x) | ||
| @list[i] | ||
| end | ||
|
|
||
| def [](i) | ||
| @list.fetch(i) | ||
| end | ||
| end | ||
|
|
||
| class Buffer | ||
| attr_reader :pos | ||
|
|
||
| def initialize(size) | ||
| @pos = 0 | ||
| @buf = "\0".b * size | ||
| end | ||
|
|
||
| def putc(c) | ||
| @buf.setbyte(@pos, c) | ||
| @pos += 1 | ||
| end | ||
|
|
||
| def [](from, len) | ||
| @buf[from, len] | ||
| end | ||
| end | ||
|
|
||
| CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".bytes | ||
|
|
||
| def self.sRGBToLinear(value) | ||
| v = value.to_f / 255 | ||
| if v <= 0.04045 | ||
| v / 12.92 | ||
| else | ||
| ((v + 0.055) / 1.055) ** 2.4 | ||
| end | ||
| end | ||
|
|
||
| def self.multiplyBasisFunction(xComponent, yComponent, width, height, rgb, bytesPerRow, factors) | ||
| r = g = b = 0.0 | ||
| normalisation = (xComponent == 0 && yComponent == 0) ? 1 : 2 | ||
|
|
||
| height.times do |y| | ||
| y_coef = Math.cos(Math::PI * yComponent * y / height) | ||
|
|
||
| width.times do |x| | ||
| basis = Math.cos(Math::PI * xComponent * x / width) * y_coef | ||
|
|
||
| r += basis * sRGBToLinear(rgb[3 * x + 0 + y * bytesPerRow]); | ||
| g += basis * sRGBToLinear(rgb[3 * x + 1 + y * bytesPerRow]); | ||
| b += basis * sRGBToLinear(rgb[3 * x + 2 + y * bytesPerRow]); | ||
| end | ||
| end | ||
|
|
||
| scale = normalisation.to_f / (width * height) | ||
| factors.set(yComponent, xComponent, 0, r * scale) | ||
| factors.set(yComponent, xComponent, 1, g * scale) | ||
| factors.set(yComponent, xComponent, 2, b * scale) | ||
| end | ||
|
|
||
|
|
||
| def self.encode_int(value, length, destination) | ||
| divisor = 83 ** (length - 1) | ||
|
|
||
| length.times do |i| | ||
| digit = (value / divisor) % 83 | ||
| divisor /= 83 | ||
| destination.putc CHARACTERS[digit] | ||
| end | ||
| end | ||
|
|
||
| def self.linearTosRGB(value) | ||
| v = max(0, min(1, value)) | ||
| if v <= 0.0031308 | ||
| (v * 12.92 * 255 + 0.5).to_i | ||
| else | ||
| ((1.055 * (v ** (1 / 2.4)) - 0.055) * 255 + 0.5).to_i | ||
| end | ||
| end | ||
|
|
||
| def self.encodeDC(r, g, b) | ||
| roundedR = linearTosRGB(r) | ||
| roundedG = linearTosRGB(g) | ||
| roundedB = linearTosRGB(b) | ||
| (roundedR << 16) + (roundedG << 8) + roundedB | ||
| end | ||
|
|
||
| def self.max(a, b) | ||
| [a, b].max | ||
| end | ||
|
|
||
| def self.min(a, b) | ||
| [a, b].min | ||
| end | ||
|
|
||
| def self.signPow(value, exp) | ||
| pow = value.abs ** exp | ||
| value < 0 ? -pow : pow | ||
| end | ||
|
|
||
| def self.encodeAC(r, g, b, maximumValue) | ||
| quantR = max(0, min(18, (signPow(r / maximumValue, 0.5) * 9 + 9.5).floor)) | ||
| quantG = max(0, min(18, (signPow(g / maximumValue, 0.5) * 9 + 9.5).floor)) | ||
| quantB = max(0, min(18, (signPow(b / maximumValue, 0.5) * 9 + 9.5).floor)) | ||
|
|
||
| quantR * 19 * 19 + quantG * 19 + quantB; | ||
| end | ||
|
|
||
| def blurHashForPixels(xComponents, yComponents, width, height, rgb, bytesPerRow) | ||
| return if xComponents < 1 || xComponents > 9 | ||
| return if yComponents < 1 || yComponents > 9 | ||
|
|
||
| factors = ThreeDArray.new(yComponents, xComponents, 3) | ||
| ptr = Buffer.new(2 + 4 + (9 * 9 - 1) * 2 + 1) | ||
|
|
||
| yComponents.times do |y| | ||
| xComponents.times do |x| | ||
| multiplyBasisFunction(x, y, width, height, rgb, bytesPerRow, factors) | ||
| end | ||
| end | ||
|
|
||
| acCount = xComponents * yComponents - 1 | ||
| sizeFlag = (xComponents - 1) + (yComponents - 1) * 9 | ||
| encode_int(sizeFlag, 1, ptr) | ||
|
|
||
| if acCount > 0 | ||
| actualMaximumValue = 0.0 | ||
| (acCount * 3).times do |i| | ||
| actualMaximumValue = max(actualMaximumValue, factors[i + 3].abs) | ||
| end | ||
| quantisedMaximumValue = max(0, min(82, (actualMaximumValue * 166 - 0.5).floor)) | ||
| maximumValue = (quantisedMaximumValue.to_f + 1) / 166 | ||
| encode_int(quantisedMaximumValue, 1, ptr) | ||
| else | ||
| maximumValue = 1; | ||
| encode_int(0, 1, ptr) | ||
| end | ||
|
|
||
| encode_int(encodeDC(factors[0], factors[1], factors[2]), 4, ptr) | ||
|
|
||
| acCount.times do |i| | ||
| encode_int( | ||
| encodeAC(factors[i * 3 + 3], factors[i * 3 + 4], factors[i * 3 + 5], maximumValue), | ||
| 2, | ||
| ptr) | ||
| end | ||
|
|
||
| ptr[0, ptr.pos] | ||
| end | ||
| module_function :blurHashForPixels | ||
| end | ||
| # :startdoc: | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. I'm just randomly subscribed to this repo and was interested by seeing such pull request.
Just wanted to say that AFAIK this won't do any profit in Ruby, you better just have real 3d array. Not sure if there are benchmarks here but I bet on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really want to rewrite this to use a "real 3d array". You're welcome to do it and test the performance. My guess is that they'd be pretty much the same, or this version is faster. Why?
In the presence of a JIT compiler, this version will no doubt be faster (math is much cheaper than reading memory). Again, you're welcome to test!