Module: DHashVips::DHash

Extended by:
DHash
Included in:
DHash
Defined in:
lib/dhash-vips.rb

Instance Method Summary collapse

Instance Method Details

#calculate(file, hash_size = 8, kernel = nil) ⇒ Object


21
22
23
24
25
# File 'lib/dhash-vips.rb', line 21

def calculate file, hash_size = 8, kernel = nil
  image = pixelate file, hash_size, kernel

  image.cast("int").conv([[1, -1]]).crop(1, 0, hash_size, hash_size).>(0)./(255).cast("uchar").to_a.join.to_i(2)
end

#hamming(a, b) ⇒ Object


8
9
10
# File 'lib/dhash-vips.rb', line 8

def hamming a, b
  (a ^ b).to_s(2).count "1"
end

#pixelate(file, hash_size, kernel = nil) ⇒ Object


12
13
14
15
16
17
18
19
# File 'lib/dhash-vips.rb', line 12

def pixelate file, hash_size, kernel = nil
  image = Vips::Image.new_from_file file, access: :sequential
  if kernel
    image.resize((hash_size + 1).fdiv(image.width), vscale: hash_size.fdiv(image.height), kernel: kernel).colourspace("b-w")
  else
    image.resize((hash_size + 1).fdiv(image.width), vscale: hash_size.fdiv(image.height)                ).colourspace("b-w")
  end
end