Class: Mir::Image

Inherits:
Grid
  • Object
show all
Includes:
InfoDisplay
Defined in:
lib/mir/01_model.rb

Instance Attribute Summary collapse

Attributes inherited from Grid

#size

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InfoDisplay

#hit_a_key, #info

Methods inherited from Grid

#[], #[]=, #each, #fill_with, from_a, #initialize, #print, #to_a

Constructor Details

This class inherits a constructor from Mir::Grid

Instance Attribute Details

#nameObject

Returns the value of attribute name.



112
113
114
# File 'lib/mir/01_model.rb', line 112

def name
  @name
end

Class Method Details

.open(filename) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mir/01_model.rb', line 113

def self.open(filename)
  unless File.exist?(filename)
    raise "file '#{filename}' not found"
  end
  image=nil
  File.open(filename, 'r') do |f|
    header=[]
    begin
      unless (line=f.gets.chomp).start_with?('#')
        header << line
      end
    end until header.size==3
    width, height = header[1].split.map {|n| n.to_i }
    if header[0] != 'P6' or header[2] != '255' or width < 1 or height < 1
      raise StandardError, "file '#{filename}' does not start with the expected header"
    end
    f.binmode
    image = self.new(width, height)

    height.times do |y|
       width.times do |x|
         # read 3 bytes
         red, green, blue = f.read(3).unpack('C3')
         image[y,x] = Rgb.new(red, green, blue)
       end
    end
  end
  image.name=File.basename(filename)
  image
end

.random(sx, sy) ⇒ Object



148
149
150
151
# File 'lib/mir/01_model.rb', line 148

def Image.random sx,sy
  ary=Grid.random(sx,sy,Rgb).to_a
  Image.from_a ary,sx,sy
end

Instance Method Details

#each_pixel(&block) ⇒ Object



144
145
146
# File 'lib/mir/01_model.rb', line 144

def each_pixel &block
  each &block
end