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.
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:
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.
A new combination is:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With