Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Bitmap-Read-a-PPM-file/Raku/bitmap-read-a-ppm-file.raku
Normal file
36
Task/Bitmap-Read-a-PPM-file/Raku/bitmap-read-a-ppm-file.raku
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue