                         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 ]


-- Load Image as grayscale image.

loadImage n = do
              exists <- fileExist n
              if not exists then return Nothing
                            else do
                              i <- withCString n $ \name -> 
                                     creatingImage ({#call cvLoadImage #} name (0))
                              bw <- imageTo32F i
                              return $ Just bw

loadImageNew :: FilePath -> IO (Maybe (NewImage GrayScale Double))
loadImageNew n = do
              exists <- fileExist n
              if not exists then return Nothing
                            else do
                              i <- withCString n $ \name -> 
                                     creatingImage ({#call cvLoadImage #} name (0))
                              bw <- imageTo32F i
                              return . Just . S $ bw

loadColorImageNew :: FilePath -> IO (Maybe (NewImage RGB Double))
loadColorImageNew n = do
              exists <- fileExist n
              if not exists then return Nothing
                            else do
                              i <- withCString n $ \name -> 
                                     creatingImage ({#call cvLoadImage #} name 1)
                              bw <- imageTo32F i
                              return . Just . S  $ bw

getSizeNew :: (Integral a, Integral b) => NewImage c d -> (a,b)
getSizeNew image = unsafePerformIO $ withImage (unS image) $ \i -> do
                 w <- {#call getImageWidth#} i
                 h <- {#call getImageHeight#} i
                 return (fromIntegral w,fromIntegral h)

loadColorImage n = do
              exists <- fileExist n
              if not exists then return Nothing
                            else do
                              i <- withCString n $ \name -> 
                                     creatingImage ({#call cvLoadImage #} name 1)
                              bw <- imageTo32F i
                              return $ Just bw

cvRGBtoGRAY = 7 :: CInt-- NOTE: This will break.
cvRGBtoLAB = 45 :: CInt-- NOTE: This will break.

convertToGrayScale img = unsafePerformIO $ creatingImage $ do
    res <- {#call wrapCreateImage32F#} w h 1
    withImage img $ \cimg -> 
        {#call cvCvtColor#} (castPtr cimg) (castPtr res) cvRGBtoGRAY
    return res
 where    
    (w,h) = getSize img

rgbToLab :: NewImage RGB Double -> NewImage LAB Double
rgbToLab = S . convertTo cvRGBtoLAB 3 . unS 

rgbToGray :: NewImage RGB Double -> NewImage GrayScale Double
rgbToGray = S . convertToGrayScale . unS

getRegionNew :: (Integral a) => (a,a) -> (a,a) -> NewImage c d -> NewImage c d
getRegionNew a b
    = S . getRegion a b . unS

class GetPixel a where
    type P a :: *
    getPixelNew   :: (Integral i) => (i,i) -> a -> P a

instance GetPixel (NewImage GrayScale Double) where
    type P (NewImage GrayScale Double) = Double 
    getPixelNew (fromIntegral -> x, fromIntegral -> y) image = realToFrac $ unsafePerformIO $ withGenImage (unS image) $ \img ->
                              {#call wrapGet32F2D#} img y x

instance  GetPixel (NewImage RGB Double) where
    type P (NewImage RGB Double) = (Double,Double,Double) 
    getPixelNew (fromIntegral -> x, fromIntegral -> y) image 
        = unsafePerformIO $ do 
                     withGenImage (unS image) $ \img -> do
                              r <- {#call wrapGet32F2DC#} img y x 0
                              g <- {#call wrapGet32F2DC#} img y x 1
                              b <- {#call wrapGet32F2DC#} img y x 2
                              return (realToFrac r,realToFrac g, realToFrac b)


convertTo code channels img = unsafePerformIO $ creatingImage $ do
    res <- {#call wrapCreateImage32F#} w h channels
    withImage img $ \cimg -> 
        {#call cvCvtColor#} (castPtr cimg) (castPtr res) code
    return res
 where    
    (w,h) = getSize img

createImage32F (w,h) nChannels = do
    creatingImage $ {#call wrapCreateImage32F#} w h nChannels

createImage64F (w,h) nChannels = do
    creatingImage $ {#call wrapCreateImage64F#} w h nChannels

createImage8U (w,h) nChannels = do
    creatingImage $ {#call wrapCreateImage8U#} w h nChannels

image32F size channels = unsafePerformIO $ createImage32F size channels
image8U size channels = unsafePerformIO $ createImage8U size channels

emptyCopy img = image32F (getSize img) 1

saveImage filename image = do
                           fpi <- imageTo8Bit image
                           withCString  filename $ \name  -> 
                            withGenImage fpi    $ \cvArr ->
							 alloca (\defs -> poke defs 0 >> {#call cvSaveImage #} name cvArr defs >> return ())

getSize image = unsafePerformIO $ withImage image $ \i -> do
                 w <- {#call getImageWidth#} i
                 h <- {#call getImageHeight#} i
                 return (fromIntegral w,fromIntegral h)

getArea = uncurry (*).getSize

getRegion :: (Integral a) => (a, a) -> (a,a) -> Image -> Image
getRegion (fromIntegral -> x,fromIntegral -> y) (fromIntegral -> w,fromIntegral -> h) image 
    | x+w <= width && y+h <= height = getRegion' (x,y) (w,h) image
    | otherwise                   = error $ "Region outside image:"
                                            ++ show (getSize image) ++
                                            "/"++show (x+w,y+h)

     ((w1,h1),(w2,h2)) = (getSize image1,getSize image2)
     badSizes = x+w2>w1 || y+h2>h1 || x<0 || y<0

blitM :: (Int,Int) -> [((Int,Int),Image c d)] -> Image c d
blitM (rw,rh) imgs = resultPic
    where
     resultPic = unsafePerformIO $ do
                    r <- create (fromIntegral rw,fromIntegral rh) 
                    sequence_ [blit r i (fromIntegral x, fromIntegral y) 
                              | ((x,y),i) <- imgs ]
                    return r


subPixelBlit
  :: Image c d -> Image c d -> (CDouble, CDouble) -> IO ()

subPixelBlit (unS -> image1) (unS -> image2) (x,y) 
    | badSizes  = error $ "Bad blit sizes: " ++ show [(w1,h1),(w2,h2)]++"<-"++show (x,y) 
    | otherwise = withImage image1 $ \i1 ->
                   withImage image2 $ \i2 ->
