-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbenchmark.rb
More file actions
183 lines (147 loc) · 4.67 KB
/
benchmark.rb
File metadata and controls
183 lines (147 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module Blurhash
def self.encode_rb(width, height, pixels, x_comp: 4, y_comp: 3)
Ruby.blurHashForPixels(x_comp, y_comp, width, height, pixels, width * 3)
end
# :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.freeze
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
require_relative "../../lib/harness/loader"
FILE = File.join(__dir__, "test.bin")
make_shareable(ARRAY = File.read(FILE).bytes)
run_benchmark(10) do
Blurhash.encode_rb(204, 204, ARRAY)
end