-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
70 lines (51 loc) · 2.52 KB
/
Main.hs
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
import Codec.BMP
import Data.ByteString
import Data.Either
import GHC.Word
import System.IO.Unsafe
loadBitmap :: FilePath -> [[(Int, Int, Int)]]
loadBitmap filename = repackAs2DList (either returnEmptyOnError processDataOnBMP (unsafePerformIO (readBMP filename)))
returnEmptyOnError :: Error -> ([(Int, Int, Int)], (Int, Int))
returnEmptyOnError _ = ([], (0, 0))
processDataOnBMP :: BMP -> ([(Int, Int, Int)], (Int, Int))
processDataOnBMP bmp = ((parseIntoRGBVals (convertToInts (unpack (unpackBMPToRGBA32 bmp)))), (bmpDimensions bmp))
convertToInts :: [Word8] -> [Int]
convertToInts [] = []
convertToInts (h:t) = (fromIntegral (toInteger h)) : (convertToInts t)
parseIntoRGBVals :: [Int] -> [(Int, Int, Int)]
parseIntoRGBVals [] = []
parseIntoRGBVals (h:i:j:_:t) = (h,i,j) : (parseIntoRGBVals t)
repackAs2DList :: ([(Int, Int, Int)], (Int, Int)) -> [[(Int, Int, Int)]]
repackAs2DList (pixels, (width, height)) = (Prelude.reverse (repackAs2DList' pixels width height))
repackAs2DList' :: [(Int, Int, Int)] -> Int -> Int -> [[(Int, Int, Int)]]
repackAs2DList' [] width height = []
repackAs2DList' pixels width height = (Prelude.take width pixels) : (repackAs2DList' (Prelude.drop width pixels) width height)
showAsASCIIArt :: [[Char]] -> IO ()
showAsASCIIArt pixels = Prelude.putStr (unlines pixels)
-----------------------------------------------------------------------------------------------------------------------------------
--Main Function ran for Q1--
displayImage :: [Char] -> Bool -> [[(Int, Int, Int)]] -> [[Char]]
displayImage str bool img = mymap (mymap (intervalFunc ((if bool then id else myReverse) str))) (convertToLum img)
--converts all rgb to lum--
convertToLum :: [[(Int, Int, Int)]] -> [[(Int)]]
convertToLum img = mymap (mymap convertPixel) img
--map function--
mymap :: (a -> b) -> [a] -> [b]
mymap f [] = []
mymap f (x:xs) = f x : mymap f xs
--convert single pixel--
convertPixel :: (Int, Int, Int) -> Int
convertPixel (r, g, b) = round((fromIntegral r * 0.3) + (fromIntegral g * 0.59) + (fromIntegral b * 0.11))
--Decide intervals for Lum values--
intervalFunc :: [Char] -> Int -> Char
intervalFunc str lum = str !! ((lum -1) `quot` round (255 / fromIntegral (myLength str)))
--length function--
myLength :: [Char] -> Int
myLength [] = 0
myLength (x:xs) = 1 + (myLength xs)
--reverse function--
myReverse :: [a] -> [a]
myReverse list = myReverse' list []
myReverse' :: [a] -> [a] -> [a]
myReverse' [] rev = rev
myReverse' (x:xs) rev = myReverse' xs (x:rev)