-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31binaryData.hs
137 lines (99 loc) · 5.03 KB
/
31binaryData.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import System.Random
import qualified Data.Text.IO as TIO
import Control.Monad
{-Q1:
Write a function that takes numbers in ASCII character form
and converts them to Ints.
For example, make the following an Int:-
bcInt :: BC.ByteString
bcInt = "6"
-}
bcInt :: BC.ByteString
bcInt = "6"
bcToInt :: BC.ByteString -> Int
bcToInt givenByteString = (read . BC.unpack) givenByteString
intToBC :: Int -> BC.ByteString
intToBC givenInt = BC.pack [toEnum (givenInt `mod` 255)]
{-This function will take the location of the byte to be replaced,
the Int value of the new Char/Byte to go there, and the bytes of the image file
.. Then you’ll drop one from the rest of the bytes to make room for your new byte.
Finally, you’ll concatenate the new byte in the middle of these two sections
-}
replaceByte :: Int -> Int -> BC.ByteString -> BC.ByteString
replaceByte loc charVal bytes = mconcat [before,middlePart,modifiedAfter]
where (before,after) = BC.splitAt loc bytes
modifiedAfter = BC.drop 1 after
middlePart = intToBC charVal
{- You’ll be using randomRIO from System.Random.
randomRIO will take a pair of values in a tuple and randomly give you a number in that range.
Your IO action will be named randomReplaceByte.
All randomReplaceByte needs to do is pick two random numbers: one for the Char, and one for the location.-}
randomReplaceByte :: BC.ByteString -> IO BC.ByteString
randomReplaceByte bytes = do
randomChar <- randomRIO(0,255)
randomLocation <- randomRIO(0,BC.length bytes)
let modifiedBytes = replaceByte randomLocation randomChar bytes
return modifiedBytes
{-Now you can use this IO action in your main to modify your image file:-}
glitch :: FilePath -> IO ()
glitch filename = do
--putStrLn "Enter filename"
--filename <- getLine
contents <- BC.readFile filename
--putStrLn (BC.unpack contents)
glitched <- randomReplaceByte contents
BC.writeFile "out.jpg" glitched
{-
<- is used because getLine returns an IO String , but we want to use name like a regular String
let statement = hello name -- you have to use let whenever we create variables that AREN'T IO type
-}
retrieveRandomByte :: BC.ByteString -> BC.ByteString
retrieveRandomByte bytes = modifiedAfter
where modifiedAfter = BC.drop ((BC.length bytes) - 1) bytes
computeNumberOfBytes :: IO ()
computeNumberOfBytes = do
putStrLn "Enter filename"
filename <- getLine
contents <- BC.readFile filename
putStrLn (show (BC.length contents ) ++ " Bytes" )
generateRandomChar :: IO Char
generateRandomChar = do
randomChar <- randomRIO(0,255)
return (toEnum randomChar)
{- Here’s your sortSection function,
which takes a starting point of the section,
a size of the section, and the byte stream.-}
sortSection :: Int -> Int -> BC.ByteString -> BC.ByteString
sortSection start size bytes = mconcat [before,sortedSection,ending]
where (before,rest) = BC.splitAt start bytes
(target,ending) = BC.splitAt size rest
sortedSection = BC.sort target
randomSortSelection :: BC.ByteString -> IO BC.ByteString
randomSortSelection bytes = do
randomStartLocation <- randomRIO(0,BC.length bytes)
randomSize <- randomRIO(0,BC.length bytes `div` 2)
let modifiedBytes = sortSection randomStartLocation randomSize bytes
return modifiedBytes
glitch2 :: String -> IO ()
glitch2 filename = do
contents <- BC.readFile filename
glitched <- randomSortSelection contents
BC.writeFile "out.jpg" glitched
glitch3 :: String -> IO ()
glitch3 filename = do
contents <- BC.readFile filename
glitched1 <- randomSortSelection contents
glitched2 <- randomReplaceByte glitched1
glitched3 <- randomSortSelection glitched2
glitched4 <- randomReplaceByte glitched3
glitched6 <- randomSortSelection glitched4
BC.writeFile "out.jpg" glitched6
-- how to foldl the glitch
glitch4 :: String -> IO ()
glitch4 filename = do
contents <- BC.readFile filename
out <- foldM (\bytes func -> func bytes ) contents [randomReplaceByte, randomSortSelection,randomReplaceByte,randomSortSelection]
BC.writeFile "out2.jpg" out