<br><br><div class="gmail_quote">On Sun, Aug 2, 2009 at 4:00 PM, CK Kashyap <span dir="ltr">&lt;<a href="mailto:ck_kashyap@yahoo.com">ck_kashyap@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div style="font-family:&#39;times new roman&#39;, &#39;new york&#39;, times, serif;font-size:12pt"><div>Hi,</div><div>Now that I&#39;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&#39;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?</div>
<div><br></div><div><div>sub setPixel{</div><div><span style="white-space:pre">        </span>my($x,$y,$red,$green,$blue)=@_;</div><div><span style="white-space:pre">        </span>my$pixel=pack &quot;CCC&quot;,$red,$green,$blue;</div>
<div><span style="white-space:pre">        </span>my$offset=$WIDTH*$y*3 + $x*3;</div><div><span style="white-space:pre">        </span>substr($image,$offset,3) = $pixel;</div><div>}</div></div></div></div></blockquote><div><br></div><div>
There&#39;s a library on hackage which does this</div><div><a href="http://hackage.haskell.org/package/ppm">http://hackage.haskell.org/package/ppm</a></div><div><br></div><div>You can install this by doing</div><div>&gt;cabal install ppm</div>
<div><br></div><div>Here&#39;s an example usage (this uses the binary version of ppm, the docs for ppm has an example for the ASCII version):</div><div><br></div><div>writePPM fname img = withBinaryFile fname WriteMode (\h -&gt; hPutStr h (ppm_p6 img) ) </div>
<div><br></div><div>If you&#39;re looking for the learning experience, you could always read the source for the library (which is pretty tiny).</div></div><br>-- <br>Sebastian Sylvan<br>+44(0)7857-300802<br>UIN: 44640862<br>