Last active
February 25, 2020 02:00
-
-
Save pedrofurla/ea2286ff3bf96d9a3baa869e35f156b9 to your computer and use it in GitHub Desktop.
Either mixing up with IO and vice-versa to my first working version using ExceptT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run0 :: IO () | |
| run0 = do | |
| args' <- getArgs | |
| let args = input args' | |
| dynImg <- sequence $ readImageWithMetadata . fst <$> args | |
| let img = getImg $ join dynImg | |
| let bytes = LB.pack . compress <$> img | |
| let writeTmp = LB.writeFile . snd <$> args | |
| wrote <- sequence $ do | |
| bits' <- bytes | |
| write' <- writeTmp | |
| return $ write' bits' | |
| let info = do | |
| img' <- img | |
| let width = imageWidth img' | |
| let height = imageHeight img' | |
| let imgSize = show . V.length $ imageData img' | |
| let pixsLength = width * height | |
| bytes' <- bytes | |
| let bytesLength = (show . LB.length) bytes' | |
| return $ "PNG size " ++ show width ++ "x" ++ show height ++ | |
| " using " ++ imgSize ++ " bytes " ++ | |
| "Reduced to " ++ bytesLength ++ | |
| " bytes (" ++ show pixsLength ++ " / 8 with " ++ show (pixsLength `rem` 8) ++ | |
| " bit(s) of padding)." | |
| either putStrLn (const $ return ()) wrote | |
| either (const $ return ()) putStrLn info | |
| where | |
| input [] = Left "Not enough arguments" | |
| input [_] = Left "Not enough arguments / TODO give info" | |
| input (infile : outfile : _) = Right (infile, outfile) | |
| getImg :: Either String (DynamicImage, Metadatas) -> Either String (Image PixelRGBA8) | |
| getImg dynImg = | |
| case dynImg of | |
| Left err -> Left err | |
| Right (ImageRGBA8 img, _) -> Right img | |
| Right _ -> Left "Unhandled image colorspace" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run1 :: IO () | |
| run1 = either putStrLn putStrLn =<< stuffs | |
| where | |
| stuffs :: IO (Either String String) | |
| stuffs = runExceptT $ do | |
| args' <- lift getArgs :: ExceptT String IO [String] | |
| (infile, outfile) <- (ExceptT . pure . input) args' | |
| (img', _) <- ExceptT $ readImageWithMetadata infile | |
| img <- (ExceptT . pure . getImg) img' | |
| let bytes = LB.pack $ compress img | |
| ExceptT . (either (Left . show) Right <$>) $ try @SomeException $ LB.writeFile outfile bytes | |
| let width = imageWidth img | |
| let height = imageHeight img | |
| let imgSize = show . V.length $ imageData img | |
| let pixsLength = width * height | |
| let bytesLength = (show . LB.length) bytes | |
| let msg = "PNG size " ++ show width ++ "x" ++ show height ++ | |
| " using " ++ imgSize ++ " bytes " ++ | |
| "Reduced to " ++ bytesLength ++ | |
| " bytes (" ++ show pixsLength ++ " / 8 with " ++ show (pixsLength `rem` 8) ++ | |
| " bit(s) of padding)." | |
| ExceptT $ Right <$> pure msg | |
| input :: [String] -> Either String (String, String) | |
| input [] = Left "Not enough arguments" | |
| input [_] = Left "Not enough arguments / TODO give info" | |
| input (infile : outfile : _) = Right (infile, outfile) | |
| getImg :: DynamicImage -> Either String (Image PixelRGBA8) | |
| getImg img = | |
| case img of | |
| ImageRGBA8 img' -> Right img' | |
| _ -> Left "Unhandled image colorspace" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment