[Haskell-cafe] Re: creating graphics the functional way

apfelmus apfelmus at quantentunnel.de
Mon Aug 6 04:37:42 EDT 2007

Frank Buss wrote:
> I've created a small program to compose images with combinators:
> http://www.frank-buss.de/haskell/OlympicRings.hs.txt
> Finally, what do you think about using this concept for generating
> images? It has some advantages, e.g. it is possible to scale the
> image without quality loss. But it needs some improvement, e.g. the
> anti-aliasing doesn't look very smooth. And it is very slow, it
> needs about 40 seconds on my computer to calculate the image.

The idea of representing images simply by a function

  Int -> Int -> RGB

is great :) You may want to look at  Pan  and its various offsprings, in
particular  Pancito


Unfortunately, there's not much you can do about the speed.  Pan  is
faster, but it creates the illusion that you're programming in Haskell
while internally, it compiles the image generation code to C++. Very
clever, but hard to maintain and one of the reasons why it only works on

> There are many functions like circle1Center, circle2Center, ... Is it
> possible to rewrite the program that it will be shorter, maybe using lists
> or an interpreter for a language for this kind of combinator programming
> style?

Well, you have lists for that

  type Point = (Int,Int)

  positions :: [Point]
  positions =
    zip [0.5 + fromIntegral n * dx | n <- [-2..2]] (cycle [y1,y2])
    dx = 0.125
    y1 = 0.15
    y2 = 0.25

  colors :: [RGB]
  colors = [blue, yellow, black, green, red]

  type Image = Point -> RGB

  circles :: RGB -> [Image]
  circles background = map circle (zip positions colors)
    circle (p,c) =
       fillMask (translate p ringCenter) c
       $ fillMask (translate p ringOutline) white background

> Is it possible to write functions with an arbitrary number of arguments?
> Would be nice if the average function would accept any number of pixel
> values.

Lists are the natural choice here.

> Is there a PNG writer library for Haskell? I've seen a zlib interface,
> should be not too difficult to implement it in Haskell itself.

Not that I know of. But gtk2hs has a Cairo-binding and I guess this one
supports PNG. Note that this is vector graphics though, your approach is
more general.


More information about the Haskell-Cafe mailing list