Advertisement
Guest User

Untitled

a guest
Mar 18th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Main (main) where
  3. import System.Environment
  4. import System.Random
  5. import qualified Data.ByteString as B
  6. import qualified Data.ByteString.Char8 as BC
  7.  
  8. intToChar :: Int -> Char
  9. intToChar int = toEnum safeInt
  10. where safeInt = int `mod` 255
  11.  
  12. intToBC :: Int -> BC.ByteString
  13. intToBC int = BC.pack [intToChar int]
  14.  
  15. replaceByte :: Int -> Int -> BC.ByteString -> BC.ByteString
  16. replaceByte loc charVal bytes = mconcat [before,newChar,after]
  17. where (before,rest) = BC.splitAt loc bytes
  18. after = BC.drop 1 rest
  19. newChar = intToBC charVal
  20.  
  21. randomReplaceByte :: BC.ByteString -> IO BC.ByteString
  22. randomReplaceByte bytes = do
  23. let bytesLength = BC.length bytes
  24. location <- randomRIO (1,bytesLength)
  25. charVal <- randomRIO (0,255)
  26. return (replaceByte location charVal bytes)
  27.  
  28. sortSection :: Int -> Int -> BC.ByteString -> BC.ByteString
  29. sortSection start size bytes = mconcat [before,changed,after]
  30. where (before,rest) = BC.splitAt start bytes
  31. (target,after) = BC.splitAt size rest
  32. changed = BC.reverse (BC.sort target)
  33.  
  34. randomSortSection :: BC.ByteString -> IO BC.ByteString
  35. randomSortSection bytes = do
  36. let sectionSize = 25
  37. let bytesLength = BC.length bytes
  38. start <- randomRIO (0,bytesLength - sectionSize)
  39. return (sortSection start sectionSize bytes)
  40.  
  41. main :: IO ()
  42. main = do
  43. args <- getArgs
  44. let fileName = head args
  45. imageFile <- BC.readFile fileName
  46. glitched <- randomSortSection imageFile
  47. let glitchedFileName = mconcat ["glitched_",fileName]
  48. BC.writeFile glitchedFileName glitched
  49. print "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement