Bitmap/Read an image through a pipe: Difference between revisions

(→‎{{header|Lua}}: added Lua solution)
Line 422:
 
=={{header|Ruby}}==
Uses [[Raster graphics operations/Ruby]].
Extending [[Read ppm file#Ruby]] and [[PPM conversion through a pipe#Ruby]]. Uses the ImageMagick <code>convert</code> tool.
 
<lang ruby>class# Pixmapfrozen_string_literal: true
 
require_relative 'raster_graphics'
 
class Pixmap
def self.read_ppm(ios)
format = ios.gets.chomp
width, height = ios.gets.chomp.split.map {|n| n.(&:to_i })
max_colour = ios.gets.chomp
 
if (not !PIXMAP_FORMATS.include?(format)) or ||
(width < 1) or|| (height < 1) or||
(max_colour != '255')
then
ios.close
raise StandardError, "file '#{filename}' does not start with the expected header"
Line 439 ⟶ 442:
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format)
 
bitmap = self.new(width, height)
height.times do |y|
width.times do |x|
# read 3 bytes
red, green, blue = case format
when 'P3' then ios.gets.chomp.split
when 'P6' then ios.read(3).unpack('C3')
end
bitmap[x, y] = RGBColour.new(red, green, blue)
end
end
Line 463 ⟶ 466:
end
 
bitmap = Pixmap.open_from_jpeg('filefoto.jpg')</lang>
bitmap.save('foto.ppm')
</lang>
 
=={{header|Tcl}}==
Anonymous user