Module: ChunkyPNG::Canvas::Operations

Included in:
ChunkyPNG::Canvas
Defined in:
lib/chunky_png/canvas/operations.rb

Overview

The ChunkyPNG::Canvas::Operations module defines methods to perform operations on a ChunkyPNG::Canvas. The module is included into the Canvas class so all these methods are available on every canvas.

Note that some of these operations modify the canvas, while some operations return a new canvas and leave the original intact.

See Also:

Instance Method Summary collapse

Instance Method Details

#compose(other, offset_x = 0, offset_y = 0) ⇒ ChunkyPNG::Canvas

Note:

API changed since 1.0 - This method now no longer is in place, but returns a new canvas and leaves the original intact. Use #compose! if you want to compose on the canvas in place.

Composes another image onto this image using alpha blending. This will return a new canvas and leave the original intact.

If you simply want to replace pixels or when the other image does not have transparency, it is faster to use #replace.

Parameters:

  • other (ChunkyPNG::Canvas)

    The foreground canvas to compose on the current canvas, using alpha compositing.

  • offset_x (Integer) (defaults to: 0)

    The x-offset to apply the new foreground on.

  • offset_y (Integer) (defaults to: 0)

    The y-offset to apply the new foreground on.

Returns:

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the other canvas doesn’t fit on this one, given the offset and size of the other canvas.

See Also:



80
81
82
# File 'lib/chunky_png/canvas/operations.rb', line 80

def compose(other, offset_x = 0, offset_y = 0)
  dup.compose!(other, offset_x, offset_y)
end

#compose!(other, offset_x = 0, offset_y = 0) ⇒ ChunkyPNG::Canvas

Composes another image onto this image using alpha blending. This will modify the current canvas.

If you simply want to replace pixels or when the other image does not have transparency, it is faster to use #replace!.

Parameters:

  • other (ChunkyPNG::Canvas)

    The foreground canvas to compose on the current canvas, using alpha compositing.

  • offset_x (Integer) (defaults to: 0)

    The x-offset to apply the new foreground on.

  • offset_y (Integer) (defaults to: 0)

    The y-offset to apply the new foreground on.

Returns:

  • (ChunkyPNG::Canvas)

    Returns itself, but with the other canvas composed onto it.

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the other canvas doesn’t fit on this one, given the offset and size of the other canvas.

See Also:



54
55
56
57
58
59
60
61
62
63
# File 'lib/chunky_png/canvas/operations.rb', line 54

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

#crop(x, y, crop_width, crop_height) ⇒ ChunkyPNG::Canvas

Crops an image, given the coordinates and size of the image that needs to be cut out. This will leave the original image intact and return a new, cropped image with pixels copied from the original image.

Parameters:

  • x (Integer)

    The x-coordinate of the top left corner of the image to be cropped.

  • y (Integer)

    The y-coordinate of the top left corner of the image to be cropped.

  • crop_width (Integer)

    The width of the image to be cropped.

  • crop_height (Integer)

    The height of the image to be cropped.

Returns:

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the crop dimensions plus the given coordinates are bigger then the original image.



137
138
139
# File 'lib/chunky_png/canvas/operations.rb', line 137

def crop(x, y, crop_width, crop_height)
  dup.crop!(x, y, crop_width, crop_height)
end

#crop!(x, y, crop_width, crop_height) ⇒ ChunkyPNG::Canvas

Crops an image, given the coordinates and size of the image that needs to be cut out.

This will change the size and content of the current canvas. Use #crop if you want to have a new canvas returned instead, leaving the current canvas intact.

Parameters:

  • x (Integer)

    The x-coordinate of the top left corner of the image to be cropped.

  • y (Integer)

    The y-coordinate of the top left corner of the image to be cropped.

  • crop_width (Integer)

    The width of the image to be cropped.

  • crop_height (Integer)

    The height of the image to be cropped.

Returns:

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the crop dimensions plus the given coordinates are bigger then the original image.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chunky_png/canvas/operations.rb', line 153

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

#flip_horizontallyChunkyPNG::Canvas Also known as: flip

Flips the image horizontally, leaving the original intact.

This will flip the image on its horizontal axis, e.g. pixels on the top will now be pixels on the bottom. Chaining this method twice will return the original canvas. This method will leave the original object intact and return a new canvas.

Returns:

See Also:



173
174
175
# File 'lib/chunky_png/canvas/operations.rb', line 173

def flip_horizontally
  dup.flip_horizontally!
end

#flip_horizontally!ChunkyPNG::Canvas Also known as: flip!

Flips the image horizontally in place.

This will flip the image on its horizontal axis, e.g. pixels on the top will now be pixels on the bottom. Chaining this method twice will return the original canvas. This method will leave the original object intact and return a new canvas.

Returns:

See Also:



185
186
187
188
189
190
191
192
193
# File 'lib/chunky_png/canvas/operations.rb', line 185

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

#flip_verticallyChunkyPNG::Canvas Also known as: mirror

Flips the image vertically, leaving the original intact.

This will flip the image on its vertical axis, e.g. pixels on the left will now be pixels on the right. Chaining this method twice will return the original canvas. This method will leave the original object intact and return a new canvas.

Returns:

See Also:



206
207
208
# File 'lib/chunky_png/canvas/operations.rb', line 206

def flip_vertically
  dup.flip_vertically!
end

#flip_vertically!ChunkyPNG::Canvas Also known as: mirror!

Flips the image vertically in place.

This will flip the image on its vertical axis, e.g. pixels on the left will now be pixels on the right. Chaining this method twice will return the original canvas. This method will leave the original object intact and return a new canvas.

Returns:

See Also:



218
219
220
221
222
223
# File 'lib/chunky_png/canvas/operations.rb', line 218

def flip_vertically!
  for y in 0...height do
    replace_row!(y, row(y).reverse)
  end
  return self
end

#grayscaleChunkyPNG::Canvas

Converts the canvas to grascale, returning a new canvas.

This method will not modify the canvas. To modift the current canvas, use #grayscale! instead.

Returns:

See Also:

  • ChunkyPNG::Canvas::Operations.{{#grayscale!}
  • ChunkyPNG::Canvas::Operations.{ChunkyPNG{ChunkyPNG::Color{ChunkyPNG::Color#to_grayscale}


35
36
37
# File 'lib/chunky_png/canvas/operations.rb', line 35

def grayscale
  dup.grayscale!
end

#grayscale!ChunkyPNG::Canvas

Converts the canvas to grascale.

This method will modify the canvas. The obtain a new canvas and leave the current instance intact, use #grayscale instead.

Returns:

See Also:

  • ChunkyPNG::Canvas::Operations.{{#grayscale}
  • ChunkyPNG::Canvas::Operations.{ChunkyPNG{ChunkyPNG::Color{ChunkyPNG::Color#to_grayscale}


22
23
24
25
# File 'lib/chunky_png/canvas/operations.rb', line 22

def grayscale!
  pixels.map! { |pixel| ChunkyPNG::Color.to_grayscale(pixel) }
  return self
end

#replace(other, offset_x = 0, offset_y = 0) ⇒ ChunkyPNG::Canvas

Note:

API changed since 1.0 - This method now no longer is in place, but returns a new canvas and leaves the original intact. Use #replace! if you want to replace pixels on the canvas in place.

Replaces pixels on this image by pixels from another pixels, on a given offset. This method will modify the current canvas.

This will completely replace the pixels of the background image. If you want to blend them with semi-transparent pixels from the foreground image, see #compose!.

Parameters:

  • other (ChunkyPNG::Canvas)

    The foreground canvas to get the pixels from.

  • offset_x (Integer) (defaults to: 0)

    The x-offset to apply the new foreground on.

  • offset_y (Integer) (defaults to: 0)

    The y-offset to apply the new foreground on.

Returns:

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the other canvas doesn’t fit on this one, given the offset and size of the other canvas.

See Also:



122
123
124
# File 'lib/chunky_png/canvas/operations.rb', line 122

def replace(other, offset_x = 0, offset_y = 0)
  dup.replace!(other, offset_x, offset_y)
end

#replace!(other, offset_x = 0, offset_y = 0) ⇒ ChunkyPNG::Canvas

Replaces pixels on this image by pixels from another pixels, on a given offset. This method will modify the current canvas.

This will completely replace the pixels of the background image. If you want to blend them with semi-transparent pixels from the foreground image, see #compose!.

Parameters:

  • other (ChunkyPNG::Canvas)

    The foreground canvas to get the pixels from.

  • offset_x (Integer) (defaults to: 0)

    The x-offset to apply the new foreground on.

  • offset_y (Integer) (defaults to: 0)

    The y-offset to apply the new foreground on.

Returns:

Raises:

  • (ChunkyPNG::OutOfBounds)

    when the other canvas doesn’t fit on this one, given the offset and size of the other canvas.

See Also:



98
99
100
101
102
103
104
105
# File 'lib/chunky_png/canvas/operations.rb', line 98

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

#rotate_180ChunkyPNG::Canvas

Rotates the image 180 degrees. This method will leave the original object intact and return a new canvas.

Returns:

See Also:



285
286
287
# File 'lib/chunky_png/canvas/operations.rb', line 285

def rotate_180
  dup.rotate_180!
end

#rotate_180!ChunkyPNG::Canvas

Rotates the image 180 degrees in place.

Returns:

See Also:



293
294
295
296
# File 'lib/chunky_png/canvas/operations.rb', line 293

def rotate_180!
  pixels.reverse!
  return self
end

#rotate_leftChunkyPNG::Canvas Also known as: rotate_counter_clockwise

Returns an image that is rotated 90 degrees counter-clockwise.

This method will leave the original object intact and return a new canvas. See #rotate_left! for the in place version.

Returns:



260
261
262
# File 'lib/chunky_png/canvas/operations.rb', line 260

def rotate_left
  dup.rotate_left!
end

#rotate_left!ChunkyPNG::Canvas Also known as: rotate_counter_clockwise!

Rotates the image 90 degrees counter-clockwise in place.

This method will change the original canvas. See #rotate_left for a version that leaves the canvas intact and returns a new rotated canvas instead.

Returns:



271
272
273
274
275
# File 'lib/chunky_png/canvas/operations.rb', line 271

def rotate_left!
  new_pixels = []
  (width - 1).downto(0) { |i| new_pixels += column(i) }
  replace_canvas!(height, width, new_pixels)
end

#rotate_rightChunkyPNG::Canvas Also known as: rotate_clockwise

Returns a new canvas instance that is rotated 90 degrees clockwise.

This method will return a new canvas and leaves the original intact. See #rotate_right! for the in place version.

Returns:



234
235
236
# File 'lib/chunky_png/canvas/operations.rb', line 234

def rotate_right
  dup.rotate_right!
end

#rotate_right!ChunkyPNG::Canvas Also known as: rotate_clockwise!

Rotates the image 90 degrees clockwise in place.

This method will change the current canvas. See #rotate_right for a version that leaves th current canvas intact

Returns:



244
245
246
247
248
249
# File 'lib/chunky_png/canvas/operations.rb', line 244

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