Grayscale image: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">T Colour |
||
Byte r, g, b |
Byte r, g, b |
||
Line 82: | Line 82: | ||
print(‘Grey:’) |
print(‘Grey:’) |
||
bitmap.togreyscale() |
bitmap.togreyscale() |
||
print(bitmap.writeppmp3())</ |
print(bitmap.writeppmp3())</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 111: | Line 111: | ||
{{libheader|Action! Bitmap tools}} |
{{libheader|Action! Bitmap tools}} |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "H6:RGB2GRAY.ACT" ;from task Grayscale image |
||
PROC PrintB3(BYTE x) |
PROC PrintB3(BYTE x) |
||
Line 185: | Line 185: | ||
LMARGIN=oldLMARGIN ;restore left margin on the screen |
LMARGIN=oldLMARGIN ;restore left margin on the screen |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Grayscale_image.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Grayscale_image.png Screenshot from Atari 8-bit computer] |
||
Line 209: | Line 209: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">type Grayscale_Image is array (Positive range <>, Positive range <>) of Luminance;</syntaxhighlight> |
||
Conversion to a grayscale image: |
Conversion to a grayscale image: |
||
< |
<syntaxhighlight lang="ada">function Grayscale (Picture : Image) return Grayscale_Image is |
||
type Extended_Luminance is range 0..10_000_000; |
type Extended_Luminance is range 0..10_000_000; |
||
Result : Grayscale_Image (Picture'Range (1), Picture'Range (2)); |
Result : Grayscale_Image (Picture'Range (1), Picture'Range (2)); |
||
Line 230: | Line 230: | ||
end loop; |
end loop; |
||
return Result; |
return Result; |
||
end Grayscale;</ |
end Grayscale;</syntaxhighlight> |
||
Conversion to a color image: |
Conversion to a color image: |
||
< |
<syntaxhighlight lang="ada">function Color (Picture : Grayscale_Image) return Image is |
||
Result : Image (Picture'Range (1), Picture'Range (2)); |
Result : Image (Picture'Range (1), Picture'Range (2)); |
||
begin |
begin |
||
Line 241: | Line 241: | ||
end loop; |
end loop; |
||
return Result; |
return Result; |
||
end Color;</ |
end Color;</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
[[Image:BASIC256_greyscale_Mona_Lisa.jpg|right]] |
[[Image:BASIC256_greyscale_Mona_Lisa.jpg|right]] |
||
[[Image:BASIC256_greysacle_Grey_Mona_lisa.jpg|right]] |
[[Image:BASIC256_greysacle_Grey_Mona_lisa.jpg|right]] |
||
< |
<syntaxhighlight lang="basic256">w = 143 |
||
h = 188 |
h = 188 |
||
name$ = "Mona_Lisa.jpg" |
name$ = "Mona_Lisa.jpg" |
||
Line 268: | Line 268: | ||
next x |
next x |
||
imgsave "Grey_"+name$,"jpg"</ |
imgsave "Grey_"+name$,"jpg"</syntaxhighlight> |
||
{{omit from|Batch File}} |
{{omit from|Batch File}} |
||
Line 276: | Line 276: | ||
[[Image:original_bbc.jpg|right]] |
[[Image:original_bbc.jpg|right]] |
||
[[Image:greyscale_bbc.jpg|right]] |
[[Image:greyscale_bbc.jpg|right]] |
||
< |
<syntaxhighlight lang="bbcbasic"> Width% = 200 |
||
Height% = 200 |
Height% = 200 |
||
Line 304: | Line 304: | ||
col% = TINT(x%*2,y%*2) |
col% = TINT(x%*2,y%*2) |
||
SWAP ?^col%,?(^col%+2) |
SWAP ?^col%,?(^col%+2) |
||
= col%</ |
= col%</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Definition/interface for a grayscale image. |
Definition/interface for a grayscale image. |
||
< |
<syntaxhighlight lang="c">typedef unsigned char luminance; |
||
typedef luminance pixel1[1]; |
typedef luminance pixel1[1]; |
||
typedef struct { |
typedef struct { |
||
Line 320: | Line 320: | ||
grayimage alloc_grayimg(unsigned int, unsigned int); |
grayimage alloc_grayimg(unsigned int, unsigned int); |
||
grayimage tograyscale(image); |
grayimage tograyscale(image); |
||
image tocolor(grayimage);</ |
image tocolor(grayimage);</syntaxhighlight> |
||
The same as <tt>alloc_img</tt>, but for grayscale images. |
The same as <tt>alloc_img</tt>, but for grayscale images. |
||
< |
<syntaxhighlight lang="c">grayimage alloc_grayimg(unsigned int width, unsigned int height) |
||
{ |
{ |
||
grayimage img; |
grayimage img; |
||
Line 332: | Line 332: | ||
img->height = height; |
img->height = height; |
||
return img; |
return img; |
||
}</ |
}</syntaxhighlight> |
||
Convert from ''color'' image to ''grayscale'' image. |
Convert from ''color'' image to ''grayscale'' image. |
||
< |
<syntaxhighlight lang="c">grayimage tograyscale(image img) |
||
{ |
{ |
||
unsigned int x, y; |
unsigned int x, y; |
||
Line 358: | Line 358: | ||
} |
} |
||
return timg; |
return timg; |
||
}</ |
}</syntaxhighlight> |
||
And back from a ''grayscale'' image to a ''color'' image. |
And back from a ''grayscale'' image to a ''color'' image. |
||
< |
<syntaxhighlight lang="c">image tocolor(grayimage img) |
||
{ |
{ |
||
unsigned int x, y; |
unsigned int x, y; |
||
Line 383: | Line 383: | ||
} |
} |
||
return timg; |
return timg; |
||
}</ |
}</syntaxhighlight> |
||
'''Notes''' |
'''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 |
* <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 |
||
< |
<syntaxhighlight lang="c">#define free_grayimg(IMG) free_img((image)(IMG))</syntaxhighlight> |
||
* ''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. |
* ''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: | Line 394: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
To convert TO grayscale: |
To convert TO grayscale: |
||
< |
<syntaxhighlight lang="csharp"> |
||
Bitmap tImage = new Bitmap("spectrum.bmp"); |
Bitmap tImage = new Bitmap("spectrum.bmp"); |
||
Line 411: | Line 411: | ||
// Save |
// Save |
||
tImage.Save("spectrum2.bmp"); |
tImage.Save("spectrum2.bmp"); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure|Clojure}}== |
=={{header|Clojure|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(import '[java.io File] |
(import '[java.io File] |
||
'[javax.imageio ImageIO] |
'[javax.imageio ImageIO] |
||
Line 453: | Line 453: | ||
"test-gray-cloj.png")) |
"test-gray-cloj.png")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(in-package #:rgb-pixel-buffer) |
(in-package #:rgb-pixel-buffer) |
||
Line 483: | Line 483: | ||
(export 'grayscale-image-to-pgm-file) |
(export 'grayscale-image-to-pgm-file) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
Extending [[Basic_bitmap_storage#Crystal]] |
Extending [[Basic_bitmap_storage#Crystal]] |
||
< |
<syntaxhighlight lang="ruby">class RGBColour |
||
def to_grayscale |
def to_grayscale |
||
luminosity = (0.2126*@red + 0.7152*@green + 0.0722*@blue).to_i |
luminosity = (0.2126*@red + 0.7152*@green + 0.0722*@blue).to_i |
||
Line 505: | Line 505: | ||
gray |
gray |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
This example uses the bitmap module defined in the [[Bitmap]] Task page. |
This example uses the bitmap module defined in the [[Bitmap]] Task page. |
||
< |
<syntaxhighlight lang="d">module grayscale_image; |
||
import core.stdc.stdio, std.array, std.algorithm, std.string, std.ascii; |
import core.stdc.stdio, std.array, std.algorithm, std.string, std.ascii; |
||
Line 626: | Line 626: | ||
img2.rgb2grayImage.savePGM("quantum_frog_grey.pgm"); |
img2.rgb2grayImage.savePGM("quantum_frog_grey.pgm"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 635: | 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. |
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. |
||
< |
<syntaxhighlight lang="erlang">-module(ros_bitmap). |
||
-export([new/2, fill/2, set_pixel/3, get_pixel/2, convert/2]). |
-export([new/2, fill/2, set_pixel/3, get_pixel/2, convert/2]). |
||
Line 708: | Line 708: | ||
convert(#bitmap{mode=Mode}=Bitmap, Mode) -> |
convert(#bitmap{mode=Mode}=Bitmap, Mode) -> |
||
Bitmap. |
Bitmap. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
>A=loadrgb("mona.jpg"); |
>A=loadrgb("mona.jpg"); |
||
>insrgb(A); |
>insrgb(A); |
||
Line 722: | Line 722: | ||
>insrgb(grayscale(A)); |
>insrgb(grayscale(A)); |
||
>insrgb(A|grayscale(A)); |
>insrgb(A|grayscale(A)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function to_gray(sequence image) |
||
sequence color |
sequence color |
||
for i = 1 to length(image) do |
for i = 1 to length(image) do |
||
Line 744: | Line 744: | ||
end for |
end for |
||
return image |
return image |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-07-03}} |
{{works with|Factor|0.99 2020-07-03}} |
||
< |
<syntaxhighlight lang="factor">USING: arrays kernel math math.matrices math.vectors ; |
||
: rgb>gray ( matrix -- new-matrix ) |
: rgb>gray ( matrix -- new-matrix ) |
||
Line 754: | Line 754: | ||
: gray>rgb ( matrix -- new-matrix ) |
: gray>rgb ( matrix -- new-matrix ) |
||
[ dup dup 3array ] matrix-map ;</ |
[ dup dup 3array ] matrix-map ;</syntaxhighlight> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
24-bpp BMP-format P.O.T.-size image solution: |
24-bpp BMP-format P.O.T.-size image solution: |
||
[[Image:FBSLLena.png|right]] |
[[Image:FBSLLena.png|right]] |
||
< |
<syntaxhighlight lang="qbasic">DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp" |
||
DIM head, tail, r, g, b, l, ptr, blobsize = 54 ' sizeof BMP file headers |
DIM head, tail, r, g, b, l, ptr, blobsize = 54 ' sizeof BMP file headers |
||
Line 773: | Line 773: | ||
NEXT |
NEXT |
||
FILEPUT(FILEOPEN(grayscale, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save buffer</ |
FILEPUT(FILEOPEN(grayscale, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save buffer</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">\ grayscale bitmap (without word-alignment for scan lines) |
||
\ bdim, bwidth, bdata all work with graymaps |
\ bdim, bwidth, bdata all work with graymaps |
||
Line 831: | Line 831: | ||
over i j rot b! |
over i j rot b! |
||
loop |
loop |
||
loop nip ;</ |
loop nip ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 839: | 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). |
First let's define a new type; the <tt>sc</tt> stands for Single Channel, which can be luminance (as it is here). |
||
< |
<syntaxhighlight lang="fortran">type scimage |
||
integer, dimension(:,:), pointer :: channel |
integer, dimension(:,:), pointer :: channel |
||
integer :: width, height |
integer :: width, height |
||
end type scimage</ |
end type scimage</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="fortran">interface alloc_img |
||
module procedure alloc_img_rgb, alloc_img_sc |
module procedure alloc_img_rgb, alloc_img_sc |
||
end interface |
end interface |
||
Line 852: | Line 852: | ||
interface free_img |
interface free_img |
||
module procedure free_img_rgb, free_img_sc |
module procedure free_img_rgb, free_img_sc |
||
end interface</ |
end interface</syntaxhighlight> |
||
Now we can define useful interfaces and subroutines more task-related: |
Now we can define useful interfaces and subroutines more task-related: |
||
< |
<syntaxhighlight lang="fortran">interface assignment(=) |
||
module procedure rgbtosc, sctorgb |
module procedure rgbtosc, sctorgb |
||
end interface</ |
end interface</syntaxhighlight> |
||
< |
<syntaxhighlight lang="fortran">subroutine alloc_img_sc(img, w, h) |
||
type(scimage) :: img |
type(scimage) :: img |
||
integer, intent(in) :: w, h |
integer, intent(in) :: w, h |
||
Line 904: | Line 904: | ||
end if |
end if |
||
end subroutine sctorgb</ |
end subroutine sctorgb</syntaxhighlight> |
||
'''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): |
'''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): |
||
< |
<syntaxhighlight lang="fortran">type(scimage) :: gray |
||
type(rgbimage) :: animage |
type(rgbimage) :: animage |
||
! ... here we "load" or create animage |
! ... here we "load" or create animage |
||
Line 916: | Line 916: | ||
gray = animage |
gray = animage |
||
animage = gray |
animage = gray |
||
call output_ppm(an_unit, animage)</ |
call output_ppm(an_unit, animage)</syntaxhighlight> |
||
=={{header|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
Line 928: | Line 928: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BASIC256}} |
{{trans|BASIC256}} |
||
< |
<syntaxhighlight lang="freebasic">Dim As Integer ancho = 143, alto = 188, x, y, p, red, green, blue, luminancia |
||
Dim As String imagen = "Mona_Lisa.bmp" |
Dim As String imagen = "Mona_Lisa.bmp" |
||
Screenres ancho,alto,32 |
Screenres ancho,alto,32 |
||
Line 947: | Line 947: | ||
Bsave "Grey_"+imagen+".bmp",0 |
Bsave "Grey_"+imagen+".bmp",0 |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 955: | Line 955: | ||
=={{header|FutureBasic}}== |
=={{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. |
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. |
||
<lang> |
<syntaxhighlight lang="text"> |
||
include resources "flowers.jpg" |
include resources "flowers.jpg" |
||
Line 1,001: | Line 1,001: | ||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 1,008: | Line 1,008: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package raster |
||
import ( |
import ( |
||
Line 1,107: | Line 1,107: | ||
} |
} |
||
return b |
return b |
||
}</ |
}</syntaxhighlight> |
||
For demonstration program see task [[Bitmap/Read a PPM file]]. |
For demonstration program see task [[Bitmap/Read a PPM file]]. |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">module Bitmap.Gray(module Bitmap.Gray) where |
||
import Bitmap |
import Bitmap |
||
Line 1,128: | Line 1,128: | ||
toGrayImage :: Color c => Image s c -> ST s (Image s Gray) |
toGrayImage :: Color c => Image s c -> ST s (Image s Gray) |
||
toGrayImage = mapImage $ Gray . luminance</ |
toGrayImage = mapImage $ Gray . luminance</syntaxhighlight> |
||
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]]. |
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: | 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. |
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. |
||
< |
<syntaxhighlight lang="j">NB. converts the image to grayscale according to formula |
||
NB. L = 0.2126*R + 0.7152*G + 0.0722*B |
NB. L = 0.2126*R + 0.7152*G + 0.0722*B |
||
toGray=: [: <. +/ .*"1&0.2126 0.7152 0.0722 |
toGray=: [: <. +/ .*"1&0.2126 0.7152 0.0722 |
||
NB. converts grayscale image to the color image, with all channels equal |
NB. converts grayscale image to the color image, with all channels equal |
||
toColor=: 3 & $"0</ |
toColor=: 3 & $"0</syntaxhighlight> |
||
Example: |
Example: |
||
<lang |
<syntaxhighlight lang="j">viewRGB toColor toGray myimg</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">void convertToGrayscale(final BufferedImage image){ |
||
for(int i=0; i<image.getWidth(); i++){ |
for(int i=0; i<image.getWidth(); i++){ |
||
for(int j=0; j<image.getHeight(); j++){ |
for(int j=0; j<image.getHeight(); j++){ |
||
Line 1,173: | Line 1,173: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
HTML 5 |
HTML 5 |
||
Demonstration: https://repl.it/repls/NiceFaroffRockrat |
Demonstration: https://repl.it/repls/NiceFaroffRockrat |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
function toGray(img) { |
function toGray(img) { |
||
let cnv = document.getElementById("canvas"); |
let cnv = document.getElementById("canvas"); |
||
Line 1,202: | Line 1,202: | ||
return cnv.toDataURL(); |
return cnv.toDataURL(); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
'''Adhering to the Task Description''' |
'''Adhering to the Task Description''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
using Color, Images, FixedPointNumbers |
using Color, Images, FixedPointNumbers |
||
Line 1,227: | Line 1,227: | ||
imc = gray2rgb(imb) |
imc = gray2rgb(imb) |
||
imwrite(imc, "grayscale_image_rc.png") |
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>). |
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''' |
'''A More Idiomatic Approach''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
using Color, Images, FixedPointNumbers |
using Color, Images, FixedPointNumbers |
||
Line 1,237: | Line 1,237: | ||
imb = convert(Image{Gray{Ufixed8}}, ima) |
imb = convert(Image{Gray{Ufixed8}}, ima) |
||
imwrite(imb, "grayscale_image_julia.png") |
imwrite(imb, "grayscale_image_julia.png") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
Line 1,250: | 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. |
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. |
||
< |
<syntaxhighlight lang="scala">// version 1.2.10 |
||
import java.io.File |
import java.io.File |
||
Line 1,276: | Line 1,276: | ||
val grayFile = File("bbc_gray.jpg") |
val grayFile = File("bbc_gray.jpg") |
||
ImageIO.write(image, "jpg", grayFile) |
ImageIO.write(image, "jpg", grayFile) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,284: | Line 1,284: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
nomainwin |
nomainwin |
||
WindowWidth = 400 |
WindowWidth = 400 |
||
Line 1,306: | Line 1,306: | ||
wait |
wait |
||
[q] unloadbmp "clr":close #1:end |
[q] unloadbmp "clr":close #1:end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">on rgbToGrayscaleImageFast (img) |
||
res = image(img.width, img.height, 8) |
res = image(img.width, img.height, 8) |
||
res.paletteRef = #grayScale |
res.paletteRef = #grayScale |
||
Line 1,327: | Line 1,327: | ||
end repeat |
end repeat |
||
return res |
return res |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function ConvertToGrayscaleImage( bitmap ) |
||
local size_x, size_y = #bitmap, #bitmap[1] |
local size_x, size_y = #bitmap, #bitmap[1] |
||
local gray_im = {} |
local gray_im = {} |
||
Line 1,356: | Line 1,356: | ||
return bitmap |
return bitmap |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="maple">with(ImageTools): |
||
#conversion forward |
#conversion forward |
||
dimensions:=[upperbound(img)]; |
dimensions:=[upperbound(img)]; |
||
Line 1,375: | Line 1,375: | ||
ToRGB(x); |
ToRGB(x); |
||
#display the result |
#display the result |
||
Embed(x);</ |
Embed(x);</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="mathematica">toGrayscale[rgb_Image] := ImageApply[#.{0.2126, 0.7152, 0.0722}&, rgb] |
||
toFakeRGB[L_Image] := ImageApply[{#, #, #}&, L] </ |
toFakeRGB[L_Image] := ImageApply[{#, #, #}&, L] </syntaxhighlight> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
Built in colour to grayscale converter uses the following forumula: |
Built in colour to grayscale converter uses the following forumula: |
||
0.2989*R + 0.5870*G + 0.1140*B |
0.2989*R + 0.5870*G + 0.1140*B |
||
< |
<syntaxhighlight lang="matlab">function [grayImage] = colortograyscale(inputImage) |
||
grayImage = rgb2gray(inputImage);</ |
grayImage = rgb2gray(inputImage);</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{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. |
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 bitmap |
||
import lenientops |
import lenientops |
||
Line 1,464: | Line 1,464: | ||
# Convert it back to RGB in order to save it in PPM format using the available procedure. |
# Convert it back to RGB in order to save it in PPM format using the available procedure. |
||
var convertedImage = grayImage.toImage() |
var convertedImage = grayImage.toImage() |
||
convertedImage.writePPM("output_gray.ppm")</ |
convertedImage.writePPM("output_gray.ppm")</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Conversion to a grayscale image: |
Conversion to a grayscale image: |
||
< |
<syntaxhighlight lang="ocaml">let to_grayscale ~img:(_, r_channel, g_channel, b_channel) = |
||
let width = Bigarray.Array2.dim1 r_channel |
let width = Bigarray.Array2.dim1 r_channel |
||
and height = Bigarray.Array2.dim2 r_channel in |
and height = Bigarray.Array2.dim2 r_channel in |
||
Line 1,488: | Line 1,488: | ||
done; |
done; |
||
done; |
done; |
||
(gray_channel)</ |
(gray_channel)</syntaxhighlight> |
||
Conversion to a color image: |
Conversion to a color image: |
||
< |
<syntaxhighlight lang="ocaml">let to_color ~img:gray_channel = |
||
let width = Bigarray.Array2.dim1 gray_channel |
let width = Bigarray.Array2.dim1 gray_channel |
||
and height = Bigarray.Array2.dim2 gray_channel in |
and height = Bigarray.Array2.dim2 gray_channel in |
||
Line 1,510: | Line 1,510: | ||
r_channel, |
r_channel, |
||
g_channel, |
g_channel, |
||
b_channel)</ |
b_channel)</syntaxhighlight> |
||
and functions to get/set a pixel: |
and functions to get/set a pixel: |
||
< |
<syntaxhighlight lang="ocaml">let gray_get_pixel_unsafe (gray_channel) = |
||
(fun x y -> gray_channel.{x,y}) |
(fun x y -> gray_channel.{x,y}) |
||
let gray_put_pixel_unsafe (gray_channel) v = |
let gray_put_pixel_unsafe (gray_channel) v = |
||
(fun x y -> gray_channel.{x,y} <- v)</ |
(fun x y -> gray_channel.{x,y} <- v)</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
Line 1,524: | Line 1,524: | ||
'''Use package''': image |
'''Use package''': image |
||
< |
<syntaxhighlight lang="octave">function [grayImage] = colortograyscale(inputImage) |
||
grayImage = rgb2gray(inputImage);</ |
grayImage = rgb2gray(inputImage);</syntaxhighlight> |
||
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) |
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) |
||
< |
<syntaxhighlight lang="octave">function gray = rgb2gray (rgb) |
||
switch(class(rgb)) |
switch(class(rgb)) |
||
case "double" |
case "double" |
||
Line 1,542: | Line 1,542: | ||
function lum = luminance(rgb) |
function lum = luminance(rgb) |
||
lum = 0.2126*rgb(:,:,1) + 0.7152*rgb(:,:,2) + 0.0722*rgb(:,:,3); |
lum = 0.2126*rgb(:,:,1) + 0.7152*rgb(:,:,2) + 0.0722*rgb(:,:,3); |
||
endfunction</ |
endfunction</syntaxhighlight> |
||
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) |
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: | 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: |
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: |
||
< |
<syntaxhighlight lang="oz">functor |
||
import |
import |
||
Array2D |
Array2D |
||
Line 1,575: | Line 1,575: | ||
color(L L L) |
color(L L L) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 1,583: | 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) |
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) |
||
< |
<syntaxhighlight lang="perl">#! /usr/bin/perl |
||
use strict; |
use strict; |
||
Line 1,609: | Line 1,609: | ||
$gscale->save("Lennagray.jpg"); |
$gscale->save("Lennagray.jpg"); |
||
exit 0;</ |
exit 0;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{Trans|Euphoria}} |
{{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]]. |
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]]. |
||
< |
<syntaxhighlight lang="phix">-- demo\rosetta\Bitmap_Greyscale.exw (runnable version) |
||
function to_grey(sequence image) |
function to_grey(sequence image) |
||
Line 1,634: | Line 1,634: | ||
sequence img = read_ppm("Lena.ppm") |
sequence img = read_ppm("Lena.ppm") |
||
img = to_grey(img) |
img = to_grey(img) |
||
write_ppm("LenaGray.ppm",img)</ |
write_ppm("LenaGray.ppm",img)</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Uses the [[Write ppm file#PHP|Bitmap class]] defined for writing a PPM file |
Uses the [[Write ppm file#PHP|Bitmap class]] defined for writing a PPM file |
||
< |
<syntaxhighlight lang="php">class BitmapGrayscale extends Bitmap { |
||
public function toGrayscale(){ |
public function toGrayscale(){ |
||
for ($i = 0; $i < $this->h; $i++){ |
for ($i = 0; $i < $this->h; $i++){ |
||
Line 1,658: | Line 1,658: | ||
$b->setPixel(0, 13, array(0,0,255)); |
$b->setPixel(0, 13, array(0,0,255)); |
||
$b->toGrayscale(); |
$b->toGrayscale(); |
||
$b->writeP6('p6-grayscale.ppm');</ |
$b->writeP6('p6-grayscale.ppm');</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp"># Convert color image (PPM) to greyscale image (PGM) |
||
(de ppm->pgm (Ppm) |
(de ppm->pgm (Ppm) |
||
(mapcar |
(mapcar |
||
Line 1,683: | Line 1,683: | ||
'((G) (list G G G)) |
'((G) (list G G G)) |
||
Y ) ) |
Y ) ) |
||
Pgm ) )</ |
Pgm ) )</syntaxhighlight> |
||
< |
<syntaxhighlight lang="picolisp"># Write greyscale image (PGM) to file |
||
(de pgmWrite (Pgm File) |
(de pgmWrite (Pgm File) |
||
(out File |
(out File |
||
Line 1,709: | Line 1,709: | ||
# Convert to color image and write to .ppm file |
# Convert to color image and write to .ppm file |
||
(ppmWrite (pgm->ppm *Pgm) "img.ppm")</ |
(ppmWrite (pgm->ppm *Pgm) "img.ppm")</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
do j = 1 to hbound(image,1); |
do j = 1 to hbound(image,1); |
||
do i = 0 to hbound(image,2); |
do i = 0 to hbound(image,2); |
||
Line 1,724: | Line 1,724: | ||
end; |
end; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure ImageGrayout(image) |
||
Protected w, h, x, y, r, g, b, gray, color |
Protected w, h, x, y, r, g, b, gray, color |
||
Line 1,761: | Line 1,761: | ||
Next |
Next |
||
StopDrawing() |
StopDrawing() |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,767: | Line 1,767: | ||
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
||
< |
<syntaxhighlight lang="python"># String masquerading as ppm file (version P3) |
||
import io |
import io |
||
ppmfileout = io.StringIO('') |
ppmfileout = io.StringIO('') |
||
Line 1,819: | Line 1,819: | ||
254 254 254 31 31 31 254 254 254 254 254 254 |
254 254 254 31 31 31 254 254 254 254 254 254 |
||
'''</ |
'''</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
{{libheader|pixmap}} |
{{libheader|pixmap}} |
||
< |
<syntaxhighlight lang="r"># Conversion from Grey to RGB uses the following code |
||
setAs("pixmapGrey", "pixmapRGB", |
setAs("pixmapGrey", "pixmapRGB", |
||
function(from, to){ |
function(from, to){ |
||
Line 1,855: | Line 1,855: | ||
# Convert back to "colour" |
# Convert back to "colour" |
||
plot(p3 <- as(p2, "pixmapRGB"))</ |
plot(p3 <- as(p2, "pixmapRGB"))</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 1,862: | Line 1,862: | ||
I gave up on uploading to Rosetta Code. |
I gave up on uploading to Rosetta Code. |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require racket/draw) |
(require racket/draw) |
||
Line 1,904: | Line 1,904: | ||
(color->gray rosetta) |
(color->gray rosetta) |
||
(gray->color (color->gray rosetta)) |
(gray->color (color->gray rosetta)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,910: | 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. |
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. |
||
<lang |
<syntaxhighlight lang="raku" line>sub MAIN ($filename = 'default.ppm') { |
||
my $in = open($filename, :r, :enc<iso-8859-1>) or die $in; |
my $in = open($filename, :r, :enc<iso-8859-1>) or die $in; |
||
Line 1,928: | Line 1,928: | ||
$in.close; |
$in.close; |
||
$out.close; |
$out.close; |
||
}</ |
}</syntaxhighlight> |
||
Using the .ppm file from the [[Bitmap/Write a PPM file#Raku|Write a PPM file]] task: |
Using the .ppm file from the [[Bitmap/Write a PPM file#Raku|Write a PPM file]] task: |
||
Line 1,936: | Line 1,936: | ||
Note: REXX uses decimal (characters) instead of binary for storing numbers, so there is no rounding (using |
Note: REXX uses decimal (characters) instead of binary for storing numbers, so there is no rounding (using |
||
<br> characters to store numbers is almost the same as using decimal floating point). |
<br> characters to store numbers is almost the same as using decimal floating point). |
||
< |
<syntaxhighlight 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= '00 00 ff'x /*define the blue color (hexadecimal).*/ |
||
@.= blue /*set the entire image to blue color.*/ |
@.= blue /*set the entire image to blue color.*/ |
||
Line 1,951: | Line 1,951: | ||
end /*row*/ /* [↑] D2C convert decimal ───► char*/ |
end /*row*/ /* [↑] D2C convert decimal ───► char*/ |
||
end /*col*/ /* [↑] x%1 is the same as TRUNC(x) */ |
end /*col*/ /* [↑] x%1 is the same as TRUNC(x) */ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight><br><br> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Extending [[Basic_bitmap_storage#Ruby]] |
Extending [[Basic_bitmap_storage#Ruby]] |
||
< |
<syntaxhighlight lang="ruby">class RGBColour |
||
def to_grayscale |
def to_grayscale |
||
luminosity = Integer(0.2126*@red + 0.7152*@green + 0.0722*@blue) |
luminosity = Integer(0.2126*@red + 0.7152*@green + 0.0722*@blue) |
||
Line 1,972: | Line 1,972: | ||
gray |
gray |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class. |
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class. |
||
< |
<syntaxhighlight lang="scala">object BitmapOps { |
||
def luminosity(c:Color)=(0.2126*c.getRed + 0.7152*c.getGreen + 0.0722*c.getBlue+0.5).toInt |
def luminosity(c:Color)=(0.2126*c.getRed + 0.7152*c.getGreen + 0.0722*c.getBlue+0.5).toInt |
||
Line 1,985: | Line 1,985: | ||
image |
image |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">require('Image::Imlib2') |
||
func tograyscale(img) { |
func tograyscale(img) { |
||
Line 2,007: | Line 2,007: | ||
var gscale = tograyscale(image) |
var gscale = tograyscale(image) |
||
gscale.set_quality(80) |
gscale.set_quality(80) |
||
gscale.save(output)</ |
gscale.save(output)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<!-- L = 0.2126·R + 0.7152·G + 0.0722·B --> |
<!-- L = 0.2126·R + 0.7152·G + 0.0722·B --> |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
< |
<syntaxhighlight lang="tcl">package require Tk |
||
proc grayscale image { |
proc grayscale image { |
||
Line 2,024: | Line 2,024: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Photo images are always 8-bits-per-channel RGBA. |
Photo images are always 8-bits-per-channel RGBA. |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Conversion to a grayscale image.<br> |
Conversion to a grayscale image.<br> |
||
< |
<syntaxhighlight lang="vedit">// Convert RGB image to grayscale (8 bit/pixel) |
||
// #10 = buffer that contains image data |
// #10 = buffer that contains image data |
||
// On return: |
// On return: |
||
Line 2,049: | Line 2,049: | ||
Buf_Switch(#10) |
Buf_Switch(#10) |
||
} |
} |
||
Return</ |
Return</syntaxhighlight> |
||
Conversion to a color image.<br> |
Conversion to a color image.<br> |
||
< |
<syntaxhighlight lang="vedit">// Convert grayscale image (8 bits/pixel) into RGB (24 bits/pixel) |
||
// #20 = buffer that contains image data |
// #20 = buffer that contains image data |
||
// On return: |
// On return: |
||
Line 2,071: | Line 2,071: | ||
Buf_Switch(#20) |
Buf_Switch(#20) |
||
} |
} |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
Line 2,077: | Line 2,077: | ||
{{works with|Visual Basic|6}} |
{{works with|Visual Basic|6}} |
||
{{libheader|Win32}} |
{{libheader|Win32}} |
||
< |
<syntaxhighlight lang="vb">Option Explicit |
||
Private Type BITMAP |
Private Type BITMAP |
||
Line 2,133: | Line 2,133: | ||
DeleteDC hdc |
DeleteDC hdc |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Line 2,139: | Line 2,139: | ||
Convert a Bitmap to Grayscale. |
Convert a Bitmap to Grayscale. |
||
< |
<syntaxhighlight lang="vbnet"> |
||
Imports System.Drawing.Imaging |
Imports System.Drawing.Imaging |
||
Line 2,196: | Line 2,196: | ||
Map.UnlockBits(oBMPData) |
Map.UnlockBits(oBMPData) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
{{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. |
This script converts the image [https://rosettacode.org/File:Lenna100.jpg Lenna100.jpg] to grayscale and then displays the two images side by side. |
||
< |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, ImageData |
||
import "dome" for Window |
import "dome" for Window |
||
Line 2,240: | Line 2,240: | ||
} |
} |
||
var Game = PercentageDifference.new(1100, 550, "Lenna100.jpg", "Lenna-grayscale.jpg")</ |
var Game = PercentageDifference.new(1100, 550, "Lenna100.jpg", "Lenna-grayscale.jpg")</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|BASIC256}} |
{{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 |
"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 |
||
< |
<syntaxhighlight lang="yabasic">import image |
||
open window 600,600 |
open window 600,600 |
||
Line 2,264: | Line 2,264: | ||
dot x+300,y+300 |
dot x+300,y+300 |
||
next y |
next y |
||
next x</ |
next x</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 2,273: | Line 2,273: | ||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
{{trans|BASIC256}} |
{{trans|BASIC256}} |
||
< |
<syntaxhighlight lang="zkl">fcn toGrayScale(img){ // in-place conversion |
||
foreach x,y in (img.w,img.h){ |
foreach x,y in (img.w,img.h){ |
||
r,g,b:=img[x,y].toBigEndian(3); |
r,g,b:=img[x,y].toBigEndian(3); |
||
Line 2,279: | Line 2,279: | ||
img[x,y]=((lum*256) + lum)*256 + lum; |
img[x,y]=((lum*256) + lum)*256 + lum; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">img:=PPM.readPPMFile("lena.ppm"); |
||
toGrayScale(img); |
toGrayScale(img); |
||
img.write(File("foo.ppm","wb"));</ |
img.write(File("foo.ppm","wb"));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
http://www.zenkinetic.com/Images/RosettaCode/lenaGray.jpg |
http://www.zenkinetic.com/Images/RosettaCode/lenaGray.jpg |