gdritter repos image-vk / a8a53bd
Changed the underlying data to be Int-based rather than Float-based Getty Ritter 8 years ago
1 changed file(s) with 9 addition(s) and 7 deletion(s). Collapse all Expand all
44 import Data.Eben (Value(..))
55 import qualified Data.Eben as Eben
66
7 type Point = (Float, Float)
7 type Point = (Int, Int)
88
99 data VKImage = VKImage
1010 { vkLines :: [[Point]]
1111 , vkMeta :: VKMeta
1212 } deriving (Eq, Show)
1313
14 data VKMeta = VKMeta { vkSize :: (Float, Float) } deriving (Eq, Show)
14 data VKMeta = VKMeta { vkSize :: (Int, Int) } deriving (Eq, Show)
1515
1616 catch :: Maybe a -> String -> Either String a
1717 catch (Just a) _ = Right a
1818 catch Nothing m = Left m
1919
2020 asPoint :: Value -> Either String Point
21 asPoint v = do
22 (x:y:_) <- Eben.asList v `catch` "Point value not a list"
23 x' <- Eben.asFloat x `catch` "Point x-value not a float"
24 y' <- Eben.asFloat y `catch` "Point x-value not a float"
25 return (x', y')
21 asPoint v = Eben.fromEben v `catch` "Unable to parse point"
2622
2723 decode :: ByteString -> Either String VKImage
2824 decode bs = do
3632 return VKImage { vkLines = points
3733 , vkMeta = VKMeta { vkSize = (w, h) }
3834 }
35
36 encode :: VKImage -> ByteString
37 encode vk = Eben.encode $ Eben.dict
38 [ ("meta", Eben.toEben (vkSize (vkMeta vk)))
39 , ("lines", Eben.toEben (vkLines vk))
40 ]