Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ssadler
Created August 17, 2014 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssadler/316daa219ff412e1b5c6 to your computer and use it in GitHub Desktop.
Save ssadler/316daa219ff412e1b5c6 to your computer and use it in GitHub Desktop.
myReadPixels:: Int -> Int -> Int -> Int -> IO [Pixel]
myReadPixels x y w h = do
let arraySize = w * h * 4
array <- mallocForeignPtrArray arraySize :: IO (ForeignPtr Word8)
let pos = Position (fromIntegral x) (fromIntegral y)
withForeignPtr array $ \ptr -> do
-- fromIntegral is needed because Position and Size store GLints not Ints
readPixels pos
(Size (fromIntegral w) (fromIntegral h))
(PixelData RGBA UnsignedByte ptr)
peekArray arraySize ptr >>= return . pixelGroups
where
pixelGroups (a:b:c:d:xs) = (Pixel a b c d) : pixelGroups xs
pixelGroups [] = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment