Module: Applitools::Utils::ImageUtils

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

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.



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

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.



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

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.



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

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.



15
16
17
# File 'lib/applitools/utils/image_utils.rb', line 15

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.


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

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



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

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



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

def stitch_images(size, images_data)
  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.compose!(image, position.left, position.top)
    end
  end
end