module Main where import Types import PPM import Functions import System.IO import System.Environment import Data.Maybe pic0, pic1 :: Picture pic0 = readPicture "P1\n1 1\n0" pic1 = readPicture "P1\n2 2\n0 1 1 0" pic2 = readPicture $ "P2\n" ++ "4 5 255\n" ++ "0 63 127 191 255\n" ++ "0 10 20 30 40\n" ++ "100 100 100 100 100\n" ++ "4 3 2 1 0\n" pic2' = (showPNM . pictureToPGM) pic2 -- -------------------------------------- -- -- basic file input output getFileContent :: String -> IO String getFileContent f | f == "" || f == "-" = getContents | f == "0" = return "P1\n1 1\n0" | otherwise = readFile f openOutputFile :: String -> IO Handle openOutputFile f | f == "" || f == "-" = return stdout | otherwise = openFile f WriteMode -- -------------------------------------- changePicture :: (Picture -> Picture) -> String -> String -> IO() changePicture fct inp outp = do p <- getFileContent inp f <- openOutputFile outp hPutStr f (processPicture p) hClose f where processPicture = showPNM . pictureToPGM . fct . readPicture -- -------------------------------------- getParams :: IO [String] getParams = do args <- getArgs return (args ++ repeat "") main :: IO() main = do args <-getParams main1 args main1 :: [String] -> IO() main1 (opr : inp : outp : _) = do changePicture (lookupOp opr) inp outp where lookupOp op = (fromMaybe id . lookup op) fctTab fctTab :: [(String, Picture -> Picture)] fctTab = [ ("id" , id ) , ("bitmap" , bitMap ) , ("fliph" , flipH ) , ("flipv" , flipV ) , ("gamma.5" , gamma 0.5 ) , ("gamma2" , gamma 2.0 ) , ("halfsize" , halfSize ) , ("invert" , invert ) -- map (map (\ x -> 1.0 - x))) , ("id" , id ) , ("reduce3" , reduceColors 3 ) , ("rot90" , rot90 ) , ("rot180" , rot180 ) , ("rot270" , rot270 ) , ("stretch" , stretch ) ] -- --------------------------------------