    {-# 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 :: forall e. Storable e => Pixbuf -> Ptr e -> Int -> PixbufData Int e
    mkPixbufData Pixbuf
    pb (Ptr e
    ptr :: Ptr e) Int
    size =
      Pixbuf -> Ptr e -> (Int, Int) -> Int -> PixbufData Int e
    forall i e. Pixbuf -> Ptr e -> (i, i) -> Int -> PixbufData i e
    PixbufData Pixbuf
    pb Ptr e
    ptr (Int
    0, Int
    count) Int
    count
      where count :: Int
    count = Int -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    size Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `div` e -> Int
    forall a. Storable a => a -> Int
    sizeOf (e
    forall a. HasCallStack => a
    undefined :: e))

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