]> gitweb.michael.orlitzky.com - spline3.git/blob - src/MRI.hs
157b89972aa8dfe817951805cdede467714e4918
[spline3.git] / src / MRI.hs
1 module MRI
2 where
3
4 import Data.Word
5 import Data.Bits
6 import Data.Array.Repa as R
7 import Data.Array.Repa.IO.Binary as R
8 import Data.Array.Repa.IO.ColorRamp as R
9 import Data.Array.Repa.IO.BMP as R (writeComponentsToBMP)
10
11 import Values
12
13 mri_depth :: Int
14 mri_depth = 109
15
16 mri_width :: Int
17 mri_width = 256
18
19 mri_height :: Int
20 mri_height = 256
21
22 mri_shape :: DIM3
23 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
24
25 mri_lower_threshold :: Double
26 mri_lower_threshold = 1400
27
28 mri_upper_threshold :: Double
29 mri_upper_threshold = 2500
30
31 mri_slice3d :: DIM3
32 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
33
34 type RawData sh = Array sh Word16
35 type RawData3D = RawData DIM3
36
37 type RGB = (Word8, Word8, Word8)
38 type ColorData sh = Array sh RGB
39
40 rgb_to_dbl :: RGB -> (Double, Double, Double)
41 rgb_to_dbl (x,y,z) = (fromIntegral x, fromIntegral y, fromIntegral z)
42
43
44 read_word16s :: FilePath -> IO RawData3D
45 read_word16s path = do
46 arr <- R.readArrayFromStorableFile path mri_shape
47 arr `deepSeqArray` return ()
48 return arr
49
50
51 bracket :: Double -> Word16
52 bracket x
53 | x < mri_lower_threshold = 0
54 | x > mri_upper_threshold = 255
55 | otherwise = truncate (r * 255)
56 where
57 numerator = x - mri_lower_threshold
58 denominator = mri_upper_threshold - mri_lower_threshold
59 r = numerator/denominator
60
61
62 flip16 :: Word16 -> Word16
63 flip16 xx =
64 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
65
66
67 swap_bytes :: (Shape sh) => (RawData sh) -> (RawData sh)
68 swap_bytes arr =
69 R.force $ R.map flip16 arr
70
71
72 bracket_array :: (Shape sh) => (Values sh) -> (RawData sh)
73 bracket_array arr =
74 R.force $ R.map bracket arr
75
76
77 round_array :: (Shape sh) => (Values sh) -> (RawData sh)
78 round_array arr =
79 R.force $ R.map round arr
80
81
82 flip_y :: RawData3D -> RawData3D
83 flip_y arr =
84 R.force $ R.traverse arr id
85 (\get (Z :. z :. y :. x) ->
86 get (Z :. z :. (mri_height - 1) - y :. x))
87
88 flip_x :: RawData3D -> RawData3D
89 flip_x arr =
90 R.force $ R.traverse arr id
91 (\get (Z :. z :. y :. x) ->
92 get (Z :. z :. y :. (mri_width - 1) - x))
93
94 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
95 write_word16s = R.writeArrayToStorableFile
96
97
98 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
99 values_to_colors arr =
100 R.force $ R.map (truncate_rgb . ramp_it) arr
101 where
102 ramp_it :: Double -> (Double, Double, Double)
103 ramp_it x =
104 if x == 0 then
105 (0, 0, 0)
106 else
107 rampColorHotToCold 0 255 x
108
109 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
110 truncate_rgb (r, g, b) =
111 (r', g', b')
112 where
113 r' = truncate (r * 255)
114 g' = truncate (g * 255)
115 b' = truncate (b * 255)
116
117
118 red_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
119 red_dbl_data =
120 R.map (get_r . rgb_to_dbl)
121 where
122 get_r :: (Double, Double, Double) -> Double
123 get_r (r, _, _) = r
124
125 green_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
126 green_dbl_data =
127 R.map (get_g . rgb_to_dbl)
128 where
129 get_g :: (Double, Double, Double) -> Double
130 get_g (_, g, _) = g
131
132
133 blue_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
134 blue_dbl_data =
135 R.map (get_b . rgb_to_dbl)
136 where
137 get_b :: (Double, Double, Double) -> Double
138 get_b (_, _, b) = b
139
140
141
142 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
143 z_slice n arr =
144 slice arr (Any :. n :. All :. All)
145
146
147 transpose_zx :: Elt a => Array DIM3 a -> Array DIM3 a
148 transpose_zx arr =
149 traverse arr
150 (\(Z :. zdim :. ydim :. xdim) -> (Z :. xdim :. ydim :. zdim))
151 (\_ -> (\(Z :. z :. y :. x) -> arr ! (Z :. x :. y :. z)))
152
153
154 z_slice3 :: Elt a => Int -> Array DIM3 a -> Array DIM3 a
155 z_slice3 n arr
156 | n == 0 = transpose_zx $ current R.++ next
157 | n == zdim-1 = transpose_zx $ previous R.++ current
158 | otherwise = transpose_zx $ previous R.++ current R.++ next
159 where
160 (Z :. zdim :. _ :. _) = extent arr
161 previous = transpose_zx $ reshape mri_slice3d (z_slice (n-1) arr)
162 current = transpose_zx $ reshape mri_slice3d (z_slice n arr)
163 next = transpose_zx $ reshape mri_slice3d (z_slice (n+1) arr)
164
165
166 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
167 write_values_slice_to_bitmap v3d path =
168 R.writeComponentsToBMP path routput goutput boutput
169 where
170 arr_bracketed = bracket_array v3d
171 colors = values_to_colors $ R.map fromIntegral arr_bracketed
172 routput = R.map (\(red, _, _) -> red) colors
173 goutput = R.map (\(_, green, _) -> green) colors
174 boutput = R.map (\(_, _, blue) -> blue) colors