Class: Wraith::CompareImages

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/wraith/compare_images.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(config) ⇒ CompareImages

Returns a new instance of CompareImages.



12
13
14
# File 'lib/wraith/compare_images.rb', line 12

def initialize(config)
  @wraith = Wraith::Wraith.new(config)
end

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



10
11
12
# File 'lib/wraith/compare_images.rb', line 10

def wraith
  @wraith
end

Instance Method Details

#compare_imagesObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/wraith/compare_images.rb', line 16

def compare_images
  files = Dir.glob("#{wraith.directory}/*/*.png").sort
  Parallel.each(files.each_slice(2), :in_processes => Parallel.processor_count) do |base, compare|
    diff = base.gsub(/([a-zA-Z0-9]+).png$/, "diff.png")
    info = base.gsub(/([a-zA-Z0-9]+).png$/, "data.txt")
    logger.info "Comparing #{base} and #{compare}"
    compare_task(base, compare, diff, info)
    logger.info "Saved diff"
  end
end

#compare_task(base, compare, output, info) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/wraith/compare_images.rb', line 33

def compare_task(base, compare, output, info)
  cmdline = "compare -fuzz #{wraith.fuzz} -metric AE -highlight-color #{wraith.highlight_color} #{base} #{compare.shellescape} #{output}"
  px_value = Open3.popen3(cmdline) { |_stdin, _stdout, stderr, _wait_thr| stderr.read }.to_f
  begin
    img_size = ImageSize.path(output).size.inject(:*)
    percentage(img_size, px_value, info)
  rescue
    File.open(info, "w") { |file| file.write("invalid") } unless File.exist?(output)
  end
end

#percentage(img_size, px_value, info) ⇒ Object



27
28
29
30
31
# File 'lib/wraith/compare_images.rb', line 27

def percentage(img_size, px_value, info)
  pixel_count = (px_value / img_size) * 100
  rounded = pixel_count.round(2)
  File.open(info, "w") { |file| file.write(rounded) }
end