aboutsummaryrefslogtreecommitdiff
path: root/src/Lib.hs
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 04:19:33 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 04:22:48 +0000
commita4aafb5caea268dae350bdaa6a9c53b65b00910e (patch)
treeb9f0b26d9c835963e7bb65ddc31ec4b49976d7d7 /src/Lib.hs
downloadlab-03-a4aafb5caea268dae350bdaa6a9c53b65b00910e.tar.gz
publishHEADmaster
Diffstat (limited to 'src/Lib.hs')
-rw-r--r--src/Lib.hs115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
new file mode 100644
index 0000000..250cc5c
--- /dev/null
+++ b/src/Lib.hs
@@ -0,0 +1,115 @@
1module Lib
2 ( encryptCaesar
3 , decryptCaesar
4 , textToBits
5 , bitsToText
6 , encodePixel
7 , extractShift
8 , extractBitsFromImage
9 ) where
10
11import Codec.Picture
12import Data.Bits ((.&.), (.|.), complement, shiftL, testBit)
13import Data.Char (chr, isDigit, isLower, isUpper, ord)
14import qualified Data.Vector.Unboxed as VU
15import Data.Word (Word8)
16import Text.Read (readMaybe)
17
18addWithWrapInRange :: Int -> Int -> Int -> Int -> Int
19addWithWrapInRange base increment lower upper =
20 lower + (base - lower + increment) `mod` (upper - lower + 1)
21
22encryptCaesar :: Int -> String -> String
23encryptCaesar shift text = map caesarChar text
24 where
25 caesarChar c
26 | isDigit c = chr $ addWithWrapInRange (ord c) shift (ord '0') (ord '9')
27 | isUpper c = chr $ addWithWrapInRange (ord c) shift (ord 'A') (ord 'Z')
28 | isLower c = chr $ addWithWrapInRange (ord c) shift (ord 'a') (ord 'z')
29 | otherwise = c
30
31decryptCaesar :: Int -> String -> String
32decryptCaesar shift = encryptCaesar (- shift)
33
34textToBits :: String -> VU.Vector Int
35textToBits text = VU.fromList $ concatMap charToBits text
36
37charToBits :: Char -> [Int]
38charToBits c =
39 [ if testBit (ord c) i
40 then 1
41 else 0
42 | i <- [7,6 .. 0]
43 ]
44
45bitsToText :: VU.Vector Int -> String
46bitsToText bits
47 | VU.null bits = []
48 | otherwise =
49 (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits)
50
51bitsToInt :: VU.Vector Int -> Int
52bitsToInt bits =
53 sum
54 [ bit * (2 ^ index)
55 | (bit, index) <- zip (VU.toList bits) [len,(len - 1) .. 0]
56 ]
57 where
58 len = VU.length bits - 1
59
60intToWord8 :: Int -> Word8
61intToWord8 x = fromIntegral x
62
63word8ToInt :: Word8 -> Int
64word8ToInt x = fromIntegral x
65
66createMask :: Int -> Int
67createMask shift = shiftL (complement 0) shift .&. complement 0
68
69encodePixel ::
70 Int -> Image PixelRGB8 -> VU.Vector Int -> Int -> Int -> PixelRGB8
71encodePixel bitsPerByte img bits x y = PixelRGB8 newR newG newB
72 where
73 width = imageWidth img
74 index = x + y * width
75 startPos = index * 3 * bitsPerByte
76 pixelBits = VU.slice startPos (3 * bitsPerByte) bits
77 bitsIntR = bitsToInt $ VU.slice 0 bitsPerByte pixelBits
78 bitsIntG = bitsToInt $ VU.slice bitsPerByte bitsPerByte pixelBits
79 bitsIntB = bitsToInt $ VU.slice (2 * bitsPerByte) bitsPerByte pixelBits
80 mask = createMask bitsPerByte
81 PixelRGB8 r g b = pixelAt img x y
82 newR = intToWord8 $ ((word8ToInt r) .&. mask) .|. bitsIntR
83 newG = intToWord8 $ ((word8ToInt g) .&. mask) .|. bitsIntG
84 newB = intToWord8 $ ((word8ToInt b) .&. mask) .|. bitsIntB
85
86extractBits :: Int -> Pixel8 -> [Int]
87extractBits bitsPerByte pixelByte =
88 [ if testBit pixelByte i
89 then 1
90 else 0
91 | i <- [bitsPerByte - 1,bitsPerByte - 2 .. 0]
92 ]
93
94extractBitsFromPixel :: Int -> PixelRGB8 -> [Int]
95extractBitsFromPixel bitsPerByte (PixelRGB8 r g b) =
96 let bitsR = extractBits bitsPerByte r
97 bitsG = extractBits bitsPerByte g
98 bitsB = extractBits bitsPerByte b
99 in bitsR ++ bitsG ++ bitsB
100
101extractBitsFromImage :: Int -> Image PixelRGB8 -> [Int]
102extractBitsFromImage bitsPerByte img =
103 let width = imageWidth img
104 height = imageHeight img
105 pixels =
106 [pixelAt img x y | y <- [0 .. height - 1], x <- [0 .. width - 1]]
107 in concatMap (extractBitsFromPixel bitsPerByte) pixels
108
109extractShift :: String -> Maybe Int
110extractShift path =
111 let shift =
112 takeWhile
113 (`elem` ['0' .. '9'])
114 (reverse $ takeWhile (/= '-') (reverse path))
115 in readMaybe shift