RosettaCodeData/Task/Grayscale-image/ATS/grayscale-image-1.ats

79 lines
2.1 KiB
Plaintext

#define ATS_PACKNAME "Rosetta_Code.bitmap_grayscale_task"
staload "bitmap_task.sats"
(*------------------------------------------------------------------*)
(* Here is a type for 8-bit grayscale pixels. It is analogous to the
rgb24 type defined in bitmap_task.sats. A gray8 is the size of a
uint8. (It is, in fact, a uint8, but here that fact is hidden, so
the ATS2 template and overload systems will know to treat gray8 as
a distinct type.) *)
abst@ype gray8 = uint8
fn {tk : tkind}
gray8_make_uint : g0uint tk -<> gray8
fn {tk : tkind}
gray8_make_int : g0int tk -<> gray8
fn {}
gray8_value : gray8 -<> uint8
overload gray8_make with gray8_make_uint
overload gray8_make with gray8_make_int
(*------------------------------------------------------------------*)
(* Pixel conversions. *)
fn {}
rgb24_to_gray8 : rgb24 -<> gray8 (* This is a lossy conversion. *)
fn {}
gray8_to_rgb24 : gray8 -<> rgb24
fn {}
rgb24_to_rgb24 : rgb24 -<> rgb24
fn {}
gray8_to_gray8 : gray8 -<> gray8
(*------------------------------------------------------------------*)
(* What follows is actually a general pixmap conversion mechanism,
not just one for conversions between gray8 and rgb24 pixels.
There are several ways to tell the function how to convert a
pixel. These methods include passing a function or any of the
different kinds of closure. However, instead I will do it with the
template system.
To wit: when calling pixmap_convert<a,b> one must have an
implementation of pixmap$pixel_convert<a,b> within the scope of the
call.
Note that pixmap_convert<a,a> can COPY a pixmap, although faster
implementations of copying may be possible. *)
fn {a, b : t@ype}
pixmap_convert_copy :
{w, h : int}
(!pixmap (a, w, h),
&array (b?, w * h) >> array (b, w * h)) ->
void
fn {a, b : t@ype}
pixmap_convert_alloc :
{w, h : int}
(!pixmap (a, w, h)) ->
[p : addr | null < p]
@(mfree_gc_v p | pixmap (b, w, h, p))
fn {a, b : t@ype}
pixmap$pixel_convert :
a -> b
overload pixmap_convert with pixmap_convert_copy
overload pixmap_convert with pixmap_convert_alloc
(*------------------------------------------------------------------*)