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)
23 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
25 mri_lower_threshold :: Double
26 mri_lower_threshold = 1400
28 mri_upper_threshold :: Double
29 mri_upper_threshold = 2500
32 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
34 type RawData sh = Array sh Word16
35 type RawData3D = RawData DIM3
37 type RGB = (Word8, Word8, Word8)
38 type ColorData sh = Array sh RGB
40 rgb_to_dbl :: RGB -> (Double, Double, Double)
41 rgb_to_dbl (x,y,z) = (fromIntegral x, fromIntegral y, fromIntegral z)
44 read_word16s :: FilePath -> IO RawData3D
45 read_word16s path = do
46 arr <- R.readArrayFromStorableFile path mri_shape
47 arr `deepSeqArray` return ()
51 {-# INLINE bracket #-}
52 bracket :: Double -> Word16
54 | x < mri_lower_threshold = 0
55 | x > mri_upper_threshold = 255
56 | otherwise = truncate (r * 255)
58 numerator = x - mri_lower_threshold
59 denominator = mri_upper_threshold - mri_lower_threshold
60 r = numerator/denominator
64 flip16 :: Word16 -> Word16
66 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
69 swap_bytes :: (Shape sh) => (RawData sh) -> (RawData sh)
71 R.force $ R.map flip16 arr
74 bracket_array :: (Shape sh) => (Values sh) -> (RawData sh)
76 R.force $ R.map bracket arr
79 round_array :: (Shape sh) => (Values sh) -> (RawData sh)
81 R.force $ R.map round arr
84 flip_y :: RawData3D -> RawData3D
86 R.force $ R.traverse arr id
87 (\get (Z :. z :. y :. x) ->
88 get (Z :. z :. (mri_height - 1) - y :. x))
90 flip_x :: RawData3D -> RawData3D
92 R.force $ R.traverse arr id
93 (\get (Z :. z :. y :. x) ->
94 get (Z :. z :. y :. (mri_width - 1) - x))
96 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
97 write_word16s = R.writeArrayToStorableFile
100 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
101 values_to_colors arr =
102 R.force $ R.map (truncate_rgb . ramp_it) arr
104 ramp_it :: Double -> (Double, Double, Double)
109 rampColorHotToCold 0 255 x
111 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
112 truncate_rgb (r, g, b) =
115 r' = truncate (r * 255)
116 g' = truncate (g * 255)
117 b' = truncate (b * 255)
120 red_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
122 R.map (get_r . rgb_to_dbl)
124 get_r :: (Double, Double, Double) -> Double
127 green_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
129 R.map (get_g . rgb_to_dbl)
131 get_g :: (Double, Double, Double) -> Double
135 blue_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
137 R.map (get_b . rgb_to_dbl)
139 get_b :: (Double, Double, Double) -> Double
144 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
146 slice arr (Any :. n :. All :. All)
149 transpose_zx :: Elt a => Array DIM3 a -> Array DIM3 a
152 (\(Z :. zdim :. ydim :. xdim) -> (Z :. xdim :. ydim :. zdim))
153 (\_ -> (\(Z :. z :. y :. x) -> arr ! (Z :. x :. y :. z)))
156 z_slice3 :: Elt a => Int -> Array DIM3 a -> Array DIM3 a
158 | n == 0 = transpose_zx $ current R.++ next
159 | n == zdim-1 = transpose_zx $ previous R.++ current
160 | otherwise = transpose_zx $ previous R.++ current R.++ next
162 (Z :. zdim :. _ :. _) = extent arr
163 previous = transpose_zx $ reshape mri_slice3d (z_slice (n-1) arr)
164 current = transpose_zx $ reshape mri_slice3d (z_slice n arr)
165 next = transpose_zx $ reshape mri_slice3d (z_slice (n+1) arr)
168 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
169 write_values_slice_to_bitmap v3d path =
170 R.writeComponentsToBMP path routput goutput boutput
172 arr_bracketed = bracket_array v3d
173 colors = values_to_colors $ R.map fromIntegral arr_bracketed
174 routput = R.map (\(red, _, _) -> red) colors
175 goutput = R.map (\(_, green, _) -> green) colors
176 boutput = R.map (\(_, _, blue) -> blue) colors