forked from diagrams/diagrams-cairo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ptr.hs
43 lines (33 loc) · 1.47 KB
/
Ptr.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Diagrams.Backend.Cairo.Ptr where
import Data.Word (Word8)
import Diagrams.Prelude (Diagram, R2, SizeSpec2D (..), renderDia)
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.Internal
import Foreign.ForeignPtr.Safe (ForeignPtr, newForeignPtr)
import Foreign.Marshal.Alloc (finalizerFree)
import Foreign.Marshal.Array (mallocArray, pokeArray)
import Foreign.Ptr (Ptr, castPtr)
import Graphics.Rendering.Cairo ( Format (..)
, formatStrideForWidth
, renderWith
, withImageSurfaceForData
)
-- | Render a diagram to a new buffer in memory, with the format ARGB32.
renderPtr :: Int -> Int -> Diagram Cairo R2 -> IO (Ptr Word8)
renderPtr w h d = do
let stride = formatStrideForWidth FormatARGB32 w
size = stride * h
opt = CairoOptions
{ cairoSizeSpec = Dims (fromIntegral w) (fromIntegral h)
, cairoOutputType = RenderOnly
, cairoBypassAdjust = False
, cairoFileName = ""
}
(_, r) = renderDia Cairo opt d
b <- mallocArray size
pokeArray b (replicate size 0)
withImageSurfaceForData b FormatARGB32 w h stride (`renderWith` r)
return (castPtr b)
-- | Like 'renderPtr' but automatically garbage collected by Haskell.
renderForeignPtr :: Int -> Int -> Diagram Cairo R2 -> IO (ForeignPtr Word8)
renderForeignPtr w h d = renderPtr w h d >>= newForeignPtr finalizerFree