9 write_values_slice_to_bitmap,
17 import Data.Array.Repa as R
18 import Data.Array.Repa.IO.Binary as R
19 import Data.Array.Repa.IO.ColorRamp as R
20 import Data.Array.Repa.IO.BMP as R (writeComponentsToBMP)
34 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
36 mri_lower_threshold :: Double
37 mri_lower_threshold = 1400
39 mri_upper_threshold :: Double
40 mri_upper_threshold = 2500
43 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
45 type RawData sh = Array sh Word16
46 type RawData3D = RawData DIM3
48 type RGB = (Word8, Word8, Word8)
49 type ColorData sh = Array sh RGB
52 read_word16s :: FilePath -> IO RawData3D
53 read_word16s path = do
54 arr <- R.readArrayFromStorableFile path mri_shape
55 arr `deepSeqArray` return ()
59 bracket :: Double -> Word16
61 | x < mri_lower_threshold = 0
62 | x > mri_upper_threshold = 255
63 | otherwise = truncate (r * 255)
65 numerator = x - mri_lower_threshold
66 denominator = mri_upper_threshold - mri_lower_threshold
67 r = numerator/denominator
70 flip16 :: Word16 -> Word16
72 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
75 swap_bytes :: (Shape sh) => (RawData sh) -> (RawData sh)
77 R.force $ R.map flip16 arr
80 bracket_array :: (Shape sh) => (Values sh) -> (RawData sh)
82 R.force $ R.map bracket arr
85 round_array :: (Shape sh) => (Values sh) -> (RawData sh)
87 R.force $ R.map round arr
90 flip_y :: RawData3D -> RawData3D
92 R.force $ R.traverse arr id
93 (\get (Z :. z :. y :. x) ->
94 get (Z :. z :. (mri_height - 1) - y :. x))
96 flip_x :: RawData3D -> RawData3D
98 R.force $ R.traverse arr id
99 (\get (Z :. z :. y :. x) ->
100 get (Z :. z :. y :. (mri_width - 1) - x))
102 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
103 write_word16s = R.writeArrayToStorableFile
106 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
107 values_to_colors arr =
108 R.force $ R.map (truncate_rgb . ramp_it) arr
110 ramp_it :: Double -> (Double, Double, Double)
115 rampColorHotToCold 0 255 x
117 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
118 truncate_rgb (r, g, b) =
121 r' = truncate (r * 255)
122 g' = truncate (g * 255)
123 b' = truncate (b * 255)
127 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
129 slice arr (Any :. n :. All :. All)
133 write_values_slice_to_bitmap :: Values2D -> FilePath -> IO ()
134 write_values_slice_to_bitmap v3d path =
135 R.writeComponentsToBMP path routput goutput boutput
137 arr_bracketed = bracket_array v3d
138 colors = values_to_colors $ R.map fromIntegral arr_bracketed
139 routput = R.map (\(red, _, _) -> red) colors
140 goutput = R.map (\(_, green, _) -> green) colors
141 boutput = R.map (\(_, _, blue) -> blue) colors