]> gitweb.michael.orlitzky.com - spline3.git/blobdiff - src/MRI.hs
Use cmdargs to parse command-line arguments.
[spline3.git] / src / MRI.hs
index 9f97b08777058ac001ebc6705b5507631b1792de..1c244c1c81c8b738b95bcf5385d52290e536082a 100644 (file)
-{-# LANGUAGE ScopedTypeVariables #-}
-
-module MRI
+{-# LANGUAGE FlexibleContexts #-}
+-- | The MRI module contains functionsd and definitions relevant (and
+--   specific) to the MRI data files found at,
+--
+--   <http://graphics.stanford.edu/data/voldata/>
+--
+module MRI (
+  flip_x,
+  flip_y,
+  read_word16s,
+  round_array,
+  swap_bytes,
+  write_values_slice_to_bitmap,
+  write_word16s,
+  z_slice
+  )
 where
 
 import Data.Word
 import Data.Bits
-import Data.Array.Repa                  as R
-import Data.Array.Repa.IO.Binary        as R
-import Data.Array.Repa.IO.ColorRamp     as R
-
-
-mri_depth :: Int
-mri_depth = 109
-
-mri_width :: Int
-mri_width = 256
-
-mri_height :: Int
-mri_height = 256
+import Data.Array.Repa                          as R
+import Data.Array.Repa.Eval                     as R (now)
+import Data.Array.Repa.Repr.Unboxed             as R
+import Data.Array.Repa.IO.Binary                as R
+import Data.Array.Repa.Algorithms.ColorRamp     as R
+import Data.Array.Repa.Operators.Traversal      as R (unsafeTraverse)
+import Data.Array.Repa.IO.BMP                   as R (writeImageToBMP)
 
-mri_shape :: DIM3
-mri_shape = (Z :. mri_depth :. mri_width  :. mri_height)
+import Values
 
-mri_lower_threshold :: Int
+mri_lower_threshold :: Double
 mri_lower_threshold = 1400
 
-mri_upper_threshold :: Int
+mri_upper_threshold :: Double
 mri_upper_threshold = 2500
 
+-- | RawData is an array of words (16 bits), as contained in the MRI
+--   data files.
+type RawData sh = Array U sh Word16
 
-type RawData sh = Array sh Word16
+-- | A specialization of the 'RawData' type, to three dimensions.
 type RawData3D = RawData DIM3
 
 type RGB = (Word8, Word8, Word8)
-type ColorData sh = Array sh RGB
+type ColorData sh = Array sh RGB
 
-rgb_to_dbl :: RGB -> (Double, Double, Double)
-rgb_to_dbl (x,y,z) = (fromIntegral x, fromIntegral y, fromIntegral z)
 
+{-# INLINE read_word16s #-}
+read_word16s :: FilePath -> DIM3 -> IO RawData3D
+read_word16s path mri_shape = do
+  arr <- R.readArrayFromStorableFile path mri_shape
+  c   <- R.copyP arr
+  now $ c
 
-read_word16s :: IO RawData3D
-read_word16s = do
-  arr <- R.readArrayFromStorableFile "../data/mri.bin" mri_shape
-  arr `deepSeqArray` return ()
-  return arr
 
 
-{-# INLINE bracket #-}
-bracket :: Int -> Int -> Int -> Word16
-bracket low high x
-        | x < low      = 0
-        | x > high     = 255
-        | otherwise    = truncate (r * 255)
+bracket :: Double -> Word16
+bracket x
+        | x < mri_lower_threshold      = 0
+        | x > mri_upper_threshold      = 255
+        | otherwise                    = truncate (r * 255)
             where
-              numerator = fromIntegral (x - low) :: Double
-              denominator = fromIntegral (high - low) :: Double
+              numerator = x - mri_lower_threshold
+              denominator = mri_upper_threshold - mri_lower_threshold
               r = numerator/denominator
 
 
-{-# INLINE flip16 #-}
 flip16 :: Word16 -> Word16
-flip16 xx = 
+flip16 xx =
   shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
 
 
-bracket_array :: (Shape sh) => (RawData sh) -> (RawData sh)
-bracket_array arr =
-  R.map (bracket mri_lower_threshold mri_upper_threshold . fromIntegral . flip16) arr
+{-# INLINE swap_bytes #-}
+swap_bytes :: (Shape sh, Source r Word16) => Array r sh Word16
+             -> Array D sh Word16
+swap_bytes =
+  R.map flip16
+
+
+bracket_array :: Shape sh => Values sh -> Array D sh Word16
+bracket_array =
+  R.map bracket
+
+
+{-# INLINE round_array #-}
+round_array :: Shape sh => Values sh -> Array D sh Word16
+round_array =
+  R.map round
 
-flip_y :: RawData3D -> RawData3D
-flip_y arr =
-  R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. (mri_height - 1) - y :. x))
 
-flip_x :: RawData3D -> RawData3D
-flip_x arr =
-  R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. y :. (mri_width - 1) - x))
+flip_y :: Source r Word16 => Int -> Array r DIM3 Word16 -> Array D DIM3 Word16
+flip_y height arr =
+  R.unsafeTraverse arr id
+              (\get (Z :. z :. y :. x) ->
+                   get (Z :. z :. (height - 1) - y :. x))
 
+flip_x :: Source r Word16 => Int -> Array r DIM3 Word16 -> Array D DIM3 Word16
+flip_x width arr =
+  R.unsafeTraverse arr id
+              (\get (Z :. z :. y :. x) ->
+                   get (Z :. z :. y :. (width - 1) - x))
+
+
+{-# INLINE write_word16s #-}
 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
-write_word16s = 
-  -- dump the slice back as word16
-  R.writeArrayToStorableFile
+write_word16s = R.writeArrayToStorableFile
 
 
-raw_data_to_color :: (Shape sh) => (RawData sh) -> (ColorData sh)
-raw_data_to_color arr =
-  R.force $ R.map (truncate_rgb . ramp_it . fromIntegral) arr
+
+--
+-- Instead of IO, we could get away with a generic monad 'm'
+-- here. However, /we/ only call this function from within IO.
+--
+values_to_colors :: (Shape sh) => (Values sh) -> IO (ColorData sh)
+values_to_colors arr =
+  R.computeUnboxedP $ R.map (truncate_rgb . ramp_it) arr
   where
     ramp_it :: Double -> (Double, Double, Double)
     ramp_it x =
@@ -102,26 +131,15 @@ raw_data_to_color arr =
         b' = truncate (b * 255)
 
 
-red_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
-red_dbl_data =
-  R.map (get_r . rgb_to_dbl)
-  where
-    get_r :: (Double, Double, Double) -> Double
-    get_r (r, _, _) = r
-
-green_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
-green_dbl_data =
-  R.map (get_g . rgb_to_dbl)
-  where
-    get_g :: (Double, Double, Double) -> Double
-    get_g (_, g, _) = g
+z_slice :: (R.Unbox a, Source r a) => Int -> Array r DIM3 a -> Array D DIM2 a
+z_slice n arr =
+  slice arr (Any :. n :. All :. All)
 
 
-blue_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
-blue_dbl_data =
-  R.map (get_b . rgb_to_dbl)
+write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
+write_values_slice_to_bitmap v3d path = do
+  values <- R.computeUnboxedP $ R.map fromIntegral arr_bracketed
+  colors <- values_to_colors $ values
+  R.writeImageToBMP path colors
   where
-    get_b :: (Double, Double, Double) -> Double
-    get_b (_, _, b) = b
-
-
+    arr_bracketed = bracket_array v3d