Module: Applitools::Utils::ImageUtils
Instance Method Summary collapse
-
#base64_from_png_image(image) ⇒ String
Get the Base64 representation of the raw PNG bytes of an image.
-
#bytes_from_png_image(image) ⇒ String
Get the raw PNG bytes of an image.
-
#cut(image, crop_region) ⇒ Object
Cuts the image according to passed crop_region.
-
#cut!(image, crop_region) ⇒ Object
Cuts the image according to passed crop_region.
-
#png_image_from_base64(png_bytes) ⇒ ChunkyPNG::Canvas
Creates an image instance from a base64 representation of its PNG encoding.
-
#png_image_from_bytes(png_bytes) ⇒ ChunkyPNG::Canvas
Creates an image object from the PNG bytes.
-
#quadrant_rotate!(image, num_quadrants) ⇒ Object
Rotates a matrix 90 deg clockwise or counter clockwise (depending whether num_quadrants is positive or negative, respectively).
- #resize_image(image, new_width, new_height) ⇒ Object
- #resize_image!(image, new_width, new_height) ⇒ Object
- #scale(image, scale_ratio) ⇒ Object
-
#scale!(image, scale_ratio) ⇒ Applitools::Screenshot
Scales image by given scale factor by modifying given image.
- #scale_image_incrementally(image, new_width, new_height) ⇒ Object
- #scale_image_incrementally!(image, new_width, new_height) ⇒ Object
Instance Method Details
#base64_from_png_image(image) ⇒ String
Get the Base64 representation of the raw PNG bytes of an image.
37 38 39 |
# File 'lib/applitools/utils/image_utils.rb', line 37 def base64_from_png_image(image) Base64.encode64(bytes_from_png_image(image)) end |
#bytes_from_png_image(image) ⇒ String
Get the raw PNG bytes of an image.
30 31 32 |
# File 'lib/applitools/utils/image_utils.rb', line 30 def bytes_from_png_image(image) image.to_blob(:fast_rgb) end |
#cut(image, crop_region) ⇒ Object
Cuts the image according to passed crop_region. The method returns new instance of the image without changing the source image.
73 74 75 |
# File 'lib/applitools/utils/image_utils.rb', line 73 def cut(image, crop_region) cut! image.dup, crop_region end |
#cut!(image, crop_region) ⇒ Object
Cuts the image according to passed crop_region. The method mutates an image.
64 65 66 |
# File 'lib/applitools/utils/image_utils.rb', line 64 def cut!(image, crop_region) image.crop! crop_region.left, crop_region.top, crop_region.width, crop_region.height end |
#png_image_from_base64(png_bytes) ⇒ ChunkyPNG::Canvas
Creates an image instance from a base64 representation of its PNG encoding.
23 24 25 |
# File 'lib/applitools/utils/image_utils.rb', line 23 def png_image_from_base64(png_bytes) png_image_from_bytes(Base64.decode64(png_bytes)) end |
#png_image_from_bytes(png_bytes) ⇒ ChunkyPNG::Canvas
Creates an image object from the PNG bytes.
16 17 18 |
# File 'lib/applitools/utils/image_utils.rb', line 16 def png_image_from_bytes(png_bytes) ChunkyPNG::Image.from_blob(png_bytes) end |
#quadrant_rotate!(image, num_quadrants) ⇒ Object
Rotates a matrix 90 deg clockwise or counter clockwise (depending whether num_quadrants is positive or negative, respectively).
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/applitools/utils/image_utils.rb', line 46 def quadrant_rotate!(image, num_quadrants) num_quadrants %= QUADRANTS_COUNT case num_quadrants when 0 image when 1 image.rotate_right! when 2 image.rotate_180! when 3 image.rotate_left! end end |
#resize_image(image, new_width, new_height) ⇒ Object
114 115 116 |
# File 'lib/applitools/utils/image_utils.rb', line 114 def resize_image(image, new_width, new_height) resize_image!(image.dup, new_width, new_height) end |
#resize_image!(image, new_width, new_height) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/applitools/utils/image_utils.rb', line 96 def resize_image!(image, new_width, new_height) Applitools::ArgumentGuard.not_nil(new_width, 'new_width') Applitools::ArgumentGuard.not_nil(new_height, 'new_height') Applitools::ArgumentGuard.not_nil(image, 'image') Applitools::ArgumentGuard.is_a?(image, 'image', ::ChunkyPNG::Image) raise Applitools::EyesIllegalArgument.new "Invalid width: #{new_width}" if new_width <= 0 raise Applitools::EyesIllegalArgument.new "Invalid height: #{new_height}" if new_height <= 0 return image if image.width == new_width && image.height == new_height if new_width > image.width || new_height > image.height image.resample_bicubic!(new_width, new_height) else scale_image_incrementally!(image, new_width, new_height) end end |
#scale(image, scale_ratio) ⇒ Object
92 93 94 |
# File 'lib/applitools/utils/image_utils.rb', line 92 def scale(image, scale_ratio) scale!(image.dup, scale_ratio) end |
#scale!(image, scale_ratio) ⇒ Applitools::Screenshot
Scales image by given scale factor by modifying given image
82 83 84 85 86 87 88 89 90 |
# File 'lib/applitools/utils/image_utils.rb', line 82 def scale!(image, scale_ratio) return image if scale_ratio == 1 image_ratio = image.width.to_f / image.height.to_f scale_width = (image.width * scale_ratio).ceil scale_height = (scale_width / image_ratio).ceil buffered_image = image.image resize_image!(buffered_image, scale_width, scale_height) image.update!(buffered_image) end |
#scale_image_incrementally(image, new_width, new_height) ⇒ Object
142 143 144 |
# File 'lib/applitools/utils/image_utils.rb', line 142 def scale_image_incrementally(image, new_width, new_height) scale_image_incrementally!(image.dup, new_width, new_height) end |
#scale_image_incrementally!(image, new_width, new_height) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/applitools/utils/image_utils.rb', line 118 def scale_image_incrementally!(image, new_width, new_height) current_width = image.width current_height = image.height while current_width != new_width || current_height != new_height prev_current_width = current_width prev_current_height = current_height if current_width > new_width current_width -= (current_width / RESAMPLE_INCREMENTALLY_FRACTION) current_width = new_width if current_width < new_width end if current_height > new_height current_height -= (current_height / RESAMPLE_INCREMENTALLY_FRACTION) current_height = new_height if current_height < new_height end return image if prev_current_width == current_width && prev_current_height == current_height Applitools::EyesLogger.debug "Incremental dimensions: #{current_width} x #{current_height}" image.resample_bicubic!(current_width, current_height) end image end |