Skip to content

Commit 0849b15

Browse files
committed
add method to calculate from buffer
1 parent e6d989c commit 0849b15

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

lib/dhash-vips.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,18 @@ def distance a, b
4545
return right.uniq[1] if left.count(left.last) > right.count(right.first)
4646
left.last
4747
end
48-
fail unless 2 == @@median[[1, 2, 2, 2, 2, 2, 3]]
49-
fail unless 3 == @@median[[1, 2, 2, 2, 2, 3, 3]]
50-
fail unless 3 == @@median[[1, 1, 2, 2, 3, 3, 3]]
51-
fail unless 2 == @@median[[1, 1, 1, 2, 3, 3, 3]]
52-
fail unless 2 == @@median[[1, 1, 2, 2, 2, 2, 3]]
53-
fail unless 2 == @@median[[1, 2, 2, 2, 2, 3]]
54-
fail unless 3 == @@median[[1, 2, 2, 3, 3, 3]]
55-
fail unless 1 == @@median[[1, 1, 1]]
56-
fail unless 1 == @@median[[1, 1]]
5748

5849
def calculate file
50+
calculate_for_image(Vips::Image.new_from_file file)
51+
end
52+
53+
def calculate_for_buffer buffer
54+
calculate_for_image(Vips::Image.new_from_buffer buffer, '')
55+
end
56+
57+
private
58+
def calculate_for_image(image)
5959
hash_size = 8
60-
image = Vips::Image.new_from_file file
6160
image = image.resize(hash_size.fdiv(image.width), vscale: hash_size.fdiv(image.height)).colourspace("b-w")
6261

6362
array = image.to_a.map &:flatten
@@ -71,7 +70,6 @@ def calculate file
7170
end
7271
(((((d1 << hash_size * hash_size) + d2) << hash_size * hash_size) + i1) << hash_size * hash_size) + i2
7372
end
74-
7573
end
7674

7775
end

0 commit comments

Comments
 (0)