 liftValues op (HGD a) = zip bins (op values)
             where (bins,values) = unzip a
 
-sub (HGD a) (HGD b) | bins a == bins b 
+sub (HGD a) (HGD b) | bins a == bins b
                     = HGD $ zip (bins a) values
                 where
                  bins a = map fst a
                  msnd = map snd
                  values = zipWith (-) (msnd a) (msnd b)
-              
+
 
 noBins (HGD a) = length a
 

--        values :: [Double]
--        (bins,values) = unzip lst

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) 
                                          (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) 
                       -> D32 -> D32 -> Int -> Bool -> [D32]
simpleGetHistogram img mask start end binCount cumulative = unsafePerformIO $
    withImage img $ \i -> do
      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) 
                                               isCum (fromIntegral binCount) bins
        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        

       
       
        
--getNormalHistogram bins image = HGD new
--    where
--        (HGD lst) = getHistogram bins image 
----
----        value :: [Double]
--        bin   :: [Double]

cmpEuclidian a b = sum $ (zipWith (\x y -> (x-y)^2) a b)
cmpAbs a b = sum $ (zipWith (\x y -> abs (x-y)) a b)

chiSqrHG  a b = chiSqr (values a) (values b)
chiSqr a b = sum $ zipWith (calc) a b
    where
     calc a b = (a-b)*(a-b) `divide` (a+b)
