Module: Applitools::Utils::ImageUtils

Extended by:
ImageUtils
Includes:
MethodTracer
Included in:
ImageUtils
Defined in:
lib/applitools/utils/image_utils.rb

Defined Under Namespace

Classes: Screenshot

Instance Method Summary collapse

Methods included from MethodTracer

included

Instance Method Details

#base64_from_png_image(image) ⇒ Object

Get the Base64 representation of the raw PNG bytes of an image.

ChunkyPNG::Canvas The image object for which to get the PNG bytes.

Returns: String the Base64 representation of the raw PNG bytes of an image.



46
47
48
# File 'lib/applitools/utils/image_utils.rb', line 46

def base64_from_png_image(image)
  Base64.encode64(bytes_from_png_image(image))
end

#bytes_from_png_image(image) ⇒ Object

Get the raw PNG bytes of an image.

ChunkyPNG::Canvas The image object for which to get the PNG bytes.

Returns: String The PNG bytes of the image.



36
37
38
# File 'lib/applitools/utils/image_utils.rb', line 36

def bytes_from_png_image(image)
  image.to_blob(:fast_rgb)
end

#png_image_from_base64(png_bytes64) ⇒ Object

Creates an image instance from a base64 representation of its PNG encoding.

png_bytes64

String The Base64 representation of a PNG image.

Returns: ChunkyPNG::Canvas An image object.



26
27
28
# File 'lib/applitools/utils/image_utils.rb', line 26

def png_image_from_base64(png_bytes64)
  png_image_from_bytes(Base64.decode64(png_bytes64))
end

#png_image_from_bytes(png_bytes) ⇒ Object

Creates an image object from the PNG bytes.

png_bytes

String A binary string of the PNG bytes of the image.

Returns: ChunkyPNG::Canvas An image object.



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).

image

ChunkyPNG::Canvas The image to rotate.

num_quadrants

Integer The number of rotations to perform. Positive values are used for clockwise rotation

and negative values are used for counter-clockwise rotation.


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/applitools/utils/image_utils.rb', line 57

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

#scale!(image, factor) ⇒ Object



72
73
74
# File 'lib/applitools/utils/image_utils.rb', line 72

def scale!(image, factor)
  image.resample_nearest_neighbor!(image.width.to_f * factor, image.height.to_f * factor)
end

#stitch_images(size, images_data) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/applitools/utils/image_utils.rb', line 76

def stitch_images(size, images_data)
  stitched_screenshot = ChunkyPNG::Image.new(size.width, size.height, ChunkyPNG::Color::TRANSPARENT).tap do |res|
    images_data.each do |image_data|
      # Crop out of bounds images.
      image = image_data.image
      position = image_data.position

      new_width = position.left + image.width > size.width ? size.width - position.left : image.width
      new_height = position.top + image.height > size.height ? size.height - position.top : image.height

      if new_width != image.width || new_height != image.height
        image = image.crop!(0, 0, new_width, new_height)
      end

      res.replace!(image.restore, position.left, position.top)
      GC.start
    end
  end
  result = Applitools::Utils::ImageUtils::Screenshot.new stitched_screenshot.to_blob.dup
  GC.start
  result
end