Anonymous user
Raster graphics operations/Ruby: Difference between revisions
m
no edit summary
m (→The Code: improve iterat) |
mNo edit summary |
||
Line 133:
ios.puts format, "#{@width} #{@height}", "255"
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format)
case format▼
▲ when "P6" then ios.print @data[x][y].values.pack('C3')
end▼
end
end
Line 164 ⟶ 162:
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
Line 183 ⟶ 192:
bitmap = self.new(width, height)
red, green,
end▼
bitmap[x,y] = RGBColour.new(red, green, blue)▼
end
end
ios.close
Line 219 ⟶ 226:
def to_grayscale
gray = self.class.new(@width, @height)
▲ gray[x,y] = self[x,y].to_grayscale
end▼
end
gray
def to_blackandwhite▼
hist = histogram
# find the median luminosity▼
median = nil▼
sum = 0▼
hist.keys.sort.each do |lum|▼
sum += hist[lum]▼
if sum > @height * @width / 2▼
break▼
# create the black and white image▼
bw = self.class.new(@width, @height)▼
bw▼
def save_as_blackandwhite(filename)
to_blackandwhite.save(filename)
end
Line 427 ⟶ 458:
###############################################
def
bigger = self.class.new(@width * factor, @height * factor)
histogram = Hash.new(0)▼
(x*factor .. x*factor + factor-1).each do |xx|
histogram[self[x,y].luminosity] += 1▼
(y*factor .. y*factor + factor-1).each do |yy|
bigger[xx,yy] = colour
▲ end
end
end
end
###############################################
▲ def to_blackandwhite
▲ histogram = Hash.new(0)
▲ # find the median luminosity
▲ median = nil
▲ sum = 0
▲ hist.keys.sort.each do |lum|
▲ sum += hist[lum]
▲ if sum > @height * @width / 2
▲ break
▲ end
end
histogram
▲ # create the black and white image
▲ bw = self.class.new(@width, @height)
▲ @height.times do |y|
▲ @width.times do |x|
▲ bw[x,y] = self[x,y].luminosity < median ? RGBColour::BLACK : RGBColour::WHITE
▲ end
▲ end
▲ bw
▲ end
▲ def save_as_blackandwhite(filename)
▲ to_blackandwhite.save(filename)
end
Line 608 ⟶ 623:
(2 .. self).reduce(1, :*)
end
end</lang>
==A Test Suite==
|