]> gitweb.michael.orlitzky.com - spline3.git/blob - src/MRI.hs
Make the minimum number of changes necessary to work with repa-3.1.1.1. Unfortunately...
[spline3.git] / src / MRI.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 -- | The MRI module contains functionsd and definitions relevant (and
3 -- specific) to the MRI data files found at,
4 --
5 -- <http://graphics.stanford.edu/data/voldata/>
6 --
7 module MRI (
8 flip_x,
9 flip_y,
10 mri_shape,
11 mri_slice3d,
12 read_word16s,
13 round_array,
14 swap_bytes,
15 write_values_slice_to_bitmap,
16 write_word16s,
17 z_slice
18 )
19 where
20
21 import Data.Word
22 import Data.Bits
23 import Data.Array.Repa as R
24 import Data.Array.Repa.Eval as R (now)
25 import Data.Array.Repa.Repr.Unboxed as R
26 import Data.Array.Repa.IO.Binary as R
27 import Data.Array.Repa.Algorithms.ColorRamp as R
28 import Data.Array.Repa.Operators.Traversal as R (unsafeTraverse)
29 import Data.Array.Repa.IO.BMP as R (writeImageToBMP)
30
31 import Values
32
33 mri_depth :: Int
34 mri_depth = 109
35
36 mri_width :: Int
37 mri_width = 256
38
39 mri_height :: Int
40 mri_height = 256
41
42 mri_shape :: DIM3
43 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
44
45 mri_lower_threshold :: Double
46 mri_lower_threshold = 1400
47
48 mri_upper_threshold :: Double
49 mri_upper_threshold = 2500
50
51 mri_slice3d :: DIM3
52 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
53
54 -- | RawData is an array of words (16 bits), as contained in the MRI
55 -- data files.
56 type RawData sh = Array U sh Word16
57
58 -- | A specialization of the 'RawData' type, to three dimensions.
59 type RawData3D = RawData DIM3
60
61 type RGB = (Word8, Word8, Word8)
62 type ColorData sh = Array U sh RGB
63
64
65 {-# INLINE read_word16s #-}
66 read_word16s :: FilePath -> IO RawData3D
67 read_word16s path = do
68 arr <- R.readArrayFromStorableFile path mri_shape
69 now $ R.copyS arr
70
71
72
73 bracket :: Double -> Word16
74 bracket x
75 | x < mri_lower_threshold = 0
76 | x > mri_upper_threshold = 255
77 | otherwise = truncate (r * 255)
78 where
79 numerator = x - mri_lower_threshold
80 denominator = mri_upper_threshold - mri_lower_threshold
81 r = numerator/denominator
82
83
84 flip16 :: Word16 -> Word16
85 flip16 xx =
86 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
87
88
89 {-# INLINE swap_bytes #-}
90 swap_bytes :: (Shape sh, Repr r Word16) => Array r sh Word16
91 -> Array D sh Word16
92 swap_bytes =
93 R.map flip16
94
95
96 bracket_array :: Shape sh => Values sh -> Array D sh Word16
97 bracket_array =
98 R.map bracket
99
100
101 {-# INLINE round_array #-}
102 round_array :: Shape sh => Values sh -> Array D sh Word16
103 round_array =
104 R.map round
105
106
107 flip_y :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
108 flip_y arr =
109 R.unsafeTraverse arr id
110 (\get (Z :. z :. y :. x) ->
111 get (Z :. z :. (mri_height - 1) - y :. x))
112
113 flip_x :: Repr r Word16 => Array r DIM3 Word16 -> Array D DIM3 Word16
114 flip_x arr =
115 R.unsafeTraverse arr id
116 (\get (Z :. z :. y :. x) ->
117 get (Z :. z :. y :. (mri_width - 1) - x))
118
119
120 {-# INLINE write_word16s #-}
121 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
122 write_word16s = R.writeArrayToStorableFile
123
124
125
126 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
127 values_to_colors arr =
128 R.computeS $ R.map (truncate_rgb . ramp_it) arr
129 where
130 ramp_it :: Double -> (Double, Double, Double)
131 ramp_it x =
132 if x == 0 then
133 (0, 0, 0)
134 else
135 rampColorHotToCold 0 255 x
136
137 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
138 truncate_rgb (r, g, b) =
139 (r', g', b')
140 where
141 r' = truncate (r * 255)
142 g' = truncate (g * 255)
143 b' = truncate (b * 255)
144
145
146 z_slice :: (R.Unbox a, Repr r a) => Int -> Array r DIM3 a -> Array D DIM2 a
147 z_slice n arr =
148 slice arr (Any :. n :. All :. All)
149
150
151 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
152 write_values_slice_to_bitmap v3d path =
153 R.writeImageToBMP path colors
154 where
155 arr_bracketed = bracket_array v3d
156 colors = values_to_colors $ R.computeS $ R.map fromIntegral arr_bracketed