1 -- | The MRI module contains functionsd and definitions relevant (and
2 -- specific) to the MRI data files found at,
4 -- <http://graphics.stanford.edu/data/voldata/>
14 write_values_slice_to_bitmap,
22 import Data.Array.Repa as R
23 import Data.Array.Repa.IO.Binary as R
24 import Data.Array.Repa.IO.ColorRamp as R
25 import Data.Array.Repa.IO.BMP as R (writeComponentsToBMP)
39 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
41 mri_lower_threshold :: Double
42 mri_lower_threshold = 1400
44 mri_upper_threshold :: Double
45 mri_upper_threshold = 2500
48 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
50 -- | RawData is an array of words (16 bits), as contained in the MRI
52 type RawData sh = Array sh Word16
54 -- | A specialization of the 'RawData' type, to three dimensions.
55 type RawData3D = RawData DIM3
57 type RGB = (Word8, Word8, Word8)
58 type ColorData sh = Array sh RGB
61 read_word16s :: FilePath -> IO RawData3D
62 read_word16s path = do
63 arr <- R.readArrayFromStorableFile path mri_shape
64 arr `deepSeqArray` return ()
68 bracket :: Double -> Word16
70 | x < mri_lower_threshold = 0
71 | x > mri_upper_threshold = 255
72 | otherwise = truncate (r * 255)
74 numerator = x - mri_lower_threshold
75 denominator = mri_upper_threshold - mri_lower_threshold
76 r = numerator/denominator
79 flip16 :: Word16 -> Word16
81 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
84 swap_bytes :: (Shape sh) => (RawData sh) -> (RawData sh)
86 R.force $ R.map flip16 arr
89 bracket_array :: (Shape sh) => (Values sh) -> (RawData sh)
91 R.force $ R.map bracket arr
94 round_array :: (Shape sh) => (Values sh) -> (RawData sh)
96 R.force $ R.map round arr
99 flip_y :: RawData3D -> RawData3D
101 R.force $ R.traverse arr id
102 (\get (Z :. z :. y :. x) ->
103 get (Z :. z :. (mri_height - 1) - y :. x))
105 flip_x :: RawData3D -> RawData3D
107 R.force $ R.traverse arr id
108 (\get (Z :. z :. y :. x) ->
109 get (Z :. z :. y :. (mri_width - 1) - x))
111 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
112 write_word16s = R.writeArrayToStorableFile
115 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
116 values_to_colors arr =
117 R.force $ R.map (truncate_rgb . ramp_it) arr
119 ramp_it :: Double -> (Double, Double, Double)
124 rampColorHotToCold 0 255 x
126 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
127 truncate_rgb (r, g, b) =
130 r' = truncate (r * 255)
131 g' = truncate (g * 255)
132 b' = truncate (b * 255)
136 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
138 slice arr (Any :. n :. All :. All)
142 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
143 write_values_slice_to_bitmap v3d path =
144 R.writeComponentsToBMP path routput goutput boutput
146 arr_bracketed = bracket_array v3d
147 colors = values_to_colors $ R.map fromIntegral arr_bracketed
148 routput = R.map (\(red, _, _) -> red) colors
149 goutput = R.map (\(_, green, _) -> green) colors
150 boutput = R.map (\(_, _, blue) -> blue) colors