Graphics in Pivotal


Graphics (or "pictures") are defined in terms of the datatype:
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 the Rect, Arc and Poly constructors of the datatype. For example: PicShapes.hs

Superposition and linear translation

Pictures may be superimposed and translated using the Super and Trans constructors. For example: PicSuper.hs

Using these constructors more complex pictures can readily be constructed. For example:

Text

Strings (as pictures) are created using the PicText constructor. For example:

Arbitrary graphics

Arbitrary graphical images may be represented as pictures (ie, values of type Pic) in Pivotal in two different ways: These two complementary representations are described below.

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 the Bitmap 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: PicBitmaps.hs (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 the PicImage 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: PicConvert3.hs

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: PicColorChange.hs 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: PicCombine.hs

Timing

The Pivotal system, running on a stock 2.4GHz Pentium 4 pc, takes about 2 seconds to compile and render the PicBitmaps.hs document in this section and about 3 seconds for the PicConvert3.hs one.