aboutsummaryrefslogtreecommitdiff
path: root/src/Lib.hs
blob: 250cc5c013e2dd7b9f7f15ac8f17f69b3084bdbe (plain)
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
module Lib
  ( encryptCaesar
  , decryptCaesar
  , textToBits
  , bitsToText
  , encodePixel
  , extractShift
  , extractBitsFromImage
  ) where

import Codec.Picture
import Data.Bits ((.&.), (.|.), complement, shiftL, testBit)
import Data.Char (chr, isDigit, isLower, isUpper, ord)
import qualified Data.Vector.Unboxed as VU
import Data.Word (Word8)
import Text.Read (readMaybe)

addWithWrapInRange :: Int -> Int -> Int -> Int -> Int
addWithWrapInRange base increment lower upper =
  lower + (base - lower + increment) `mod` (upper - lower + 1)

encryptCaesar :: Int -> String -> String
encryptCaesar shift text = map caesarChar text
  where
    caesarChar c
      | isDigit c = chr $ addWithWrapInRange (ord c) shift (ord '0') (ord '9')
      | isUpper c = chr $ addWithWrapInRange (ord c) shift (ord 'A') (ord 'Z')
      | isLower c = chr $ addWithWrapInRange (ord c) shift (ord 'a') (ord 'z')
      | otherwise = c

decryptCaesar :: Int -> String -> String
decryptCaesar shift = encryptCaesar (- shift)

textToBits :: String -> VU.Vector Int
textToBits text = VU.fromList $ concatMap charToBits text

charToBits :: Char -> [Int]
charToBits c =
  [ if testBit (ord c) i
    then 1
    else 0
  | i <- [7,6 .. 0]
  ]

bitsToText :: VU.Vector Int -> String
bitsToText bits
  | VU.null bits = []
  | otherwise =
    (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits)

bitsToInt :: VU.Vector Int -> Int
bitsToInt bits =
  sum
    [ bit * (2 ^ index)
    | (bit, index) <- zip (VU.toList bits) [len,(len - 1) .. 0]
    ]
  where
    len = VU.length bits - 1

intToWord8 :: Int -> Word8
intToWord8 x = fromIntegral x

word8ToInt :: Word8 -> Int
word8ToInt x = fromIntegral x

createMask :: Int -> Int
createMask shift = shiftL (complement 0) shift .&. complement 0

encodePixel ::
     Int -> Image PixelRGB8 -> VU.Vector Int -> Int -> Int -> PixelRGB8
encodePixel bitsPerByte img bits x y = PixelRGB8 newR newG newB
  where
    width = imageWidth img
    index = x + y * width
    startPos = index * 3 * bitsPerByte
    pixelBits = VU.slice startPos (3 * bitsPerByte) bits
    bitsIntR = bitsToInt $ VU.slice 0 bitsPerByte pixelBits
    bitsIntG = bitsToInt $ VU.slice bitsPerByte bitsPerByte pixelBits
    bitsIntB = bitsToInt $ VU.slice (2 * bitsPerByte) bitsPerByte pixelBits
    mask = createMask bitsPerByte
    PixelRGB8 r g b = pixelAt img x y
    newR = intToWord8 $ ((word8ToInt r) .&. mask) .|. bitsIntR
    newG = intToWord8 $ ((word8ToInt g) .&. mask) .|. bitsIntG
    newB = intToWord8 $ ((word8ToInt b) .&. mask) .|. bitsIntB

extractBits :: Int -> Pixel8 -> [Int]
extractBits bitsPerByte pixelByte =
  [ if testBit pixelByte i
    then 1
    else 0
  | i <- [bitsPerByte - 1,bitsPerByte - 2 .. 0]
  ]

extractBitsFromPixel :: Int -> PixelRGB8 -> [Int]
extractBitsFromPixel bitsPerByte (PixelRGB8 r g b) =
  let bitsR = extractBits bitsPerByte r
      bitsG = extractBits bitsPerByte g
      bitsB = extractBits bitsPerByte b
   in bitsR ++ bitsG ++ bitsB

extractBitsFromImage :: Int -> Image PixelRGB8 -> [Int]
extractBitsFromImage bitsPerByte img =
  let width = imageWidth img
      height = imageHeight img
      pixels =
        [pixelAt img x y | y <- [0 .. height - 1], x <- [0 .. width - 1]]
   in concatMap (extractBitsFromPixel bitsPerByte) pixels

extractShift :: String -> Maybe Int
extractShift path =
  let shift =
        takeWhile
          (`elem` ['0' .. '9'])
          (reverse $ takeWhile (/= '-') (reverse path))
   in readMaybe shift