I have an application that uses OpenGL and GLUT to show some stuff on the screen. I'd like to access the colors displayed "pixel by pixel" but I'm having trouble comprehending the methods provided.
It seems the way to access such data is with the function:
readPixels :: Position -> Size -> PixelData a -> IO ()
which is decidedly non-Haskelly as it uses the C pattern of taking a destination pointer and writing to that pointer.
The third argument is the important part, and it is constructed like PixelData PixelFormat DataType (Ptr a)
. PixelFormat
and DataType
are both enums, the former taking values such as RGBAInteger, RGBA, CMYK, etc
and the latter taking values such as UnsignedByte, Short, Float, etc.
The choices are confusing to me, but that's not the real issue. Where I am really struggling with the use of the pointer. I have no idea what kind of Storable
data to malloc
to create the pointer, or how many bytes to allocate if I choose to use mallocBytes
.
I have a function I'm messing around with that currently looks something like this:
pixels :: IO ()
pixels = do
pointer <- mallocBytes 50000
let pdata = PixelData RGBA Int pointer
let pos = Position 0 0
let siz = Size 10 10
readPixels pos siz pdata
print pdata
mypixels <- peek pointer
-- TODO, what kind of data is mypixels?
free pointer
It runs fine I just have no idea how to use the data I am getting from the Ptr
. Maybe I'm not understanding the documentation fully, but how can I determine the type of data at the pointer and how can I use it in my program? Note that my choice of the arguments RGBA
and Int
was arbitrary, they just sounded harmless enough. What I really want is some list or multi-dimensional list of RGBA pixel values in some format (Color 4
or something of that nature). Any help would be greatly appreciated, I seem to be in over my head.
withForeignPtr
, any idea? – Moskow