gdritter repos image-vk / 0ea5bcf
Basic definition of a colorless path file Getty Ritter 7 years ago
6 changed file(s) with 118 addition(s) and 0 deletion(s). Collapse all Expand all
1 *~
2 dist
3 dist-newstyle
4 cabal.sandbox.config
5 .cabal-sandbox
1 module Image.VK where
2
3 import Data.ByteString.Lazy (ByteString)
4 import Data.Eben (Value(..))
5 import qualified Data.Eben as Eben
6
7 type Point = (Float, Float)
8
9 data VKImage = VKImage
10 { vkLines :: [[Point]]
11 , vkMeta :: VKMeta
12 } deriving (Eq, Show)
13
14 data VKMeta = VKMeta { vkSize :: (Float, Float) } deriving (Eq, Show)
15
16 catch :: Maybe a -> String -> Either String a
17 catch (Just a) _ = Right a
18 catch Nothing m = Left m
19
20 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')
26
27 decode :: ByteString -> Either String VKImage
28 decode bs = do
29 (ds, _) <- Eben.decode bs `catch` "Invalid EBEN value"
30 metaV <- Eben.lookup "meta" ds `catch` "Missing field: `meta`"
31 (w,h) <- asPoint metaV
32 linesV <- Eben.lookup "lines" ds `catch` "Missing field: `lines`"
33 ls <- Eben.asList linesV `catch` "`lines` field not a list"
34 ls' <- mapM Eben.asList ls `catch` "arglbargl"
35 points <- sequence [ mapM asPoint l | l <- ls' ]
36 return VKImage { vkLines = points
37 , vkMeta = VKMeta { vkSize = (w, h) }
38 }
1 Copyright (c) 2016, Getty Ritter
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
8 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
10 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(New empty file)
1 name: image-vk
2 version: 0.1.0.0
3 -- synopsis:
4 -- description:
5 license: BSD3
6 license-file: LICENSE
7 author: Getty Ritter <gdritter@galois.com>
8 maintainer: Getty Ritter <gdritter@galois.com>
9 copyright: ©2016 Getty Ritter
10 -- category:
11 build-type: Simple
12 cabal-version: >= 1.12
13
14 library
15 exposed-modules: Image.VK
16 ghc-options: -Wall
17 build-depends: base >=4.7 && <4.9,
18 eben,
19 bytestring
20 default-language: Haskell2010
21 default-extensions: OverloadedStrings,
22 ScopedTypeVariables
23
24 executable vk2svg
25 hs-source-dirs: vk2svg
26 main-is: Main.hs
27 default-extensions: OverloadedStrings,
28 ScopedTypeVariables
29 build-depends: base >=4.7 && <4.9, image-vk, bytestring
30 default-language: Haskell2010
1 module Main where
2
3 import qualified Data.ByteString.Lazy as BS
4 import Image.VK
5
6 mkImage :: VKImage -> IO ()
7 mkImage img = do
8 let VKMeta (w, h) = vkMeta img
9 putStr "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\""
10 putStrLn $ concat [" width=\"", show w, "\" height=\"", show h, "\">" ]
11 mapM_ pLine (vkLines img)
12 putStrLn "</svg>"
13 where pLine [] = return ()
14 pLine [x] = return ()
15 pLine ((x,y):a@(x',y'):as) = do
16 putStrLn $ concat [ "<line x1=\""
17 , show x
18 , "\" y1=\""
19 , show y
20 , "\" x2=\""
21 , show x'
22 , "\" y2=\""
23 , show y'
24 , "\" stroke=\"black\" stroke-width=\"2\" />"
25 ]
26 pLine (a:as)
27
28 main :: IO ()
29 main = do
30 cs <- BS.getContents
31 case decode cs of
32 Left err -> putStrLn err
33 Right vk -> mkImage vk