Module: ChunkyPNG::RMagick

Extended by:
RMagick
Included in:
RMagick
Defined in:
lib/chunky_png/rmagick.rb

Overview

Methods for importing and exporting RMagick image objects.

By default, this module is disabled because of the dependency on RMagick. You need to include this module yourself if you want to use it.

Examples:


require 'rmagick'
require 'chunky_png/rmagick'

canvas = ChunkyPNG::Canvas.from_file('filename.png')
image = ChunkyPNG::RMagick.export(canvas)

# do something with the image using RMagick

updated_canvas = ChunkyPNG::RMagick.import(image)

Instance Method Summary collapse

Instance Method Details

#export(canvas) ⇒ Magick::Image

Exports a Canvas as RMagick image instance.

Parameters:

  • The canvas to export.

Returns:

  • The RMagick image constructed from the Canvas instance.



37
38
39
40
41
# File 'lib/chunky_png/rmagick.rb', line 37

def export(canvas)
  image = Magick::Image.new(canvas.width, canvas.height)
  image.import_pixels(0, 0, canvas.width, canvas.height, "RGBA", canvas.pixels.pack("N*"))
  image
end

#import(image) ⇒ ChunkyPNG::Canvas

Imports an RMagick image as Canvas object.

Parameters:

  • The image to import

Returns:

  • The canvas, constructed from the RMagick image.



29
30
31
32
# File 'lib/chunky_png/rmagick.rb', line 29

def import(image)
  pixels = image.export_pixels_to_str(0, 0, image.columns, image.rows, "RGBA")
  ChunkyPNG::Canvas.from_rgba_stream(image.columns, image.rows, pixels)
end