dhash-vips 0.0.2.0 → 0.0.3.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: cf9095db2c280a43b60bd9368892760b04c76d34
4
- data.tar.gz: 247f6ea261c5d9fdf65c462127546259f8e36453
3
+ metadata.gz: e1493f450019e00cceb6c31ce3d36addc04dc4e3
4
+ data.tar.gz: 5a0ae28a8c5d48ce59e46b0306e8b3d786ae64c2
5
5
  SHA512:
6
- metadata.gz: 0e9935a7db96888ca51750fa02ffea220fe20a9e5eb499fdc88b9b421d68d8f2699345702b2281de4d400c691feafeb69014b7c21a7fd37ac0ae853716231d1a
7
- data.tar.gz: c2df29036d4b80174df137c976fb6396704bbde4cc05299d6751b519a928e11f6061eff6fc7b4c225e48d8a38c26b1a6cfefd763f0a3a4c1df738051997af84f
6
+ metadata.gz: 54e3a2b45b2040ffd30a0618850c173996c27e0d26fbe1495633b96ebb70586c426a3e0a9bc1e8f1403bd8c06d620eb9cb234cf84b5922a968eb2c69b0fec3a8
7
+ data.tar.gz: 9a26b10786c5db24093b1b9934d2a48bbc77bc7e31c780e70898f8be6816803b4a9ab817139abc243c8c7782e0a8973474e27f35fe64c4283742af8db34bcd6b
@@ -1,5 +1,6 @@
1
1
  require_relative "dhash-vips/version"
2
2
  require "vips"
3
+ require "mll"
3
4
 
4
5
  module DHashVips
5
6
  module DHash
@@ -22,7 +23,7 @@ module DHashVips
22
23
  def calculate file, hash_size = 8, kernel = nil
23
24
  image = pixelate file, hash_size, kernel
24
25
 
25
- image.cast("int").conv([1, -1]).crop(1, 0, 8, 8).>(0)./(255).cast("uchar").to_a.join.to_i(2)
26
+ image.cast("int").conv([1, -1]).crop(1, 0, hash_size, hash_size).>(0)./(255).cast("uchar").to_a.join.to_i(2)
26
27
  end
27
28
 
28
29
  end
@@ -31,10 +32,10 @@ module DHashVips
31
32
  extend self
32
33
 
33
34
  def hamming a, b
34
- ad = a >> 64
35
- ai = a - (ad << 64)
36
- bd = b >> 64
37
- bi = b - (bd << 64)
35
+ ad = a >> 128
36
+ ai = a - (ad << 128)
37
+ bd = b >> 128
38
+ bi = b - (bd << 128)
38
39
  ((ai | bi) & (ad ^ bd)).to_s(2).count "1"
39
40
  end
40
41
 
@@ -60,12 +61,18 @@ module DHashVips
60
61
 
61
62
  def calculate file, hash_size = 8
62
63
  image = Vips::Image.new_from_file file
63
- image = image.resize((hash_size + 1).fdiv(image.width), vscale: hash_size.fdiv(image.height)).colourspace("b-w")
64
+ image = image.resize(hash_size.fdiv(image.width), vscale: hash_size.fdiv(image.height)).colourspace("b-w")
64
65
 
65
- conv = image.cast("int").conv([1, -1]).crop(1, 0, 8, 8)
66
- d = conv.>(0)./(255).cast("uchar").to_a.join.to_i(2)
67
- i = conv.abs.>=(median conv.abs.to_a.flatten.sort)./(255).cast("uchar").to_a.join.to_i(2)
68
- (d << 64) + i
66
+ array = image.to_a.map &:flatten
67
+ d1, i1, d2, i2 = [array, array.transpose].flat_map do |a|
68
+ d = MLL::subtract[a, a.rotate(1)].to_a.map &:to_a
69
+ m = median d.flatten.map(&:abs).sort
70
+ [
71
+ d.flatten.map{ |c| c < 0 ? 1 : 0 }.join.to_i(2),
72
+ d.flatten.map{ |c| c.abs >= m ? 1 : 0 }.join.to_i(2),
73
+ ]
74
+ end
75
+ (((((d1 << 64) + d2) << 64) + i1) << 64) + i2
69
76
  end
70
77
 
71
78
  end
@@ -1,3 +1,3 @@
1
1
  module DHashVips
2
- VERSION = "0.0.2.0"
2
+ VERSION = "0.0.3.0"
3
3
  end
@@ -1,21 +1,28 @@
1
1
  require "dhash-vips"
2
2
 
3
+ require "pp"
4
+
3
5
  [
4
- [DHashVips::DHash, 17, 18, 22],
5
- # [[0, 17, 29, 27, 22, 29],
6
- # [17, 0, 30, 26, 33, 36],
7
- # [29, 30, 0, 18, 39, 30],
8
- # [27, 26, 18, 0, 35, 30],
9
- # [22, 33, 39, 35, 0, 17],
10
- # [29, 36, 30, 30, 17, 0]]
11
- [DHashVips::IDHash, 5, 10, 15],
12
- # [[0, 5, 21, 23, 18, 23],
13
- # [5, 0, 15, 16, 17, 28],
14
- # [21, 15, 0, 10, 31, 25],
15
- # [23, 16, 10, 0, 26, 28],
16
- # [18, 17, 31, 26, 0, 8],
17
- # [23, 28, 25, 28, 8, 0]]
18
- ].each do |lib, min_similar, max_similar, min_not_similar|
6
+ [DHashVips::DHash, 17, 18, 22, 39],
7
+ # [[0, 17, 29, 27, 22, 29, 30, 29],
8
+ # [17, 0, 30, 26, 33, 36, 37, 36],
9
+ # [29, 30, 0, 18, 39, 30, 39, 36],
10
+ # [27, 26, 18, 0, 35, 30, 35, 34],
11
+ # [22, 33, 39, 35, 0, 17, 28, 23],
12
+ # [29, 36, 30, 30, 17, 0, 33, 30],
13
+ # [30, 37, 39, 35, 28, 33, 0, 5],
14
+ # [29, 36, 36, 34, 23, 30, 5, 0]]
15
+ [DHashVips::IDHash, 15, 23, 28, 64],
16
+ # [[0, 16, 30, 32, 46, 58, 43, 43],
17
+ # [16, 0, 28, 28, 47, 59, 46, 47],
18
+ # [30, 28, 0, 15, 53, 49, 53, 52],
19
+ # [32, 28, 15, 0, 56, 53, 61, 64],
20
+ # [46, 47, 53, 56, 0, 23, 43, 45],
21
+ # [58, 59, 49, 53, 23, 0, 44, 44],
22
+ # [43, 46, 53, 61, 43, 44, 0, 0],
23
+ # [43, 47, 52, 64, 45, 44, 0, 0]]
24
+ # [DHashVips::IDHash, 14, 14, 21],
25
+ ].each do |lib, min_similar, max_similar, min_not_similar, max_not_similar|
19
26
 
20
27
  describe lib do
21
28
 
@@ -69,7 +76,7 @@ describe lib do
69
76
  when (j - i).abs == 1 && (i + j - 1) % 4 == 0
70
77
  if [i, j] == [hashes.size - 2, hashes.size - 1]
71
78
  if described_class == DHashVips::DHash
72
- expect(table[i][j]).to be > 0
79
+ expect(table[i][j]).to be == 5
73
80
  else
74
81
  expect(table[i][j]).to eq 0
75
82
  end
@@ -77,7 +84,7 @@ describe lib do
77
84
  expect(table[i][j]).to be_between(min_similar, max_similar).inclusive
78
85
  end
79
86
  else
80
- expect(table[i][j]).to be >= min_not_similar
87
+ expect(table[i][j]).to be_between(min_not_similar, max_not_similar).inclusive
81
88
  end
82
89
  end
83
90
 
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.2.0
4
+ version: 0.0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov