From 3205247f1c2dbece325a4dc8784d5f719013cff5 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 19 Nov 2024 12:33:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=B2=20=D0=BA?= =?UTF-8?q?=D0=B0=D1=80=D1=82=D0=B8=D0=BD=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/src/Lib.hs | 58 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index da7f866..b3bf126 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -1,18 +1,19 @@ module Lib - ( - createAlphabetFromText, - encryptCaesar, - decryptCaesar, - textToBits, - bitsToText, - encodePixel - ) where + -- ( + -- createAlphabetFromText, + -- encryptCaesar, + -- decryptCaesar, + -- textToBits, + -- bitsToText, + -- encodePixel + -- ) + where import Codec.Picture import Data.Word (Word8) import Data.Char (ord, chr) -import Data.Bits (testBit) +import Data.Bits (testBit, shiftL, complement, (.|.), (.&.)) import qualified Data.Vector.Unboxed as VU createAlphabetFromText :: String -> [Char] @@ -46,14 +47,34 @@ 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]] +intToBits :: Int -> [Int] +intToBits n = [if testBit n i then 1 else 0 | i <- [31,30..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 charBits = - sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]] +bitsToInt bits = + sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList bits) [len,(len - 1)..0]] + where + len = VU.length bits - 1 + +setLastBits :: VU.Vector Int -> VU.Vector Int -> VU.Vector Int +setLastBits byte newBits = byte VU.// updates + where + newBitsLastIndex = VU.length newBits - 1 + updates = [(7 - i, newBits VU.! (newBitsLastIndex - i)) | i <- [0..newBitsLastIndex]] + +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 @@ -64,14 +85,13 @@ encodePixel bitsPerByte img bits x y = PixelRGB8 newR newG newB startPos = index * 3 * bitsPerByte pixelBits = VU.slice startPos (3 * bitsPerByte) bits - intToWord8 :: Int -> Word8 - intToWord8 x = fromIntegral x + bitsIntR = bitsToInt $ VU.slice 0 bitsPerByte pixelBits + bitsIntG = bitsToInt $ VU.slice bitsPerByte bitsPerByte pixelBits + bitsIntB = bitsToInt $ VU.slice (2 * bitsPerByte) bitsPerByte pixelBits - bitsR = intToWord8 $ (VU.take bitsPerByte pixelBits) VU.! 0 - bitsG = bitsToInt $ VU.take bitsPerByte $ VU.drop bitsPerByte pixelBits - bitsB = bitsToInt $ VU.drop (2 * bitsPerByte) pixelBits + mask = createMask bitsPerByte PixelRGB8 r g b = pixelAt img x y - newR = bitsR - newG = intToWord8 bitsG - newB = b + newR = intToWord8 $ ((word8ToInt r) .&. mask) .|. bitsIntR + newG = intToWord8 $ ((word8ToInt g) .&. mask) .|. bitsIntG + newB = intToWord8 $ ((word8ToInt b) .&. mask) .|. bitsIntB