Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell gloss: render Picture to Bitmap

Tags:

haskell

gloss

I want to access the pixel data of what is being displayed to the window, but I have not had any luck finding such a function in gloss, nor by attempting to call OpenGL readPixels in a keyboard event callback. It looks like gloss renders a Picture to the window without exposing the rendered bitmap.

If this is hard to do in gloss, is there an alternative which has realtime high-level bitmap manipulation (translation, rotation, transparency)?

like image 656
Chris Wendt Avatar asked Mar 03 '14 10:03

Chris Wendt


2 Answers

It turns out readPixels can be used in this case. I found this snippet while digging through #haskell chat logs:

-- save a screenshot to a handle as binary PPM
snapshotWith :: (BS.ByteString -> IO b) -> Position -> Size -> IO b
snapshotWith f p0 vp@(Size vw vh) = do
  let fi q = fromIntegral q
      p6 = "P6\n" ++ show vw ++ " " ++ show vh ++ " 255\n"
  allocaBytes (fi (vw*vh*3)) $ \ptr -> do
    readPixels p0 vp $ PixelData RGB UnsignedByte ptr
    px <- BSI.create (fi $ vw * vh * 3) $ \d -> forM_ [0..vh-1] $ \y ->
      BSI.memcpy
        (d`plusPtr`fi(y*vw*3))
        (ptr`plusPtr`fi ((vh-1-y)*vw*3))
        (fi(vw*3))
    f $ BS.pack (map (toEnum . fromEnum) p6) `BS.append` px

writeSnapshot :: FilePath -> Position -> Size -> IO ()
writeSnapshot f = snapshotWith (BS.writeFile f)

From https://gitorious.org/maximus/mandulia/source/58695617c322b0b37ec72f9a0bd3eed8308bf700:src/Snapshot.hs

like image 157
Chris Wendt Avatar answered Oct 13 '22 00:10

Chris Wendt


Recently a new package gloss-export was released with the appropriate purpose:

Export gloss pictures as PNG, Bitmap, TGA, TIFF, Gif

like image 26
AleXoundOS Avatar answered Oct 13 '22 01:10

AleXoundOS