Красивенький вывод в консольку
This commit is contained in:
@@ -11,20 +11,31 @@ caesarShift = 66
|
||||
bitsPerByte :: Int
|
||||
bitsPerByte = 1
|
||||
|
||||
sourceTextPath :: String
|
||||
sourceTextPath = "resources/biography.txt"
|
||||
sourceImagePath :: String
|
||||
sourceImagePath = "resources/david.bmp"
|
||||
encodedImagePath :: String
|
||||
encodedImagePath = "tmp/david.bmp"
|
||||
decodedTextPath :: String
|
||||
decodedTextPath = "tmp/biography.txt"
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "Чтение текста из файла \"resources/biography.txt\""
|
||||
inputText <- readFile "resources/biography.txt"
|
||||
putStrLn $ "Чтение текста из файла \"" ++ sourceTextPath ++ "\""
|
||||
inputText <- readFile sourceTextPath
|
||||
putStrLn $ "10 символов текста: \"" ++ take 10 inputText ++ "\""
|
||||
|
||||
putStrLn "\nШифрование текста"
|
||||
let alphabet = createAlphabetFromText inputText
|
||||
putStrLn $ "Размер алфавита: " ++ show (length alphabet)
|
||||
let encryptedText = encryptCaesar alphabet caesarShift inputText
|
||||
putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedText ++ "\""
|
||||
let encryptedTextBits = textToBits encryptedText
|
||||
putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList encryptedTextBits) ++ "\""
|
||||
|
||||
putStrLn "\nКодирование текста в изображение"
|
||||
readSourceImageResult <- readImage "resources/david.bmp"
|
||||
readSourceImageResult <- readImage sourceImagePath
|
||||
case readSourceImageResult of
|
||||
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
||||
Right dynImg -> do
|
||||
@@ -32,20 +43,24 @@ main = do
|
||||
let width = imageWidth img
|
||||
let height = imageHeight img
|
||||
let totalBits = width * height * 3 * bitsPerByte
|
||||
let bitsPadded = encryptedTextBits VU.++ VU.replicate (totalBits - VU.length encryptedTextBits) 0
|
||||
let bits = encryptedTextBits VU.++ VU.replicate (totalBits - VU.length encryptedTextBits) 0
|
||||
|
||||
let resultImage = generateImage (encodePixel bitsPerByte img bitsPadded) width height
|
||||
saveBmpImage "tmp/david.bmp" (ImageRGB8 resultImage)
|
||||
let resultImage = generateImage (encodePixel bitsPerByte img bits) width height
|
||||
saveBmpImage encodedImagePath (ImageRGB8 resultImage)
|
||||
putStrLn $ "Изображение сохранено по пути: \"" ++ encodedImagePath ++ "\""
|
||||
|
||||
|
||||
putStrLn "\nДекодирование текста из изображения"
|
||||
readEncodedImageResult <- readImage "tmp/david.bmp"
|
||||
readEncodedImageResult <- readImage encodedImagePath
|
||||
case readEncodedImageResult of
|
||||
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
||||
Right dynImg -> do
|
||||
let img = convertRGB8 dynImg
|
||||
let bits = VU.fromList $ extractBitsFromImage bitsPerByte img
|
||||
let fullText = bitsToText bits
|
||||
let encryptedTextFromImage = takeWhile (/= '\NUL') fullText
|
||||
putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList bits) ++ "\""
|
||||
let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits)
|
||||
putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedTextFromImage ++ "\""
|
||||
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromImage
|
||||
writeFile "tmp/biography.txt" decryptedText
|
||||
putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\""
|
||||
writeFile decodedTextPath decryptedText
|
||||
putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\""
|
||||
Reference in New Issue
Block a user