Hi Chris,

On 13.04.2012, at 22:06, Chris Moline wrote:

> Hi, I am slowly working on improving HOpenCV and to support displaying
> IplImages using Gtk I needed pixbufNewFromData. I've attached a patch
> but I'm not sure that I've implemented it correctly. Thanks for your
> time.

+pixbufNewFromData :: Ptr CUChar -> Colorspace -> Bool -> Int -> Int - 
 > Int -> Int -> IO Pixbuf
+pixbufNewFromData imData cSpace hasAlpha bitsPerSample width height  
rowStride
+  = wrapNewGObject mkPixbuf $
+     {#call pixbuf_new_from_data #}
+       imData
+       (fromIntegral . fromEnum $ cSpace)
+       (fromBool hasAlpha)
+       (fromIntegral bitsPerSample)
+       (fromIntegral width)
+       (fromIntegral height)
+       (fromIntegral rowStride)
+       nullFunPtr nullPtr


I think there is a problem here in that you pass a simple pointer to  
the C function which takes ownership of the memory. In order for your  
program not to crash you have to ensure that the pointer remains valid  
until the program exists.

We haven't bound this function since it wasn't clear how you can  
always ensure that the passed in data is freed without forcing the  
user to commit to a specific way of allocating it (e.g. with  
malloc() ). When keeping the function agnostic to the way the buffer  
was allocated, it is necessary to pass in a custom free function.  
Could you try to change the function as follows:

{#pointer GdkPixbufDestroyNotify as PixbufDestroyNotify#}

foreign import ccall "wrapper" mkPixbufDestroyNotifyPtr :: (Ptr CUChar  
-> IO ()) -> IO PixbufDestroyNotify

+pixbufNewFromData :: Ptr CUChar -> PixbufDestroyNotify -> Colorspace - 
 > Bool -> Int -> Int -> Int -> Int -> IO Pixbuf
+pixbufNewFromData imData free cSpace hasAlpha bitsPerSample width  
height rowStride
+  = wrapNewGObject mkPixbuf $
+     {#call pixbuf_new_from_data #}
+       imData
+       (fromIntegral . fromEnum $ cSpace)
+       (fromBool hasAlpha)
+       (fromIntegral bitsPerSample)
+       (fromIntegral width)
+       (fromIntegral height)
+       (fromIntegral rowStride)
+       free nullPtr


I hope this compiles and works :-)

Cheers,
Axel


------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Gtk2hs-devel mailing list
Gtk2hs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gtk2hs-devel

Reply via email to