module ChunkyPNG::Canvas::Operations

def check_size_constraints!(other, offset_x, offset_y)

Raises:
  • (ChunkyPNG::OutOfBounds) - when the other image doesn't fit.

Parameters:
  • offset_y (Integer) -- The y offset on which the other image will be applied.
  • offset_x (Integer) -- The x offset on which the other image will be applied.
  • other (ChunkyPNG::Canvas) -- The other canvas
def check_size_constraints!(other, offset_x, offset_y)
  raise ChunkyPNG::OutOfBounds, "Background image width is too small!"  if width  < other.width  + offset_x
  raise ChunkyPNG::OutOfBounds, "Background image height is too small!" if height < other.height + offset_y
end

def compose(other, offset_x = 0, offset_y = 0)

Other tags:
    See: #replace -

Other tags:
    Note: - API changed since 1.0 - This method now no longer is in place, but returns

Raises:
  • (ChunkyPNG::OutOfBounds) - when the other canvas doesn't fit on this one,

Returns:
  • (ChunkyPNG::Canvas) - Returns the new canvas, composed of the other 2.

Parameters:
  • () --
def compose(other, offset_x = 0, offset_y = 0)
  dup.compose!(other, offset_x, offset_y)
end

def compose!(other, offset_x = 0, offset_y = 0)

Other tags:
    See: #compose -
    See: #replace! -

Raises:
  • (ChunkyPNG::OutOfBounds) - when the other canvas doesn't fit on this one,

Returns:
  • (ChunkyPNG::Canvas) - Returns itself, but with the other canvas composed onto it.

Parameters:
  • offset_y (Integer) -- The y-offset to apply the new forgeround on.
  • offset_x (Integer) -- The x-offset to apply the new forgeround on.
  • other (ChunkyPNG::Canvas) -- The foreground canvas to compose on the
def compose!(other, offset_x = 0, offset_y = 0)
  check_size_constraints!(other, offset_x, offset_y)
  for y in 0...other.height do
    for x in 0...other.width do
      set_pixel(x + offset_x, y + offset_y, ChunkyPNG::Color.compose(other.get_pixel(x, y), get_pixel(x + offset_x, y + offset_y)))
    end
  end
  self
end

def crop(x, y, crop_width, crop_height)

Raises:
  • (ChunkyPNG::OutOfBounds) - when the crop dimensions plus the given coordinates

Returns:
  • (ChunkyPNG::Canvas) - Returns the newly created cropped image.

Parameters:
  • crop_height (Integer) -- The height of the image to be cropped.
  • crop_width (Integer) -- The width of the image to be cropped.
  • y (Integer) -- The y-coordinate of the top left corner of the image to be cropped.
  • x (Integer) -- The x-coordinate of the top left corner of the image to be cropped.
def crop(x, y, crop_width, crop_height)
  dup.crop!(x, y, crop_width, crop_height)
end

def crop!(x, y, crop_width, crop_height)

Raises:
  • (ChunkyPNG::OutOfBounds) - when the crop dimensions plus the given coordinates

Returns:
  • (ChunkyPNG::Canvas) - Returns itself, but cropped.

Parameters:
  • crop_height (Integer) -- The height of the image to be cropped.
  • crop_width (Integer) -- The width of the image to be cropped.
  • y (Integer) -- The y-coordinate of the top left corner of the image to be cropped.
  • x (Integer) -- The x-coordinate of the top left corner of the image to be cropped.
def crop!(x, y, crop_width, crop_height)
  
  raise ChunkyPNG::OutOfBounds, "Image width is too small!" if crop_width + x > width
  raise ChunkyPNG::OutOfBounds, "Image width is too small!" if crop_height + y > height
  
  new_pixels = []
  for cy in 0...crop_height do
    new_pixels += pixels.slice((cy + y) * width + x, crop_width)
  end
  replace_canvas!(crop_width, crop_height, new_pixels)
end

def flip_horizontally

Other tags:
    See: #flip_horizontally! -

Returns:
  • (ChunkyPNG::Canvas) - The flipped image
def flip_horizontally
  dup.flip_horizontally!
end

def flip_horizontally!

Other tags:
    See: #flip_horizontally -

Returns:
  • (ChunkyPNG::Canvas) - Itself, but flipped
def flip_horizontally!
  for y in 0..((height - 1) >> 1) do
    other_y   = height - (y + 1)
    other_row = row(other_y)
    replace_row!(other_y, row(y))
    replace_row!(y, other_row)
  end
  return self
end

def flip_vertically

Other tags:
    See: #flip_vertically! -

Returns:
  • (ChunkyPNG::Canvas) - The flipped image
def flip_vertically
  dup.flip_vertically!
end

def flip_vertically!

Other tags:
    See: #flip_vertically -

Returns:
  • (ChunkyPNG::Canvas) - Itself, but flipped
def flip_vertically!
  for y in 0...height do
    replace_row!(y, row(y).reverse)
  end
  return self
end

def replace(other, offset_x = 0, offset_y = 0)

Other tags:
    See: #compose -

Other tags:
    Note: - API changed since 1.0 - This method now no longer is in place, but returns

Raises:
  • (ChunkyPNG::OutOfBounds) - when the other canvas doesn't fit on this one,

Returns:
  • (ChunkyPNG::Canvas) - Returns a new, combined canvas.

Parameters:
  • () --
def replace(other, offset_x = 0, offset_y = 0)
  dup.replace!(other, offset_x, offset_y)
end

def replace!(other, offset_x = 0, offset_y = 0)

Other tags:
    See: #replace -
    See: #compose! -

Raises:
  • (ChunkyPNG::OutOfBounds) - when the other canvas doesn't fit on this one,

Returns:
  • (ChunkyPNG::Canvas) - Returns itself, but with the other canvas placed onto it.

Parameters:
  • offset_y (Integer) -- The y-offset to apply the new forgeround on.
  • offset_x (Integer) -- The x-offset to apply the new forgeround on.
  • other (ChunkyPNG::Canvas) -- The foreground canvas to get the pixels from.
def replace!(other, offset_x = 0, offset_y = 0)
  check_size_constraints!(other, offset_x, offset_y)
  for y in 0...other.height do
    pixels[(y + offset_y) * width + offset_x, other.width] = other.pixels[y * other.width, other.width]
  end
  self
end

def rotate_180

Other tags:
    See: #rotate_180! -

Returns:
  • (ChunkyPNG::Canvas) - The rotated image.
def rotate_180
  dup.rotate_180!
end

def rotate_180!

Other tags:
    See: #rotate_180 -

Returns:
  • (ChunkyPNG::Canvas) - Itself, but rotated 180 degrees.
def rotate_180!
  pixels.reverse!
  return self
end

def rotate_left

Returns:
  • (ChunkyPNG::Canvas) - A rotated copy of itself.
def rotate_left
  dup.rotate_left!
end

def rotate_left!

Returns:
  • (ChunkyPNG::Canvas) - Itself, but rotated.
def rotate_left!
  new_pixels = []
  (width - 1).downto(0) { |i| new_pixels += column(i) }
  replace_canvas!(height, width, new_pixels)
end

def rotate_right

Returns:
  • (ChunkyPNG::Canvas) - A clockwise-rotated copy.
def rotate_right
  dup.rotate_right!
end

def rotate_right!

Returns:
  • (ChunkyPNG::Canvas) - Itself, but rotated clockwise.
def rotate_right!
  rotated = self.class.new(height, width)
  new_pixels = []
  0.upto(width - 1) { |i| new_pixels += column(i).reverse }
  replace_canvas!(height, width, new_pixels)
end