Class: SnapDiff::AnnotationService

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

Overview

Draws diff/skip-area rectangles and the heatmap overlay for a Difference, and saves the resulting images to their .diff.* / .heatmap.diff.* paths.

Extracted from Reporters::Default (ADR-004 PR 7) so the reporter only builds error messages; this class owns all image annotation work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difference) ⇒ AnnotationService

Returns a new instance of AnnotationService.



17
18
19
20
21
22
23
24
25
26
# File 'lib/snap_diff/annotation_service.rb', line 17

def initialize(difference)
  @difference = difference

  comparison = difference.comparison
  ext = comparison.new_image_path.extname.delete_prefix(".")
  screenshot_format = comparison.options[:screenshot_format] || (ext unless ext.empty?) || "png"
  @annotated_image_path = comparison.new_image_path.sub_ext(".diff.#{screenshot_format}")
  @annotated_base_image_path = comparison.base_image_path.sub_ext(".diff.#{screenshot_format}")
  @heatmap_diff_path = comparison.new_image_path.sub_ext(".heatmap.diff.#{screenshot_format}")
end

Instance Attribute Details

#annotated_base_image_pathObject (readonly)

Returns the value of attribute annotated_base_image_path.



15
16
17
# File 'lib/snap_diff/annotation_service.rb', line 15

def annotated_base_image_path
  @annotated_base_image_path
end

#annotated_image_pathObject (readonly)

Returns the value of attribute annotated_image_path.



15
16
17
# File 'lib/snap_diff/annotation_service.rb', line 15

def annotated_image_path
  @annotated_image_path
end

#heatmap_diff_pathObject (readonly)

Returns the value of attribute heatmap_diff_path.



15
16
17
# File 'lib/snap_diff/annotation_service.rb', line 15

def heatmap_diff_path
  @heatmap_diff_path
end

Instance Method Details

#annotate_and_save_imagesObject



34
35
36
37
38
# File 'lib/snap_diff/annotation_service.rb', line 34

def annotate_and_save_images
  save_annotation_for(new_image, annotated_image_path)
  save_annotation_for(base_image, annotated_base_image_path)
  save_heatmap_diff if difference.diff_mask
end

#annotate_difference(image, region) ⇒ Object



47
48
49
# File 'lib/snap_diff/annotation_service.rb', line 47

def annotate_difference(image, region)
  driver.draw_rectangles([image], region, RED_RGBA, offset: 1).first
end

#annotate_skip_areas(image, skip_areas) ⇒ Object



51
52
53
54
55
# File 'lib/snap_diff/annotation_service.rb', line 51

def annotate_skip_areas(image, skip_areas)
  skip_areas.reduce(image) do |memo, region|
    driver.draw_rectangles([memo], region, ORANGE_RGBA).first
  end
end

#clean_tmp_filesObject



28
29
30
31
32
# File 'lib/snap_diff/annotation_service.rb', line 28

def clean_tmp_files
  annotated_base_image_path.unlink if annotated_base_image_path.exist?
  annotated_image_path.unlink if annotated_image_path.exist?
  heatmap_diff_path.unlink if heatmap_diff_path.exist?
end

#save(image, image_path) ⇒ Object



57
58
59
# File 'lib/snap_diff/annotation_service.rb', line 57

def save(image, image_path)
  driver.save_image_to(image, image_path.to_s)
end

#save_annotation_for(image, image_path) ⇒ Object



40
41
42
43
44
45
# File 'lib/snap_diff/annotation_service.rb', line 40

def save_annotation_for(image, image_path)
  image = annotate_difference(image, difference.region)
  image = annotate_skip_areas(image, difference.comparison.skip_area) if difference.comparison.skip_area

  save(image, image_path.to_path)
end