15

I am playing around with haskell, starting with simple plotting programs to wet my feet. I need a library that will let me save a 2D array/vector to an image file. I don't want to write a list of colors. I want to use containers that are meant for array/vector like computations and can be (well, almost) automagically parallelized.

EDIT Ability to store color images is a must.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
rpg
  • 975
  • 1
  • 8
  • 18
  • See http://stackoverflow.com/questions/6006304/what-haskell-representation-is-recommended-for-2d-unboxed-pixel-arrays-with-mill/6007139 – Don Stewart May 21 '11 at 16:49

4 Answers4

8

I'd start with PGM library. This is a very simple uncompressed graymap format. Almost no additinal dependencies. You can convert PGM to other formats with ImageMagick or other tools.

PGM supports generic IArray interface, and should work with most of the standard Haskell arrays. You can easily parallelize array computations with Control.Parallel.Strategies.

PGM usage example:

ghci> :m + Data.Array Graphics.Pgm 
ghci> let a = accumArray (+) 0 ((0::Int,0::Int),(127,127)) [ ((i,i), 1.0::Double) | i <- [0..127] ]
ghci> arrayToFile "t.pgm" (fmap round a)

And this is the image:

t.pgm

Otherwise you may use Codec-Image-DevIL which can save unboxed arrays to many of the image formats. You'll need DevIL library too. And you'll need to convert all arrays to that particular type of them (UArray (Int, Int, Int) Word8).

Finally, if you want bleeding edge, you may consider repa parallel arrays and corresponding repa-io library, which can write them to BMP images. Unfortunately, today repa is not yet buildable with the new GHC 7.0.2 and doesn't give performance advantages on old GHC 6.12.

sastanin
  • 40,473
  • 13
  • 103
  • 130
  • Thanks for this. I need color images, so I'd look towards the devil functions. – rpg Mar 04 '11 at 15:54
  • 1
    @rpg [Codec.Image.PPM](http://hackage.haskell.org/packages/archive/ppm/latest/doc/html/Codec-Image-PPM.html) supports color. But one of the great things about the NetPBM formats is that you don't need a library to write them; they're simple enough to just open-code anywhere. – ephemient Mar 04 '11 at 17:31
6

A new combination is:

  • repa; for n-dimensional arrays, plus
  • repa-devil, for image loading in dozens of formats.

Repa is the only widely used array library that is automatically parallelized.

An example, from the repa tutorial, using readImage and writeImage, to read an image, rotate it, and write it back out, in whatever format:

import System.Environment
import Data.Word
import Data.Array.Repa hiding ((++))
import Data.Array.Repa.IO.DevIL

main = do
    [f] <- getArgs
    runIL $ do
        v   <- readImage f
        writeImage ("flip-"++f) (rot180 v)

rot180 :: Array DIM3 Word8 -> Array DIM3 Word8
rot180 g = backpermute e flop g
    where
        e@(Z :. x :. y :. _)   = extent g

        flop (Z :. i         :. j         :. k) =
             (Z :. x - i - 1 :. y - j - 1 :. k)
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • repa's interoperability with the rest of ecosystem is nice. Now just add SIMD and it will be the greatest thing since sliced bread. – rpg May 22 '11 at 07:04
2

The more recent JuicyPixels library let you save image to Jpg/Png/Tiff easily, you can use it in combination with Repa with the JuicyPixels-repa library.

Raoul Supercopter
  • 5,076
  • 1
  • 34
  • 37
1

You might also want to check out Diagrams

Example code for the dragon fractal:

{- Heighway dragon.  See http://en.wikipedia.org/wiki/Dragon_curve. -}
module Main where

import Graphics.Rendering.Diagrams
import Control.Monad.State
import Data.Maybe

dragonStr :: Int -> String
dragonStr 0 = "FX"
dragonStr n = concatMap rules $ dragonStr (n-1)
  where rules 'X' = "X+YF+"
        rules 'Y' = "-FX-Y"
        rules c = [c]

strToPath :: String -> Path
strToPath s = pathFromVectors . catMaybes $ evalState c (0,-1)
  where c        = mapM exec s
        exec 'F' = Just `fmap` get
        exec '-' = modify left >> return Nothing
        exec '+' = modify right >> return Nothing
        exec _   = return Nothing
        left (x,y)  = (-y,x)
        right (x,y) = (y,-x)

dragon :: Int -> Diagram
dragon = lc red . curved 0.8 . strToPath . dragonStr

main = renderAs PNG "dragon.png" (Width 300) (dragon 12)
Theo Belaire
  • 2,980
  • 2
  • 22
  • 33