From 117f99d04b39955840373685a5194927fcf73d82 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 29 Jan 2019 20:07:13 +0000 Subject: Add initial files --- LICENSE | 30 ++++++++++++++++++ README.md | 1 + Setup.hs | 2 ++ app/Main.hs | 10 ++++++ pfm.cabal | 37 +++++++++++++++++++++ src/PFM.hs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stack.yaml | 64 +++++++++++++++++++++++++++++++++++++ 7 files changed, 248 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Setup.hs create mode 100644 app/Main.hs create mode 100644 pfm.cabal create mode 100644 src/PFM.hs create mode 100644 stack.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91132e7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,30 @@ +Copyright Author name here (c) 2019 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * 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. + + * Neither the name of Author name here nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +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 +OWNER 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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..77a6ed8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# pfm diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..22c0fba --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,10 @@ +module Main where + +import qualified Data.ByteString as B +import qualified Data.Text as T +import PFM + +main :: IO () +main = do + s <- B.readFile "/home/yannherklotz/Imperial/AdvancedGraphics/coursework1/CO417-Assignment1/UrbanProbe/urbanEM_latlong.pfm" + parse s diff --git a/pfm.cabal b/pfm.cabal new file mode 100644 index 0000000..ddd4eef --- /dev/null +++ b/pfm.cabal @@ -0,0 +1,37 @@ +name: pfm +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/ymherklotz/pfm#readme +license: BSD3 +license-file: LICENSE +author: Yann Herklotz Grave +maintainer: ymherklotz@gmail.com +copyright: 2019 Yann Herklotz Grave +category: Image +build-type: Simple +cabal-version: >=1.10 +extra-source-files: README.md + +library + hs-source-dirs: src + default-language: Haskell2010 + ghc-options: -Wall + exposed-modules: PFM + build-depends: attoparsec + , base >= 4.7 && < 5 + , bytestring + , text + , binary + , data-binary-ieee754 + default-extensions: OverloadedStrings + +executable readpfm + hs-source-dirs: app + main-is: Main.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , pfm + , text + , bytestring + default-extensions: OverloadedStrings diff --git a/src/PFM.hs b/src/PFM.hs new file mode 100644 index 0000000..f16400f --- /dev/null +++ b/src/PFM.hs @@ -0,0 +1,104 @@ +{-| +Module : PFM +Description : Debevec PFM reader +Copyright : (c) 2019, Yann Herklotz Grave +License : GPL-3 +Maintainer : ymherklotz [at] gmail [dot] com +Stability : experimental +Portability : POSIX + +Debevec PFM reader +-} + +module PFM where + +import Control.Applicative ((<|>)) +import Control.Monad (void) +import Data.Attoparsec.ByteString (Parser) +import qualified Data.Attoparsec.ByteString as P +import Data.Binary.Get (runGet) +import Data.Binary.IEEE754 (getFloat32be, getFloat32le) +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import Data.ByteString.Lazy (fromStrict) +import Data.Text (Text) +import qualified Data.Text as T +import qualified Data.Text.Encoding as T +import Data.Word (Word8) + +data Image = Image { width :: Int + , height :: Int + , colour :: [Colour] + } deriving (Show) + +data Colour = Colour { r :: Float + , g :: Float + , b :: Float + } + | Mono Float + deriving (Show) + +matchText :: Text -> Parser ByteString +matchText = P.string . T.encodeUtf8 + +magicNumMono :: Parser () +magicNumMono = void $ matchText "Pf" + +magicNumRGB :: Parser () +magicNumRGB = void $ matchText "PF" + +skipNewline :: Parser () +skipNewline = P.skip isNewline + where + isNewline w = w == 13 || w == 10 + +skipSpace :: Parser () +skipSpace = P.skip (== 32) + +decode :: (Read a) => [Word8] -> a +decode = read . T.unpack . T.decodeUtf8 . B.pack + +matchMult :: String -> Parser [Word8] +matchMult = P.many1 . P.satisfy . P.inClass + +num :: Parser Int +num = decode <$> matchMult "0-9" + +endianness :: Parser Float +endianness = decode <$> matchMult "0-9.-" + +float :: Parser Float +float = runGet getFloat32le . fromStrict <$> P.take 4 + +header :: Parser (Int, Int, Float) +header = do + magicNumRGB + skipNewline + n1 <- num + skipSpace + n2 <- num + skipNewline + s <- endianness + skipNewline + return (n1, n2, s) + +parseColour :: Parser Colour +parseColour = do + ri <- float + gi <- float + bi <- float + return $ Colour ri gi bi + +parseMono :: Parser Colour +parseMono = Mono <$> float + +parser :: Parser Image +parser = do + (w, h, f) <- header + c <- P.many1 parseColour + return $ Image w h c + +parse :: ByteString -> IO () +parse s = case P.parseOnly parser s of + Left str -> putStrLn str + Right i -> print i diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..d658bf4 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,64 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# https://docs.haskellstack.org/en/stable/yaml_configuration/ + +# Resolver to choose a 'specific' stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# resolver: lts-3.5 +# resolver: nightly-2015-09-21 +# resolver: ghc-7.10.2 +# +# The location of a snapshot can be provided as a file or url. Stack assumes +# a snapshot provided as a file might change, whereas a url resource does not. +# +# resolver: ./custom-snapshot.yaml +# resolver: https://example.com/snapshots/2018-01-01.yaml +resolver: lts-13.5 + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# - location: +# git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# subdirs: +# - auto-update +# - wai +packages: +- . +# Dependency packages to be pulled from upstream that are not in the resolver +# using the same syntax as the packages field. +# (e.g., acme-missiles-0.3) +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=1.9" +# +# Override the architecture used by stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor -- cgit