Class: Mir::Image
- Includes:
- InfoDisplay
- Defined in:
- lib/mir/01_model.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Attributes inherited from Grid
Class Method Summary collapse
Instance Method Summary collapse
Methods included from InfoDisplay
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
#name ⇒ Object
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 |
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 |