File: PixbufData.hs

package info (click to toggle)
haskell-gtk 0.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,964 kB
  • sloc: haskell: 3,346; ansic: 826; makefile: 161
file content (72 lines) | stat: -rw-r--r-- 2,508 bytes parent folder | download | duplicates (9)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses #-}
{-# OPTIONS_HADDOCK hide #-}
-- -*-haskell-*-
--  GIMP Toolkit (GTK) Pixbuf as Array
--
--  Author : Ciancia, Axel Simon
--
--  Created: 26 March 2002
--
--  Copyright (C) 2002-2005 Axel Simon
--
--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Lesser General Public
--  License as published by the Free Software Foundation; either
--  version 2.1 of the License, or (at your option) any later version.
--
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Lesser General Public License for more details.
--
-- |
-- Maintainer  : gtk2hs-users@lists.sourceforge.net
-- Stability   : provisional
-- Portability : no (uses MTC, depends on internal GHC module)
--
-- 'PixbufData' exposes 'Pixbuf's as mutable array.
--

-- #hide
module Graphics.UI.Gtk.Gdk.PixbufData (
  PixbufData,
  mkPixbufData
  ) where

import System.Glib.FFI
import Graphics.UI.Gtk.Types
-- internal module of GHC
import Data.Array.Base ( MArray(..), newArray_, unsafeRead, unsafeWrite,
                         getBounds, getNumElements )

-- | An array that stored the raw pixel data of a 'Pixbuf'.
--
-- * See 'Graphics.UI.Gtk.Gdk.Pixbuf.pixbufGetPixels'.
--
data PixbufData i e = PixbufData !Pixbuf
                  {-# UNPACK #-} !(Ptr e)
                                 !(i,i)
                  {-# UNPACK #-} !Int

mkPixbufData :: Storable e => Pixbuf -> Ptr e -> Int -> PixbufData Int e
mkPixbufData pb (ptr :: Ptr e) size =
  PixbufData pb ptr (0, count) count
  where count = fromIntegral (size `div` sizeOf (undefined :: e))

-- | 'PixbufData' is a mutable array.
instance Storable e => MArray PixbufData e IO where
  newArray (l,u) e = error "Gtk.Gdk.Pixbuf.newArray: not implemented"
  newArray_ (l,u)  = error "Gtk.Gdk.Pixbuf.newArray_: not implemented"
  {-# INLINE unsafeRead #-}
  unsafeRead (PixbufData (Pixbuf pb) pixPtr _ _) idx = do
      e <- peekElemOff pixPtr idx
      touchForeignPtr pb
      return e
  {-# INLINE unsafeWrite #-}
  unsafeWrite (PixbufData (Pixbuf pb) pixPtr _ _) idx elem = do
      pokeElemOff pixPtr idx elem
      touchForeignPtr pb
  {-# INLINE getBounds #-}
  getBounds (PixbufData _ _ bd _) = return bd
  {-# INLINE getNumElements #-}
  getNumElements (PixbufData _ _ _ count) = return count