Class: AverageHash::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/average_hash/image.rb

Constant Summary collapse

DEFAULT_DUPE_THRESHOLD =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Image

Returns a new instance of Image.



9
10
11
# File 'lib/average_hash/image.rb', line 9

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



7
8
9
# File 'lib/average_hash/image.rb', line 7

def file_path
  @file_path
end

Instance Method Details

#distance_from(other) ⇒ Object



18
19
20
# File 'lib/average_hash/image.rb', line 18

def distance_from(other)
  (fingerprint ^ other.fingerprint).to_s(2).count('1')
end

#duplicate?(other, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/average_hash/image.rb', line 22

def duplicate?(other, opts = {})
  threshold = opts[:threshold] || DEFAULT_DUPE_THRESHOLD
  distance_from(other) <= threshold
end

#fingerprintObject



13
14
15
16
# File 'lib/average_hash/image.rb', line 13

def fingerprint
  @average_hash ||= HashingStrategy.new(@file_path).generate_hash
  @average_hash.to_i(2)
end