module ChunkyPNG::Canvas::Operations

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

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

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
  • x (Integer) -- The x-coordinate of the top left corner of the image
def crop!(x, y, crop_width, crop_height)
  if crop_width + x > width
    raise ChunkyPNG::OutOfBounds, 'Original image width is too small!'
  end
  if crop_height + y > height
    raise ChunkyPNG::OutOfBounds, 'Original image height is too small!'
  end
  if crop_width == width && x == 0
    # We only need to crop off the top and/or bottom, so we can take a
    # shortcut.
    replace_canvas!(crop_width, crop_height,
                    pixels.slice(y * width, width * crop_height))
  else
    new_pixels = []
    for cy in 0...crop_height do
      new_pixels.concat pixels.slice((cy + y) * width + x, crop_width)
    end
    replace_canvas!(crop_width, crop_height, new_pixels)
  end
end