]> gitweb.michael.orlitzky.com - spline3.git/blob - src/MRI.hs
Update some documentation.
[spline3.git] / src / MRI.hs
1 -- | The MRI module contains functionsd and definitions relevant (and
2 -- specific) to the MRI data files found at,
3 --
4 -- <http://graphics.stanford.edu/data/voldata/>
5 --
6 module MRI (
7 flip_x,
8 flip_y,
9 mri_shape,
10 mri_slice3d,
11 read_word16s,
12 round_array,
13 swap_bytes,
14 write_values_slice_to_bitmap,
15 write_word16s,
16 z_slice
17 )
18 where
19
20 import Data.Word
21 import Data.Bits
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)
26
27 import Values
28
29 mri_depth :: Int
30 mri_depth = 109
31
32 mri_width :: Int
33 mri_width = 256
34
35 mri_height :: Int
36 mri_height = 256
37
38 mri_shape :: DIM3
39 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
40
41 mri_lower_threshold :: Double
42 mri_lower_threshold = 1400
43
44 mri_upper_threshold :: Double
45 mri_upper_threshold = 2500
46
47 mri_slice3d :: DIM3
48 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
49
50 -- | RawData is an array of words (16 bits), as contained in the MRI
51 -- data files.
52 type RawData sh = Array sh Word16
53
54 -- | A specialization of the 'RawData' type, to three dimensions.
55 type RawData3D = RawData DIM3
56
57 type RGB = (Word8, Word8, Word8)
58 type ColorData sh = Array sh RGB
59
60
61 read_word16s :: FilePath -> IO RawData3D
62 read_word16s path = do
63 arr <- R.readArrayFromStorableFile path mri_shape
64 arr `deepSeqArray` return ()
65 return arr
66
67
68 bracket :: Double -> Word16
69 bracket x
70 | x < mri_lower_threshold = 0
71 | x > mri_upper_threshold = 255
72 | otherwise = truncate (r * 255)
73 where
74 numerator = x - mri_lower_threshold
75 denominator = mri_upper_threshold - mri_lower_threshold
76 r = numerator/denominator
77
78
79 flip16 :: Word16 -> Word16
80 flip16 xx =
81 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
82
83
84 swap_bytes :: (Shape sh) => (RawData sh) -> (RawData sh)
85 swap_bytes arr =
86 R.force $ R.map flip16 arr
87
88
89 bracket_array :: (Shape sh) => (Values sh) -> (RawData sh)
90 bracket_array arr =
91 R.force $ R.map bracket arr
92
93
94 round_array :: (Shape sh) => (Values sh) -> (RawData sh)
95 round_array arr =
96 R.force $ R.map round arr
97
98
99 flip_y :: RawData3D -> RawData3D
100 flip_y arr =
101 R.force $ R.traverse arr id
102 (\get (Z :. z :. y :. x) ->
103 get (Z :. z :. (mri_height - 1) - y :. x))
104
105 flip_x :: RawData3D -> RawData3D
106 flip_x arr =
107 R.force $ R.traverse arr id
108 (\get (Z :. z :. y :. x) ->
109 get (Z :. z :. y :. (mri_width - 1) - x))
110
111 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
112 write_word16s = R.writeArrayToStorableFile
113
114
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
118 where
119 ramp_it :: Double -> (Double, Double, Double)
120 ramp_it x =
121 if x == 0 then
122 (0, 0, 0)
123 else
124 rampColorHotToCold 0 255 x
125
126 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
127 truncate_rgb (r, g, b) =
128 (r', g', b')
129 where
130 r' = truncate (r * 255)
131 g' = truncate (g * 255)
132 b' = truncate (b * 255)
133
134
135
136 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
137 z_slice n arr =
138 slice arr (Any :. n :. All :. All)
139
140
141
142 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
143 write_values_slice_to_bitmap v3d path =
144 R.writeComponentsToBMP path routput goutput boutput
145 where
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