From 01003c1ee04c8dff8114f120d4f836ebba818b7e Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 19 Nov 2024 14:17:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D1=80=D0=B0=D1=81=D0=B8=D0=B2=D0=B5?= =?UTF-8?q?=D0=BD=D1=8C=D0=BA=D0=B8=D0=B9=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 462ae35..96a7a03 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -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 \ No newline at end of file + putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\"" + writeFile decodedTextPath decryptedText + putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\"" \ No newline at end of file