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/>
13 write_values_slice_to_bitmap,
21 import Data.Array.Repa as R
22 import Data.Array.Repa.Eval as R (now)
23 import Data.Array.Repa.Repr.Unboxed as R
24 import Data.Array.Repa.IO.Binary as R
25 import Data.Array.Repa.Algorithms.ColorRamp as R
26 import Data.Array.Repa.Operators.Traversal as R (unsafeTraverse)
27 import Data.Array.Repa.IO.BMP as R (writeImageToBMP)
31 mri_lower_threshold :: Double
32 mri_lower_threshold = 1400
34 mri_upper_threshold :: Double
35 mri_upper_threshold = 2500
37 -- | RawData is an array of words (16 bits), as contained in the MRI
39 type RawData sh = Array U sh Word16
41 -- | A specialization of the 'RawData' type, to three dimensions.
42 type RawData3D = RawData DIM3
44 type RGB = (Word8, Word8, Word8)
45 type ColorData sh = Array U sh RGB
48 {-# INLINE read_word16s #-}
49 read_word16s :: FilePath -> DIM3 -> IO RawData3D
50 read_word16s path mri_shape = do
51 arr <- R.readArrayFromStorableFile path mri_shape
57 bracket :: Double -> Word16
59 | x < mri_lower_threshold = 0
60 | x > mri_upper_threshold = 255
61 | otherwise = truncate (r * 255)
63 numerator = x - mri_lower_threshold
64 denominator = mri_upper_threshold - mri_lower_threshold
65 r = numerator/denominator
68 flip16 :: Word16 -> Word16
70 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
73 {-# INLINE swap_bytes #-}
74 swap_bytes :: (Shape sh, Source r Word16) => Array r sh Word16
80 bracket_array :: Shape sh => Values sh -> Array D sh Word16
85 {-# INLINE round_array #-}
86 round_array :: Shape sh => Values sh -> Array D sh Word16
91 flip_y :: Source r Word16 => Int -> Array r DIM3 Word16 -> Array D DIM3 Word16
93 R.unsafeTraverse arr id
94 (\get (Z :. z :. y :. x) ->
95 get (Z :. z :. (height - 1) - y :. x))
97 flip_x :: Source r Word16 => Int -> Array r DIM3 Word16 -> Array D DIM3 Word16
99 R.unsafeTraverse arr id
100 (\get (Z :. z :. y :. x) ->
101 get (Z :. z :. y :. (width - 1) - x))
104 {-# INLINE write_word16s #-}
105 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
106 write_word16s = R.writeArrayToStorableFile
111 -- Instead of IO, we could get away with a generic monad 'm'
112 -- here. However, /we/ only call this function from within IO.
114 values_to_colors :: (Shape sh) => (Values sh) -> IO (ColorData sh)
115 values_to_colors arr =
116 R.computeUnboxedP $ R.map (truncate_rgb . ramp_it) arr
118 ramp_it :: Double -> (Double, Double, Double)
123 rampColorHotToCold 0 255 x
125 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
126 truncate_rgb (r, g, b) =
129 r' = truncate (r * 255)
130 g' = truncate (g * 255)
131 b' = truncate (b * 255)
134 z_slice :: (R.Unbox a, Source r a) => Int -> Array r DIM3 a -> Array D DIM2 a
136 slice arr (Any :. n :. All :. All)
139 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
140 write_values_slice_to_bitmap v3d path = do
141 values <- R.computeUnboxedP $ R.map fromIntegral arr_bracketed
142 colors <- values_to_colors $ values
143 R.writeImageToBMP path colors
145 arr_bracketed = bracket_array v3d