Sha256: 3c1abb22cd38afe72d9873ded2f5ace936ce12074b61275019af174b061293fe

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

require 'RMagick'

module Mork
  # The class Mimage is a wrapper for the core image library, currently RMagick
  class Mimage
    def initialize(img, page=0)
      if img.class == String
        if File.extname(img) == '.pdf'
          @image = Magick::Image.read(img) { self.density = 200 }[page]
        else
          @image = Magick::ImageList.new(img)[page]
        end
      elsif img.class == Magick::ImageList
        @image = img[page]
      elsif img.class == Magick::Image
        @image = img
      else
        raise "Invalid initialization argument"
      end
    end
    
    # =============
    # = Highlight =
    # =============
    def highlight!(c)
      m = Magick::Image.new(c[:w], c[:h]) { self.background_color = "red" }
      @image.composite! m, c[:x], c[:y], Magick::CopyCompositeOp
    end
    
    # ============
    # = Cropping =
    # ============
    def crop(c)
      Mimage.new @image.crop(c[:x], c[:y], c[:w], c[:h])
    end
    
    def crop!(c)
      @image.crop!(c[:x], c[:y], c[:w], c[:h])
      self
    end
    
    # ============
    # = Blurring =
    # ============
    def blur(a, b)
      Mimage.new @image.blur_image(a, b)
    end

    def blur!(a, b)
      @image = @image.blur_image(a, b)
      self
    end
    
    # ==============
    # = Stretching =
    # ==============
    def stretch(points)
      Mimage.new @image.distort(Magick::PerspectiveDistortion, points)
    end
    
    def stretch!(points)
      @image = @image.distort(Magick::PerspectiveDistortion, points)
      self
    end
    
    # returns the raw pixels from the entire image or from the area
    # defined in opts
    def pixels(opts = {})
      c = {x: 0, y: 0, w: width, h: height}.merge(opts)
      @image.export_pixels(c[:x], c[:y], c[:w], c[:h], "I")
    end
    
    def width
      @image.columns
    end
    
    def height
      @image.rows
    end
    
    # write the underlying Magick::Image to disk
    def write(fname)
      @image.write fname
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mork-0.0.3 lib/mork/mimage.rb
mork-0.0.2 lib/mork/mimage.rb