title: PNG to BMP in Haskell (for Gloss)
body: <p>I have PNG files and <a href="http://hackage.haskell.org/packages/archive/gloss/1.7.4.1/doc/html/Graphics-Gloss-Data-Picture.html#t%3aBitmapData" rel="nofollow" title="the Gloss library">the Gloss library</a> has a <code>Bitmap</code> constructor for <code>Picture</code>. I can't use <code>loadBMP :: FilePath -&gt; IO Picture</code> because of the filetype, so I'm searching how to load a PNG file, convert it to BMP, and feed it to either  <code>bitmapOfBMP :: BMP -&gt; Picture</code>, <code>bitmapOfForeignPtr :: Int -&gt; Int -&gt; ForeignPtr Word8 -&gt; Bool -&gt; Picture</code> or <code>bitmapOfByteString :: Int -&gt; Int -&gt; ByteString -&gt; Bool -&gt; Picture</code>.</p>
  
  <hr>
  
  <p>Test with JuicyPixels</p>
  
  <pre><code>import Data.ByteString as B
  import System.IO as A
  
  import Codec.Picture.Png
  import Graphics.Gloss.Interface.Pure.Game
  
  
  main = do
      png &lt;- B.readFile "samus.png"
      let img = decodePng png
      case img of
          Left x -&gt; A.putStrLn x
          Right x -&gt; do
              let bmp = encodeDynamicPng x
              case bmp of
                  Left x -&gt; A.putStrLn x
                  Right x -&gt; do
                      let pic = bitmapOfByteString 29 52 x True
                      game pic
  
  game pic
      =  play
          (InWindow "Test" (700, 500) (10, 10))
          white
          30
          pic
          draw
          (const id)
          (const id)
  
  draw bmp
      = bmp
  </code></pre>
  
  <p>Everything succeeds but the image is not the same at all.</p>