Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. -- import Data.Char
  3. import System.IO
  4. import System.Environment
  5. -- import qualified Data.Text as T
  6. -- import qualified Data.Text.IO as TI
  7. import System.Random
  8. import qualified Data.ByteString as B
  9. import qualified Data.ByteString.Char8 as BC
  10.  
  11. intToChar :: Int -> Char
  12. intToChar int = toEnum safeInt
  13. where safeInt = int `mod` 255
  14.  
  15. intToBC :: Int -> BC.ByteString
  16. intToBC int = BC.pack [intToChar int]
  17.  
  18. replaceByte :: Int -> Int -> BC.ByteString -> BC.ByteString
  19. replaceByte loc charVal bytes = mconcat [before,newChar,after]
  20. where (before,rest) = BC.splitAt loc bytes
  21. after = BC.drop 1 rest
  22. newChar = intToBC charVal
  23.  
  24. randomReplaceByte :: BC.ByteString -> IO BC.ByteString
  25. randomReplaceByte bytes = do
  26. let bytesLength = BC.length bytes
  27. location <- randomRIO (1,bytesLength)
  28. charVal <- randomRIO (0,255)
  29. return (replaceByte location charVal bytes)
  30.  
  31. main :: IO ()
  32. main = do
  33. args <- getArgs
  34. let filename = args !! 0
  35. imageFile <- BC.readFile filename
  36. glitched <- randomReplaceByte imageFile
  37. let glitchedFileName = mconcat ["glitched_",filename]
  38. BC.writeFile glitchedFileName glitched
  39. putStrLn "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement