aboutsummaryrefslogtreecommitdiffstats
path: root/src/Main.hs
blob: 6e0122d1ac8608edef001863034248f0427141e6 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module Main
    ( main
    )
where

import           Data.Bifunctor       (Bifunctor, bimap)
import qualified Data.ByteString      as B
import qualified Data.ByteString.Lazy as BL
import           Data.List            (foldl', transpose)
import           PFM

data Direction = Horizontal | Vertical
               deriving (Show, Eq)

data Cut = Cut Direction Int
         deriving (Show, Eq)

bbimap :: Bifunctor p => (a -> d) -> p a a -> p d d
bbimap a = bimap a a

clamp :: PFMColour -> PPMColour
clamp (PFMColour ri gi bi) = PPMColour (f ri) (f gi) (f bi)
  where
    v = (255 *)
    f s = if v s > 255 then 255 else fromInteger (floor (v s))
clamp _ = undefined

clampImage :: PFMImage -> PPMImage
clampImage (PFMImage w h c) = PPMImage w h $ fmap clamp <$> c

applyStop :: Float -> PFMImage -> PFMImage
applyStop stop (PFMImage w h c) = PFMImage w h $ fmap multStop <$> c
    where
        multStop (PFMColour r g b) = PFMColour (f r) (f g) (f b)
        f = ((2 ** stop) *)

fixIntensity :: Int -> Int -> PFMColour -> Double
fixIntensity sizeY y (PFMColour r g b) =
    sin ((fromIntegral y / fromIntegral sizeY) * pi) * (f r + f g + f b) / 3
    where f = realToFrac
fixIntensity _ _ _ = error "Mono not supported"

findSplit :: [Double] -> Int
findSplit d = findSplit' d [] 0
  where
    findSplit' (x : ds) e i | sum ds < sum e = i
                            | otherwise      = findSplit' ds (x : e) $ i + 1
    findSplit' _ _ i = i

cfmap :: Functor f => (t -> a -> b) -> t -> f a -> f b
cfmap f a b = f a <$> b

findSplitLine' :: [[Double]] -> Int
findSplitLine' = findSplit . fmap sum

findSplitLine :: [[Double]] -> Cut
findSplitLine d | length d > length (head d) = Cut Horizontal $ findSplitLine' d
                | otherwise = Cut Vertical . findSplitLine' $ transpose d

drawCut' :: PFMColour -> Cut -> (Int, Int) -> PFMColour -> PFMColour
drawCut' c (Cut Vertical n) (_, x) c' | x == n    = c
                                      | otherwise = c'
drawCut' c (Cut Horizontal n) (y, _) c' | y == n    = c
                                        | otherwise = c'

drawCut :: PFMColour -> Cut -> [[PFMColour]] -> [[PFMColour]]
drawCut c cut colour = (zipWith . zipWith)
    (drawCut' c cut)
    [ [ (y, x) | x <- [0 .. length $ head colour] ]
    | y <- [0 .. length colour]
    ]
    colour

applyGamma :: Float -> PFMImage -> PFMImage
applyGamma g (PFMImage w h c) = PFMImage w h $ fmap gc <$> c
  where
    gc (PFMColour r gr b) = PFMColour (gamma g r) (gamma g gr) (gamma g b)

split :: Cut -> [[a]] -> ([[a]], [[a]])
split (Cut Horizontal n) l = splitAt n l
split (Cut Vertical   n) l = bbimap transpose . splitAt n $ transpose l

combine :: Cut -> ([[a]], [[a]]) -> [[a]]
combine (Cut Horizontal _) (a, b) = a ++ b
combine (Cut Vertical   _) (a, b) = zipWith (++) a b

fIntens :: [[PFMColour]] -> [[Double]]
fIntens d = zipWith (cfmap . fixIntensity $ length d - 1) [0 ..] d

findCentroid :: [[Double]] -> (Int, Int)
findCentroid d = (y, x)
  where
    y = findSplitLine' d
    x = findSplitLine' $ transpose d

drawCentroid'
    :: PFMColour -> Int -> (Int, Int) -> (Int, Int) -> PFMColour -> PFMColour
drawCentroid' ci s (xi, yi) (x, y) c
    | (x - xi) ^ 2 + (y - yi) ^ 2 < round ((fromIntegral s / 2) ** 2) = ci
    | otherwise = c

drawCentroid :: PFMColour -> [[Double]] -> [[PFMColour]] -> [[PFMColour]]
drawCentroid ic d c = (zipWith . zipWith)
    (drawCentroid' ic 9 pt)
    [ [ (y, x) | x <- [0 .. length $ head c] ] | y <- [0 .. length c] ]
    c
    where pt = findCentroid d

drawCentroidBlack'
    :: PFMColour -> Int -> (Int, Int) -> (Int, Int) -> PFMColour -> PFMColour
drawCentroidBlack' ci s (xi, yi) (x, y) _
    | (x - xi) ^ 2 + (y - yi) ^ 2 < round ((fromIntegral s / 2) ** 2) = ci
    | otherwise = PFMColour 0 0 0

drawCentroidBlack :: PFMColour -> [[Double]] -> [[PFMColour]] -> [[PFMColour]]
drawCentroidBlack ic d c = (zipWith . zipWith)
    (drawCentroidBlack' (totalRadiance c) 9 pt)
    [ [ (y, x) | x <- [0 .. length $ head c] ] | y <- [0 .. length c] ]
    c
    where pt = findCentroid d

add :: PFMColour -> PFMColour -> PFMColour
add (PFMColour r1 g1 b1) (PFMColour r2 g2 b2) =
    PFMColour (r1 + r2) (g1 + g2) (b1 + b2)
add _ _ = error "Mono not supported"

totalRadiance :: [[PFMColour]] -> PFMColour
totalRadiance c = f $ f <$> c where f = foldl' add (PFMColour 0 0 0)

makeRecSplit
    :: (Eq a1, Num a1)
    => (PFMColour -> [[Double]] -> [[a2]] -> [[a3]])
    -> (PFMColour -> Cut -> [[a3]] -> [[a3]])
    -> a1
    -> [[Double]]
    -> [[a2]]
    -> [[a3]]
makeRecSplit dFun _    0 d c = dFun (PFMColour 0 0 1) d c
makeRecSplit dFun dCut n d c = dCut (PFMColour 1 1 1) cut a
  where
    cut      = findSplitLine d
    a        = combine cut . apply nrec $ split cut c
    (d1, d2) = split cut d
    nrec     = bbimap (makeRecSplit dFun dCut (n - 1)) (d1, d2)
    apply (f, g) (a', c') = (f a', g c')

recSplitRadiance, recSplit
    :: Int -> [[Double]] -> [[PFMColour]] -> [[PFMColour]]
recSplit = makeRecSplit drawCentroid drawCut
recSplitRadiance = makeRecSplit drawCentroidBlack (\_ _ -> id)

generateCuts
    :: Show a
    => Float
    -> (a -> [[Double]] -> [[PFMColour]] -> [[PFMColour]])
    -> PFMImage
    -> String
    -> a
    -> IO ()
generateCuts stop splitFun image prefix i = do
    putStrLn $ "data/" ++ prefix ++ show i ++ ".ppm"
    BL.writeFile ("data/" ++ prefix ++ show i ++ ".ppm")
        . encodePPM
        . clampImage
        . applyGamma 2.2
        . applyStop stop
        $ img
    putStrLn $ "data/" ++ prefix ++ show i ++ ".pfm"
    BL.writeFile ("data/" ++ prefix ++ show i ++ ".pfm")
        . encode
        . revColour
        $ img
  where
    newColour = splitFun i (fIntens $ pfmColour image) $ pfmColour image
    img       = PFMImage (pfmWidth image) (pfmHeight image) newColour

convertPFMtoPPM :: String -> IO ()
convertPFMtoPPM name = do
    putStrLn $ "Convert " <> name <> ".pfm -> " <> name <> ".ppm"
    im <- B.readFile $ name <> ".pfm"
    let image = revColour $ parse im
    BL.writeFile (name <> ".ppm")
        . encodePPM
        . clampImage
        . applyGamma 2.2
        $ image

main :: IO ()
main = do
    im <- B.readFile "data/grace_latlong.pfm"
    let grace = revColour $ parse im
    mapM_ (generateCuts 0 recSplit grace "median_cut") [1, 2, 4, 6]
    mapM_ (generateCuts (-13) recSplitRadiance grace "median_cut_radiance") [6]
    mapM_ convertPFMtoPPM
        $   ("data/simple_sphere" <>)
        <$> ["08", "16", "32", "64"]