               exists <- fileExist n
               if not exists then return Nothing
                             else do
-                              i <- withCString n $ \name -> 
+                              i <- withCString n $ \name ->
                                      creatingBareImage ({#call cvLoadImage #} name (0))
                               bw <- imageTo32F i
+                              return . Just . S $ bw
+
+loadImage8 :: FilePath -> IO (Maybe (Image GrayScale D8))
+loadImage8 n = do
+              exists <- fileExist n
+              if not exists then return Nothing
+                            else do
+                              i <- withCString n $ \name ->
+                                     creatingBareImage ({#call cvLoadImage #} name (0))
+                              bw <- imageTo8Bit i
                               return . Just . S $ bw
 
 loadColorImage :: FilePath -> IO (Maybe (Image RGB D32))


-- #define UGETC(img,color,x,y) (((uint8_t *)((img)->imageData + (y)*(img)->widthStep))[(x)*3+(color)])
instance GetPixel (Image RGB D32) where
    type P (Image RGB D32) = (D32,D32,D32) 
    {-#INLINE getPixel#-}
    getPixel (x,y) i = unsafePerformIO $
                        withGenImage i $ \c_i -> do

                                         b <- peek (castPtr (d`plusPtr` (y*cs +(x*3+2)*fs)))
                                         return (r,g,b)

getPixelOldRGB (fromIntegral -> x, fromIntegral -> y) image
        = unsafePerformIO $ do
                     withGenImage 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)

-- | Perform (a destructive) inplace map of the image. This should be wrapped inside
-- withClone or an image operation
mapImageInplace :: (P (Image GrayScale D32) -> P (Image GrayScale D32))
            -> Image GrayScale D32
            -> IO ()
mapImageInplace f image = withGenImage image $ \c_i -> do
             d <- {#get IplImage->imageData#} c_i
