Raster graphics operations/Ruby: Difference between revisions
Content added Content deleted
m (→The Code: improve iterat) |
mNo edit summary |
||
Line 133: | Line 133: | ||
ios.puts format, "#{@width} #{@height}", "255" |
ios.puts format, "#{@width} #{@height}", "255" |
||
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format) |
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format) |
||
each_pixel do |x, y| |
|||
case format |
|||
⚫ | |||
⚫ | |||
when "P6" then ios.print @data[x][y].values.pack('C3') |
|||
⚫ | |||
⚫ | |||
end |
end |
||
end |
end |
||
Line 164: | Line 162: | ||
pipe.close rescue false |
pipe.close rescue false |
||
end |
end |
||
⚫ | |||
⚫ | |||
require 'chunky_png' |
|||
#require 'stringio' |
|||
stream = StringIO.new("", "r+") |
|||
each_pixel {|x, y| stream << self[x, y].values.pack("ccc")} |
|||
stream.seek(0) |
|||
ChunkyPNG::Canvas.extend(ChunkyPNG::Canvas::StreamImporting) |
|||
canvas = ChunkyPNG::Canvas.from_rgb_stream(width, height, stream) |
|||
⚫ | |||
end |
end |
||
Line 183: | Line 192: | ||
bitmap = self.new(width, height) |
bitmap = self.new(width, height) |
||
bitmap.each_pixel do |x,y| |
|||
# read 3 bytes |
|||
red, green, blue = case format |
|||
when 'P3' then ios.gets.chomp.split |
|||
when 'P6' then ios.read(3).unpack('C3') |
|||
when 'P6' then ios.read(3).unpack('C3') |
|||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
end |
end |
||
ios.close |
ios.close |
||
Line 219: | Line 226: | ||
def to_grayscale |
def to_grayscale |
||
gray = self.class.new(@width, @height) |
gray = self.class.new(@width, @height) |
||
each_pixel do |x,y| |
|||
⚫ | |||
@height.times do |y| |
|||
⚫ | |||
⚫ | |||
end |
end |
||
gray |
gray |
||
⚫ | |||
⚫ | |||
hist = histogram |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
def save_as_blackandwhite(filename) |
|||
to_blackandwhite.save(filename) |
|||
end |
end |
||
Line 427: | Line 458: | ||
############################################### |
############################################### |
||
def |
def magnify(factor) |
||
bigger = self.class.new(@width * factor, @height * factor) |
|||
⚫ | |||
each_pixel do |x,y| |
|||
colour = self[x,y] |
|||
(x*factor .. x*factor + factor-1).each do |xx| |
|||
⚫ | |||
(y*factor .. y*factor + factor-1).each do |yy| |
|||
bigger[xx,yy] = colour |
|||
⚫ | |||
end |
end |
||
end |
end |
||
bigger |
|||
end |
end |
||
############################################### |
|||
⚫ | |||
def histogram |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
median = lum |
|||
⚫ | |||
⚫ | |||
end |
end |
||
histogram |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 608: | Line 623: | ||
(2 .. self).reduce(1, :*) |
(2 .. self).reduce(1, :*) |
||
end |
end |
||
end |
end</lang> |
||
</lang> |
|||
==A Test Suite== |
==A Test Suite== |