[Haskell-cafe] Writing a pnm file
CK Kashyap
ck_kashyap at yahoo.com
Mon Aug 3 01:38:47 EDT 2009
Thanks Sebastian,
ppm module is indeed very useful. So, I guess my question then just boils down to, how can I write a function to mimic the setPixel function ->
Basically, a blank white image would look like this (as per ppm module)
[
[ (255, 255, 255) , (255, 255, 255) , (255, 255, 255) ] , -- 3 columns of row 1
[ (255, 255, 255) , (255, 255, 255) , (255, 255, 255) ] --- 3 columns of row 2
]
setPixel x y r g b when called like this - setPixel 0,0,255,0,0
[
[ (255, 0, 0) , (255, 255, 255) , (255, 255, 255) ] , -- 3 columns of row 1
[ (255, 255, 255) , (255, 255, 255) , (255, 255, 255) ] --- 3 columns of row 2
]
What would be a good way to implement such a function?
Regards,
Kashyap
________________________________
From: Sebastian Sylvan <sebastian.sylvan at gmail.com>
To: CK Kashyap <ck_kashyap at yahoo.com>
Cc: haskell-cafe at haskell.org
Sent: Sunday, August 2, 2009 9:30:08 PM
Subject: Re: [Haskell-cafe] Writing a pnm file
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
http://hackage.haskell.org/package/ppm
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
+44(0)7857-300802
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090803/6a877f51/attachment.html
More information about the Haskell-Cafe
mailing list