<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>Hi,<br>I had posted a note on line drawing algo with Haskell some time back. Now, I am trying to write a PNM image.<br><br>import qualified Data.ByteString as B<br><br>width = 256<br>height = 256<br>bytesInImage = width * height * 3<br>blankImage = B.pack $ take bytesInImage (repeat 0)<br><br>type Color = (Int,Int,Int)<br>setPixel :: B.ByteString -> Int -> Int -> Color -> B.ByteString<br>setPixel image x y (r,g,b) = B.concat [beforePixel, pixel, afterPixel]<br> where<br> beforePixel = B.take before image<br> afterPixel = B.drop (before+3)
image<br> pixel=B.pack [(fromIntegral r),(fromIntegral g),(fromIntegral b)]<br> -- number of bytes before the 3 bytes of<br> -- the pixel at x y<br> before = (y * width * 3) + (x * 3) - 3<br><br>main = do<br> putStrLn "P6"<br> putStrLn ( (show width) ++ " " ++ (show height) )<br> putStrLn "255"<br> -- Set a red pixel at 100 100<br> B.putStr (setPixel blankImage 100 100 (255,0,0))
<br><br><br>Can I please have some review comments on the code above? Would recreating the entire ByteString for each setPixel be an overhead?<br>Also, I am barely beginning to grasp the Monad concept....I was wondering if there could be a monadic style of implementation of this - that could potentially have a series of setPixels inside a do block?<br><br>Regards,<br>Kashyap<br></div></div><br>
</body></html>