Haskell gloss: rendering an image for a bitmap

I want to access the pixel data of what is displayed in the window, but I was not lucky to find such a function in glitter or try to call OpenGL readPixels in a callback on the keyboard. It appears that gloss displays the image in a window without displaying the displayed bitmap.

If it's hard to do in gloss, is there an alternative that has real-time high-level raster manipulation (translation, rotation, transparency)?

+6
source share
2 answers

It turns out readPixels can be used in this case. I found this snippet digging #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

+1
source

I once had the same problem, and I could not find a good solution, so my answer would probably be inappropriate. My workaround was to use the bmp package, process the contents of bmp manually (using ByteString ), and then convert it to a glossy bitmap using bitmapOfBMP . For example, it was a function to mix a bitmap with color:

 recolor :: (Float, Float, Float) -> BMP -> BMP recolor (rc, gc, bc) bmp@BMP {bmpRawImageData = raw} = bmp {bmpRawImageData = B.pack $ process $ B.unpack raw} where process (b:g:r:a:xs) = (mul b bc):(mul g gc):(mul r rc):a:process xs process xs = xs mul c cc = round $ cc * fromIntegral c 

That was enough for me at that time, so I stopped finding the best solution. If you find something, share it.

0
source

All Articles