Jump to content

Grayscale image: Difference between revisions

<code>
(Vedit macro language added)
(<code>)
Line 8:
 
=={{header|Ada}}==
<code ada>
type Grayscale_Image is array (Positive range <>, Positive range <>) of Luminance;
</adacode>
Conversion to a grayscale image:
<code ada>
function Grayscale (Picture : Image) return Grayscale_Image is
type Extended_Luminance is range 0..10_000_000;
Line 33:
return Result;
end Grayscale;
</adacode>
Conversion to a color image:
<code ada>
function Color (Picture : Grayscale_Image) return Image is
Result : Image (Picture'Range (1), Picture'Range (2));
Line 46:
return Result;
end Color;
</adacode>
 
=={{header|C}}==
Line 52:
Definition for a grayscale image.
 
<code c>
<c>typedef unsigned char luminance;
typedef luminance pixel1[1];
typedef struct {
Line 59 ⟶ 60:
luminance *buf;
} grayimage_t;
typedef grayimage_t *grayimage;</c>
</code>
 
The same as <tt>alloc_img</tt>, but for grayscale images.
 
<code c>
<c>grayimage alloc_grayimg(unsigned int width, unsigned int height)
{
grayimage img;
Line 71 ⟶ 74:
img->height = height;
return img;
}
}</c>
</code>
 
Convert from ''color'' image to ''grayscale'' image.
 
<code c>
<c>grayimage tograyscale(image img)
{
unsigned int x, y;
Line 97 ⟶ 102:
}
return timg;
}
}</c>
</code>
 
And back from a ''grayscale'' image to a ''color'' image.
 
<code c>image tocolor(grayimage img)
{
unsigned int x, y;
Line 122 ⟶ 128:
}
return timg;
}
}</c>
</code>
 
'''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
 
<code c>
<c>#define free_grayimg(IMG) free_img((image)(IMG))</c>
</code>
 
* ''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 183 ⟶ 192:
 
Conversion to a grayscale image:
<code ocaml>
<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 202 ⟶ 212:
done;
done;
(gray_channel)</ocaml>
</code>
 
Conversion to a color image:
<code ocaml>
<ocaml>let to_color ~img:gray_channel =
let width = Bigarray.Array2.dim1 gray_channel
and height = Bigarray.Array2.dim2 gray_channel in
Line 224 ⟶ 236:
r_channel,
g_channel,
b_channel)</ocaml>
</code>
 
=={{header|Vedit macro language}}==
973

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.