Bitmap/Read a PPM file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 88:
Close (F2);
end;</lang>
 
=={{header|AutoHotkey}}==
{{works with | AutoHotkey_L}}
Line 439 ⟶ 440:
.writePPM(<import:java.io.makeFileOutputStream>(outputFile))
}</lang>
 
 
=={{header|Erlang}}==
Line 721:
* doing formatted I/O with Fortran is a pain... And unformatted does not mean ''free''; Fortran2003 has ''streams'', but they are not implemented (yet) in GNU Fortran compiler. Here (as in the write part) I've tried to handle the PPM format through formatted I/O. The tests worked but I have not tried still everything.
* comments after the first line are not handled
 
=={{header|Go}}==
<lang go>package raster
Line 1,002 ⟶ 1,003:
}
}</lang>
 
=={{header|Lua}}==
<lang lua>function Read_PPM( filename )
local fp = io.open( filename, "rb" )
if fp == nil then return nil end
 
local data = fp:read( "*line" )
if data ~= "P6" then return nil end
repeat
data = fp:read( "*line" )
until string.find( data, "#" ) == nil
 
local image = {}
local size_x, size_y
size_x = string.match( data, "%d+" )
size_y = string.match( data, "%s%d+" )
 
data = fp:read( "*line" )
if tonumber(data) ~= 255 then return nil end
 
for i = 1, size_x do
image[i] = {}
end
for j = 1, size_y do
for i = 1, size_x do
image[i][j] = { string.byte( fp:read(1) ), string.byte( fp:read(1) ), string.byte( fp:read(1) ) }
end
end
fp:close()
return image
end</lang>
 
=={{header|M2000 Interpreter}}==
Now function Bitmap has double signature. With two numbers make a bitmap,with all pixels white. With one number, expect that it is a file number and read file, and then return the bitmap.
Line 1,181 ⟶ 1,219:
<lang Mathematica>Import["file.ppm","PPM"]
</lang>
 
=={{header|Lua}}==
<lang lua>function Read_PPM( filename )
local fp = io.open( filename, "rb" )
if fp == nil then return nil end
 
local data = fp:read( "*line" )
if data ~= "P6" then return nil end
repeat
data = fp:read( "*line" )
until string.find( data, "#" ) == nil
 
local image = {}
local size_x, size_y
size_x = string.match( data, "%d+" )
size_y = string.match( data, "%s%d+" )
 
data = fp:read( "*line" )
if tonumber(data) ~= 255 then return nil end
 
for i = 1, size_x do
image[i] = {}
end
for j = 1, size_y do
for i = 1, size_x do
image[i][j] = { string.byte( fp:read(1) ), string.byte( fp:read(1) ), string.byte( fp:read(1) ) }
end
end
fp:close()
return image
end</lang>
 
=={{header|Nim}}==
Line 1,417 ⟶ 1,419:
 
exit 0;</lang>
 
 
=={{header|Perl 6}}==
{{works with|Rakudo|2017.09}}
Uses pieces from [[Bitmap#Perl_6| Bitmap]], [[Bitmap/Write_a_PPM_file#Perl_6| Write a PPM file]] and [[Grayscale_image#Perl_6| Grayscale image]] tasks. Included here to make a complete, runnable program.
 
<lang perl6>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @.data;
}
 
role PGM {
has @.GS;
method P5 returns Blob {
"P5\n{self.width} {self.height}\n255\n".encode('ascii')
~ Blob.new: self.GS
}
}
 
sub load-ppm ( $ppm ) {
my $fh = $ppm.IO.open( :enc('ISO-8859-1') );
my $type = $fh.get;
my ($width, $height) = $fh.get.split: ' ';
my $depth = $fh.get;
Bitmap.new( width => $width.Int, height => $height.Int,
data => ( $fh.slurp.ords.rotor(3).map:
{ Pixel.new(R => $_[0], G => $_[1], B => $_[2]) } )
)
}
 
sub grayscale ( Bitmap $bmp ) {
$bmp.GS = map { (.R*0.2126 + .G*0.7152 + .B*0.0722).round(1) min 255 }, $bmp.data;
}
 
my $filename = './camelia.ppm';
 
my Bitmap $b = load-ppm( $filename ) but PGM;
 
grayscale($b);
 
'./camelia-gs.pgm'.IO.open(:bin, :w).write: $b.P5;</lang>
 
See [https://github.com/thundergnat/rc/blob/master/img/camelia.png camelia], and [https://github.com/thundergnat/rc/blob/master/img/camelia-gs.png camelia-gs] images. (converted to .png as .ppm format is not widely supported).
 
=={{header|Phix}}==
Line 1,752 ⟶ 1,710:
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.09}}
Uses pieces from [[Bitmap#Perl_6| Bitmap]], [[Bitmap/Write_a_PPM_file#Perl_6| Write a PPM file]] and [[Grayscale_image#Perl_6| Grayscale image]] tasks. Included here to make a complete, runnable program.
 
<lang perl6>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @.data;
}
 
role PGM {
has @.GS;
method P5 returns Blob {
"P5\n{self.width} {self.height}\n255\n".encode('ascii')
~ Blob.new: self.GS
}
}
 
sub load-ppm ( $ppm ) {
my $fh = $ppm.IO.open( :enc('ISO-8859-1') );
my $type = $fh.get;
my ($width, $height) = $fh.get.split: ' ';
my $depth = $fh.get;
Bitmap.new( width => $width.Int, height => $height.Int,
data => ( $fh.slurp.ords.rotor(3).map:
{ Pixel.new(R => $_[0], G => $_[1], B => $_[2]) } )
)
}
 
sub grayscale ( Bitmap $bmp ) {
$bmp.GS = map { (.R*0.2126 + .G*0.7152 + .B*0.0722).round(1) min 255 }, $bmp.data;
}
 
my $filename = './camelia.ppm';
 
my Bitmap $b = load-ppm( $filename ) but PGM;
 
grayscale($b);
 
'./camelia-gs.pgm'.IO.open(:bin, :w).write: $b.P5;</lang>
 
See [https://github.com/thundergnat/rc/blob/master/img/camelia.png camelia], and [https://github.com/thundergnat/rc/blob/master/img/camelia-gs.png camelia-gs] images. (converted to .png as .ppm format is not widely supported).
 
=={{header|REXX}}==
10,333

edits