[Haskell-cafe] Writing a pnm file

Sebastian Sylvan sebastian.sylvan at gmail.com
Mon Aug 3 05:44:47 EDT 2009


On Mon, Aug 3, 2009 at 6:38 AM, CK Kashyap <ck_kashyap at yahoo.com> wrote:

>  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?
>

Well you could start by writing a function like:

adjustElem :: Int -> ( a -> a ) -> [a] -> [a]

That would basically apply a function to a specific element in a list
(indexed by the first parameter). Look at splitAt in Data.List, it may be
useful.
Then you can use this in a nested way, by calling adjustElem to modify the
row you're interested in, and the function you pass in to adjust that row
would in turn call adjustElem on the specific pixel in that row).

However, this may be very slow. If you don't care about speed it'll work
fine, but if you really do want to build up an image by successive
single-pixel modifications, you should consider first using an Array and
accumArray, this will be much faster as internally accumArray can use a
mutable array (while the external interface is still pure).


Sebastian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090803/98808b13/attachment.html


More information about the Haskell-Cafe mailing list