summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrewCoppin <>2010-01-18 22:26:54 (GMT)
committerLuite Stegeman <luite@luite.com>2010-01-18 22:26:54 (GMT)
commit0f782ad4e066138b5dea8addce80b35ebe6a0111 (patch)
treecb756f6ee86a327ad03074c077662c65cb05ca29
version 1.1.1HEAD1.1.1master
-rw-r--r--AC-PPM.cabal32
-rw-r--r--Codec/PPM.hs11
-rw-r--r--Codec/PPM/Binary.hs48
-rw-r--r--Codec/PPM/Text.hs53
-rw-r--r--License.txt10
-rw-r--r--Setup.hs3
6 files changed, 157 insertions, 0 deletions
diff --git a/AC-PPM.cabal b/AC-PPM.cabal
new file mode 100644
index 0000000..11ad9d8
--- /dev/null
+++ b/AC-PPM.cabal
@@ -0,0 +1,32 @@
+Cabal-Version: >= 1.6
+Name: AC-PPM
+Version: 1.1.1
+Stability: Experimental
+Synopsis: Trivial package for writing PPM images.
+
+Description:
+
+ This package is written in 100% Haskell and enables you to
+ write PPM (\"Portable Pixel Map\") images in human-readable
+ text format as well as the more efficient binary format.
+ It uses the ByteString library for maximum efficiency.
+
+ For those that don't know, PPM is an image file format
+ which is trivially simple, but therefore horribly inefficient.
+ None the less, many programs can read PPM files, so this
+ package represents an easy way to write Haskell code that
+ generates images, without having to depend on any external
+ C code.
+
+Category: Codec, Graphics
+License: BSD3
+License-file: License.txt
+Author: Andrew Coppin
+Maintainer: Andrew Coppin <MathematicalOrchid@hotmail.com>
+Build-Type: Simple
+Tested-With: GHC == 6.10.3
+
+Library
+ Exposed-modules: Codec.PPM, Codec.PPM.Text, Codec.PPM.Binary
+ Build-Depends: base >= 4 && < 5, bytestring >= 0.9 && < 1.0
+ HS-Source-Dirs: .
diff --git a/Codec/PPM.hs b/Codec/PPM.hs
new file mode 100644
index 0000000..dd88662
--- /dev/null
+++ b/Codec/PPM.hs
@@ -0,0 +1,11 @@
+{-|
+ The two modules "Codec.PPM.Text" and "Codec.PPM.Binary" support
+ the generation of text or binary PPM files (\"P3\" or \"P6\"
+ respectively). Default is binary (since it is more efficient.)
+ Both modules have an identical API; just change the import
+ declaration to change file mode.
+-}
+
+module Codec.PPM (module Codec.PPM.Binary) where
+
+import Codec.PPM.Binary
diff --git a/Codec/PPM/Binary.hs b/Codec/PPM/Binary.hs
new file mode 100644
index 0000000..9878f11
--- /dev/null
+++ b/Codec/PPM/Binary.hs
@@ -0,0 +1,48 @@
+{-|
+ Generate binary PPM files (\"P6\"). These are smaller
+ on disk and faster to generate than text PPM files.
+-}
+
+module Codec.PPM.Binary where
+
+import Data.Word
+import qualified Data.ByteString.Lazy as BIN
+import System.IO
+
+{-|
+ Given the image size (X pixels, Y pixels) and a list of
+ pixel values (red, green, blue), produce a lazy ByteString
+ containing the PPM data.
+-}
+stringPPM :: (Integer,Integer) -> [(Word8,Word8,Word8)] -> BIN.ByteString
+stringPPM (sx,sy) ps =
+ BIN.pack (map (fromIntegral . fromEnum) $ "P6\n" ++ show sx ++ " " ++ show sy ++ "\n255\n") `BIN.append`
+ BIN.concat (map (\(r,g,b) -> BIN.pack [r,g,b]) ps)
+
+{-|
+ Convenience function: Generate PPM data and write it to the
+ specified 'Handle'. The handle is not closed or flushed afterwards.
+ This allows writing PPM data to network streams, etc. This function
+ does not return until all the data has been completely written.
+ (It may of course throw some kind of I/O exception instead.)
+-}
+putPPM :: Handle -> (Integer, Integer) -> [(Word8,Word8,Word8)] -> IO ()
+putPPM h sz ps = BIN.hPut h (stringPPM sz ps)
+
+{-|
+ Convenience function: Write PPM data directly to a file. If the file
+ does not exist, it is created. If it exists, it is overwritten.
+ This function does not return until the file has been completely
+ created. (It may of course throw some kind of I/O exception instead.)
+-}
+writePPM :: FilePath -> (Integer, Integer) -> [(Word8,Word8,Word8)] -> IO ()
+writePPM f sz ps = BIN.writeFile f (stringPPM sz ps)
+
+
+{-|
+ Helper function: Given a function from pixel coordinates to pixel
+ colour, produce a list of pixel data ready to feed to one of the
+ other PPM functions.
+-}
+fn_list :: ((Integer,Integer) -> (Word8,Word8,Word8)) -> (Integer, Integer) -> [(Word8,Word8,Word8)]
+fn_list fn (sx, sy) = map fn [ (x,y) | y <- [0..sy-1], x <- [0..sx-1] ]
diff --git a/Codec/PPM/Text.hs b/Codec/PPM/Text.hs
new file mode 100644
index 0000000..0ee00ba
--- /dev/null
+++ b/Codec/PPM/Text.hs
@@ -0,0 +1,53 @@
+{-|
+ Generate textual PPM files (\"P3\"). These are much larger
+ than binary files and slower to generate, but have the
+ advantage of being /vaguely/ human-readable. (Apart from
+ the file header, each line is three decimal numbers
+ representing the RGB values of a single pixel.) Such files
+ will also survive transfer in \"text mode\" without
+ being mutilated, unlike binary image files.
+-}
+
+module Codec.PPM.Text where
+
+import Data.Word
+import qualified Data.ByteString.Lazy.Char8 as TXT
+import System.IO
+
+{-|
+ Given the image size (X pixels, Y pixels) and a list of
+ pixel values (red, green, blue), produce a lazy ByteString
+ containing the PPM data.
+-}
+stringPPM :: (Integer,Integer) -> [(Word8,Word8,Word8)] -> TXT.ByteString
+stringPPM (sx,sy) ps =
+ TXT.pack ("P3\n" ++ show sx ++ " " ++ show sy ++ "\n255\n") `TXT.append`
+ TXT.unlines (map (\(r,g,b) -> TXT.pack $ unwords [show r, show g,show b]) ps)
+
+{-|
+ Convenience function: Generate PPM data and write it to the
+ specified 'Handle'. The handle is not closed or flushed afterwards.
+ This allows writing PPM data to network streams, etc. This function
+ does not return until all the data has been completely written.
+ (It may of course throw some kind of I/O exception instead.)
+-}
+putPPM :: Handle -> (Integer, Integer) -> [(Word8,Word8,Word8)] -> IO ()
+putPPM h sz ps = TXT.hPut h (stringPPM sz ps)
+
+{-|
+ Convenience function: Write PPM data directly to a file. If the file
+ does not exist, it is created. If it exists, it is overwritten.
+ This function does not return until the file has been completely
+ created. (It may of course throw some kind of I/O exception instead.)
+-}
+writePPM :: FilePath -> (Integer, Integer) -> [(Word8,Word8,Word8)] -> IO ()
+writePPM f sz ps = TXT.writeFile f (stringPPM sz ps)
+
+
+{-|
+ Helper function: Given a function from pixel coordinates to pixel
+ colour, produce a list of pixel data ready to feed to one of the
+ other PPM functions.
+-}
+fn_list :: ((Integer,Integer) -> (Word8,Word8,Word8)) -> (Integer, Integer) -> [(Word8,Word8,Word8)]
+fn_list fn (sx, sy) = map fn [ (x,y) | y <- [0..sy-1], x <- [0..sx-1] ]
diff --git a/License.txt b/License.txt
new file mode 100644
index 0000000..60dd2ca
--- /dev/null
+++ b/License.txt
@@ -0,0 +1,10 @@
+Copyright (c) 2009, Andrew Coppin
+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 Andrew Coppin nor the names of the 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 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.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..40f133f
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain