 --        values :: [Double]
 --        (bins,values) = unzip lst
 
-weightedHistogram img weights start end binCount = unsafePerformIO $ 
-    withImage img $ \i -> 
+weightedHistogram img weights start end binCount = unsafePerformIO $
+    withImage img $ \i ->
      withImage weights $ \w -> do
       bins <- mallocArray (fromIntegral binCount)
-      {#call get_weighted_histogram#} i w (realToFrac start) 
-                                          (realToFrac end) 
+      {#call get_weighted_histogram#} i w (realToFrac start)
+                                          (realToFrac end)
                                           (fromIntegral binCount) bins
       r <- peekArray binCount bins >>= return.map realToFrac
       free bins
       return r
 
 -- TODO: Add binary images
-simpleGetHistogram :: Image GrayScale D32 -> Maybe (Image GrayScale D8) 
+simpleGetHistogram :: Image GrayScale D32 -> Maybe (Image GrayScale D8)
                        -> D32 -> D32 -> Int -> Bool -> [D32]
 simpleGetHistogram img mask start end binCount cumulative = unsafePerformIO $
     withImage img $ \i -> do
-      bins <- mallocArray binCount    
+      bins <- mallocArray binCount
       let isCum | cumulative == True  = 1
                 | cumulative == False = 0
-                
+
       case mask of
         (Just msk) -> do
                    withImage msk $ \m -> do
-                    {#call get_histogram#} i m (realToFrac start) (realToFrac end) 
+                    {#call get_histogram#} i m (realToFrac start) (realToFrac end)
                                                isCum (fromIntegral binCount) bins
-        Nothing  -> {#call get_histogram#} i (nullPtr) 
-                                             (realToFrac start) (realToFrac end) 
+        Nothing  -> {#call get_histogram#} i (nullPtr)
+                                             (realToFrac start) (realToFrac end)
                                              isCum (fromIntegral binCount) bins
 
       r <- peekArray binCount bins >>= return.map realToFrac
       free bins
-      return r        
+      return r
 
-       
-       
-        
+
+
+
 --getNormalHistogram bins image = HGD new
 --    where
---        (HGD lst) = getHistogram bins image 
+--        (HGD lst) = getHistogram bins image
 ----
 ----        value :: [Double]
 --        bin   :: [Double]

--        size = fromIntegral $ uncurry (*) $ getSize image

--getHistogram :: Int -> Image GrayScale D32 -> HistogramData Double
--getHistogram bins image = unsafePerformIO $ do 
--                            h <- buildHistogram cbins image 
--                            values <- mapM (getBin h) 
--                                        [0..fromIntegral bins-1] 
--                            return.HGD $ 
--                                zip [-1,-1+2/(realToFrac bins)..1] values
--                        where
--                         cbins = fromIntegral bins

-- | Given a set of images, such as the color channels of color image, and
--   a histogram with corresponding number of channels, replace the pixels of
--   the image with the likelihoods from the histogram
backProjectHistogram :: [Image GrayScale D8] -> I.Histogram -> Image GrayScale D8
backProjectHistogram images@(img:_) (I.Histogram hist) = unsafePerformIO $ do
    r <- cloneImage img
    withImage r $ \c_r ->
     withPtrList (map imageFPTR images) $ \ptrs ->
      withForeignPtr hist $ \c_hist ->
        I.c'cvCalcArrBackProject (castPtr ptrs) (castPtr c_r) c_hist
    return r
backProjectHistogram _ _ = error "Empty list of images"

-- | Calculate an opencv histogram object from set of images, each with it's
-- own number of bins.
--histogram :: [(Image GrayScale D8, Int)] -> Bool -> Maybe (Image GrayScale D8)
--               -> I.Histogram

--histogram imageBins
--  I.creatingHistogram $ do
--        hist <-  I.emptyUniformHistogramND ds
--        withPtrList (map imageFPTR images) $ \ptrs ->
--         case mask of
--            Just m -> do
--                withImage m $ \c_mask -> do
--                I.c'cvCalcArrHist (castPtr ptrs) hist c_accumulate (castPtr c_mask)
--                return hist
--            Nothing -> do
--                I.c'cvCalcArrHist (castPtr ptrs) hist c_accumulate (nullPtr)
--                return hist
--   where
--    (images,ds) = unzip imageBins
--    c_accumulate = 0

-- getHistogramBin (I.Histogram hs) n = unsafePerformIO $ withForeignPtr hs (\h -> I.c'cvGetHistValue_1D (castPtr h) n)

