fork download
  1.  
  2. import System.Environment
  3. import qualified Data.ByteString as B
  4. import qualified Data.ByteString.Char8 as BC
  5. import System.Random
  6.  
  7. main :: IO ()
  8. main = do
  9. args <- getArgs
  10. let fileName = head args
  11. imageFile <- BC.readFile fileName
  12. glitched <- randomReplaceByte imageFile
  13. let glitchedFileName = mconcat ["glitched_", fileName]
  14. BC.writeFile glitchedFileName glitched
  15. print "all done"
  16.  
  17.  
  18. intToChar :: Int -> Char
  19. intToChar int = toEnum safeInt
  20. where safeInt = int `mod` 255
  21.  
  22. intToBC :: Int -> BC.ByteString
  23. intToBC int = BC.pack [intToChar int]
  24.  
  25.  
  26. replaceByte :: Int -> Int -> BC.ByteString -> BC.ByteString
  27. replaceByte loc charVal bytes = mconcat [before,newChar,after]
  28. where (before,rest) = BC.splitAt loc bytes
  29. after = BC.drop 1 rest
  30. newChar = intToBC charVal
  31.  
  32.  
  33. randomReplaceByte :: BC.ByteString -> IO BC.ByteString
  34. randomReplaceByte bytes = do
  35. let bytesLength = BC.length bytes
  36. location <- randomRIO (1, bytesLength)
  37. charVal <- randomRIO (0, 255)
  38. return (replaceByte location charVal bytes)
  39.  
  40.  
  41.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:29:9: error: parse error on input ‘after’
   |
29 |         after = BC.drop 1 rest
   |         ^^^^^
stdout
Standard output is empty