packages feed

gloss-juicy (empty) → 0.1

raw patch · 5 files changed

+156/−0 lines, 5 filesdep +JuicyPixelsdep +basedep +bmpsetup-changed

Dependencies added: JuicyPixels, base, bmp, bytestring, gloss, vector

Files

+ Graphics/Gloss/Juicy.hs view
@@ -0,0 +1,62 @@+module Graphics.Gloss.Juicy ( fromDynamicImage+	                        , fromImageRGBA8+	                        , fromImageRGB8+	                        , fromImageY8+	                        , fromImageYA8+	                        , fromImageYCbCr8+	                        ) +where++import Codec.Picture+import Codec.Picture.Types+import Graphics.Gloss.Data.Picture+import Data.Vector.Storable        (unsafeToForeignPtr)++-- | Tries to convert a 'DynamicImage' from JuicyPixels to a gloss 'Picture'.  All formats except RGBF and YF should successfully+--   yield a 'Picture'.+fromDynamicImage :: DynamicImage -> Maybe Picture+fromDynamicImage (ImageY8 img)     = Just $ fromImageY8 img+fromDynamicImage (ImageYA8 img)    = Just $ fromImageYA8 img+fromDynamicImage (ImageRGB8 img)   = Just $ fromImageRGB8 img+fromDynamicImage (ImageRGBA8 img)  = Just $ fromImageRGBA8 img+fromDynamicImage (ImageYCbCr8 img) = Just $ fromImageYCbCr8 img+fromDynamicImage (ImageRGBF _)     = Nothing+fromDynamicImage (ImageYF _)       = Nothing++-- Courtesy of Vincent Berthoux, JuicyPixels' author+-- bmp (and thus gloss) starts by the lines at the bottom+-- JuicyPixels does the converse+horizontalSwap :: Image PixelRGBA8 -> Image PixelRGBA8+horizontalSwap img@(Image { imageWidth = w, imageHeight = h }) =+    generateImage swapper w h+      where swapper x y = PixelRGBA8 a b g r+                where PixelRGBA8 r g b a = pixelAt img x (h - y - 1)+{-# INLINE horizontalSwap #-}++-- | O(N) conversion from 'PixelRGBA8' image to gloss 'Picture', where N is the number of pixels.+fromImageRGBA8 :: Image PixelRGBA8 -> Picture+fromImageRGBA8 img@(Image { imageWidth = w, imageHeight = h, imageData = _ }) =+  bitmapOfForeignPtr w h ptr False+    where swapedImage = horizontalSwap img+          (ptr, _, _) = unsafeToForeignPtr $ imageData swapedImage+{-# INLINE fromImageRGBA8 #-}++-- | Creation of a gloss 'Picture' by promoting (through 'promoteImage') the 'PixelRGB8' image to 'PixelRGBA8' and calling 'fromImageRGBA8'.+fromImageRGB8 :: Image PixelRGB8 -> Picture+fromImageRGB8 = fromImageRGBA8 . promoteImage+{-# INLINE fromImageRGB8 #-}++-- | Creation of a gloss 'Picture' by promoting (through 'promoteImage') the 'PixelY8' image to 'PixelRGBA8' and calling 'fromImageRGBA8'.+fromImageY8 :: Image Pixel8 -> Picture+fromImageY8 = fromImageRGBA8 . promoteImage+{-# INLINE fromImageY8 #-}++-- | Creation of a gloss 'Picture' by promoting (through 'promoteImage') the 'PixelYA8' image to 'PixelRGBA8' and calling 'fromImageRGBA8'.+fromImageYA8 :: Image PixelYA8 -> Picture+fromImageYA8 = fromImageRGBA8 . promoteImage+{-# INLINE fromImageYA8 #-}++-- | Creation of a gloss 'Picture' by promoting (through 'promoteImage') the 'PixelYCbCr8' image to 'PixelRGBA8' and calling 'fromImageRGBA8'.+fromImageYCbCr8 :: Image PixelYCbCr8 -> Picture+fromImageYCbCr8 = fromImageRGB8 . convertImage+{-# INLINE fromImageYCbCr8 #-}
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Alp Mestanogullari <alpmestan@gmail.com>++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 Alp Mestanogullari <alpmestan@gmail.com> 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
+ gloss-juicy.cabal view
@@ -0,0 +1,34 @@+name:                gloss-juicy+version:             0.1+synopsis:            Load any image supported by Juicy.Pixels in your gloss application+description:         Lets you convert any image supported by Juicy.Pixels in a gloss application by converting to gloss' Bitmap representation.+homepage:            http://github.com/alpmestan/gloss-juicy+license:             BSD3+license-file:        LICENSE+author:              Alp Mestanogullari <alpmestan@gmail.com>+maintainer:          Alp Mestanogullari <alpmestan@gmail.com>+copyright:           2013 Alp Mestanogullari+category:            Graphics+build-type:          Simple+cabal-version:       >=1.8++library+  exposed-modules:     Graphics.Gloss.Juicy+     +  build-depends:       base >=4 && < 5,+                       bytestring,+                       bmp >= 1.2.4.1,+                       gloss,+                       JuicyPixels,+                       vector+  ghc-options:         -O2 -Wall -threaded++executable gloss-juicy-viewer+  main-is:             gloss-juicy.hs+  build-depends:       base >= 4 && < 5,+                       bytestring,+                       bmp >= 1.2.4.1,+                       gloss,+                       JuicyPixels,+                       vector+  ghc-options:         -O2 -Wall -threaded
+ gloss-juicy.hs view
@@ -0,0 +1,28 @@+module Main where++import Codec.Picture+import Graphics.Gloss+import Graphics.Gloss.Juicy+import System.Environment++main :: IO ()+main = do+	args <- getArgs+	case args of +		[filename] -> do+			tryRead <- readImage filename+			case tryRead of+			  Left err -> putStrLn ("error reading png file " ++ filename ++ ": " ++ err)+			  Right im -> tryConvert im++		_          -> putStrLn "usage: gloss-juicy <file> -- displays the image in a gloss window"+ +  where tryConvert img = case fromDynamicImage img of+            Just p  -> displayPic p+            Nothing -> putStrLn "Couldn't convert"++displayPic :: Picture -> IO ()+displayPic p@(Bitmap width height _ _) = display (InWindow "Image Viewer" (width, height) (10, 10))+                                                 white+                                                 p+displayPic _ = error "only the Bitmap constructor should be used here"