Jump to content

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)
@height.timeseach_pixel do |x, y|
@width.timescase do |x|format
when "P6P3" then ios.print @data[x][y].values.packjoin('C3'" "),"\n"
case format
when "P3P6" then ios.print @data[x][y].values.joinpack(" "'C3'),"\n"
when "P6" then ios.print @data[x][y].values.pack('C3')
end
end
end
Line 164 ⟶ 162:
pipe.close rescue false
end
end
 
def save_as_blackandwhitesave_as_png(filename)
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)
to_blackandwhitecanvas.to_image.save(filename)
end
 
Line 183 ⟶ 192:
 
bitmap = self.new(width, height)
heightbitmap.timeseach_pixel do |x,y|
width.times# doread |x|3 bytes
red, green, #blue read= 3case bytesformat
red,when green,'P3' bluethen = case formatios.gets.chomp.split
when 'P3P6' then ios.getsread(3).chomp.splitunpack('C3')
when 'P6' then ios.read(3).unpack('C3')
end
bitmap[x,y] = RGBColour.new(red, green, blue)
end
bitmap[x,y] = RGBColour.new(red, green, blue)
end
ios.close
Line 219 ⟶ 226:
def to_grayscale
gray = self.class.new(@width, @height)
@width.timeseach_pixel do |x,y|
gray[x,y] = self[x,y].to_grayscale
@height.times do |y|
gray[x,y] = self[x,y].to_grayscale
end
end
gray
end
 
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
casemedian format= lum
break
end
end
 
# create the black and white image
bw = self.class.new(@width, @height)
@height.timeseach_pixel do |x,y|
bw[x,y] = self[x,y].luminosity < median ? RGBColour::BLACK : RGBColour::WHITE
end
bw
end
 
def save_as_blackandwhite(filename)
to_blackandwhite.save(filename)
end
 
Line 427 ⟶ 458:
 
###############################################
def histogrammagnify(factor)
bigger = self.class.new(@width * factor, @height * factor)
histogram = Hash.new(0)
@height.timeseach_pixel do |x,y|
@width.timescolour do= |self[x|,y]
(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
histogram bigger
end
 
###############################################
def to_blackandwhite
hist =def histogram
histogram = Hash.new(0)
 
@width.timeseach_pixel do |x,y|
# find the median luminosity
histogram[self[x,y].luminosity] += 1
median = nil
sum = 0
hist.keys.sort.each do |lum|
sum += hist[lum]
if sum > @height * @width / 2
median = lum
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>
</lang>
 
==A Test Suite==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.