]> gitweb.michael.orlitzky.com - spline3.git/blobdiff - src/Main.hs
Flip bytes again before outputting the 3D data.
[spline3.git] / src / Main.hs
index c8ba34554feb27bb2cd4f0f2d16aa36b0f3bd34a..6848ff86264c3fc461e554cc2067cd6f312db2d4 100644 (file)
+{-# LANGUAGE RecordWildCards, DoAndIfThenElse #-}
+
 module Main
 where
 
-import Data.Array.Repa (
-  DIM3,
-  Z(..),
-  (:.)(..),
+import Data.Maybe (fromJust)
+import Control.Monad (when)
+import qualified Data.Array.Repa as R
+import Data.Maybe (isJust)
+import GHC.Conc (getNumProcessors, setNumCapabilities)
+import System.IO (hPutStrLn, stderr)
+import System.Exit (exitSuccess, exitWith, ExitCode(..))
+
+import CommandLine (Args(..), apply_args)
+import ExitCodes
+import Grid (zoom)
+import Volumetric (
+  bracket_array,
+  flip_x,
+  flip_y,
+  read_word16s,
+  round_array,
+  swap_bytes,
+  write_values_to_bmp,
+  write_word16s,
+  z_slice
   )
 
-import System.Environment (getArgs)
 
-import Values (read_values_3d, write_values_1d)
-import Grid (make_grid, zoom)
+validate_args :: Args -> IO ()
+validate_args Args{..} = do
+  when (scale <= 0) $ do
+    hPutStrLn stderr "ERROR: scale must be greater than zero."
+    exitWith (ExitFailure exit_arg_not_positive)
+
+  when (width <= 0) $ do
+    hPutStrLn stderr "ERROR: width must be greater than zero."
+    exitWith (ExitFailure exit_arg_not_positive)
+
+  when (height <= 0) $ do
+    hPutStrLn stderr "ERROR: height must be greater than zero."
+    exitWith (ExitFailure exit_arg_not_positive)
+
+  when (depth <= 0) $ do
+    hPutStrLn stderr "ERROR: depth must be greater than zero."
+    exitWith (ExitFailure exit_arg_not_positive)
+
+  case slice of
+    Just s ->
+      when (s < 0 || s > depth) $ do
+        hPutStrLn stderr "ERROR: slice must be between zero and depth."
+        exitWith (ExitFailure exit_arg_out_of_bounds)
+    Nothing -> return ()
 
-mri_shape :: DIM3
-mri_shape = (Z :. 256 :. 256 :. 1)
 
 main :: IO ()
 main = do
-  args <- getArgs
-  let color = head args
-  let in_file  = "./data/MRbrain.40." ++ color
-  let out_file = "MRbrain.40." ++ color ++ ".out"
-  mridata <- read_values_3d mri_shape in_file
-  let g = make_grid 1 mridata
-  let output = zoom g (8,8,1)
-  write_values_1d output out_file
+  args@Args{..} <- apply_args
+  -- validate_args will simply exit if there's a problem.
+  validate_args args
+
+  -- The first thing we do is set the number of processors. We get the
+  -- number of processors (cores) in the machine with
+  -- getNumProcessors, and set it with setNumCapabilities. This is so
+  -- we don't have to pass +RTS -Nfoo on the command line every time.
+  num_procs <- getNumProcessors
+  setNumCapabilities num_procs
+
+  -- Determine whether we're doing 2d or 3d. If we're given a slice,
+  -- assume 2d.
+  let shape = (R.Z R.:. depth R.:. height R.:. width) :: R.DIM3
+
+  if (isJust slice) then
+    main2d args shape
+  else
+    main3d args shape
+
+  exitSuccess
+
+  where
+
+
+
+main3d :: Args -> R.DIM3 -> IO ()
+main3d Args{..} shape = do
+  let zoom_factor = (scale, scale, scale)
+  arr <- read_word16s input shape
+  let arr_swapped = swap_bytes arr
+  let arr_shaped  = R.reshape shape arr_swapped
+  dbl_data <- R.computeUnboxedP $ R.map fromIntegral arr_shaped
+  raw_output <- zoom dbl_data zoom_factor
+  let word16_output = round_array raw_output
+  -- Switch the bytes order back to what it was. This lets us use the
+  -- same program to view the input/output data.
+  swapped_output <- R.computeUnboxedP $ swap_bytes word16_output
+  write_word16s output swapped_output
+
+
+main2d :: Args -> R.DIM3 -> IO ()
+main2d Args{..} shape = do
+  let zoom_factor = (1, scale, scale)
+  arr <- read_word16s input shape
+  arrSlice <- R.computeUnboxedP
+               $ z_slice (fromJust slice)
+               $ flip_x width
+               $ flip_y height
+               $ swap_bytes arr
+  let arrSlice' = R.reshape slice3d arrSlice
+
+  -- If zoom isn't being inlined we need to extract the slice before hand,
+  -- and convert it to the require formed.
+  dbl_data   <- R.computeUnboxedP $ R.map fromIntegral arrSlice'
+  raw_output <- zoom dbl_data zoom_factor
+  arrSlice0  <- R.computeUnboxedP $ z_slice 0 raw_output
+
+  -- Make doubles from the thresholds which are given as Ints.
+  let lt = fromIntegral lower_threshold
+  let ut = fromIntegral upper_threshold
+
+  let arr_bracketed = bracket_array lt ut arrSlice0
+  values <- R.computeUnboxedP $ R.map fromIntegral arr_bracketed
+  write_values_to_bmp output values
+
+  where
+    slice3d :: R.DIM3
+    slice3d = (R.Z R.:. 1 R.:. height  R.:. width)