How to resolve the algorithm Grayscale image step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Grayscale image step by step in the Raku programming language
Table of Contents
Problem Statement
Many image processing algorithms are defined for grayscale (or else monochromatic) images.
Extend the data storage type defined on this page to support grayscale images. Define two operations, one to convert a color image to a grayscale image and one for the backward conversion. To get luminance of a color use the formula recommended by CIE: When using floating-point arithmetic make sure that rounding errors would not cause run-time problems or else distorted results when calculated luminance is stored as an unsigned integer.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Grayscale image step by step in the Raku programming language
Source code in the raku programming language
sub MAIN ($filename = 'default.ppm') {
my $in = open($filename, :r, :enc) or die $in;
my ($type, $dim, $depth) = $in.lines[^3];
my $outfile = $filename.subst('.ppm', '.pgm');
my $out = open($outfile, :w, :enc) or die $out;
$out.say("P5\n$dim\n$depth");
for $in.lines.ords -> $r, $g, $b {
my $gs = $r * 0.2126 + $g * 0.7152 + $b * 0.0722;
$out.print: chr($gs.floor min 255);
}
$in.close;
$out.close;
}
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the PL/I programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the jq programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Python programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Rhovas programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the D programming language