From: Duncan C. <dun...@wo...> - 2007-11-02 23:44:38
|
Tue Oct 30 11:38:25 PDT 2007 ag...@im... * Add support for getting the stride and image data of an image surface This wraps the cairo functions: cairo_image_surface_get_stride cairo_image_surface_get_data Note that these functions were introduced in cairo 1.2, and so this sets a new high bar for gtk2hs cairo. hunk ./cairo/Graphics/Rendering/Cairo/Internal/Surfaces/Image.chs 26 +{#fun image_surface_get_stride as imageSurfaceGetStride { withSurface* `Surface' } -> `Int'#} +{#fun image_surface_get_data as imageSurfaceGetData { withSurface* `Surface' } -> `(Ptr CUChar)' id#} hunk ./cairo/Graphics/Rendering/Cairo.hs.pp 172 + , imageSurfaceGetStride + , imageSurfaceGetData hunk ./cairo/Graphics/Rendering/Cairo.hs.pp 242 +import Foreign.Ptr (castPtr) +import qualified Data.ByteString as BS hunk ./cairo/Graphics/Rendering/Cairo.hs.pp 1526 --- [_$_] +-- hunk ./cairo/Graphics/Rendering/Cairo.hs.pp 1552 +-- | Get the number of bytes from the start of one row to the start of the +-- next. If the image data contains no padding, then this is equal to +-- the pixel depth * the width. +imageSurfaceGetStride :: Surface -> Render Int +imageSurfaceGetStride = liftIO . Internal.imageSurfaceGetStride + +-- | Return a ByteString of the image data for a surface. In order to remain +-- safe the returned ByteString is a copy of the data. This is a little +-- slower than returning a pointer into the image surface object itself, but +-- much safer +imageSurfaceGetData :: Surface -> IO BS.ByteString +imageSurfaceGetData a = do + height <- Internal.imageSurfaceGetHeight a + stride <- Internal.imageSurfaceGetStride a + ptr <- Internal.imageSurfaceGetData a + BS.copyCStringLen (castPtr ptr, height * stride) + |