Bitmap/Read an image through a pipe: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
|||
Line 422: | Line 422: | ||
=={{header|Ruby}}== |
=={{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> |
<lang ruby># frozen_string_literal: true |
||
require_relative 'raster_graphics' |
|||
class Pixmap |
|||
def self.read_ppm(ios) |
def self.read_ppm(ios) |
||
format = ios.gets.chomp |
format = ios.gets.chomp |
||
width, height = ios.gets.chomp.split.map |
width, height = ios.gets.chomp.split.map(&:to_i) |
||
max_colour = ios.gets.chomp |
max_colour = ios.gets.chomp |
||
if |
if !PIXMAP_FORMATS.include?(format) || |
||
(width < 1) || (height < 1) || |
|||
(max_colour != '255') |
|||
then |
|||
ios.close |
ios.close |
||
raise StandardError, "file '#{filename}' does not start with the expected header" |
raise StandardError, "file '#{filename}' does not start with the expected header" |
||
Line 439: | Line 442: | ||
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format) |
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format) |
||
bitmap = |
bitmap = new(width, height) |
||
height.times do |y| |
height.times do |y| |
||
width.times do |x| |
width.times do |x| |
||
# read 3 bytes |
# read 3 bytes |
||
red, green, blue = case format |
red, green, blue = case format |
||
when 'P3' then ios.gets.chomp.split |
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 |
||
bitmap[x,y] = RGBColour.new(red, green, blue) |
bitmap[x, y] = RGBColour.new(red, green, blue) |
||
end |
end |
||
end |
end |
||
Line 463: | Line 466: | ||
end |
end |
||
bitmap = Pixmap.open_from_jpeg(' |
bitmap = Pixmap.open_from_jpeg('foto.jpg') |
||
bitmap.save('foto.ppm') |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |