image_compare 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9172a89fc7bd3779f58bf4291d045db2e9de6ce8e1d17d7eed9be6970c6faed9
4
- data.tar.gz: f3d1a90a07c4d728ea51c2effd424d9a32b4567b53b59119a996637defebc4b7
3
+ metadata.gz: efc745c05f8c87808311a102dc55c92e030de6ce85a6512aed95161a817258ce
4
+ data.tar.gz: a243eda35bc9edf3313a8d34c7ad71ae2a066f3553faada189a58ea9db04c1e6
5
5
  SHA512:
6
- metadata.gz: c8a0fcbd0668b614592b65972d1326a7a2f2853da2a133150fd6d5d0e8153a46f7d986033cec071d6df6b6cdd6c5b0c7950cd76b07494dc6bd8e240ee4d928b3
7
- data.tar.gz: 8c81cf6accfeb98d780ff3ed724834b5816c0d245186086d9b075e77f56a8f29f9d52d20fa4d01210bc5a8ddd36dfb7e7694ffc9e8024fc809e4a10b31437a42
6
+ metadata.gz: 6009fd283629fc28bc28e5ed4a4e74dcf39946e23d16dd0bb03a35ca98fb069f8a5a3279d5b46cd3373550d80cccc7a5fda0c360277062a092c220ba4415827e
7
+ data.tar.gz: 3ea374f5b4c3e184ee34f9c0ff3aa36c8a400e3923bafa34bd90ca66b79c2da080f606bf31fb31d717496bf045f091eba9d6a7df96eb0283517587615f019233
data/CHANGELOG.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # Change log
2
2
 
3
3
  ## main
4
- - Add Color mode to generate "normal" diff images ([@gsguma][], [@cristianofmc][])
4
+ - v1.0.0: Add Color mode to generate "normal" diff images ([@gsguma][], [@cristianofmc][])
5
+ - v1.0.1: Fix all code smells and integration tests ([@gsguma][])
6
+ - v1.0.2: Fix gem documentation uri ([@gsguma][])
5
7
 
6
8
  [@gsguma]: https://github.com/gsguma
7
9
  [@cristianofmc]: https://github.com/cristianofmc
data/README.md CHANGED
@@ -11,7 +11,7 @@ This is an utility library for image regression testing.
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'image_compare'
14
+ gem "image_compare"
15
15
  ```
16
16
 
17
17
  Or adding to your project:
@@ -20,7 +20,7 @@ Or adding to your project:
20
20
  # my-cool-gem.gemspec
21
21
  Gem::Specification.new do |spec|
22
22
  # ...
23
- spec.add_dependency 'image_compare'
23
+ spec.add_dependency "image_compare"
24
24
  # ...
25
25
  end
26
26
  ```
@@ -33,38 +33,47 @@ ImageCompare supports different ways (_modes_) of comparing images.
33
33
 
34
34
  Source images used in examples:
35
35
 
36
- <img alt='a.png' src='spec/fixtures/a.png' />
37
- <img alt='b.png' src='spec/fixtures/b.png' />
36
+ a1.png
38
37
 
39
- ### Color
38
+ <img alt="a.png" src="spec/fixtures/a.png" />
39
+
40
+ b.png
41
+
42
+ <img alt="b.png" src="spec/fixtures/b.png" />
43
+
44
+ a1.png
45
+
46
+ <img alt="a1.png" src="spec/fixtures/a1.png" />
47
+
48
+ ### Base (Color) mode (a.png X a1.png)
40
49
 
41
50
  Compare pixels, resulting score is a ratio of unequal pixels (with respect to provided tolerance).
42
51
 
43
52
  Resulting diff contains version of the first image with different pixels highlighted in red and red bounding box.
44
53
 
45
- <img alt='color_diff' src='spec/fixtures/color_diff.png' />
54
+ <img alt="color_diff" src="spec/fixtures/color_diff.png" />
46
55
 
47
- ### Base (RGB) mode
56
+ ### RGB mode (a.png X b.png)
48
57
 
49
58
  Compare pixels by values, resulting score is a ratio of unequal pixels.
50
59
  Resulting diff represents per-channel difference.
51
60
 
52
- <img alt='rgb_diff.png' src='spec/fixtures/rgb_diff.png' width='480' />
61
+ <img alt="rgb_diff.png" src="spec/fixtures/rgb_diff.png" />
53
62
 
54
- ### Grayscale mode
63
+ ### Grayscale mode (a.png X a1.png)
55
64
 
56
65
  Compare pixels as grayscale (by brightness and alpha), resulting score is a ratio of unequal pixels (with respect to provided tolerance).
57
66
 
58
67
  Resulting diff contains grayscale version of the first image with different pixels highlighted in red and red bounding box.
59
68
 
60
- <img alt='grayscale_diff.png' src='spec/fixtures/grayscale_diff.png' width='480' />
69
+ <img alt="grayscale_diff.png" src="spec/fixtures/grayscale_diff.png" />
61
70
 
62
- ### Delta
71
+ ### Delta (a.png X a1.png)
63
72
 
64
73
  Compare pixels using [Delta E](https://en.wikipedia.org/wiki/Color_difference) distance.
65
74
  Resulting diff contains grayscale version of the first image with different pixels highlighted in red (with respect to diff score).
66
75
 
67
- <img alt='delta_diff.png' src='spec/fixtures/delta_diff.png' width='480' />
76
+ <img alt="delta_diff.png" src="spec/fixtures/delta_diff.png" />
68
77
 
69
78
  ## Usage
70
79
 
@@ -91,7 +100,7 @@ cmp.lower_threshold #=> 0.01
91
100
  cmp = ImageCompare::Matcher.new mode: :grayscale, tolerance: 0
92
101
  cmp.mode #=> ImageCompare::Modes::Grayscale
93
102
 
94
- res = cmp.compare('path/image1.png', 'path/image2.png')
103
+ res = cmp.compare("path/image1.png", "path/image2.png")
95
104
  res #=> ImageCompare::Result
96
105
  res.match? #=> true
97
106
  res.score #=> 0.0
@@ -101,44 +110,43 @@ res.difference_image #=> ImageCompare::Image
101
110
  res.difference_image.save(result)
102
111
 
103
112
  # without explicit matcher
104
- res = ImageCompare.compare('path/image1.png', 'path/image2.png', options)
113
+ res = ImageCompare.compare("path/image1.png", "path/image2.png", options)
105
114
  res.match? #=> true
106
115
  res.score #=> 0.0
107
116
 
108
117
  # equals to
109
- res = ImageCompare::Matcher.new(options).compare('my_images_path/image1.png', 'my_images_path/image2.png')
118
+ res = ImageCompare::Matcher.new(options).compare("my_images_path/image1.png", "my_images_path/image2.png")
110
119
  res.match? #=> true
111
120
  res.score #=> 0.0
112
121
  ```
113
122
 
114
- ## Excluding rectangle
123
+ ## Excluding rectangle (a.png X a1.png)
115
124
 
116
- <img alt='a.png' src='spec/fixtures/a.png' />
117
- <img alt='a1.png' src='spec/fixtures/a1.png' />
125
+ <img alt="a1.png" src="spec/fixtures/rgb_exclude_rect.png" />
118
126
 
119
127
  You can exclude rectangle from comparing by passing `:exclude_rect` to `compare`.
120
128
  E.g., if `path_1` and `path_2` contain images above
121
129
  ```ruby
122
- ImageCompare.compare('path/image1.png', 'path/image2.png', exclude_rect: [200, 150, 275, 200]).match? # => true
130
+ ImageCompare.compare("path/image1.png", "path/image2.png", mode: :rgb, exclude_rect: [200, 150, 275, 200]).match? # => true
123
131
 
124
132
  # or
125
133
 
126
- cmp = ImageCompare::Matcher.new exclude_rect: [200, 150, 275, 200]
127
- res = cmp.compare('path/image1.png', 'path/image2.png')
134
+ cmp = ImageCompare::Matcher.new mode: :rgb, exclude_rect: [200, 150, 275, 200]
135
+ res = cmp.compare("path/image1.png", "path/image2.png")
128
136
  res #=> ImageCompare::Result
129
137
  res.match? #=> true
130
138
  res.score #=> 0.0
131
139
 
132
140
  # Return diff image object
133
141
  res.difference_image #=> ImageCompare::Image
134
- res.difference_image.save('path/diff.png')
142
+ res.difference_image.save("path/diff.png")
135
143
  ```
136
144
  `[200, 150, 275, 200]` is array of two vertices of rectangle -- (200, 150) is left-top vertex and (275, 200) is right-bottom.
137
145
 
138
146
  ### Cucumber + Capybara example
139
147
  `support/env.rb`:
140
148
  ```ruby
141
- require 'image_compare'
149
+ require "image_compare"
142
150
  ```
143
151
 
144
152
  `support/capybara_extensions.rb`:
@@ -147,15 +155,15 @@ require 'image_compare'
147
155
 
148
156
  Capybara::Node::Element.class_eval do
149
157
  def rect_area
150
- rect_area = self.evaluate_script('this.getBoundingClientRect()')
158
+ rect_area = self.evaluate_script("this.getBoundingClientRect()") # rubocop:disable Style/RedundantSelf
151
159
 
152
160
  [
153
- rect_area['left'].to_f.round,
154
- rect_area['top'].to_f.round,
155
- rect_area['right'].to_f.round,
156
- rect_area['bottom'].to_f.round
161
+ rect_area["left"].to_f.round,
162
+ rect_area["top"].to_f.round,
163
+ rect_area["right"].to_f.round,
164
+ rect_area["bottom"].to_f.round
157
165
  ]
158
- rescue StandardError => e
166
+ rescue StandardError => e # rubocop:disable Style/RescueStandardError
159
167
  raise "Error getting element rect area: #{e.inspect}!"
160
168
  end
161
169
  end
@@ -163,11 +171,11 @@ end
163
171
 
164
172
  `steps/my_step.rb`:
165
173
  ```ruby
166
- my_element = page.find('#my_element').rect_area
174
+ my_element = page.find("#my_element").rect_area
167
175
 
168
176
  cmp = ImageCompare::Matcher.new exclude_rect: my_element.rect_area
169
- res = cmp.compare('path/image1.png', 'path/image2.png')
170
- res.difference_image.save('path/diff.png')
177
+ res = cmp.compare("path/image1.png", "path/image2.png")
178
+ res.difference_image.save("path/diff.png")
171
179
 
172
180
  expect(res.match?).to eq(true)
173
181
  ```
@@ -180,6 +188,20 @@ You can set bounds of comparing by passing `:include_rect` to `compare` with arr
180
188
 
181
189
  Bug reports and pull requests are welcome on GitHub at https://github.com/instantink/image_compare.
182
190
 
191
+ ### Running tests and code inspections
192
+
193
+ - `rake rubocop`
194
+ - `rake rubocop:md`
195
+ - `rake spec`
196
+ - `ruby examples/performance.rb` Create the "gemfile.local" file with the content below to run the performance tests:
197
+ ```ruby
198
+ # frozen_string_literal: true
199
+
200
+ source "https://rubygems.org"
201
+ gem "benchmark-ips", "~> 2.7", ">= 2.7.2"
202
+ ```
203
+
204
+
183
205
  ## License
184
206
 
185
207
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'chunky_png'
3
+ require "chunky_png"
4
4
 
5
5
  begin
6
- require 'oily_png' unless RUBY_PLATFORM == 'java'
6
+ require "oily_png" unless RUBY_PLATFORM == "java"
7
7
  rescue LoadError => _e
8
8
  nil
9
9
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'image_compare/color_methods'
3
+ require "image_compare/color_methods"
4
4
 
5
5
  module ImageCompare
6
6
  class Image < ChunkyPNG::Image
@@ -2,15 +2,15 @@
2
2
 
3
3
  module ImageCompare
4
4
  class Matcher
5
- require 'image_compare/image'
6
- require 'image_compare/result'
7
- require 'image_compare/modes'
5
+ require "image_compare/image"
6
+ require "image_compare/result"
7
+ require "image_compare/modes"
8
8
 
9
9
  MODES = {
10
- rgb: 'RGB',
11
- delta: 'Delta',
12
- grayscale: 'Grayscale',
13
- color: 'Color'
10
+ rgb: "RGB",
11
+ delta: "Delta",
12
+ grayscale: "Grayscale",
13
+ color: "Color"
14
14
  }.freeze
15
15
 
16
16
  attr_reader :mode
@@ -26,25 +26,27 @@ module ImageCompare
26
26
  b = Image.from_file(b) unless b.is_a?(Image)
27
27
 
28
28
  unless a.sizes_match?(b)
29
- raise SizesMismatchError,
30
- "Size mismatch: first image size: #{a.width}x#{a.height}, second image size: #{b.width}x#{b.height}"
29
+ raise(
30
+ SizesMismatchError,
31
+ "Size mismatch: first image size: #{a.width}x#{a.height}, second image size: #{b.width}x#{b.height}"
32
+ )
31
33
  end
32
34
 
33
35
  image_area = Rectangle.new(0, 0, a.width - 1, a.height - 1)
34
36
 
35
37
  unless mode.exclude_rect.nil?
36
38
  unless image_area.contains?(mode.exclude_rect)
37
- raise ArgumentError, 'Bounds must be in image'
39
+ raise ArgumentError, "Bounds must be in image"
38
40
  end
39
41
  end
40
42
 
41
43
  unless mode.include_rect.nil?
42
44
  unless image_area.contains?(mode.include_rect)
43
- raise ArgumentError, 'Bounds must be in image'
45
+ raise ArgumentError, "Bounds must be in image"
44
46
  end
45
47
  unless mode.exclude_rect.nil?
46
48
  unless mode.include_rect.contains?(mode.exclude_rect)
47
- raise ArgumentError, 'Included area must contain excluded'
49
+ raise ArgumentError, "Included area must contain excluded"
48
50
  end
49
51
  end
50
52
  end
@@ -3,7 +3,7 @@
3
3
  module ImageCompare
4
4
  module Modes
5
5
  class Base
6
- require 'image_compare/rectangle'
6
+ require "image_compare/rectangle"
7
7
  include ColorMethods
8
8
 
9
9
  attr_reader :result, :threshold, :lower_threshold, :bounds, :exclude_rect, :include_rect
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ImageCompare
4
4
  module Modes
5
- require 'image_compare/modes/base'
5
+ require "image_compare/modes/base"
6
6
 
7
7
  class Color < Base
8
8
  include ColorMethods
@@ -33,20 +33,20 @@ module ImageCompare
33
33
  left: bounds.bounds[0],
34
34
  top: bounds.bounds[1],
35
35
  right: bounds.bounds[2],
36
- bot: bounds.bounds[3]
36
+ bot: bounds.bounds[3]
37
37
  }
38
38
 
39
39
  exclude_area = {
40
40
  left: exclude_rect.bounds[0],
41
41
  top: exclude_rect.bounds[1],
42
42
  right: exclude_rect.bounds[2],
43
- bot: exclude_rect.bounds[3]
43
+ bot: exclude_rect.bounds[3]
44
44
  }
45
45
 
46
46
  diff_area[:left] <= exclude_area[:left] &&
47
- diff_area[:top] <= exclude_area[:top] &&
48
- diff_area[:right] >= exclude_area[:right] &&
49
- diff_area[:bot] >= exclude_area[:bot]
47
+ diff_area[:top] <= exclude_area[:top] &&
48
+ diff_area[:right] >= exclude_area[:right] &&
49
+ diff_area[:bot] >= exclude_area[:bot]
50
50
  end
51
51
 
52
52
  def pixels_equal?(a, b)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ImageCompare
4
4
  module Modes
5
- require 'image_compare/modes/base'
5
+ require "image_compare/modes/base"
6
6
 
7
7
  class Delta < Base
8
8
  attr_reader :tolerance
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ImageCompare
4
4
  module Modes
5
- require 'image_compare/modes/base'
5
+ require "image_compare/modes/base"
6
6
 
7
7
  class Grayscale < Base
8
8
  DEFAULT_TOLERANCE = 16
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ImageCompare
4
4
  module Modes
5
- require 'image_compare/modes/base'
5
+ require "image_compare/modes/base"
6
6
 
7
7
  class RGB < Base
8
8
  def pixels_equal?(a, b)
@@ -2,9 +2,9 @@
2
2
 
3
3
  module ImageCompare
4
4
  module Modes
5
- require 'image_compare/modes/color'
6
- require 'image_compare/modes/delta'
7
- require 'image_compare/modes/grayscale'
8
- require 'image_compare/modes/rgb'
5
+ require "image_compare/modes/color"
6
+ require "image_compare/modes/delta"
7
+ require "image_compare/modes/grayscale"
8
+ require "image_compare/modes/rgb"
9
9
  end
10
10
  end
@@ -28,8 +28,8 @@ module ImageCompare
28
28
 
29
29
  def contains_point?(x, y)
30
30
  (x >= left && y >= top && x <= right && y <= bot) ||
31
- (x <= right && y <= top && x >= left && y >= bot) ||
32
- (x.between?(right, left) && y.between?(bot, top))
31
+ (x <= right && y <= top && x >= left && y >= bot) ||
32
+ (x.between?(right, left) && y.between?(bot, top))
33
33
  end
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImageCompare
4
- VERSION = '1.0.1'
4
+ VERSION = "1.0.3"
5
5
  end
data/lib/image_compare.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'image_compare/version'
3
+ require "image_compare/version"
4
4
 
5
5
  module ImageCompare
6
6
  class SizesMismatchError < StandardError
7
7
  end
8
8
 
9
- require 'image_compare/color_methods'
10
- require 'image_compare/matcher'
9
+ require "image_compare/color_methods"
10
+ require "image_compare/matcher"
11
11
 
12
12
  def self.compare(path_a, path_b, **options)
13
13
  Matcher.new(**options).compare(path_a, path_b)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_compare
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - cristianofmc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-03-28 00:00:00.000000000 Z
12
+ date: 2023-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chunky_png
@@ -85,7 +85,7 @@ licenses:
85
85
  metadata:
86
86
  bug_tracker_uri: https://github.com/instantink/image_compare/issues
87
87
  changelog_uri: https://github.com/instantink/image_compare/blob/master/CHANGELOG.md
88
- documentation_uri: https://github.com/instantink/image_compare
88
+ documentation_uri: https://www.rubydoc.info/gems/image_compare/1.0.3
89
89
  homepage_uri: https://github.com/instantink/image_compare
90
90
  source_code_uri: https://github.com/instantink/image_compare
91
91
  post_install_message: