gl-capture (empty) → 0.1.0.0
raw patch · 4 files changed
+115/−0 lines, 4 filesdep +OpenGLdep +basedep +bytestringsetup-changed
Dependencies added: OpenGL, base, bytestring
Files
- Graphics/Rendering/OpenGL/Capture.hs +57/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- gl-capture.cabal +26/−0
+ Graphics/Rendering/OpenGL/Capture.hs view
@@ -0,0 +1,57 @@+{- |++Module : Graphics.Rendering.OpenGL.Capture+Copyright : (c) Claude Heiland-Allen 2012+License : BSD3++Maintainer : claude@mathr.co.uk+Stability : provisional+Portability : portable++Simple image capture from OpenGL.++-}+module Graphics.Rendering.OpenGL.Capture (capturePPM) where++import Control.Monad (forM_)+import Foreign (allocaBytes, plusPtr)++import qualified Data.ByteString.Internal as BSI+import qualified Data.ByteString as BS+import Data.ByteString (ByteString)++import Graphics.Rendering.OpenGL++{- | Capture the current viewport to a ByteString in PPM P6 format.+ Correctly handles pixel alignment for oddly-sized regions.+ Corrects the image orientation so that the origin is top left.+-}+-- This is too monolithic - patches welcome.+capturePPM :: IO ByteString+capturePPM = do+ (p0, s0@(Size vw vh)) <- get viewport+ let p6 = "P6\n" ++ show vw ++ " " ++ show vh ++ "\n255\n"+ allocaBytes (fromIntegral (vw*vh*3)) $ \ptr -> do+ preservingBufferBinding PixelPackBuffer $ do+ -- pack buffer needs OpenGL version >= 2.1+ bindBuffer PixelPackBuffer $= Nothing+ -- client attrib needs OpenGL version >= 1.1+ preservingClientAttrib [PixelStoreAttributes] $ do+ -- FIXME what else needs setting to avoid crashes or bad output?+ rowAlignment Pack $= 1+ readPixels p0 s0 $ PixelData RGB UnsignedByte ptr+ px <- BSI.create (fromIntegral $ vw * vh * 3) $ \d ->+ forM_ [0..vh-1] $ \y ->+ BSI.memcpy+ (d `plusPtr` fromIntegral (y*vw*3))+ (ptr `plusPtr` fromIntegral ((vh-1-y)*vw*3))+ (fromIntegral (vw*3))+ return $ BS.pack (map (toEnum . fromEnum) p6) `BS.append` px++-- This should check OpenGL version >= 2.1 - patches welcome.+preservingBufferBinding :: BufferTarget -> IO a -> IO a+preservingBufferBinding target action = do+ buffer <- get $ bindBuffer target+ result <- action+ bindBuffer target $= buffer+ return result
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Claude Heiland-Allen++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 Claude Heiland-Allen 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ gl-capture.cabal view
@@ -0,0 +1,26 @@+name: gl-capture+version: 0.1.0.0+synopsis: simple image capture from OpenGL+description:+ Simplify image capture from OpenGL to ByteString in PPM P6 format.++license: BSD3+license-file: LICENSE+author: Claude Heiland-Allen+maintainer: claude@mathr.co.uk+category: Graphics+build-type: Simple+cabal-version: >=1.8++library+ exposed-modules: Graphics.Rendering.OpenGL.Capture+ build-depends: base < 5, bytestring, OpenGL++source-repository head+ type: git+ location: git://gitorious.org/maximus/gl-capture.git+ +source-repository this+ type: git+ location: git://gitorious.org/maximus/gl-capture.git+ tag: v0.1.0.0