From 54e266b8991bec7f8c4e3bf841f324f6c4a5e19a Mon Sep 17 00:00:00 2001 From: Arity-T Date: Mon, 18 Nov 2024 03:45:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=B3=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 10 +++++++++- lab3/src/Lib.hs | 34 +++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 297644f..ae30eeb 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -4,9 +4,14 @@ import Codec.Picture import qualified Data.Vector.Unboxed as VU import Lib + caesarShift :: Int caesarShift = 66 +bitsPerByte :: Int +bitsPerByte = 1 + + main :: IO () main = do inputText <- readFile "resources/biography.txt" @@ -29,5 +34,8 @@ main = do let img = convertRGB8 dynImg let width = imageWidth img let height = imageHeight img - let resultImage = generateImage (encodePixel 1 img encryptedTextBits) width height + let totalBits = width * height * 3 * bitsPerByte + let bitsPadded = encryptedTextBits VU.++ VU.replicate (totalBits - VU.length encryptedTextBits) 0 + + let resultImage = generateImage (encodePixel bitsPerByte img bitsPadded) width height saveBmpImage "tmp/david.bmp" (ImageRGB8 resultImage) diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index 192ffc9..da7f866 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -10,6 +10,7 @@ module Lib import Codec.Picture +import Data.Word (Word8) import Data.Char (ord, chr) import Data.Bits (testBit) import qualified Data.Vector.Unboxed as VU @@ -41,21 +42,36 @@ decryptCaesar alphabet shift = textToBits :: String -> VU.Vector Int textToBits text = VU.fromList $ concatMap charToBits text - where - charToBits c = [if testBit (ord c) i then 1 else 0 | i <- [7,6..0]] + +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) - where - bitsToInt charBits = - sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]] + +bitsToInt :: VU.Vector Int -> Int +bitsToInt charBits = + sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]] encodePixel :: Int -> Image PixelRGB8 -> VU.Vector Int -> Int -> Int -> PixelRGB8 -encodePixel encodeBitsCount img bits x y = PixelRGB8 newR newG newB +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 + + intToWord8 :: Int -> Word8 + intToWord8 x = fromIntegral x + + bitsR = intToWord8 $ (VU.take bitsPerByte pixelBits) VU.! 0 + bitsG = bitsToInt $ VU.take bitsPerByte $ VU.drop bitsPerByte pixelBits + bitsB = bitsToInt $ VU.drop (2 * bitsPerByte) pixelBits + PixelRGB8 r g b = pixelAt img x y - newR = b - newG = g - newB = r + newR = bitsR + newG = intToWord8 bitsG + newB = b