1 {-# LANGUAGE ScopedTypeVariables #-}
8 import Data.Array.Repa as R
9 import Data.Array.Repa.IO.Binary as R
10 import Data.Array.Repa.IO.ColorRamp as R
23 mri_shape = (Z :. mri_depth :. mri_width :. mri_height)
25 mri_lower_threshold :: Int
26 mri_lower_threshold = 1400
28 mri_upper_threshold :: Int
29 mri_upper_threshold = 2500
32 type RawData sh = Array sh Word16
33 type RawData3D = RawData DIM3
35 type RGB = (Word8, Word8, Word8)
36 type ColorData sh = Array sh RGB
38 rgb_to_dbl :: RGB -> (Double, Double, Double)
39 rgb_to_dbl (x,y,z) = (fromIntegral x, fromIntegral y, fromIntegral z)
42 read_word16s :: IO RawData3D
44 arr <- R.readArrayFromStorableFile "../data/mri.bin" mri_shape
45 arr `deepSeqArray` return ()
49 {-# INLINE bracket #-}
50 bracket :: Int -> Int -> Int -> Word16
54 | otherwise = truncate (r * 255)
56 numerator = fromIntegral (x - low) :: Double
57 denominator = fromIntegral (high - low) :: Double
58 r = numerator/denominator
62 flip16 :: Word16 -> Word16
64 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
67 bracket_array :: (Shape sh) => (RawData sh) -> (RawData sh)
69 R.map (bracket mri_lower_threshold mri_upper_threshold . fromIntegral . flip16) arr
71 flip_y :: RawData3D -> RawData3D
73 R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. (mri_height - 1) - y :. x))
75 flip_x :: RawData3D -> RawData3D
77 R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. y :. (mri_width - 1) - x))
79 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
81 -- dump the slice back as word16
82 R.writeArrayToStorableFile
85 raw_data_to_color :: (Shape sh) => (RawData sh) -> (ColorData sh)
86 raw_data_to_color arr =
87 R.force $ R.map (truncate_rgb . ramp_it . fromIntegral) arr
89 ramp_it :: Double -> (Double, Double, Double)
94 rampColorHotToCold 0 255 x
96 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
97 truncate_rgb (r, g, b) =
100 r' = truncate (r * 255)
101 g' = truncate (g * 255)
102 b' = truncate (b * 255)
105 red_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
107 R.map (get_r . rgb_to_dbl)
109 get_r :: (Double, Double, Double) -> Double
112 green_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
114 R.map (get_g . rgb_to_dbl)
116 get_g :: (Double, Double, Double) -> Double
120 blue_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
122 R.map (get_b . rgb_to_dbl)
124 get_b :: (Double, Double, Double) -> Double