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.Repr.Unboxed as R
25 import Data.Array.Repa.IO.Binary as R
26 import Data.Array.Repa.Algorithms.ColorRamp as R
27 import Data.Array.Repa.IO.BMP as R (writeImageToBMP)
41 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
43 mri_lower_threshold :: Double
44 mri_lower_threshold = 1400
46 mri_upper_threshold :: Double
47 mri_upper_threshold = 2500
50 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
52 -- | RawData is an array of words (16 bits), as contained in the MRI
54 type RawData sh = Array U sh Word16
56 -- | A specialization of the 'RawData' type, to three dimensions.
57 type RawData3D = RawData DIM3
59 type RGB = (Word8, Word8, Word8)
60 type ColorData sh = Array U sh RGB
63 {-# INLINE read_word16s #-}
64 read_word16s :: FilePath -> IO RawData3D
65 read_word16s path = do
66 arr <- R.readArrayFromStorableFile path mri_shape
67 arr' <- now $ R.copy arr
71 bracket :: Double -> Word16
73 | x < mri_lower_threshold = 0
74 | x > mri_upper_threshold = 255
75 | otherwise = truncate (r * 255)
77 numerator = x - mri_lower_threshold
78 denominator = mri_upper_threshold - mri_lower_threshold
79 r = numerator/denominator
82 flip16 :: Word16 -> Word16
84 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
87 {-# INLINE swap_bytes #-}
88 swap_bytes :: (Shape sh, Repr r Word16) => Array r sh Word16
94 bracket_array :: Shape sh => Values sh -> Array D sh Word16
99 {-# INLINE round_array #-}
100 round_array :: Shape sh => Values sh -> Array D sh Word16
105 flip_y :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
107 R.unsafeTraverse arr id
108 (\get (Z :. z :. y :. x) ->
109 get (Z :. z :. (mri_height - 1) - y :. x))
111 flip_x :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
113 R.unsafeTraverse arr id
114 (\get (Z :. z :. y :. x) ->
115 get (Z :. z :. y :. (mri_width - 1) - x))
118 {-# INLINE write_word16s #-}
119 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
120 write_word16s = R.writeArrayToStorableFile
124 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
125 values_to_colors arr =
126 R.compute $ R.map (truncate_rgb . ramp_it) arr
128 ramp_it :: Double -> (Double, Double, Double)
133 rampColorHotToCold 0 255 x
135 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
136 truncate_rgb (r, g, b) =
139 r' = truncate (r * 255)
140 g' = truncate (g * 255)
141 b' = truncate (b * 255)
144 z_slice :: (R.Unbox a, Repr r a) => Int -> Array r DIM3 a -> Array D DIM2 a
146 slice arr (Any :. n :. All :. All)
149 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
150 write_values_slice_to_bitmap v3d path =
151 R.writeImageToBMP path colors
153 arr_bracketed = bracket_array v3d
154 colors = values_to_colors $ R.compute $ R.map fromIntegral arr_bracketed