Grayscale image: Difference between revisions

m
<lang>
(<code>)
m (<lang>)
Line 8:
 
=={{header|Ada}}==
<codelang ada>
type Grayscale_Image is array (Positive range <>, Positive range <>) of Luminance;
</codelang>
Conversion to a grayscale image:
<codelang ada>
function Grayscale (Picture : Image) return Grayscale_Image is
type Extended_Luminance is range 0..10_000_000;
Line 33:
return Result;
end Grayscale;
</codelang>
Conversion to a color image:
<codelang ada>
function Color (Picture : Grayscale_Image) return Image is
Result : Image (Picture'Range (1), Picture'Range (2));
Line 46:
return Result;
end Color;
</codelang>
 
=={{header|C}}==
Line 52:
Definition for a grayscale image.
 
<codelang c>
typedef unsigned char luminance;
typedef luminance pixel1[1];
Line 61:
} grayimage_t;
typedef grayimage_t *grayimage;
</codelang>
 
The same as <tt>alloc_img</tt>, but for grayscale images.
 
<codelang c>
grayimage alloc_grayimg(unsigned int width, unsigned int height)
{
Line 75:
return img;
}
</codelang>
 
Convert from ''color'' image to ''grayscale'' image.
 
<codelang c>
grayimage tograyscale(image img)
{
Line 103:
return timg;
}
</codelang>
 
And back from a ''grayscale'' image to a ''color'' image.
 
<lang c>
<code c>image tocolor(grayimage img)
{
unsigned int x, y;
Line 129 ⟶ 130:
return timg;
}
</codelang>
 
'''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
 
<codelang c>
#define free_grayimg(IMG) free_img((image)(IMG))
</codelang>
 
* ''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 192 ⟶ 193:
 
Conversion to a grayscale image:
<codelang ocaml>
let to_grayscale ~img:(_, r_channel, g_channel, b_channel) =
let width = Bigarray.Array2.dim1 r_channel
Line 213 ⟶ 214:
done;
(gray_channel)
</codelang>
 
Conversion to a color image:
<codelang ocaml>
let to_color ~img:gray_channel =
let width = Bigarray.Array2.dim1 gray_channel
Line 237 ⟶ 238:
g_channel,
b_channel)
</codelang>
 
=={{header|Vedit macro language}}==
973

edits