Module: Dhash

Extended by:
Dhash
Included in:
Dhash
Defined in:
lib/dhash.rb,
lib/dhash/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#calculate(file, hash_size = 8) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dhash.rb', line 9

def calculate(file, hash_size = 8)
  image = Magick::Image.read(file).first
  image = image.quantize(256, Magick::Rec601LumaColorspace, Magick::NoDitherMethod, 8)
  image = image.resize!(hash_size + 1, hash_size)

  difference = []

  hash_size.times do |row|
    hash_size.times do |col|
      pixel_left  = image.pixel_color(col, row).intensity
      pixel_right = image.pixel_color(col + 1, row).intensity
      difference << (pixel_left > pixel_right)
    end
  end

  difference.map {|d| d ? 1 : 0 }.join('').to_i(2)
end

#hamming(a, b) ⇒ Object



5
6
7
# File 'lib/dhash.rb', line 5

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