data Pic -- Pictures
= NoPic -- No picture
| Rect Color Bool Int Int -- Rectangle
| Arc Color Bool Int Int Int Int -- Arc
| Poly Color Bool [(Int, Int)] -- Polygon
| PicText Color Int String -- Text
| PicImage Pixbuf -- Image
| Bitmap Int Int (Int -> Int -> Color) -- Bitmap
| Trans Int Int Pic -- Linear translation
| Super Pic Pic -- Superposition
This datatype is declared in a Pic library module that, like the standard prelude,
is implicitly imported into every module.
Basic shapes
Rectangles, elliptic arcs and polygons are created by theRect, Arc
and Poly constructors of the datatype. For example:
Superposition and linear translation
Pictures may be superimposed and translated using theSuper and Trans
constructors. For example:
Using these constructors more complex pictures can readily be constructed. For example:
Text
Strings (as pictures) are created using thePicText constructor. For example:
Arbitrary graphics
Arbitrary graphical images may be represented as pictures (ie, values of typePic) in Pivotal in
two different ways:
- Using the
PicImageconstructor. This represents a picture using the host platform's low-level representation (a one-dimensional byte array). This representation is fast but relatively inflexible. - Using the
Bitmapconstructor. This represents a picture as a bitmap function of typeInt -> Int -> Colorthat maps a pixel's x and y coordinates to its colour. This representation allows arbitrary bitmap operations to be performed (but with some loss of speed).
Images
Images (represented by the primitive ADT,Pixbuf)
may be copied from disc files (in .jpg, .gif, or .png format) using the
function
readImage :: FilePath -> Pic
and may be scaled in size using the function
scaleImage :: Double -> Image -> Image
(Both functions are defined in the Pic module.) For example:
Bitmaps
Bitmapped pictures are constructed using theBitmap constructor (see datatype definition above).
This takes the dimensions of the required picture and a colour-valued function on the X and Y coordinates,
and yields a picture. For example:
(The red/green/blue components of colours are represented in Pivotal by integers of type Word16
and hence the Data.Word library module is imported to allow arithmetic on this type.)
Convertion of images to bitmaps
Pictures represented as images (ie, those formed using thePicImage constructor) may be
converted to pictures represented as bitmaps (ie, those formed using the Bitmap constructor)
using the function
imageToBitmap :: Pic -> Pic
This allows images to be manipulated. For example, in the following document, the width, height and bitmap
function of a picture are extracted and then used to form pictures using variants of the bitmap:
Pointwise operations on bitmaps
A variety of operations may be carried out by mapping functions pointwise over one or more bitmaps. For example, the colour balance of an image may be altered:
Likewise, two bitmaps can be combined by taking the darker (ie, lower numerical value) of each of
their red, green and blue components. As a (somewhat contrived!) example:
Timing
The Pivotal system, running on a stock 2.4GHz Pentium 4 pc, takes about 2 seconds to compile and render thePicBitmaps.hs document in this section and about 3 seconds for the PicConvert3.hs one.