JuicyPixels-util (empty) → 0.1
raw patch · 4 files changed
+122/−0 lines, 4 filesdep +JuicyPixelsdep +basedep +vectorsetup-changed
Dependencies added: JuicyPixels, base, vector
Files
- Codec/Picture/RGBA8.hs +68/−0
- JuicyPixels-util.cabal +26/−0
- LICENSE +26/−0
- Setup.hs +2/−0
+ Codec/Picture/RGBA8.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE TypeSynonymInstances, FlexibleContexts, MagicHash #-} +module Codec.Picture.RGBA8 where + +import Codec.Picture +import qualified Data.Vector.Storable as V +import qualified Data.Vector.Storable.Mutable as MV +import Foreign.Marshal.Utils +import Control.Monad +import Foreign.ForeignPtr +import Foreign.Ptr +import System.IO.Unsafe +import Data.Bits +import GHC.Ptr +import GHC.Base +import GHC.Num + +class ToPixelRGBA8 a where + toRGBA8 :: a -> PixelRGBA8 + +instance ToPixelRGBA8 Pixel8 where + toRGBA8 b = PixelRGBA8 b b b 255 + +instance ToPixelRGBA8 PixelYA8 where + toRGBA8 (PixelYA8 l a) = PixelRGBA8 l l l a + +instance ToPixelRGBA8 PixelRGB8 where + toRGBA8 (PixelRGB8 r g b) = PixelRGBA8 r g b 255 + +instance ToPixelRGBA8 PixelRGBA8 where + toRGBA8 = id + +fromColorAndOpacity :: PixelRGB8 -> Image Pixel8 -> Image PixelRGBA8 +fromColorAndOpacity (PixelRGB8 r g b) (Image w h vec) = Image w h $ V.generate (w * h * 4) pix where + pix i = if testBit i 0 + then if testBit i 1 + then vec V.! unsafeShiftR i 2 + else b + else if testBit i 1 + then g + else r + {-# INLINE pix #-} + +trimImage :: Image PixelRGBA8 -> (Int, Int) -> (Int, Int) -> Image PixelRGBA8 +trimImage (Image w _ vec) (w', h') (x0, y0) = unsafePerformIO $ V.unsafeWith vec $ \ptr -> do + mv <- MV.unsafeNew $ w' * h' * 4 + MV.unsafeWith mv $ \dst -> forM_ [0..h'-1] $ \y -> do + copyBytes (plusPtr dst $ y * w' * 4) (plusPtr ptr $ (*4) $ (y + y0) * w + x0) (4 * w') + Image w' h' `fmap` V.unsafeFreeze mv + +flipVertically :: Image PixelRGBA8 -> Image PixelRGBA8 +flipVertically (Image w h v) = unsafePerformIO $ V.unsafeWith v $ \ptr -> do + mv <- MV.unsafeNew $ w * h * 4 + MV.unsafeWith mv $ \dst -> forM_ [0..h-1] $ \y -> copyBytes (plusPtr dst $ y * w * 4) (plusPtr ptr $ (h - y - 1) * w * 4) (4 * w) + Image w h `fmap` V.unsafeFreeze mv + +fromDynamicImage :: DynamicImage -> Image PixelRGBA8 +fromDynamicImage (ImageY8 img) = pixelMap toRGBA8 img +fromDynamicImage (ImageYA8 img) = pixelMap toRGBA8 img +fromDynamicImage (ImageRGB8 img) = pixelMap toRGBA8 img +fromDynamicImage (ImageRGBA8 img) = img +fromDynamicImage _ = error "Unsupported format" + +readImageRGBA8 :: FilePath -> IO (Image PixelRGBA8) +readImageRGBA8 path = readImage path >>= either fail (return . fromDynamicImage) + +addrImage :: Image PixelRGBA8 -> IO Int +addrImage (Image _ _ v) = let (fptr, _) = V.unsafeToForeignPtr0 v + in withForeignPtr fptr $ \(Ptr a) -> return $ fromEnum $ wordToInteger (int2Word# (addr2Int# a))
+ JuicyPixels-util.cabal view
@@ -0,0 +1,26 @@+name: JuicyPixels-util +version: 0.1 +synopsis: Convert JuicyPixel images into RGBA format, flip, trim and so on +description: +homepage: https://github.com/fumieval/JuicyPixels-util +bug-reports: https://github.com/fumieval/JuicyPixels-util/issues +license: BSD3 +license-file: LICENSE +author: Fumiaki Kinoshita +maintainer: Fumiaki Kinoshita <fumiexcel@gmail.com> +copyright: Copyright (C) 2012-2013 Fumiaki Kinoshita +category: Control +build-type: Simple +stability: experimental +cabal-version: >=1.10 + +source-repository head + type: git + location: https://github.com/fumieval/JuicyPixels-util.git + +library + default-language: Haskell2010 + exposed-modules: Codec.Picture.RGBA8 + ghc-options: -Wall + + build-depends: base == 4.*, JuicyPixels >= 3.1, vector
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright 2009-2013 Edward Kmett + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +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. + +THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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