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
22 mri_shape = (Z :. mri_depth :. mri_height :. mri_width)
24 mri_lower_threshold :: Int
25 mri_lower_threshold = 1400
27 mri_upper_threshold :: Int
28 mri_upper_threshold = 2500
31 mri_slice3d = (Z :. 1 :. mri_height :. mri_width)
33 type RawData sh = Array sh Word16
34 type RawData3D = RawData DIM3
36 type RGB = (Word8, Word8, Word8)
37 type ColorData sh = Array sh RGB
39 rgb_to_dbl :: RGB -> (Double, Double, Double)
40 rgb_to_dbl (x,y,z) = (fromIntegral x, fromIntegral y, fromIntegral z)
43 read_word16s :: FilePath -> IO RawData3D
44 read_word16s path = do
45 arr <- R.readArrayFromStorableFile path mri_shape
46 arr `deepSeqArray` return ()
50 {-# INLINE bracket #-}
51 bracket :: Int -> Int -> Int -> Word16
55 | otherwise = truncate (r * 255)
57 numerator = fromIntegral (x - low) :: Double
58 denominator = fromIntegral (high - low) :: Double
59 r = numerator/denominator
63 flip16 :: Word16 -> Word16
65 shift xx 8 .|. (shift xx (-8) .&. 0x00ff)
68 bracket_array :: (Shape sh) => (RawData sh) -> (RawData sh)
70 R.map (bracket mri_lower_threshold mri_upper_threshold . fromIntegral . flip16) arr
72 flip_y :: RawData3D -> RawData3D
74 R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. (mri_height - 1) - y :. x))
76 flip_x :: RawData3D -> RawData3D
78 R.traverse arr id (\get (Z :. z :. y :. x) -> get (Z :. z :. y :. (mri_width - 1) - x))
80 write_word16s :: (Shape sh) => FilePath -> (RawData sh) -> IO ()
81 write_word16s = R.writeArrayToStorableFile
84 values_to_colors :: (Shape sh) => (Values sh) -> (ColorData sh)
85 values_to_colors arr =
86 R.force $ R.map (truncate_rgb . ramp_it) arr
88 ramp_it :: Double -> (Double, Double, Double)
93 rampColorHotToCold 0 255 x
95 truncate_rgb :: (Double, Double, Double) -> (Word8, Word8, Word8)
96 truncate_rgb (r, g, b) =
99 r' = truncate (r * 255)
100 g' = truncate (g * 255)
101 b' = truncate (b * 255)
104 red_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
106 R.map (get_r . rgb_to_dbl)
108 get_r :: (Double, Double, Double) -> Double
111 green_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
113 R.map (get_g . rgb_to_dbl)
115 get_g :: (Double, Double, Double) -> Double
119 blue_dbl_data :: (Shape sh) => (ColorData sh) -> Array sh Double
121 R.map (get_b . rgb_to_dbl)
123 get_b :: (Double, Double, Double) -> Double
128 z_slice :: Elt a => Int -> Array DIM3 a -> Array DIM2 a
130 slice arr (Any :. n :. All :. All)