dhash-vips 0.0.3.2 → 0.0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef9b532c31699dd2445292844b5d0760a9e79351
4
- data.tar.gz: b4e52489d194424a01c9954053bd38a910178124
3
+ metadata.gz: bb131dc947f50e6d428a75c6518704b6797e1d30
4
+ data.tar.gz: 63dc5d70626192321675dbfeaa020c31812095ac
5
5
  SHA512:
6
- metadata.gz: f8a286f3b357af1b1fddfc20bda62bb018868b68952f4e706c2a2a336ae12ca24f15d32a0b267ed574295740cb3e3b8efcb83fbf3a8eb75be2f5894662c46444
7
- data.tar.gz: f643c1d3b9e6bd13a269cf3ceb5b9ea6ec7dffc27927c4d1cc9384201285be74b335e62a167faf89c5cb0295936a449f10c0201eb987937ae4f050119d662fff
6
+ metadata.gz: 8d1129484277dfcb2630aba294fdc2b2443017836376b556842a7d0faacbb4d4f77ef9e4e5f82653a32946c815c550182e26dd61b60f3c61a49fb589d539aa12
7
+ data.tar.gz: 8ca92c6b85bf56415ea5c3be928c93141fb8b4d3d733845843699ee028103e5416b0fe2520fb23817d9ee9596fad4dea3764d08d0a1c0edd79a47a047572949b
data/Rakefile CHANGED
@@ -56,7 +56,7 @@ task :compare_matrixes do |_|
56
56
  require "dhash"
57
57
  require_relative "lib/dhash-vips"
58
58
  require "mll"
59
- [Dhash, DHashVips::DHash, DHashVips::IDHash].each do |m|
59
+ [[Dhash, :hamming], [DHashVips::DHash, :hamming], [DHashVips::IDHash, :distance]].each do |m, dm|
60
60
  hashes = %w{
61
61
  71662d4d4029a3b41d47d5baf681ab9a.jpg
62
62
  ad8a37f872956666c3077a3e9e737984.jpg
@@ -67,7 +67,7 @@ task :compare_matrixes do |_|
67
67
  df0a3b93e9412536ee8a11255f974141.jpg
68
68
  679634ff89a31279a39f03e278bc9a01.jpg
69
69
  }.map{ |filename| m.calculate "images/#{filename}" }
70
- table = MLL::table[m.method(:hamming), [hashes], [hashes]]
70
+ table = MLL::table[m.method(dm), [hashes], [hashes]]
71
71
  array = Array.new(4){ [] }
72
72
  hashes.size.times.to_a.repeated_combination(2) do |i, j|
73
73
  array[i == j ? 0 : (j - i).abs == 1 && (i + j - 1) % 4 == 0 ? [i, j] == [0, 1] ? 1 : 2 : 3].push table[i][j]
@@ -82,7 +82,7 @@ task :compare_images do |_|
82
82
  abort "there should be two image filenames passed as arguments" unless ARGV.size == 3
83
83
  require_relative "lib/dhash-vips"
84
84
  ha, hb = ARGV.drop(1).map &DHashVips::IDHash.method(:calculate)
85
- puts "distance: #{DHashVips::IDHash.hamming ha, hb}"
85
+ puts "distance: #{DHashVips::IDHash.distance ha, hb}"
86
86
 
87
87
  require "delegate"
88
88
  class ImageMutable < SimpleDelegator
@@ -178,12 +178,12 @@ task :compare_speed do
178
178
  end
179
179
  end
180
180
  end
181
- puts "hamming (1000 times):"
181
+ puts "distance (1000 times):"
182
182
  Benchmark.bm 18 do |bm|
183
- [Dhash, DHashVips::DHash, DHashVips::IDHash].zip(hashes) do |m, hs|
183
+ [[Dhash, :hamming], [DHashVips::DHash, :hamming], [DHashVips::IDHash, :distance]].zip(hashes) do |(m, dm), hs|
184
184
  bm.report m do
185
185
  hs.product hs do |h1, h2|
186
- 1000.times{ m.hamming h1, h2 }
186
+ 1000.times{ m.send dm, h1, h2 }
187
187
  end
188
188
  end
189
189
  end
@@ -31,12 +31,12 @@ module DHashVips
31
31
  module IDHash
32
32
  extend self
33
33
 
34
- def hamming a, b
34
+ def distance a, b
35
35
  # TODO: the hash_size=8 is hardcoded here
36
36
  ((a | b) & (a >> 128 ^ b >> 128)).to_s(2).count "1"
37
37
  end
38
38
 
39
- def median array
39
+ @@median = lambda do |array|
40
40
  h = array.size / 2
41
41
  return array[h] if array[h] != array[h - 1]
42
42
  right = array.dup
@@ -46,15 +46,15 @@ module DHashVips
46
46
  return right.uniq[1] if left.count(left.last) > right.count(right.first)
47
47
  left.last
48
48
  end
49
- fail unless 2 == median([1, 2, 2, 2, 2, 2, 3])
50
- fail unless 3 == median([1, 2, 2, 2, 2, 3, 3])
51
- fail unless 3 == median([1, 1, 2, 2, 3, 3, 3])
52
- fail unless 2 == median([1, 1, 1, 2, 3, 3, 3])
53
- fail unless 2 == median([1, 1, 2, 2, 2, 2, 3])
54
- fail unless 2 == median([1, 2, 2, 2, 2, 3])
55
- fail unless 3 == median([1, 2, 2, 3, 3, 3])
56
- fail unless 1 == median([1, 1, 1])
57
- fail unless 1 == median([1, 1])
49
+ fail unless 2 == @@median[[1, 2, 2, 2, 2, 2, 3]]
50
+ fail unless 3 == @@median[[1, 2, 2, 2, 2, 3, 3]]
51
+ fail unless 3 == @@median[[1, 1, 2, 2, 3, 3, 3]]
52
+ fail unless 2 == @@median[[1, 1, 1, 2, 3, 3, 3]]
53
+ fail unless 2 == @@median[[1, 1, 2, 2, 2, 2, 3]]
54
+ fail unless 2 == @@median[[1, 2, 2, 2, 2, 3]]
55
+ fail unless 3 == @@median[[1, 2, 2, 3, 3, 3]]
56
+ fail unless 1 == @@median[[1, 1, 1]]
57
+ fail unless 1 == @@median[[1, 1]]
58
58
 
59
59
  def calculate file, hash_size = 8
60
60
  image = Vips::Image.new_from_file file
@@ -62,11 +62,11 @@ module DHashVips
62
62
 
63
63
  array = image.to_a.map &:flatten
64
64
  d1, i1, d2, i2 = [array, array.transpose].flat_map do |a|
65
- d = MLL::subtract[a, a.rotate(1)].to_a.map &:to_a
66
- m = median d.flatten.map(&:abs).sort
65
+ d = MLL::subtract[a, a.rotate(1)].to_a.flat_map(&:to_a)
66
+ m = @@median.call d.map(&:abs).sort
67
67
  [
68
- d.flatten.map{ |c| c < 0 ? 1 : 0 }.join.to_i(2),
69
- d.flatten.map{ |c| c.abs >= m ? 1 : 0 }.join.to_i(2),
68
+ d.map{ |c| c < 0 ? 1 : 0 }.join.to_i(2),
69
+ d.map{ |c| c.abs >= m ? 1 : 0 }.join.to_i(2),
70
70
  ]
71
71
  end
72
72
  (((((d1 << hash_size * hash_size) + d2) << hash_size * hash_size) + i1) << hash_size * hash_size) + i2
@@ -1,3 +1,3 @@
1
1
  module DHashVips
2
- VERSION = "0.0.3.2"
2
+ VERSION = "0.0.4.0"
3
3
  end
@@ -3,7 +3,7 @@ require "dhash-vips"
3
3
  require "pp"
4
4
 
5
5
  [
6
- [DHashVips::DHash, 17, 18, 22, 39],
6
+ [DHashVips::DHash, :hamming, 17, 18, 22, 39],
7
7
  # [[0, 17, 29, 27, 22, 29, 30, 29],
8
8
  # [17, 0, 30, 26, 33, 36, 37, 36],
9
9
  # [29, 30, 0, 18, 39, 30, 39, 36],
@@ -12,7 +12,7 @@ require "pp"
12
12
  # [29, 36, 30, 30, 17, 0, 33, 30],
13
13
  # [30, 37, 39, 35, 28, 33, 0, 5],
14
14
  # [29, 36, 36, 34, 23, 30, 5, 0]]
15
- [DHashVips::IDHash, 15, 23, 28, 64],
15
+ [DHashVips::IDHash, :distance, 15, 23, 28, 64],
16
16
  # [[0, 16, 30, 32, 46, 58, 43, 43],
17
17
  # [16, 0, 28, 28, 47, 59, 46, 47],
18
18
  # [30, 28, 0, 15, 53, 49, 53, 52],
@@ -22,7 +22,7 @@ require "pp"
22
22
  # [43, 46, 53, 61, 43, 44, 0, 0],
23
23
  # [43, 47, 52, 64, 45, 44, 0, 0]]
24
24
  # [DHashVips::IDHash, 14, 14, 21],
25
- ].each do |lib, min_similar, max_similar, min_not_similar, max_not_similar|
25
+ ].each do |lib, dm, min_similar, max_similar, min_not_similar, max_not_similar|
26
26
 
27
27
  describe lib do
28
28
 
@@ -62,7 +62,7 @@ describe lib do
62
62
  end
63
63
 
64
64
  hashes = [*images, bw1, bw2].map &described_class.method(:calculate)
65
- table = MLL::table[described_class.method(:hamming), [hashes], [hashes]]
65
+ table = MLL::table[described_class.method(dm), [hashes], [hashes]]
66
66
 
67
67
  # require "pp"
68
68
  # pp table
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dhash-vips
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.2
4
+ version: 0.0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov