[Haskell-cafe] Writing a pnm file

Sebastian Sylvan sebastian.sylvan at gmail.com
Sun Aug 2 12:00:08 EDT 2009

On Sun, Aug 2, 2009 at 4:00 PM, CK Kashyap <ck_kashyap at yahoo.com> wrote:

> Hi,
> Now that I've understood how to generate raster points of a line in Haskell
> - the next thing I want to do is generate a pnm file with it. I've done it
> in perl as of now. In perl, I can have a scalar variable $x contain a string
> of 256*256*3 bytes (for 24-bit 256x256 image) and set pixels using substr on
> LHS. I was wondering how I could do something similar in Haskell?
> sub setPixel{
> my($x,$y,$red,$green,$blue)=@_;
> my$pixel=pack "CCC",$red,$green,$blue;
> my$offset=$WIDTH*$y*3 + $x*3;
> substr($image,$offset,3) = $pixel;
> }

There's a library on hackage which does this

You can install this by doing
>cabal install ppm

Here's an example usage (this uses the binary version of ppm, the docs for
ppm has an example for the ASCII version):

writePPM fname img = withBinaryFile fname WriteMode (\h -> hPutStr h (ppm_p6
img) )

If you're looking for the learning experience, you could always read the
source for the library (which is pretty tiny).

Sebastian Sylvan
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090802/5b7e59eb/attachment.html

More information about the Haskell-Cafe mailing list