diff options
| author | 2026-11-08 04:19:33 +0000 | |
|---|---|---|
| committer | 2026-11-08 04:22:48 +0000 | |
| commit | a4aafb5caea268dae350bdaa6a9c53b65b00910e (patch) | |
| tree | b9f0b26d9c835963e7bb65ddc31ec4b49976d7d7 /src | |
| download | lab-03-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/Lib.hs | 115 |
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 @@ | |||
| 1 | module Lib | ||
| 2 | ( encryptCaesar | ||
| 3 | , decryptCaesar | ||
| 4 | , textToBits | ||
| 5 | , bitsToText | ||
| 6 | , encodePixel | ||
| 7 | , extractShift | ||
| 8 | , extractBitsFromImage | ||
| 9 | ) where | ||
| 10 | |||
| 11 | import Codec.Picture | ||
| 12 | import Data.Bits ((.&.), (.|.), complement, shiftL, testBit) | ||
| 13 | import Data.Char (chr, isDigit, isLower, isUpper, ord) | ||
| 14 | import qualified Data.Vector.Unboxed as VU | ||
| 15 | import Data.Word (Word8) | ||
| 16 | import Text.Read (readMaybe) | ||
| 17 | |||
| 18 | addWithWrapInRange :: Int -> Int -> Int -> Int -> Int | ||
| 19 | addWithWrapInRange base increment lower upper = | ||
| 20 | lower + (base - lower + increment) `mod` (upper - lower + 1) | ||
| 21 | |||
| 22 | encryptCaesar :: Int -> String -> String | ||
| 23 | encryptCaesar 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 | |||
| 31 | decryptCaesar :: Int -> String -> String | ||
| 32 | decryptCaesar shift = encryptCaesar (- shift) | ||
| 33 | |||
| 34 | textToBits :: String -> VU.Vector Int | ||
| 35 | textToBits text = VU.fromList $ concatMap charToBits text | ||
| 36 | |||
| 37 | charToBits :: Char -> [Int] | ||
| 38 | charToBits c = | ||
| 39 | [ if testBit (ord c) i | ||
| 40 | then 1 | ||
| 41 | else 0 | ||
| 42 | | i <- [7,6 .. 0] | ||
| 43 | ] | ||
| 44 | |||
| 45 | bitsToText :: VU.Vector Int -> String | ||
| 46 | bitsToText bits | ||
| 47 | | VU.null bits = [] | ||
| 48 | | otherwise = | ||
| 49 | (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits) | ||
| 50 | |||
| 51 | bitsToInt :: VU.Vector Int -> Int | ||
| 52 | bitsToInt 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 | |||
| 60 | intToWord8 :: Int -> Word8 | ||
| 61 | intToWord8 x = fromIntegral x | ||
| 62 | |||
| 63 | word8ToInt :: Word8 -> Int | ||
| 64 | word8ToInt x = fromIntegral x | ||
| 65 | |||
| 66 | createMask :: Int -> Int | ||
| 67 | createMask shift = shiftL (complement 0) shift .&. complement 0 | ||
| 68 | |||
| 69 | encodePixel :: | ||
| 70 | Int -> Image PixelRGB8 -> VU.Vector Int -> Int -> Int -> PixelRGB8 | ||
| 71 | encodePixel 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 | |||
| 86 | extractBits :: Int -> Pixel8 -> [Int] | ||
| 87 | extractBits bitsPerByte pixelByte = | ||
| 88 | [ if testBit pixelByte i | ||
| 89 | then 1 | ||
| 90 | else 0 | ||
| 91 | | i <- [bitsPerByte - 1,bitsPerByte - 2 .. 0] | ||
| 92 | ] | ||
| 93 | |||
| 94 | extractBitsFromPixel :: Int -> PixelRGB8 -> [Int] | ||
| 95 | extractBitsFromPixel 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 | |||
| 101 | extractBitsFromImage :: Int -> Image PixelRGB8 -> [Int] | ||
| 102 | extractBitsFromImage 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 | |||
| 109 | extractShift :: String -> Maybe Int | ||
| 110 | extractShift path = | ||
| 111 | let shift = | ||
| 112 | takeWhile | ||
| 113 | (`elem` ['0' .. '9']) | ||
| 114 | (reverse $ takeWhile (/= '-') (reverse path)) | ||
| 115 | in readMaybe shift | ||
