1 {-# LANGUAGE FlexibleContexts #-}
2 -- | The MRI module contains functionsd and definitions relevant (and
3 -- specific) to the MRI data files found at,
5 -- <http://graphics.stanford.edu/data/voldata/>
15 write_values_slice_to_bitmap,
23 import Data.Array.Repa as R
24 import Data.Array.Repa.Eval as R (now)
25 import Data.Array.Repa.Repr.Unboxed as R
26 import Data.Array.Repa.IO.Binary as R
27 import Data.Array.Repa.Algorithms.ColorRamp as R
28 import Data.Array.Repa.Operators.Traversal as R (unsafeTraverse)
29 import Data.Array.Repa.IO.BMP as R (writeImageToBMP)
43 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
45 mri_lower_threshold :: Double
46 mri_lower_threshold = 1400
48 mri_upper_threshold :: Double
49 mri_upper_threshold = 2500
52 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
54 -- | RawData is an array of words (16 bits), as contained in the MRI
56 type RawData sh = Array U sh Word16
58 -- | A specialization of the 'RawData' type, to three dimensions.
59 type RawData3D = RawData DIM3
61 type RGB = (Word8, Word8, Word8)
62 type ColorData sh = Array U sh RGB
65 {-# INLINE read_word16s #-}
66 read_word16s :: FilePath -> IO RawData3D
67 read_word16s path = do
68 arr <- R.readArrayFromStorableFile path mri_shape
73 bracket :: Double -> Word16
75 | x < mri_lower_threshold = 0
76 | x > mri_upper_threshold = 255
77 | otherwise = truncate (r * 255)
79 numerator = x - mri_lower_threshold
80 denominator = mri_upper_threshold - mri_lower_threshold
81 r = numerator/denominator
84 flip16 :: Word16 -> Word16
86 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
89 {-# INLINE swap_bytes #-}
90 swap_bytes :: (Shape sh, Repr r Word16) => Array r sh Word16
96 bracket_array :: Shape sh => Values sh -> Array D sh Word16
101 {-# INLINE round_array #-}
102 round_array :: Shape sh => Values sh -> Array D sh Word16
107 flip_y :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
109 R.unsafeTraverse arr id
110 (\get (Z :. z :. y :. x) ->
111 get (Z :. z :. (mri_height - 1) - y :. x))
113 flip_x :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
115 R.unsafeTraverse arr id
116 (\get (Z :. z :. y :. x) ->
117 get (Z :. z :. y :. (mri_width - 1) - x))
120 {-# INLINE write_word16s #-}
121 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
122 write_word16s = R.writeArrayToStorableFile
126 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
127 values_to_colors arr =
128 R.computeS $ R.map (truncate_rgb . ramp_it) arr
130 ramp_it :: Double -> (Double, Double, Double)
135 rampColorHotToCold 0 255 x
137 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
138 truncate_rgb (r, g, b) =
141 r' = truncate (r * 255)
142 g' = truncate (g * 255)
143 b' = truncate (b * 255)
146 z_slice :: (R.Unbox a, Repr r a) => Int -> Array r DIM3 a -> Array D DIM2 a
148 slice arr (Any :. n :. All :. All)
151 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
152 write_values_slice_to_bitmap v3d path =
153 R.writeImageToBMP path colors
155 arr_bracketed = bracket_array v3d
156 colors = values_to_colors $ R.computeS $ R.map fromIntegral arr_bracketed