                         return x
 
 -- Manipulating image pixels
-setPixel :: (CInt,CInt) -> CDouble -> Image -> IO ()
-setPixel (x,y) v image = withGenImage image $ \img ->
-                          {#call wrapSet32F2D#} img y x v
-
-getPixel :: (CInt,CInt) -> Image -> CDouble
-getPixel (x,y) image = unsafePerformIO $ withGenImage image $ \img ->
-                          {#call wrapGet32F2D#} img y x
+--setPixel :: (CInt,CInt) -> CDouble -> Image c d -> IO ()
+--setPixel (x,y) v image = withGenImage image $ \img ->
+--                          {#call wrapSet32F2D#} img y x v
+
 
 getAllPixels image =  [getPixel (i,j) image 
                       | i <- [0..width-1 ]

import System.IO.Unsafe


data GrayScale
data RGB
data RGBA
data LAB

newtype NewImage channels depth = S Image

unS (S i ) = i -- Unsafe and ugly

withNewImage (S i) op = withImage i op >>= return . S
withGenNewImage (S i) op = withGenImage i op >>= return . S

{#pointer *IplImage as Image foreign newtype#}

foreign import ccall "& wrapReleaseImage" releaseImage :: FinalizerPtr Image

instance NFData Image where
    rnf a@(Image fptr) = (unsafeForeignPtrToPtr) fptr `seq` a `seq` ()-- This might also need peek?


creatingImage fun = do
              iptr <- fun
--              {#call incrImageC#} -- Uncomment this line to get statistics of number of images allocated by ghc
              fptr <- newForeignPtr releaseImage iptr
              return.Image $ fptr

unImage (Image fptr) = fptr

data Tag tp;
rgb = undefined :: Tag RGB
lab = undefined :: Tag LAB


composeMultichannelImageNew :: Maybe (NewImage GrayScale a) -> Maybe (NewImage GrayScale a) -> Maybe (NewImage GrayScale a) -> Maybe (NewImage GrayScale a) -> Tag tp -> NewImage tp a
composeMultichannelImageNew c1 c2 c3 c4 totag = S $ composeMultichannelImage (fmap unS c1) (fmap unS c2) (fmap unS c3) (fmap unS c4)

composeMultichannelImage :: Maybe Image -> Maybe Image -> Maybe Image -> Maybe Image -> Image 
composeMultichannelImage c1 c2 c3 c4 = unsafePerformIO $ do
        res <- createImage32F (size) 4 -- TODO: Check channel count -- This is NOT correct
        withMaybe c1 $ \cc1 -> 
         withMaybe c2 $ \cc2 -> 
          withMaybe c3 $ \cc3 -> 


-- |Create a montage form given images (u,v) determines the layout and space the spacing
--  between images. Images are assumed to be the same size (determined by the first image)
montage :: (Int,Int) -> Int -> [Image c d] -> Image c d
montage (u',v') space' imgs = resultPic
    where
     space = fromIntegral space'
