Grayscale image: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Colour
Byte r, g, b
 
Line 82:
print(‘Grey:’)
bitmap.togreyscale()
print(bitmap.writeppmp3())</langsyntaxhighlight>
 
{{out}}
Line 111:
{{libheader|Action! Bitmap tools}}
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:RGB2GRAY.ACT" ;from task Grayscale image
 
PROC PrintB3(BYTE x)
Line 185:
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Grayscale_image.png Screenshot from Atari 8-bit computer]
Line 209:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">type Grayscale_Image is array (Positive range <>, Positive range <>) of Luminance;</langsyntaxhighlight>
Conversion to a grayscale image:
<langsyntaxhighlight lang="ada">function Grayscale (Picture : Image) return Grayscale_Image is
type Extended_Luminance is range 0..10_000_000;
Result : Grayscale_Image (Picture'Range (1), Picture'Range (2));
Line 230:
end loop;
return Result;
end Grayscale;</langsyntaxhighlight>
Conversion to a color image:
<langsyntaxhighlight lang="ada">function Color (Picture : Grayscale_Image) return Image is
Result : Image (Picture'Range (1), Picture'Range (2));
begin
Line 241:
end loop;
return Result;
end Color;</langsyntaxhighlight>
 
=={{header|BASIC256}}==
[[Image:BASIC256_greyscale_Mona_Lisa.jpg|right]]
[[Image:BASIC256_greysacle_Grey_Mona_lisa.jpg|right]]
<langsyntaxhighlight BASIC256lang="basic256">w = 143
h = 188
name$ = "Mona_Lisa.jpg"
Line 268:
next x
 
imgsave "Grey_"+name$,"jpg"</langsyntaxhighlight>
{{omit from|Batch File}}
 
Line 276:
[[Image:original_bbc.jpg|right]]
[[Image:greyscale_bbc.jpg|right]]
<langsyntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 304:
col% = TINT(x%*2,y%*2)
SWAP ?^col%,?(^col%+2)
= col%</langsyntaxhighlight>
 
=={{header|C}}==
Definition/interface for a grayscale image.
 
<langsyntaxhighlight lang="c">typedef unsigned char luminance;
typedef luminance pixel1[1];
typedef struct {
Line 320:
grayimage alloc_grayimg(unsigned int, unsigned int);
grayimage tograyscale(image);
image tocolor(grayimage);</langsyntaxhighlight>
 
The same as <tt>alloc_img</tt>, but for grayscale images.
 
<langsyntaxhighlight lang="c">grayimage alloc_grayimg(unsigned int width, unsigned int height)
{
grayimage img;
Line 332:
img->height = height;
return img;
}</langsyntaxhighlight>
 
Convert from ''color'' image to ''grayscale'' image.
 
<langsyntaxhighlight lang="c">grayimage tograyscale(image img)
{
unsigned int x, y;
Line 358:
}
return timg;
}</langsyntaxhighlight>
 
And back from a ''grayscale'' image to a ''color'' image.
 
<langsyntaxhighlight lang="c">image tocolor(grayimage img)
{
unsigned int x, y;
Line 383:
}
return timg;
}</langsyntaxhighlight>
 
'''Notes'''
* <tt>tocolor</tt> and <tt>tograyscale</tt> do not free the previous image, so it must be freed normally calling <tt>free_img</tt>. With a cast we can use the same function also for grayscale images, or we can define something like
 
<langsyntaxhighlight lang="c">#define free_grayimg(IMG) free_img((image)(IMG))</langsyntaxhighlight>
 
* ''Luminance'' is rounded. Since the C implementation is based on unsigned char (256 possible values per components), L can be at most 255.0 and rounding gives 255, as we expect. Changing the color_component type would only change 256, 255.0 and 255 values here written in something else, the code would work the same.
Line 394:
=={{header|C sharp|C#}}==
To convert TO grayscale:
<langsyntaxhighlight lang="csharp">
Bitmap tImage = new Bitmap("spectrum.bmp");
 
Line 411:
// Save
tImage.Save("spectrum2.bmp");
</syntaxhighlight>
</lang>
 
=={{header|Clojure|Clojure}}==
<langsyntaxhighlight lang="clojure">
(import '[java.io File]
'[javax.imageio ImageIO]
Line 453:
"test-gray-cloj.png"))
 
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
 
Use the function rgb-to-gray-image to convert a rgb-image as loaded by the function defined [[Bitmap/Read a PPM file#Common Lisp]]. The package identifier assumes that you have the package as defined in [[Basic bitmap storage#Common Lisp]]. With the function grayscale-image-to-pgm-file it is possible to write out the gray image as pgm file which can then be further processed.
<langsyntaxhighlight lang="lisp">
(in-package #:rgb-pixel-buffer)
 
Line 483:
(export 'grayscale-image-to-pgm-file)
 
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
{{trans|Ruby}}
Extending [[Basic_bitmap_storage#Crystal]]
<langsyntaxhighlight lang="ruby">class RGBColour
def to_grayscale
luminosity = (0.2126*@red + 0.7152*@green + 0.0722*@blue).to_i
Line 505:
gray
end
end</langsyntaxhighlight>
 
=={{header|D}}==
This example uses the bitmap module defined in the [[Bitmap]] Task page.
 
<langsyntaxhighlight lang="d">module grayscale_image;
 
import core.stdc.stdio, std.array, std.algorithm, std.string, std.ascii;
Line 626:
img2.rgb2grayImage.savePGM("quantum_frog_grey.pgm");
}
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 635:
The code below extends the erlang module on [[Bitmap]] task. This module supports RGB and grayscale modes. RGB colors are specified as {rgb, R, G, B} and saved as bytes into an array. Grayscale colors are likewise specified as {gray, L} where L is luminance.
 
<langsyntaxhighlight lang="erlang">-module(ros_bitmap).
 
-export([new/2, fill/2, set_pixel/3, get_pixel/2, convert/2]).
Line 708:
convert(#bitmap{mode=Mode}=Bitmap, Mode) ->
Bitmap.
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="text">
>A=loadrgb("mona.jpg");
>insrgb(A);
Line 722:
>insrgb(grayscale(A));
>insrgb(A|grayscale(A));
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function to_gray(sequence image)
sequence color
for i = 1 to length(image) do
Line 744:
end for
return image
end function</langsyntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang="factor">USING: arrays kernel math math.matrices math.vectors ;
 
: rgb>gray ( matrix -- new-matrix )
Line 754:
 
: gray>rgb ( matrix -- new-matrix )
[ dup dup 3array ] matrix-map ;</langsyntaxhighlight>
 
=={{header|FBSL}}==
24-bpp BMP-format P.O.T.-size image solution:
[[Image:FBSLLena.png|right]]
<langsyntaxhighlight lang="qbasic">DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp"
DIM head, tail, r, g, b, l, ptr, blobsize = 54 ' sizeof BMP file headers
 
Line 773:
NEXT
 
FILEPUT(FILEOPEN(grayscale, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save buffer</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">\ grayscale bitmap (without word-alignment for scan lines)
 
\ bdim, bwidth, bdata all work with graymaps
Line 831:
over i j rot b!
loop
loop nip ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 839:
First let's define a new type; the <tt>sc</tt> stands for Single Channel, which can be luminance (as it is here).
 
<langsyntaxhighlight lang="fortran">type scimage
integer, dimension(:,:), pointer :: channel
integer :: width, height
end type scimage</langsyntaxhighlight>
 
In order to allow proper overloading, the following subroutines of the [[Basic bitmap storage#Fortran|storage]] should be renamed appending the <tt>_rgb</tt> suffix: valid_image, inside_image, alloc_img, free_img, fill_img, get_pixel, put_pixel, init_img. The ''single channel'' version would be named with the <tt>_sc</tt> suffix, then we should define the proper interfaces to use the already written code as before. Here there are only the interfaces and subroutines needed for the task.
 
<langsyntaxhighlight lang="fortran">interface alloc_img
module procedure alloc_img_rgb, alloc_img_sc
end interface
Line 852:
interface free_img
module procedure free_img_rgb, free_img_sc
end interface</langsyntaxhighlight>
 
Now we can define useful interfaces and subroutines more task-related:
 
<langsyntaxhighlight lang="fortran">interface assignment(=)
module procedure rgbtosc, sctorgb
end interface</langsyntaxhighlight>
 
<langsyntaxhighlight lang="fortran">subroutine alloc_img_sc(img, w, h)
type(scimage) :: img
integer, intent(in) :: w, h
Line 904:
end if
 
end subroutine sctorgb</langsyntaxhighlight>
 
'''Usage example''' (fragment) which can be used to convert from ''rgb'' image to ''grayscale'' image and back (since we only can output the ''rgb'' kind):
 
<langsyntaxhighlight lang="fortran">type(scimage) :: gray
type(rgbimage) :: animage
! ... here we "load" or create animage
Line 916:
gray = animage
animage = gray
call output_ppm(an_unit, animage)</langsyntaxhighlight>
 
=={{header|Fōrmulæ}}==
Line 928:
=={{header|FreeBASIC}}==
{{trans|BASIC256}}
<langsyntaxhighlight lang="freebasic">Dim As Integer ancho = 143, alto = 188, x, y, p, red, green, blue, luminancia
Dim As String imagen = "Mona_Lisa.bmp"
Screenres ancho,alto,32
Line 947:
 
Bsave "Grey_"+imagen+".bmp",0
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 955:
=={{header|FutureBasic}}==
There are several ways to handle grayscaling images in FB. Here's a function that accepts any of a variety of color images — JPEG, TIFF, PNG, BMP, GIF, etc. — and converts them to grayscale. The function uses a convenient build-in Core Image filter to generate the optimized grayscale image. This code compiles as a standalone application featuring a window with two image views, one showing the original color image, and the other with the converted grayscale image. The app uses a relatively square color image of flowers. It proportionately resizes the image to fit the left hand image view, and displays the converted image in the right hand view.
<syntaxhighlight lang="text">
include resources "flowers.jpg"
 
Line 1,001:
 
HandleEvents
</syntaxhighlight>
</lang>
 
 
Line 1,008:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package raster
 
import (
Line 1,107:
}
return b
}</langsyntaxhighlight>
For demonstration program see task [[Bitmap/Read a PPM file]].
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">module Bitmap.Gray(module Bitmap.Gray) where
 
import Bitmap
Line 1,128:
 
toGrayImage :: Color c => Image s c -> ST s (Image s Gray)
toGrayImage = mapImage $ Gray . luminance</langsyntaxhighlight>
 
A <tt>Gray</tt> image can be converted to an <tt>RGB</tt> image with <tt>Bitmap.RGB.toRGBImage</tt>, defined [[Basic bitmap storage|here]].
Line 1,138:
Grayscale image is stored as two-dimensional array of luminance values. Allowed luminance scale is the same as for the color bitmap; the functions below are neutral to scale.
 
<langsyntaxhighlight lang="j">NB. converts the image to grayscale according to formula
NB. L = 0.2126*R + 0.7152*G + 0.0722*B
toGray=: [: <. +/ .*"1&0.2126 0.7152 0.0722
 
NB. converts grayscale image to the color image, with all channels equal
toColor=: 3 & $"0</langsyntaxhighlight>
 
Example:
 
<syntaxhighlight lang ="j">viewRGB toColor toGray myimg</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">void convertToGrayscale(final BufferedImage image){
for(int i=0; i<image.getWidth(); i++){
for(int j=0; j<image.getHeight(); j++){
Line 1,173:
}
}
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
HTML 5
Demonstration: https://repl.it/repls/NiceFaroffRockrat
<syntaxhighlight lang="javascript">
<lang JavaScript>
function toGray(img) {
let cnv = document.getElementById("canvas");
Line 1,202:
return cnv.toDataURL();
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
'''Adhering to the Task Description'''
<syntaxhighlight lang="julia">
<lang Julia>
using Color, Images, FixedPointNumbers
 
Line 1,227:
imc = gray2rgb(imb)
imwrite(imc, "grayscale_image_rc.png")
</syntaxhighlight>
</lang>
Rounding errors are unlikely to be an issue for <code>rgb2gray</code>. The calculation of <code>g</code> promotes it to the literal float type (typically <code>Float64</code>).
 
'''A More Idiomatic Approach'''
<syntaxhighlight lang="julia">
<lang Julia>
using Color, Images, FixedPointNumbers
 
Line 1,237:
imb = convert(Image{Gray{Ufixed8}}, ima)
imwrite(imb, "grayscale_image_julia.png")
</syntaxhighlight>
</lang>
 
{{output}}
Line 1,250:
 
As it's not possible to recover the original colored image (because different combinations of RGB values could have produced the same luminance), I have not bothered with the reverse operation.
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.io.File
Line 1,276:
val grayFile = File("bbc_gray.jpg")
ImageIO.write(image, "jpg", grayFile)
}</langsyntaxhighlight>
 
{{out}}
Line 1,284:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
WindowWidth = 400
Line 1,306:
wait
[q] unloadbmp "clr":close #1:end
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on rgbToGrayscaleImageFast (img)
res = image(img.width, img.height, 8)
res.paletteRef = #grayScale
Line 1,327:
end repeat
return res
end</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang="lua">function ConvertToGrayscaleImage( bitmap )
local size_x, size_y = #bitmap, #bitmap[1]
local gray_im = {}
Line 1,356:
return bitmap
end</langsyntaxhighlight>
 
=={{header|Maple}}==
Maple has builtin command for conversion from RGB to Grayscale image: ImageTools:-ToGrayScale, which uses gray = 0.30 red + 0.59 green + 0.11 blue, the following implementation uses the CIE formula. Note that the conversion back from GrayScale to RGB uses Maple's builtin command: ImageTools:-ToRGB.
<langsyntaxhighlight Maplelang="maple">with(ImageTools):
#conversion forward
dimensions:=[upperbound(img)];
Line 1,375:
ToRGB(x);
#display the result
Embed(x);</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has a built-in grayscale conversion function called "ColorConvert". This example does not use it since it appears the luminance calculation is different from the CIE spec. Grayscale to RGB "conversion" just changes the single channel grayscale image to a triple channel image.
<langsyntaxhighlight Mathematicalang="mathematica">toGrayscale[rgb_Image] := ImageApply[#.{0.2126, 0.7152, 0.0722}&, rgb]
toFakeRGB[L_Image] := ImageApply[{#, #, #}&, L] </langsyntaxhighlight>
 
=={{header|MATLAB}}==
Built in colour to grayscale converter uses the following forumula:
0.2989*R + 0.5870*G + 0.1140*B
<langsyntaxhighlight Matlablang="matlab">function [grayImage] = colortograyscale(inputImage)
grayImage = rgb2gray(inputImage);</langsyntaxhighlight>
 
=={{header|Nim}}==
The right way to proceed would have been to add the case of gray scale images to our Image type (using a “variant object” with a discriminator). But we didn’t want to change the Image type, so we have created a GrayImage type and duplicated most procedures.
 
<syntaxhighlight lang="nim">
<lang Nim>
import bitmap
import lenientops
Line 1,464:
# Convert it back to RGB in order to save it in PPM format using the available procedure.
var convertedImage = grayImage.toImage()
convertedImage.writePPM("output_gray.ppm")</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
Conversion to a grayscale image:
<langsyntaxhighlight lang="ocaml">let to_grayscale ~img:(_, r_channel, g_channel, b_channel) =
let width = Bigarray.Array2.dim1 r_channel
and height = Bigarray.Array2.dim2 r_channel in
Line 1,488:
done;
done;
(gray_channel)</langsyntaxhighlight>
 
Conversion to a color image:
<langsyntaxhighlight lang="ocaml">let to_color ~img:gray_channel =
let width = Bigarray.Array2.dim1 gray_channel
and height = Bigarray.Array2.dim2 gray_channel in
Line 1,510:
r_channel,
g_channel,
b_channel)</langsyntaxhighlight>
 
and functions to get/set a pixel:
 
<langsyntaxhighlight lang="ocaml">let gray_get_pixel_unsafe (gray_channel) =
(fun x y -> gray_channel.{x,y})
 
let gray_put_pixel_unsafe (gray_channel) v =
(fun x y -> gray_channel.{x,y} <- v)</langsyntaxhighlight>
 
=={{header|Octave}}==
Line 1,524:
'''Use package''': image
 
<langsyntaxhighlight lang="octave">function [grayImage] = colortograyscale(inputImage)
grayImage = rgb2gray(inputImage);</langsyntaxhighlight>
 
Differently from [[Grayscale image#MATLAB|MATLAB]], the grayscale is computed as mean of the three RGB values. Changing this non-optimal behaviour is a matter of fixing three lines in the <tt>rgb2gray.m</tt> file; since it's a GPL-ed code, here it is a semplified version (error checking, usage help, argument checking removed)
 
<langsyntaxhighlight lang="octave">function gray = rgb2gray (rgb)
switch(class(rgb))
case "double"
Line 1,542:
function lum = luminance(rgb)
lum = 0.2126*rgb(:,:,1) + 0.7152*rgb(:,:,2) + 0.0722*rgb(:,:,3);
endfunction</langsyntaxhighlight>
 
Original code of the <tt>rgb2gray.m</tt> in the image package version 1.0.8 is by Kai Habel (under the GNU General Public License)
Line 1,549:
We define a "graymap" as a two-dimensional array of floats. In module <code>"Grayscale.oz"</code>, we implement conversion functions from and to bitmaps:
 
<langsyntaxhighlight lang="oz">functor
import
Array2D
Line 1,575:
color(L L L)
end
end</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 1,583:
Since we are using Imlib2, this one '''does''' '''not''' implement really a gray-scale (single channel) storage; it only ''converts'' an RGB image to an RGB image with the same three colour components for each pixel (which result in a gray-scale-like image)
 
<langsyntaxhighlight lang="perl">#! /usr/bin/perl
 
use strict;
Line 1,609:
$gscale->save("Lennagray.jpg");
 
exit 0;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{Trans|Euphoria}}
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read a PPM file]] and write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write a PPM file]].
<langsyntaxhighlight Phixlang="phix">-- demo\rosetta\Bitmap_Greyscale.exw (runnable version)
 
function to_grey(sequence image)
Line 1,634:
sequence img = read_ppm("Lena.ppm")
img = to_grey(img)
write_ppm("LenaGray.ppm",img)</langsyntaxhighlight>
 
=={{header|PHP}}==
Uses the [[Write ppm file#PHP|Bitmap class]] defined for writing a PPM file
<langsyntaxhighlight PHPlang="php">class BitmapGrayscale extends Bitmap {
public function toGrayscale(){
for ($i = 0; $i < $this->h; $i++){
Line 1,658:
$b->setPixel(0, 13, array(0,0,255));
$b->toGrayscale();
$b->writeP6('p6-grayscale.ppm');</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp"># Convert color image (PPM) to greyscale image (PGM)
(de ppm->pgm (Ppm)
(mapcar
Line 1,683:
'((G) (list G G G))
Y ) )
Pgm ) )</langsyntaxhighlight>
<langsyntaxhighlight PicoLisplang="picolisp"># Write greyscale image (PGM) to file
(de pgmWrite (Pgm File)
(out File
Line 1,709:
 
# Convert to color image and write to .ppm file
(ppmWrite (pgm->ppm *Pgm) "img.ppm")</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
do j = 1 to hbound(image,1);
do i = 0 to hbound(image,2);
Line 1,724:
end;
end;
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure ImageGrayout(image)
Protected w, h, x, y, r, g, b, gray, color
Line 1,761:
Next
StopDrawing()
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
Line 1,767:
 
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]]
<langsyntaxhighlight lang="python"># String masquerading as ppm file (version P3)
import io
ppmfileout = io.StringIO('')
Line 1,819:
254 254 254 31 31 31 254 254 254 254 254 254
 
'''</langsyntaxhighlight>
 
=={{header|R}}==
{{libheader|pixmap}}
<langsyntaxhighlight lang="r"># Conversion from Grey to RGB uses the following code
setAs("pixmapGrey", "pixmapRGB",
function(from, to){
Line 1,855:
 
# Convert back to "colour"
plot(p3 <- as(p2, "pixmapRGB"))</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,862:
I gave up on uploading to Rosetta Code.
 
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 1,904:
(color->gray rosetta)
(gray->color (color->gray rosetta))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,910:
 
This script expects to be fed a P6 .ppm file name at the command line. It will convert it to grey scale and save it as a binary portable grey map (P5 .pgm) file.
<syntaxhighlight lang="raku" perl6line>sub MAIN ($filename = 'default.ppm') {
 
my $in = open($filename, :r, :enc<iso-8859-1>) or die $in;
Line 1,928:
$in.close;
$out.close;
}</langsyntaxhighlight>
Using the .ppm file from the [[Bitmap/Write a PPM file#Raku|Write a PPM file]] task:
 
Line 1,936:
Note: &nbsp; REXX uses decimal (characters) instead of binary for storing numbers, &nbsp; so there is no rounding &nbsp; (using
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;characters to store numbers is almost the same as using decimal floating point).
<langsyntaxhighlight lang="rexx">/*REXX program converts a RGB (red─green─blue) image into a grayscale/greyscale image.*/
blue= '00 00 ff'x /*define the blue color (hexadecimal).*/
@.= blue /*set the entire image to blue color.*/
Line 1,951:
end /*row*/ /* [↑] D2C convert decimal ───► char*/
end /*col*/ /* [↑] x%1 is the same as TRUNC(x) */
/*stick a fork in it, we're all done. */</langsyntaxhighlight><br><br>
 
=={{header|Ruby}}==
Extending [[Basic_bitmap_storage#Ruby]]
<langsyntaxhighlight lang="ruby">class RGBColour
def to_grayscale
luminosity = Integer(0.2126*@red + 0.7152*@green + 0.0722*@blue)
Line 1,972:
gray
end
end</langsyntaxhighlight>
 
=={{header|Scala}}==
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
<langsyntaxhighlight lang="scala">object BitmapOps {
def luminosity(c:Color)=(0.2126*c.getRed + 0.7152*c.getGreen + 0.0722*c.getBlue+0.5).toInt
 
Line 1,985:
image
}
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('Image::Imlib2')
 
func tograyscale(img) {
Line 2,007:
var gscale = tograyscale(image)
gscale.set_quality(80)
gscale.save(output)</langsyntaxhighlight>
 
=={{header|Tcl}}==
<!-- L = 0.2126·R + 0.7152·G + 0.0722·B -->
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
proc grayscale image {
Line 2,024:
}
}
}</langsyntaxhighlight>
Photo images are always 8-bits-per-channel RGBA.
 
=={{header|Vedit macro language}}==
Conversion to a grayscale image.<br>
<langsyntaxhighlight lang="vedit">// Convert RGB image to grayscale (8 bit/pixel)
// #10 = buffer that contains image data
// On return:
Line 2,049:
Buf_Switch(#10)
}
Return</langsyntaxhighlight>
 
Conversion to a color image.<br>
<langsyntaxhighlight lang="vedit">// Convert grayscale image (8 bits/pixel) into RGB (24 bits/pixel)
// #20 = buffer that contains image data
// On return:
Line 2,071:
Buf_Switch(#20)
}
Return</langsyntaxhighlight>
 
=={{header|Visual Basic}}==
Line 2,077:
{{works with|Visual Basic|6}}
{{libheader|Win32}}
<langsyntaxhighlight lang="vb">Option Explicit
 
Private Type BITMAP
Line 2,133:
DeleteDC hdc
 
End Sub</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
Line 2,139:
Convert a Bitmap to Grayscale.
 
<langsyntaxhighlight lang="vbnet">
Imports System.Drawing.Imaging
 
Line 2,196:
Map.UnlockBits(oBMPData)
 
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
This script converts the image [https://rosettacode.org/File:Lenna100.jpg Lenna100.jpg] to grayscale and then displays the two images side by side.
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, ImageData
import "dome" for Window
 
Line 2,240:
}
 
var Game = PercentageDifference.new(1100, 550, "Lenna100.jpg", "Lenna-grayscale.jpg")</langsyntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|BASIC256}}
"image" is a library created by Hermang Mansilla for import and show .BMP files. http://www.oocities.org/sunsetstrip/palms/1624/yabasic/libs/IMAGE.TXT
<langsyntaxhighlight Yabasiclang="yabasic">import image
 
open window 600,600
Line 2,264:
dot x+300,y+300
next y
next x</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 2,273:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|BASIC256}}
<langsyntaxhighlight lang="zkl">fcn toGrayScale(img){ // in-place conversion
foreach x,y in (img.w,img.h){
r,g,b:=img[x,y].toBigEndian(3);
Line 2,279:
img[x,y]=((lum*256) + lum)*256 + lum;
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">img:=PPM.readPPMFile("lena.ppm");
toGrayScale(img);
img.write(File("foo.ppm","wb"));</langsyntaxhighlight>
{{out}}
http://www.zenkinetic.com/Images/RosettaCode/lenaGray.jpg
10,333

edits