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
74 bracket :: Double -> Word16
76 | x < mri_lower_threshold = 0
77 | x > mri_upper_threshold = 255
78 | otherwise = truncate (r * 255)
80 numerator = x - mri_lower_threshold
81 denominator = mri_upper_threshold - mri_lower_threshold
82 r = numerator/denominator
85 flip16 :: Word16 -> Word16
87 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
90 {-# INLINE swap_bytes #-}
91 swap_bytes :: (Shape sh, Source r Word16) => Array r sh Word16
97 bracket_array :: Shape sh => Values sh -> Array D sh Word16
102 {-# INLINE round_array #-}
103 round_array :: Shape sh => Values sh -> Array D sh Word16
108 flip_y :: Source r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
110 R.unsafeTraverse arr id
111 (\get (Z :. z :. y :. x) ->
112 get (Z :. z :. (mri_height - 1) - y :. x))
114 flip_x :: Source r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
116 R.unsafeTraverse arr id
117 (\get (Z :. z :. y :. x) ->
118 get (Z :. z :. y :. (mri_width - 1) - x))
121 {-# INLINE write_word16s #-}
122 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
123 write_word16s = R.writeArrayToStorableFile
128 -- Instead of IO, we could get away with a generic monad 'm'
129 -- here. However, /we/ only call this function from within IO.
131 values_to_colors :: (Shape sh) => (Values sh) -> IO (ColorData sh)
132 values_to_colors arr =
133 R.computeUnboxedP $ R.map (truncate_rgb . ramp_it) arr
135 ramp_it :: Double -> (Double, Double, Double)
140 rampColorHotToCold 0 255 x
142 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
143 truncate_rgb (r, g, b) =
146 r' = truncate (r * 255)
147 g' = truncate (g * 255)
148 b' = truncate (b * 255)
151 z_slice :: (R.Unbox a, Source r a) => Int -> Array r DIM3 a -> Array D DIM2 a
153 slice arr (Any :. n :. All :. All)
156 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
157 write_values_slice_to_bitmap v3d path = do
158 values <- R.computeUnboxedP $ R.map fromIntegral arr_bracketed
159 colors <- values_to_colors $ values
160 R.writeImageToBMP path colors
162 arr_bracketed = bracket_array v3d