Sha256: 3f1b40b559a6501a003d6e67c568e910dfecae6bdbe490e44bed19fe892eb0f7

Contents?: true

Size: 1.37 KB

Versions: 48

Compression:

Stored size: 1.37 KB

Contents

require 'rmagick'

module ChunkyPNG
  
  # 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. 
  #
  # @example
  #
  #    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)
  #
  module RMagick
    
    extend self
    
    # Imports an RMagick image as Canvas object.
    # @param [Magick::Image] image The image to import
    # @return [ChunkyPNG::Canvas] The canvas, constructed from the RMagick image.
    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

    # Exports a Canvas as RMagick image instance.
    # @param [ChunkyPNG::Canvas] canvas The canvas to export.
    # @return [Magick::Image] The RMagick image constructed from the Canvas instance.
    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
  end
end

Version data entries

48 entries across 47 versions & 4 rubygems

Version Path
chunky_png-1.3.11 lib/chunky_png/rmagick.rb
chunky_png-1.3.10 lib/chunky_png/rmagick.rb
chunky_png-1.3.9 lib/chunky_png/rmagick.rb
arcabouco-0.2.13 vendor/bundle/gems/chunky_png-1.3.8/lib/chunky_png/rmagick.rb
arcabouco-0.2.13 vendor/bundle/gems/chunky_png-1.3.6/lib/chunky_png/rmagick.rb
chunky_png-1.3.8 lib/chunky_png/rmagick.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/chunky_png-1.3.7/lib/chunky_png/rmagick.rb
chunky_png-1.3.7 lib/chunky_png/rmagick.rb
chunky_png-1.3.6 lib/chunky_png/rmagick.rb
chunky_png-1.3.5 lib/chunky_png/rmagick.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/chunky_png-1.2.5/lib/chunky_png/rmagick.rb
chunky_png-1.2.5 lib/chunky_png/rmagick.rb
chunky_png-1.2.4 lib/chunky_png/rmagick.rb
chunky_png-1.2.3 lib/chunky_png/rmagick.rb
chunky_png-1.2.2 lib/chunky_png/rmagick.rb
chunky_png-1.2.1 lib/chunky_png/rmagick.rb
chunky_png-1.2.0 lib/chunky_png/rmagick.rb
chunky_png-1.1.2 lib/chunky_png/rmagick.rb
chunky_png-1.1.1 lib/chunky_png/rmagick.rb
chunky_png-1.1.0 lib/chunky_png/rmagick.rb