Class: ImageRuby::Image

Inherits:
Object show all
Defined in:
lib/imageruby/image.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width_, height_, color = nil) ⇒ Image

Create an image with the given width and height filled with the specified color (black if not specified) Also, the method allow the construction of image pixel by pixel if a block is passed

Examples:

image = Image.new(30,20) # creates an image of width 30, height 20 and filled with black
image = Image.new(30,20,Color.orange) # creates an image of width 30, height 20 and filled with orange
image = Image.new(30,20) {|x,y| Color.from_rgb(x*8,0,0) } # creates an image of width 30, height 20 and filled with a gradient


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/imageruby/image.rb', line 43

def initialize(width_, height_, color = nil)
  super()
  initialize_bitmap_representation(width_, height_, color)

  if block_given?
    (0..width_-1).each do |x_|
      (0..height_-1).each do |y_|
        set_pixel(x_,y_, yield(x_,y_) )
      end
    end
  end
end

Class Method Details

.from_file(path) ⇒ Object

load a image from file



65
66
67
# File 'lib/imageruby/image.rb', line 65

def self.from_file(path)
  FileLoader.load(path)
end

Instance Method Details

#encode(format, output) ⇒ Object

encodes the image with the specific format writting the result to output output can be a string or a open file on binary mode

To save a image to a file on disk use the method Image#save



60
61
62
# File 'lib/imageruby/image.rb', line 60

def encode(format,output)
  Encoder.encode(self,format,output)
end