-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.X11.Xlib.Image
-- Copyright   :  (c) Frederik Eaton 2006
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org, frederik@ofb.net
-- Stability   :  provisional
-- Portability :  portable
--
-- Xlib image routines
--
-----------------------------------------------------------------------------

module Graphics.X11.Xlib.Image(
        Image,
        createImage,
        putImage,
        destroyImage,
        getImage,
        xGetPixel,
        getPixel
        ) where

import Graphics.X11.Types
import Graphics.X11.Xlib.Types

import Foreign (Ptr, throwIfNull)
import Foreign.C.Types

import System.IO.Unsafe (unsafePerformIO)

----------------------------------------------------------------
-- Image
----------------------------------------------------------------

-- | interface to the X11 library function @XCreateImage()@.
createImage :: Display -> Visual -> CInt -> ImageFormat -> CInt -> Ptr CChar -> Dimension -> Dimension -> CInt -> CInt -> IO Image
createImage :: Display
-> Visual
-> CInt
-> CInt
-> CInt
-> Ptr CChar
-> Dimension
-> Dimension
-> CInt
-> CInt
-> IO Image
createImage Display
display Visual
vis CInt
depth CInt
format CInt
offset Ptr CChar
dat Dimension
width Dimension
height CInt
bitmap_pad CInt
bytes_per_line = do
    Ptr Image
image <- forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwIfNull String
"createImage" (Display
-> Visual
-> CInt
-> CInt
-> CInt
-> Ptr CChar
-> Dimension
-> Dimension
-> CInt
-> CInt
-> IO (Ptr Image)
xCreateImage Display
display Visual
vis CInt
depth CInt
format CInt
offset Ptr CChar
dat Dimension
width Dimension
height CInt
bitmap_pad CInt
bytes_per_line)
    forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Image -> Image
Image Ptr Image
image)
foreign import ccall unsafe "HsXlib.h XCreateImage"
    xCreateImage :: Display -> Visual -> CInt -> ImageFormat -> CInt ->
        Ptr CChar -> Dimension -> Dimension -> CInt -> CInt -> IO (Ptr Image)

-- | interface to the X11 library function @XPutImage()@.
foreign import ccall unsafe "HsXlib.h XPutImage"
    putImage :: Display -> Drawable -> GC -> Image ->
        Position -> Position -> Position -> Position  -> Dimension -> Dimension -> IO ()

-- | interface to the X11 library function @XDestroyImage()@.
foreign import ccall unsafe "HsXlib.h XDestroyImage"
    destroyImage :: Image -> IO ()

-- | interface to the X11 library function @XGetImage()@.
getImage :: Display -> Drawable -> CInt -> CInt -> CUInt -> CUInt -> CULong -> ImageFormat -> IO Image
getImage :: Display
-> Drawable
-> CInt
-> CInt
-> CUInt
-> CUInt
-> CULong
-> CInt
-> IO Image
getImage Display
display Drawable
d CInt
x CInt
y CUInt
width CUInt
height CULong
plane_mask CInt
format = do
    Ptr Image
image <- forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwIfNull String
"getImage" (Display
-> Drawable
-> CInt
-> CInt
-> CUInt
-> CUInt
-> CULong
-> CInt
-> IO (Ptr Image)
xGetImage Display
display Drawable
d CInt
x CInt
y CUInt
width CUInt
height CULong
plane_mask CInt
format)
    forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Image -> Image
Image Ptr Image
image)

foreign import ccall unsafe "HsXlib.h XGetImage"
    xGetImage :: Display -> Drawable -> CInt -> CInt -> CUInt -> CUInt -> CULong -> ImageFormat -> IO (Ptr Image)

foreign import ccall unsafe "HsXlib.h XGetPixel"
    xGetPixel :: Image -> CInt -> CInt -> IO CULong

-- | interface to the X11 library function @XGetPixel()@.
getPixel :: Image -> CInt -> CInt -> CULong
getPixel :: Image -> CInt -> CInt -> CULong
getPixel Image
i CInt
x CInt
y = forall a. IO a -> a
unsafePerformIO (Image -> CInt -> CInt -> IO CULong
xGetPixel Image
i CInt
x CInt
y)

{- don't need XInitImage since Haskell users probably won't be setting
members of the XImage structure themselves -}
-- XInitImage omitted

-- XGetSubImage omitted