Class: RPDiff

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(img1, img2, options = {}) ⇒ RPDiff

Returns a new instance of RPDiff.



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

def initialize(img1, img2, options={})
  @img1, @img2, @options = img1, img2, options
end

Instance Attribute Details

#fovObject (readonly)

Returns the value of attribute fov.



6
7
8
# File 'lib/rpdiff.rb', line 6

def fov
  @fov
end

#gammaObject (readonly)

Returns the value of attribute gamma.



6
7
8
# File 'lib/rpdiff.rb', line 6

def gamma
  @gamma
end

#identicalObject (readonly)

Returns the value of attribute identical.



6
7
8
# File 'lib/rpdiff.rb', line 6

def identical
  @identical
end

#img1Object (readonly)

Returns the value of attribute img1.



6
7
8
# File 'lib/rpdiff.rb', line 6

def img1
  @img1
end

#img2Object (readonly)

Returns the value of attribute img2.



6
7
8
# File 'lib/rpdiff.rb', line 6

def img2
  @img2
end

#incomparableObject (readonly)

Returns the value of attribute incomparable.



6
7
8
# File 'lib/rpdiff.rb', line 6

def incomparable
  @incomparable
end

#indistinguishableObject (readonly)

Returns the value of attribute indistinguishable.



6
7
8
# File 'lib/rpdiff.rb', line 6

def indistinguishable
  @indistinguishable
end

#luminanceObject (readonly)

Returns the value of attribute luminance.



6
7
8
# File 'lib/rpdiff.rb', line 6

def luminance
  @luminance
end

#match_percentageObject (readonly)

Returns the value of attribute match_percentage.



6
7
8
# File 'lib/rpdiff.rb', line 6

def match_percentage
  @match_percentage
end

#pixel_differenceObject (readonly)

Returns the value of attribute pixel_difference.



6
7
8
# File 'lib/rpdiff.rb', line 6

def pixel_difference
  @pixel_difference
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



6
7
8
# File 'lib/rpdiff.rb', line 6

def threshold
  @threshold
end

#visibly_differentObject (readonly)

Returns the value of attribute visibly_different.



6
7
8
# File 'lib/rpdiff.rb', line 6

def visibly_different
  @visibly_different
end

Instance Method Details

#calculate_match_percentageObject

Optional gem requirement to get a % value out of image similarity.



36
37
38
39
40
# File 'lib/rpdiff.rb', line 36

def calculate_match_percentage
  return 0 if incomparable
  @image_sizes = FastImage.size(@img1)
  @match_percentage = 100 - (@pixel_difference.to_f * 100 / (@image_sizes.first * @image_sizes.last).to_f)
end

#diffObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rpdiff.rb', line 13

def diff
  @indistinguishable = @visibly_different = @identical = false
  @pixel_difference = 0

  opts = "#{@img1} #{@img2} "
  @options.each {|k, v| opts << "-#{k} #{v} "}
  io = `perceptualdiff #{opts}`

  io.split(/\n/).each do |line|
    @fov               = $1   if line =~ /Field of view is (\d+\.\d+) degrees/
    @threshold         = $1   if line =~ /Threshold pixels is (\d+) pixels/
    @gamma             = $1   if line =~ /The Gamma is (\d+\.\d+)/
    @luminance         = $1   if line =~ /The Display's luminance is (\d+\.\d+) candela per meter squared/
    @pixel_difference  = $1   if line =~ /(\d+) pixels are different/
    @visibly_different = true if line =~ /FAIL: Images are visibly different/
    @identical         = true if line =~ /PASS: Images are binary identical/
    @indistinguishable = true if line =~ /PASS: Images are perceptually indistinguishable/
    @incomparable      = true if line =~ /FAIL: Image dimensions do not match/
  end
  io
end